Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romuloctba/0e1a86b2fb44e38312032ae94cc0ef01 to your computer and use it in GitHub Desktop.
Save romuloctba/0e1a86b2fb44e38312032ae94cc0ef01 to your computer and use it in GitHub Desktop.
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