Question:
What is the best method for generating a unique and consistent checksum across all browsers? Additionally, how can a SHA256/MD5 checksum string be converted to 64-bit?
How can files be read without requiring excessive amounts of RAM when generating a checksum? For example, how would one handle a 1 GB file without compromising RAM usage?
For more information on reading files without loading them into memory, you can visit this helpful thread on Stack Overflow.
If you're looking for a promising project to assist with this process, consider checking out this particular project.
I am aiming to generate the checksum progressively in chunks of X MBs to avoid overwhelming RAM usage. Below is the code I have been working with:
let SIZE_CHECKSUM = 10 * Math.pow(1024, 2); // 10 MB; Can also be adjusted to 1 MB
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();
}
This approach has shown varying results in different web browsers.