Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Node.Js - CRYPTO How to verify a signed file
//how to execute: node verify.js <path file that you want to verify> <certificate path> <hash generate by sign.js>
//output: true if files are equal, false otherwise.
var crypto = require('crypto');
var fs = require('fs');
var args = process.argv.slice(2);
var fileName = args[0];
var certPath = args[1];
var sig = args[2];
//openssl req -key key.pem -new -x509 -out cert.pem
var publicPem = fs.readFileSync(certPath);
var pubKey = publicPem.toString();
var buffer = fs.readFileSync(fileName);
var verify = crypto.createVerify('RSA-SHA256');
verify.update(buffer);
var isValid = verify.verify(pubKey, sig, 'hex');
console.log(isValid);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment