Question:
How to generate a checksum correctly, which is unique, consistent independent of browsers? Also, I would like to convert a SHA256/MD5 checksum string to 64-bit.
How to properly read a file without huge RAM requirement to generate checksum? i.e. how do we deal with 1 GB file without compromising RAM
e.g. Is it possible to read a file without loading it into memory? (see the answer)
This project seems promising, but couldn't get it worked either.
My intention is to generate the checksum progressively/incrementally in chunks of X MBs. This may help to avoid using too much RAM at a time.
Following is the code, which is not working as expected:
let SIZE_CHECKSUM = 10 * Math.pow(1024, 2); // 10 MB; But can be 1 MB too
async function GetChecksum (file: File):
Promise<string>
{
let hashAlgorithm: CryptoJS.lib.IHasher<Object> = CryptoJS.algo.SHA256.create();
let totalChunks: number = Math.ceil(file.size / SIZE_CHECKSUM);
for (let chunkCount = 0, start = 0, end = 0; chunkCount < totalChunks; ++chunkCount)
{
end = Math.min(start + SIZE_CHECKSUM, file.size);
let resultChunk: string = await (new Response(file.slice(start, end)).text());
hashAlgorithm.update(resultChunk);
start = chunkCount * SIZE_CHECKSUM;
}
let long: bigInt.BigInteger = bigInt.fromArray(hashAlgorithm.finalize().words, 16, false);
if(long.compareTo(bigInt.zero) < 0)
long = long.add(bigInt.one.shiftLeft(64));
return long.toString();
}
It shows different results in different browsers.