Created
February 28, 2024 05:48
-
-
Save modrobert/7484196f2c6dac606df867533399956c to your computer and use it in GitHub Desktop.
nodejs sha256 implementation
This file contains 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
#!/usr/bin/env node | |
// sha256.js by modrobert in 2024 | |
"use strict"; | |
const fs = require("fs"); | |
const crypto = require("crypto"); | |
if (process.argv.length === 2 || process.argv[2] === "-h") { | |
console.error("usage; sha256.js <file> || -"); | |
process.exit(1); | |
} | |
let fileName = process.argv[2]; | |
const sha256 = async (fileName) => { | |
let input; | |
if (fileName === "-") { | |
input = process.stdin; | |
} else { | |
input = fs.createReadStream(fileName); | |
} | |
let hasher = crypto.createHash("sha256"); | |
for await (let chunk of input) { | |
hasher.update(chunk); | |
} | |
return hasher.digest("hex"); | |
}; | |
sha256(fileName).then(hash => { console.log(hash); }); |
Author
modrobert
commented
Feb 28, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment