Created
March 2, 2021 13:50
-
-
Save helen-dikareva/060c9dcd5bfaab6064086ba0527bcabf to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const fs = require('fs'); | |
const crypto = require('crypto'); | |
function hashFile (file, algorithm = 'sha512', encoding = 'base64', options) { | |
return new Promise((resolve, reject) => { | |
const hash = crypto.createHash(algorithm); | |
hash.on('error', reject).setEncoding(encoding); | |
fs.createReadStream( | |
file, | |
Object.assign({}, options, { | |
highWaterMark: 1024 * 1024, | |
/* better to use more memory but hash faster */ | |
}) | |
) | |
.on('error', reject) | |
.on('end', () => { | |
hash.end(); | |
console.log('hash done for '+ file); | |
console.log(hash.read()); | |
console.log(); | |
resolve(hash.read()); | |
}) | |
.pipe( | |
hash, | |
{ | |
end: false, | |
} | |
); | |
}); | |
} | |
fs.readdir(__dirname, (err, files) => { | |
files.forEach(file => { | |
hashFile(file); | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment