ECDSA sign using OpenSSL without ASN1 encoding the hash

纵然是瞬间 提交于 2019-12-08 01:10:25

问题


Im doing ECDSA signatures using dgst command with OpenSSL as follows:

openssl dgst -sha256 -sign key.pem -out my_signature data_file

which works just fine. However I read in this SO answer that it first SHA256 hashes the data_file, and ASN.1 encodes the hash before signing it.

I would like to create the SHA256 hash of the data and make ECDSA sign just the raw bytes of this hash. (As this is the ECDSA signature, I cannot use rsautl as in the mentioned SO answer.)

How do I achieve this using OpenSSL?


回答1:


You can do it with openssl pkeyutl which is a replacement for openssl rsautl that supports ECDSA.

Suppose you want to hash and sign a 'data.txt' file with openssl. At first you need to hash the file:

openssl dgst -sha256 -binary -out data.sha256 data.txt

after you can sign it:

openssl pkeyutl -sign -inkey private.pem -in data.sha256 -out data.sig

However the signature is still in ASN.1 format. To receive r and s values of signature use openssl asn1parse:

openssl asn1parse -inform DER -in data.sig


来源:https://stackoverflow.com/questions/25421399/ecdsa-sign-using-openssl-without-asn1-encoding-the-hash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!