-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdigest.ts
30 lines (28 loc) · 1.12 KB
/
digest.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* This module provides cryptographic hash digests of JSON trees.
* It guarantee that `digest()` function always returns the same digest
* for the equivalent JSON trees. It means you don't have to care about
* the order of how object keys occurs or how characters in string are encoded.
* It also provides various hash algorithms; see also the docs of Deno's
* std/crypto module.
* @license LGPL-3.0-or-later
*/
import { crypto, type DigestAlgorithmType } from "./crypto.ts";
import { canonicalize, type Tree } from "./canon.ts";
/**
* Digests a tree using the specified hash algorithm.
* @param algorithm The hash algorithm to use.
* @param data The JSON tree to digest.
* @returns The hash digest.
* @throws {DOMException} Thrown when the algorithm is not supported.
*/
export async function digest(
algorithm: DigestAlgorithmType,
data: Tree,
): Promise<Uint8Array> {
const canon = canonicalize(data);
const canonBytes = new TextEncoder().encode(canon);
const digestBuffer = await crypto.subtle.digest(algorithm, canonBytes);
return new Uint8Array(digestBuffer);
}
export { type DigestAlgorithmType, type Tree };