Skip to content

Instantly share code, notes, and snippets.

@zfael
Created June 20, 2017 13:57
Show Gist options
  • Star 45 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save zfael/a1a6913944c55843ed3e999b16350b50 to your computer and use it in GitHub Desktop.
Save zfael/a1a6913944c55843ed3e999b16350b50 to your computer and use it in GitHub Desktop.
NODE.JS - How to generate file's Checksum (CRYPTO)
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');
}
@Disorrder
Copy link

Neat. Thank you!

@sihunqu123
Copy link

Thank y♂u Sir!

@restimguay
Copy link

how to use the output since it is printed in console?

@neighborhoodnerd21
Copy link

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