Created
June 20, 2017 13:57
-
-
Save zfael/a1a6913944c55843ed3e999b16350b50 to your computer and use it in GitHub Desktop.
NODE.JS - How to generate file's Checksum (CRYPTO)
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
var fs = require('fs'); | |
var crypto = require('crypto'); | |
fs.readFile('file.pdf', function(err, data) { | |
var checksum = generateChecksum(data); | |
console.log(checksum); | |
}); | |
function generateChecksum(str, algorithm, encoding) { | |
return crypto | |
.createHash(algorithm || 'md5') | |
.update(str, 'utf8') | |
.digest(encoding || 'hex'); | |
} |
Neat. Thank you!
Thank y♂u Sir!
how to use the output since it is printed in console?
You could change the console.log to a return statement and have the whole fs.readFile function saved to a variable as a function expression. Then you would have your hash string in a variable to use as you wish.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you! 🤓
@github — I miss a 👍 button for gists. 🙃