Last active
April 10, 2020 12:08
-
-
Save mei23/b067b55cccfcb101c07f1c20f4561aa9 to your computer and use it in GitHub Desktop.
Calculate hash from file path in Node v10 or later
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
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