How do you verify an RSA SHA1 signature in Python?

后端 未结 8 1473
面向向阳花
面向向阳花 2020-11-29 00:35

I\'ve got a string, a signature, and a public key, and I want to verify the signature on the string. The key looks like this:

-----BEGIN PUBLIC KEY-----
MIGf         


        
8条回答
  •  半阙折子戏
    2020-11-29 01:07

    Use M2Crypto. Here's how to verify for RSA and any other algorithm supported by OpenSSL:

    pem = """-----BEGIN PUBLIC KEY-----
    MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDfG4IuFO2h/LdDNmonwGNw5srW
    nUEWzoBrPRF1NM8LqpOMD45FAPtZ1NmPtHGo0BAS1UsyJEGXx0NPJ8Gw1z+huLrl
    XnAVX5B4ec6cJfKKmpL/l94WhP2v8F3OGWrnaEX1mLMoxe124Pcfamt0SPCGkeal
    VvXw13PLINE/YptjkQIDAQAB
    -----END PUBLIC KEY-----""" # your example key
    
    from M2Crypto import BIO, RSA, EVP
    bio = BIO.MemoryBuffer(pem)
    rsa = RSA.load_pub_key_bio(bio)
    pubkey = EVP.PKey()
    pubkey.assign_rsa(rsa)
    
    # if you need a different digest than the default 'sha1':
    pubkey.reset_context(md='sha1')
    pubkey.verify_init()
    pubkey.verify_update('test  message')
    assert pubkey.verify_final(signature) == 1
    

提交回复
热议问题