deno.land / std@0.177.1 / crypto / to_hash_string.ts

to_hash_string.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
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.import { encode as hexEncode } from "../encoding/hex.ts";import { encode as base64Encode } from "../encoding/base64.ts";
const decoder = new TextDecoder();
/** * Converts a hash to a string with a given encoding. * @example * ```ts * import { crypto } from "https://deno.land/std@$STD_VERSION/crypto/crypto.ts"; * import { toHashString } from "https://deno.land/std@$STD_VERSION/crypto/to_hash_string.ts" * * const hash = await crypto.subtle.digest("SHA-384", new TextEncoder().encode("You hear that Mr. Anderson?")); * * // Hex encoding by default * console.log(toHashString(hash)); * * // Or with base64 encoding * console.log(toHashString(hash, "base64")); * ``` */export function toHashString( hash: ArrayBuffer, encoding: "hex" | "base64" = "hex",): string { switch (encoding) { case "hex": return decoder.decode(hexEncode(new Uint8Array(hash))); case "base64": return base64Encode(hash); }}
std

Version Info

Tagged at
11 months ago