Last active
October 11, 2021 08:52
-
-
Save zfael/aa8e885b64ffa92e4670c9e549b686a4 to your computer and use it in GitHub Desktop.
Node.Js - CRYPTO How to verify a signed file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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