加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

如何在Python中使用M2Crypto重新创建以下签名cmd-line OpenSSL调

发布时间:2020-12-20 13:29:20 所属栏目:Python 来源:网络整理
导读:这在命令行中完美运行,我想在 Python代码中使用M2Crypto做同样的事情. openssl smime -binary -sign -signer certificate.pem -inkey key.pem -in some_file.txt -out signed_file -outform DER -passin pass:somepassword 解决方法 这就是我使用M2Crypt
这在命令行中完美运行,我想在 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中使用标记.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读