SHA hash in NodeJS

Posted by Joe Enos on June 10, 2021 · 1 min read
const crypto = require("crypto");
const filename = process.argv[2];
const fs = require("fs");

const hash = crypto.createHash("sha256");

const input = fs.createReadStream(filename);
input.on("readable", () => {
const data = input.read();
if (data) {
hash.update(data);
} else {
console.log(`${hash.digest("hex")} ${filename}`);
}
});

Reference

https://nodejs.org/api/crypto.html