admin管理员组文章数量:1024627
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.
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- 2 System tools don't care about line endings. Your function shouldn't produce different hashes on different platforms for the same file. If the files have different line endings then the hashes are expected to be different because the files are different. – robertklep Commented Nov 19, 2024 at 7:06
-
I'm testing by hashing a file in the git repository. So git must be transforming the line endings when running a
git checkout
– Kim T Commented Nov 19, 2024 at 15:58
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.
本文标签: nodejsNodeJS consistent file hashing across operating systemsStack Overflow
版权声明:本文标题:node.js - NodeJS consistent file hashing across operating systems - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745583258a2157433.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
git checkout
– Kim T Commented Nov 19, 2024 at 15:58