13 lines
515 B
TypeScript
13 lines
515 B
TypeScript
/**
|
|
* Generates a SHA-256 checksum for a file.
|
|
* @param file The file to hash.
|
|
* @returns A promise that resolves to the hex string of the checksum.
|
|
*/
|
|
export const generateFileChecksum = async (file: File): Promise<string> => {
|
|
const buffer = await file.arrayBuffer();
|
|
const hashBuffer = await crypto.subtle.digest('SHA-256', buffer);
|
|
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
|
|
return hashHex;
|
|
};
|