deno: sha1 hex result is different from nodejs crypto
sspilleman opened this issue · comments
Sander Spilleman commented
Hi,
I'm trying your lib as part of migrating some scripts to deno. It seems to work, but somehow I get different results than in nodeJS unfortunately.
Im using the following code on deno:
var processfilesize = Math.round(this.size / divider);
const file = await Deno.open(this.path, { read: true, write: true });
const iterations = Math.floor(processfilesize / 16384);
const remainder = processfilesize % 16384;
const exit = (reason: string) => {
console.log("error", reason);
Deno.exit(1);
}
const shasum = new SHA1();
for (let i = 0; i < iterations; i++) {
const buf = new Uint8Array(16384);
const numberOfBytesRead = await Deno.read(file.rid, buf);
if (numberOfBytesRead !== 16384) exit("something went wrong while reading");
shasum.update(buf);
}
if (remainder > 0) {
const buf = new Uint8Array(remainder);
const numberOfBytesRead = await Deno.read(file.rid, buf);
if (numberOfBytesRead !== remainder) exit("something went wrong while reading");
shasum.update(buf);
}
Deno.close(file.rid);
// console.log(shasum.digest('utf8'));
// console.log(shasum.digest('hex'));
// console.log(shasum.digest('base64'));
this.sha1 = shasum.digest('hex').toString();
On nodeJS Im using the following code:
// console.log('shaFile', file);
var processfilesize = Math.round(file.stats.size / 200);
var shasum = crypto.createHash('sha1');
var readable = fs.createReadStream(file.filepath, {
start: 0,
end: processfilesize
});
readable.on('data', function (d) {
shasum.update(d);
});
readable.on('end', function () {
var d = shasum.digest('hex');
file.sha1 = d;
return callback(null, file);
});
As you can see in both scenarios Im only reading 5% of the file (divider/200)...
But as said.... the hex values are different on both :-(
Do you have any idea what the issue could be? Obviously because I don't have createReadStream
available, I needed to change the reading logic.
Thanks!
Sander
Sander Spilleman commented
Found it. The node code was actually reading 1 bit more from the file :(