使用 openssl 对文件进行数字签名 [英] Digital signature for a file using openssl
问题描述
有没有办法使用 openssl 对 x509 证书或任何文档进行数字签名?
Is there a way to digitally sign a x509 certificate or any document using openssl?
推荐答案
是的,OpenSSL 的 dgst 和 rsautl 组件可用于计算签名给定一个 RSA 密钥对.
Yes, the dgst and rsautl component of OpenSSL can be used to compute a signature given an RSA key pair.
openssl dgst -sha256 data.txt > hash
openssl rsautl -sign -inkey privatekey.pem -keyform PEM -in hash >signature
仅验证签名:
openssl rsautl -verify -inkey publickey.pem -pubin -keyform PEM -in signature
更新:从下面捕捉 Reto 的评论,因为这是一个重要的细微差别.据推测,如果您要麻烦验证,您想知道签名是在它所附加的明文上产生的:
这对某些人来说可能听起来很明显,但是:请注意,rsautl verify
只是解密文件 signature
.此调用的输出保证由私钥的所有者生成,但除此之外不会检查其他任何内容.因此,要实际验证 data.txt
的一致性,您必须重新生成摘要,然后将其与 openssl rsautl -verify
的输出进行比较.
This might sound obvious for some but: Be aware, rsautl verify
just decrypts the file signature
. The output of this call is guaranteed to be produced by the owner of the private key, but beside that nothing else is being checked. So to actually verify the consistency of data.txt
you have to regenerate the digest and then compare it against the output of openssl rsautl -verify
.
openssl dgst -sha256 -verify publickey.pem -signature signature data.txt
对于此操作,openssl 需要公钥、签名和消息.
For this operation, openssl requires the public key, the signature, and the message.
这篇关于使用 openssl 对文件进行数字签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!