I have a foo.txt
like this:
foo
bar
baz
(LF. No LF at the end.)
And I have the following script:
const { sha1 } = require("crypto-hash");
const fs = require("fs");
const path = require("path");
const crypto = require("crypto");
main();
async function main() {
const filePath = "foo.txt";
const buf = fs.readFileSync(filePath);
const str = fs.readFileSync(filePath, {
encoding: "utf8",
});
console.log(await sha1(buf));
console.log(await sha1(str));
console.log(
await new Promise((resolve, reject) => {
const hash = crypto.createHash("sha1");
const input = fs.createReadStream(filePath);
input.on("readable", () => {
const data = input.read();
if (data) {
hash.update(data);
} else {
resolve(hash.digest("hex"));
}
});
})
);
}
When I run it 3 times I get:
PS C:\Temp> node .\hashtest.js
4c37dbe6f4897fbed62502239273902421e1ea6a
01e06a68df2f0598042449c4088842bb4e92ca75
01e06a68df2f0598042449c4088842bb4e92ca75
PS C:\Temp> node .\hashtest.js
678082bb5b423c3dec7eb593465bd2d2cfa54287
01e06a68df2f0598042449c4088842bb4e92ca75
01e06a68df2f0598042449c4088842bb4e92ca75
PS C:\Temp> node .\hashtest.js
35d9940f295a0205252456e8c808eb5f651711b1
01e06a68df2f0598042449c4088842bb4e92ca75
01e06a68df2f0598042449c4088842bb4e92ca75
The output of sha1(buf)
seems to constantly change, and never match the expected result.
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too