diff --git a/frontend/sync-client/src/utils/hash.test.ts b/frontend/sync-client/src/utils/hash.test.ts new file mode 100644 index 00000000..9cf80545 --- /dev/null +++ b/frontend/sync-client/src/utils/hash.test.ts @@ -0,0 +1,7 @@ +import assert from "node:assert"; +import { test } from "node:test"; +import { EMPTY_HASH, hash } from "./hash"; + +void test("EMPTY_HASH is the SHA-256 hash of empty content", async () => { + assert.strictEqual(await hash(new Uint8Array()), EMPTY_HASH); +}); diff --git a/frontend/sync-client/src/utils/hash.ts b/frontend/sync-client/src/utils/hash.ts index 906b6fad..508001a8 100644 --- a/frontend/sync-client/src/utils/hash.ts +++ b/frontend/sync-client/src/utils/hash.ts @@ -1,12 +1,13 @@ -// 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 { + const digest = await globalThis.crypto.subtle.digest( + "SHA-256", + new Uint8Array(content) + ); + + return Array.from(new Uint8Array(digest), (byte) => + byte.toString(16).padStart(2, "0") + ).join(""); } -export const EMPTY_HASH = hash(new Uint8Array(0)); +export const EMPTY_HASH = + "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";