Openssl and Node's Crypto have different outputs

廉价感情. 提交于 2019-12-24 02:07:46

问题


in short, I want to sign a string("a") just like openssl using crypto library in node. but I get wrong output over and over again.

Long Description

signing with openssl

I have two generated rsa keys : public-key.pem and private-key.pem that I have made them with openssl :

$ openssl genrsa 2048 > private-key.pem
$ openssl rsa -in private-key.pem -pubout >public-key.pem

when I type these in shell:

$ echo "a" | openssl dgst -sha256 -sign private-key.pem >signature_openssl.bin

It generates a file named siganture_openssl.bin.

signing with node crypto

const crypto = require('crypto')
const sign = crypto.createSign('SHA256')
const fs = require('fs')
const privateKey = fs.readFileSync('./private-key.pem', 'utf8')

sign.write("a")
sign.end()
let res = sign.sign(privateKey, 'binary')
fs.writeFileSync('./signature_node.bin', res, 'binary')

this block generates a file named signature_node.bin.

verifying generated binaries

to verify generated signatues, I do:

$ echo "a" | openssl dgst -sha256 -signature signature_openssl.bin -verify public-key.pem
Verified OK

but node doesn't verify :

$ echo "a" | openssl dgst -sha256 -signature signature_node.bin -verify public-key.pem
Verification Failure

the question is: what am I doing wrong?!

来源:https://stackoverflow.com/questions/48041678/openssl-and-nodes-crypto-have-different-outputs

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