I am hashing files in NodeJS using code similar to this:
import { createReadStream } from 'fs';
import { createHash } from 'crypto';
import stream from 'stream/promises';
export async function fileHash(filePath, algorithm = 'sha256') {
const input = createReadStream(filePath);
const hash = createHash(algorithm);
await stream.pipeline(input, hash);
return hash.digest('hex');
}
However when run on Linux, MacOS and Windows systems this can create different hashes due to different end of lines (EOL).
When I update my code to strip EOL differences:
import { createReadStream } from 'fs';
import { createHash } from 'crypto';
import stream from 'stream/promises';
import { Transform } from 'stream';
export async function fileHash(filePath, algorithm = 'sha256') {
const normalizeLineEndings = new Transform({
transform(chunk: Buffer, encoding, callback) {
const normalized = chunk.toString('utf8').replace(/\r\n/g, '\n').replace(/\r/g, '\n');
callback(null, Buffer.from(normalized));
},
});
const input = createReadStream(filePath);
const hash = createHash(algorithm);
await stream.pipeline(input, normalizeLineEndings, hash);
return hash.digest('hex');
}
Using this code produces consistent hashes across Operating Systems, but the hashes do not match a hash generated using a system program e.g.
shasum -a 256 myfile.zip
Is there away to generate hashes that match system tools AND produce the same hash cross Operating System?
I am hashing files in NodeJS using code similar to this:
import { createReadStream } from 'fs';
import { createHash } from 'crypto';
import stream from 'stream/promises';
export async function fileHash(filePath, algorithm = 'sha256') {
const input = createReadStream(filePath);
const hash = createHash(algorithm);
await stream.pipeline(input, hash);
return hash.digest('hex');
}
However when run on Linux, MacOS and Windows systems this can create different hashes due to different end of lines (EOL).
When I update my code to strip EOL differences:
import { createReadStream } from 'fs';
import { createHash } from 'crypto';
import stream from 'stream/promises';
import { Transform } from 'stream';
export async function fileHash(filePath, algorithm = 'sha256') {
const normalizeLineEndings = new Transform({
transform(chunk: Buffer, encoding, callback) {
const normalized = chunk.toString('utf8').replace(/\r\n/g, '\n').replace(/\r/g, '\n');
callback(null, Buffer.from(normalized));
},
});
const input = createReadStream(filePath);
const hash = createHash(algorithm);
await stream.pipeline(input, normalizeLineEndings, hash);
return hash.digest('hex');
}
Using this code produces consistent hashes across Operating Systems, but the hashes do not match a hash generated using a system program e.g.
shasum -a 256 myfile.zip
Is there away to generate hashes that match system tools AND produce the same hash cross Operating System?
Share Improve this question asked Nov 19, 2024 at 6:40 Kim TKim T 6,4922 gold badges62 silver badges84 bronze badges 2 |1 Answer
Reset to default 0I was hashing a file committed to a git code repository. During git checkout file line-endings are updated based on the operating system. This meant that the same file in git was producing different hashes. The solution is to not hash files from git. Download the file independently of git.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745583260a4634378.html
git checkout
– Kim T Commented Nov 19, 2024 at 15:58