Skip to content

Instantly share code, notes, and snippets.

@mei23
Last active April 10, 2020 12:08
Show Gist options
  • Save mei23/b067b55cccfcb101c07f1c20f4561aa9 to your computer and use it in GitHub Desktop.
Save mei23/b067b55cccfcb101c07f1c20f4561aa9 to your computer and use it in GitHub Desktop.
Calculate hash from file path in Node v10 or later
import * as fs from 'fs';
import * as crypto from 'crypto';
import * as stream from 'stream';
import * as util from 'util';
const pipeline = util.promisify(stream.pipeline);
export async function calcHash(path: string, algorithm: 'md5' | 'sha1' | 'sha256' = 'md5') {
const hash = crypto.createHash(algorithm).setEncoding('hex');
await pipeline(fs.createReadStream(path), hash);
return hash.read();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment