简体   繁体   English

尝试使用Python验证SHA1消息签名。我究竟做错了什么?

[英]Trying to verify SHA1 message signature using Python. What am I doing wrong?

I'm attempting to verify the SHA1 signature of a message by downloading a certificate from a website and extracting its public key. 我试图通过从网站下载证书并提取其公钥来验证消息的SHA1签名。 There's a few bits of sample code elsewhere on SO ( here and here ), however I haven't yet figured out what I'm doing wrong. 在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

This gives me the following assertion error: 这给了我以下断言错误:

  File "/tmp/test.py", line 71, in verify_message
    assert pubkey.verify_final(sig) == 1
AssertionError

However, if I use openssl from the command line along with the files generated from the above Python script, it works fine: 但是,如果我从命令行使用openssl以及从上面的Python脚本生成的文件,它可以正常工作:

[jamie@test5 tmp]$ openssl dgst -sha1 -verify mypubkey.pem -signature sig msg
Verified OK

I've hit a brick wall here; 我在这里碰了一堵砖墙; any suggestions would be greatly appreciated. 任何建议将不胜感激。 Thanks! 谢谢!

你的代码工作正常 - https://gist.github.com/kalloc/5106808我在这里看到其他错误

这段代码在我的最后工作得非常好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用 python 中的 mechanize 登录。 我究竟做错了什么? - Can't login using mechanize from python. What am I doing wrong? 我无法使用 Python 将名称中包含空格的文件上传到 Google Cloud Storage。 我究竟做错了什么? - I'm unable to upload a file whose name has a space in it to Google Cloud Storage using Python. What am I doing wrong? Lexer在Python中输出“ TypeError:write()参数必须为str,而不是字节”。 我究竟做错了什么? - Lexer Outputs “TypeError: write() argument must be str, not bytes” in Python. What am I doing wrong? Python:如何使用 pyopenssl 验证 sha1 rsa 签名? - Python: How to verify sha1 rsa signature with pyopenssl? 将带字母的电话号码转换为python中的所有数字。 我究竟做错了什么? - translating phone number with letter to all numbers in python. what am i doing wrong? 如何验证Python中的RSA SHA1签名? - How do you verify an RSA SHA1 signature in Python? 如果==不适用于python中的列表。 不知道我在做什么错。 数据的print()表明它们是相等的……我缺少什么? - IF == not working for lists in python. No idea what I am doing wrong. A print() of the data reveals they are equal…what am I missing? 尝试使用Python解析XML文件-我在做什么错? - Trying to parse an XML file with Python - what am I doing wrong? 在 Python 中使用季节性分解时我做错了什么? - What am I doing wrong when using seasonal decompose in Python? 我在使用函数的Python程序中怎么了? - What am I doing wrong in this Python program using functions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM