import { createHash } from 'crypto' import { createReadStream } from 'fs' /** * Generate hash from file. * @param {string} filename - path to file. * @param {string} algorithm * @returns {Promise} */ export const shasum = (filename, algorithm = 'sha256') => ( new Promise((resolve, reject) => { if (!(filename && typeof (filename) !== 'string')) { reject(new Error('file not found.')) } const hash = createHash(algorithm) const input = createReadStream(filename) input.on('readable', () => { // Only one element is going to be produced by the // hash stream. const data = input.read() if (data) { hash.update(data) } else { const result = hash.digest('hex') console.log(`${result} ${filename}`) resolve(result) } }) hash.on('error', error => { reject(error) }) }) )