Skip to content

Instantly share code, notes, and snippets.

@6220119
Forked from zfael/nodejs.checksum.js
Created May 25, 2022 10:53
Show Gist options
  • Save 6220119/b603627874be1e67f1525062fc98b1f4 to your computer and use it in GitHub Desktop.
Save 6220119/b603627874be1e67f1525062fc98b1f4 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');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment