deno.land / std@0.166.0 / crypto / _util.ts
123456789101112131415161718192021222324252627282930313233343536373839// Copyright 2018-2022 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/mod.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);}
Version Info