Skip to content

Instantly share code, notes, and snippets.

@monochromer monochromer/hash-lib.js

Last active Oct 17, 2019
Embed
What would you like to do?
node.js hashing files
const crypto = require('crypto');
const fs = require('fs');
async function hashFile(inputStream, algorithm = 'md5', encoding = 'hex') {
return new Promise((resolve, reject) => {
const hash = crypto.createHash(algorithm);
inputStream
.on('error', reject)
.on('data', data => hash.update(data))
.on('end', () => resolve(hash.digest(encoding)))
});
}
async function getHashManifest(filesList) {
const manifest = {};
const promises = filesList.map(file => {
return hashFile(fs.createReadStream(file))
.then(hash => manifest[file] = hash)
});
return Promise
.all(promises)
.then(() => manifest)
}
module.exports = {
hashFile,
getHashManifest
}
const path = require('path');
const { getHashManifest } = require('./hash-lib');
const files = [
path.join(__dirname, 'public', 'style.css'),
path.join(__dirname, 'public', 'client.js'),
path.join(__dirname, 'index.js'),
path.join(__dirname, '.env'),
path.join(__dirname, 'package.json'),
];
getHashManifest(files)
.then(console.log)
.catch(console.error)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.