Skip to content

Instantly share code, notes, and snippets.

@elis
Forked from zfael/nodejs.verifysignedfile.js
Created March 23, 2021 21:16
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 elis/8a08135171b40675b45052d8d546b078 to your computer and use it in GitHub Desktop.
Save elis/8a08135171b40675b45052d8d546b078 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