Skip to main content
Module

std/hash/_wasm/hash.ts

Deno standard library
Go to Latest
File
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { create_hash as createHash, DenoHash, digest_hash as digestHash, update_hash as updateHash,} from "./wasm.js";
import * as hex from "../../encoding/hex.ts";import * as base64 from "../../encoding/base64.ts";import type { Hasher, Message, OutputFormat } from "../hasher.ts";
const TYPE_ERROR_MSG = "hash: `data` is invalid type";
export class Hash implements Hasher { #hash: DenoHash; #digested: boolean;
constructor(algorithm: string) { this.#hash = createHash(algorithm); this.#digested = false; }
/** * Update internal state * @param data data to update */ update(data: Message): this { let msg: Uint8Array;
if (typeof data === "string") { msg = new TextEncoder().encode(data as string); } else if (typeof data === "object") { if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) { msg = new Uint8Array(data); } else { throw new Error(TYPE_ERROR_MSG); } } else { throw new Error(TYPE_ERROR_MSG); }
// Messages will be split into chunks of this size to avoid unneccessarily // increasing the size of the WASM heap. const CHUNK_SIZE = 65_536;
for (let offset = 0; offset < msg.length; offset += CHUNK_SIZE) { updateHash( this.#hash, new Uint8Array( msg.buffer, offset, Math.min(CHUNK_SIZE, msg.length - offset), ), ); }
return this; }
/** Returns final hash */ digest(): ArrayBuffer { if (this.#digested) throw new Error("hash: already digested");
this.#digested = true; return digestHash(this.#hash); }
/** * Returns hash as a string of given format * @param format format of output string (hex or base64). Default is hex */ toString(format: OutputFormat = "hex"): string { const finalized = new Uint8Array(this.digest());
switch (format) { case "hex": return new TextDecoder().decode(hex.encode(finalized)); case "base64": return base64.encode(finalized); default: throw new Error("hash: invalid format"); } }}