Update hashing

This commit is contained in:
Andras Schmelczer 2026-03-28 12:07:44 +00:00
parent 65d75dec40
commit 1c6cd80b64
3 changed files with 9 additions and 14 deletions

View file

@ -1,12 +1,7 @@
// https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript
export function hash(content: Uint8Array): string {
let result = 0;
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (let i = 0; i < content.length; i++) {
result = (result << 5) - result + content[i];
result |= 0; // Convert to 32bit integer
}
return Math.abs(result).toString(16).padStart(8, "0");
export async function hash(content: Uint8Array): Promise<string> {
const digest = await crypto.subtle.digest("SHA-256", content);
const bytes = new Uint8Array(digest);
return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
}
export const EMPTY_HASH = hash(new Uint8Array(0));
export const EMPTY_HASH = await hash(new Uint8Array(0));