Change to SHA256 hashing
This commit is contained in:
parent
5624e51f52
commit
02f70d5f9f
2 changed files with 18 additions and 10 deletions
7
frontend/sync-client/src/utils/hash.test.ts
Normal file
7
frontend/sync-client/src/utils/hash.test.ts
Normal file
|
|
@ -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);
|
||||||
|
});
|
||||||
|
|
@ -1,12 +1,13 @@
|
||||||
// https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript
|
export async function hash(content: Uint8Array): Promise<string> {
|
||||||
export function hash(content: Uint8Array): string {
|
const digest = await globalThis.crypto.subtle.digest(
|
||||||
let result = 0;
|
"SHA-256",
|
||||||
// eslint-disable-next-line @typescript-eslint/prefer-for-of
|
new Uint8Array(content)
|
||||||
for (let i = 0; i < content.length; i++) {
|
);
|
||||||
result = (result << 5) - result + content[i];
|
|
||||||
result |= 0; // Convert to 32bit integer
|
return Array.from(new Uint8Array(digest), (byte) =>
|
||||||
}
|
byte.toString(16).padStart(2, "0")
|
||||||
return Math.abs(result).toString(16).padStart(8, "0");
|
).join("");
|
||||||
}
|
}
|
||||||
|
|
||||||
export const EMPTY_HASH = hash(new Uint8Array(0));
|
export const EMPTY_HASH =
|
||||||
|
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue