gpt4 book ai didi

python rfc3161验证失败但openssl验证正常

转载 作者:太空宇宙 更新时间:2023-11-03 14:46:21 25 4
gpt4 key购买 nike

我正在尝试为我的内容添加时间戳,以便我知道它何时更改。首先我使用了一个 shell 脚本,但我想在我的 python 程序中实现它。 shell 脚本现在可以正常工作,但我无法让 python 版本为我工作。这是工作 shell 版本

in_file='test_content'
out_file="${in_file}.tsr"
ts_server='http://time.certum.pl/'
openssl ts -query -data "$in_file" -sha1 -cert | curl -o "$out_file" -sSH 'Content-Type: application/timestamp-query' --data-binary @- "$ts_server"
openssl ts -verify -data "$in_file" -in "$out_file" -CAfile "/usr/lib/ssl/certs/Certum_Trusted_Network_CA.pem"
openssl ts -reply -in "$out_file" -text

我试着用 rfc3161 package 来模仿这个但验证没有按预期进行。这是python代码

import rfc3161

cert = file('/usr/lib/ssl/certs/Certum_Trusted_Network_CA.pem').read()
rt = rfc3161.RemoteTimestamper('http://time.certum.pl/', certificate=cert)
data_to_sign = file('test_content').read()
print rt.timestamp(data=data_to_sign)
>>> (False, 'Bad signature')

我不知道哪里出了问题,因为两个脚本应该做同样的事情。有人可以告诉我 python 版本有什么问题吗?

最佳答案

以下代码使用

因为

  • 不再维护 Python 2 和模块 rfc3161
  • 我不确定 certum.pl 端点 url 以及从何处获取正确的 CA 证书

您应该选择哈希算法(默认为 sha1,已弃用)。

import rfc3161ng
import os
from struct import unpack

with open('freetsa.pem', 'rb') as cert_fh:
cert = cert_fh.read()

rt = rfc3161ng.RemoteTimestamper('https://freetsa.org/tsr', certificate=cert, hashname='sha256')

with open('test_content', 'rb') as content_fh:
data_to_sign = content_fh.read()

nonce = unpack('<q', os.urandom(8))[0]

print(rt.timestamp(data=data_to_sign))

安全警告

Nonce 是可选的,但建议使用,如 RFC3161 的第 2.4.1 段中所述

The nonce, if included, allows the client to verify the timeliness of the response when no local clock is available. The nonce is a large random number with a high probability that the client generates it only once (e.g., a 64 bit integer).

关于python rfc3161验证失败但openssl验证正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38717609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com