gpt4 book ai didi

python - pyopenssl 验证文件签名

转载 作者:行者123 更新时间:2023-11-28 20:39:27 27 4
gpt4 key购买 nike

我想使用pyopenssl验证下载文件的签名和证书,但文档不明确,谷歌也无济于事。

我在用户的机器上有一个根 CA 证书,现在当用户下载文件时,我会连同它一起发送一个证书和签名。首先我需要在机器上使用 rootCA 验证证书然后我需要使用文件验证签名

在 openssl 中我可以使用以下来验证 ca 证书

openssl verify -CAfile <root_pem> <cert_pem>

然后验证文件

openssl dgst <algo> -verify <cert_pub_key> -signature <signature> <file>

我正在寻找使用 python 的等效方法,最好是 pyopenssl

最佳答案

我总体上仍在学习 OpenSSL,更不用说 PyOpenSSL 了。话虽如此,我能够使用以下命令在 PyOpenSSL 中验证文件(您的第二个命令):

from OpenSSL.crypto import load_publickey, FILETYPE_PEM, verify, X509

with open(file_to_verify, 'rb') as f:
file_data = f.read()

with open(signature_filename, 'rb') as f:
signature = f.read()

with open(public_key_filename) as f:
public_key_data = f.read()

# load in the publickey file, in my case, I had a .pem file.
# If the file starts with
# "-----BEGIN PUBLIC KEY-----"
# then it is of the PEM type. The only other FILETYPE is
# "FILETYPE_ASN1".
pkey = load_publickey(FILETYPE_PEM, public_key_data)

# the verify() function expects that the public key is
# wrapped in an X.509 certificate
x509 = X509()
x509.set_pubkey(pkey)

# perform the actual verification. We need the X509 object,
# the signature to verify, the file to verify, and the
# algorithm used when signing.
verify(x509, signature, file_data, 'sha256')

如果验证成功(即什么都不做),verify() 函数将返回 None,如果出现问题,它将引发异常。

关于python - pyopenssl 验证文件签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38123106/

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