UNPKG

572 BJavaScriptView Raw
1"use strict";
2
3const fs = require('fs');
4const crypto = require('crypto');
5
6function hashFile(file_path, hash, cb) {
7
8 if(!Array.isArray(hash))
9 hash = [hash];
10
11 let hashes = hash.reduce((acc, hash) => ({
12 ...acc,
13 [hash] : crypto.createHash(hash)
14 }), {});
15
16 var input = fs.createReadStream(file_path);
17 for(let hash in hashes)
18 input.on('data', hashes[hash].update.bind(hashes[hash]));
19
20 input.on('end', function() {
21 for(let hash in hashes)
22 hashes[hash] = hashes[hash].digest('hex');
23 cb(null, hashes);
24 });
25}
26
27module.exports = hashFile;