How to recreate the following csd line command line call using M2Crypto in Python?

This works fine on the command line, I would like to do the same using M2Crypto in Python code.

openssl smime -binary -sign -signer certificate.pem -inkey key.pem \ -in some_file.txt -out signed_file -outform DER \ -passin pass:somepassword 
+4
source share
1 answer

This is how I used M2Crypto to sign the file.

 text = open('/path/to/some_file.txt').read() passphrase = 'somepassword' buffer = M2Crypto.BIO.MemoryBuffer(text) signer = M2Crypto.SMIME.SMIME() signer.load_key('/path/to/key.pem', '/path/to/certificate.pem', lambda x: passphrase) p7 = signer.sign(buffer, flags=M2Crypto.SMIME.PKCS7_DETACHED) out = M2Crypto.BIO.MemoryBuffer() p7.write_der(out) signature = out.getvalue() print signature 

This works well for me. You may need to play signer.sign with flags in signer.sign if your signature is not exactly the way you want it.

+1
source

All Articles