Skip to content

Instantly share code, notes, and snippets.

@ins429
Created August 31, 2022 17:40
Show Gist options
  • Save ins429/d71c35e5079d237dd79d516394c8a0f9 to your computer and use it in GitHub Desktop.
Save ins429/d71c35e5079d237dd79d516394c8a0f9 to your computer and use it in GitHub Desktop.
Getting the hash of a file(request.integrity)
const fs = require("fs");
const crypto = require("crypto");
// the file you want to get the hash
const stream = fs.createReadStream("xxx.ext");
const hash = crypto.createHash("sha256");
console.log("hash", hash);
hash.setEncoding("base64");
stream.on("end", function () {
hash.end();
console.log(hash.read()); // the desired sha256sum
});
// read all file and pipe it (write it) to the hash object
stream.pipe(hash);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment