I wanted to perform RSA-SHA512 on a file in node.js. I could calculate sha512 hash of the given data file which matches with that of openssl's. However when trying to get the digital signature on the same hash, node.js signature differs from openssl signature. Below is an example code snippet:
var data = new Buffer(512);
data = fs.readFileSync('/tmp/data');
var pem = fs.readFileSync('/tmp/boot2-prvKey.pem');
var privateKey = pem.toString('ascii');
var signer = crypto.createSign("RSA-SHA256");
signer.update(data);
var sign = signer.sign(privateKey, 'hex');
console.log("SIGN " + sign + '\n');
Openssl command to sign the data:
openssl rsautl -sign -in /tmp/data -inkey /tmp/boot2-prvKey.pem -out sig
Both of the above generates different signatures.
I have couple of questions 1) I want to calculate RSA-SHA256 of a file, so I calculate sha256 hash of the entire file first and pass this hash as an input to signing function. Is that a correct approach? 2) If yes, what might have gone wrong in the code above ? If no, what would be the correct approach ?
node.js version I am using is 0.10.36 and openssl version is 1.0,1.