我试图通过从网站下载证书并提取其公钥来验证消息的 SHA1 签名。SO(此处和此处)的其他地方有一些示例代码,但是我还没有弄清楚我做错了什么。
import requests
from M2Crypto import BIO, RSA, EVP, X509
def verify_message(cert_url, msg, sig):
cert_text = requests.get(cert_url, verify=True)
cert = X509.load_cert_string(cert_text.content)
pubkey = cert.get_pubkey()
sig = sig.decode('base64')
# Write a few files to disk for debugging purposes
f = open("sig", "wb")
f.write(sig)
f.close()
f = open("msg", "w")
f.write(msg)
f.close()
f = open("mypubkey.pem", "w")
f.write(pubkey.get_rsa().as_pem())
f.close()
pubkey.reset_context(md='sha1')
pubkey.verify_init()
pubkey.verify_update(msg)
assert pubkey.verify_final(sig) == 1
这给了我以下断言错误:
File "/tmp/test.py", line 71, in verify_message
assert pubkey.verify_final(sig) == 1
AssertionError
但是,如果我openssl
从命令行使用从上面的 Python 脚本生成的文件,它工作正常:
[jamie@test5 tmp]$ openssl dgst -sha1 -verify mypubkey.pem -signature sig msg
Verified OK
我在这里碰壁了;任何建议将不胜感激。谢谢!