这在命令行中完美运行,我想在Python代码中使用M2Crypto做同样的事情.
openssl smime -binary -sign -signer certificate.pem -inkey key.pem \
-in some_file.txt -out signed_file -outform DER \
-passin pass:somepassword
解决方法:
这就是我使用M2Crypto签名文件的方式.
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
这对我很有用.如果您的签名不完全符合您的要求,您可能需要在signer.sign中使用标记.