/
_util.ts
38 lines (36 loc) · 1.06 KB
/
_util.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
31
32
33
34
35
36
37
38
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import { crypto, type DigestAlgorithm } from "./mod.ts";
const encoder = new TextEncoder();
/**
* Creates a hash from a string or binary data, taking care of the boilerplate required for most cases.
*
* @example <caption>Before:</caption>
* ```ts
* import { crypto } from "https://deno.land/std@$STD_VERSION/crypto/crypto.ts";
*
* const encoder = new TextEncoder();
*
* const hash = await crypto.subtle.digest("SHA-1", encoder.encode("Hello, world!"));
* ```
*
* @example <caption>After:</caption>
* ```ts
* import { createHash } from "https://deno.land/std@$STD_VERSION/crypto/_util.ts";
*
* const hash = await createHash("SHA-1", "Hello, world!");
* ```
* @private
*/
export async function createHash(
algorithm: DigestAlgorithm,
data:
| string
| BufferSource
| AsyncIterable<BufferSource>
| Iterable<BufferSource>,
): Promise<ArrayBuffer> {
if (typeof data === "string") {
data = encoder.encode(data);
}
return await crypto.subtle.digest(algorithm, data);
}