There is a logical issue in the code at below line:
start = chunkCount * SIZE_CHECKSUM; // <--- bug
The variable start
is initialized to 0 and then again reset to 0 in the 1st iteration, which is not right.
Following is the way to get a 32 bytes SHA5 checksum with the same library mentioned in the question: "emn178/js-sha256".
That library doesn't provide a Typescript interface, but we can define trivially as following:
// Sha256.d.ts (also name the corresponding JS file as "Sha256.js")
declare class Sha256 {
update (data: ArrayBuffer): Sha256;
hex (): string;
}
declare var sha256: any;
declare interface sha256 {
create (): Sha256;
}
Then use it as following:
import "./external/Sha256"
async function GetChecksum (file: File):
Promise<string>
{
let algorithm = sha256.create();
for(let chunkCount = 0, totalChunks = Math.ceil(file.size / SIZE_CHECKSUM);
chunkCount < totalChunks;
++chunkCount)
{
let start = chunkCount * SIZE_CHECKSUM, end = Math.min(start + SIZE_CHECKSUM, file.size);
algorithm.update(await (new Response(file.slice(start, end)).arrayBuffer()));
}
return algorithm.hex();
}
Above code generates same checksums in all my browsers for any chunk size.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…