1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
export type ShaAlgorithm = "SHA-256" | "SHA-512";
export async function sha(
input: string | BufferSource | AsyncIterable<BufferSource> | Iterable<BufferSource>,
algo: ShaAlgorithm = "SHA-256",
) {
const [{ crypto }, { encodeHex }] = await Promise.all([
import("https://deno.land/std@0.207.0/crypto/mod.ts"),
import("https://deno.land/std@0.207.0/encoding/hex.ts"),
]);
const messageBuffer = typeof input === "string"
? new TextEncoder().encode(input)
: input;
const hashBuffer = await crypto.subtle.digest(algo, messageBuffer);
return encodeHex(hashBuffer);
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
February 27, 2024