Skip to content

Instantly share code, notes, and snippets.

@zfael
Last active October 11, 2021 08:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save zfael/aa8e885b64ffa92e4670c9e549b686a4 to your computer and use it in GitHub Desktop.
Save zfael/aa8e885b64ffa92e4670c9e549b686a4 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