Created
August 31, 2022 17:40
-
-
Save ins429/d71c35e5079d237dd79d516394c8a0f9 to your computer and use it in GitHub Desktop.
Getting the hash of a file(request.integrity)
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
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