Search code examples
pythonopensslpyopensslm2crypto

How to use openssl to verify without creating files in python?


I am using OpenSSL to perform verification.

openssl dgst -sha256 -verify public.pem -signature sign_file origin_file

What I need to do is that if I know the content of sign_file and origin_file, how can I do the verify work in python without creating files?


Solution

  • I find a solution on question: How do you verify an RSA SHA1 signature in Python?

    Below is the demo to do the verify work:

    import base64
    
    from M2Crypto import BIO, RSA, EVP
    
    ori = "content of origin string"
    sig = "content of signature string"
    
    with open("./public.pem") as f:
        pem = f.read()
        bio = BIO.MemoryBuffer(pem)
        rsa = RSA.load_pub_key_bio(bio)
    
        pubkey = EVP.PKey()
        pubkey.assign_rsa(rsa)
        pubkey.reset_context(md="sha256")
        pubkey.verify_init()
        pubkey.verify_update(ori)
        print pubkey.verify_final(base64.b64decode(sig))  # 1 means verify OK