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?!