summaryrefslogtreecommitdiff
path: root/std/hash/_wasm/hash.ts
diff options
context:
space:
mode:
authorCasper Beyer <caspervonb@pm.me>2021-02-02 19:05:46 +0800
committerGitHub <noreply@github.com>2021-02-02 12:05:46 +0100
commit6abf126c2a7a451cded8c6b5e6ddf1b69c84055d (patch)
treefd94c013a19fcb38954844085821ec1601c20e18 /std/hash/_wasm/hash.ts
parenta2b5d44f1aa9d64f448a2a3cc2001272e2f60b98 (diff)
chore: remove std directory (#9361)
This removes the std folder from the tree. Various parts of the tests are pretty tightly dependent on std (47 direct imports and 75 indirect imports, not counting the cli tests that use them as fixtures) so I've added std as a submodule for now.
Diffstat (limited to 'std/hash/_wasm/hash.ts')
-rw-r--r--std/hash/_wasm/hash.ts76
1 files changed, 0 insertions, 76 deletions
diff --git a/std/hash/_wasm/hash.ts b/std/hash/_wasm/hash.ts
deleted file mode 100644
index eb6636dab..000000000
--- a/std/hash/_wasm/hash.ts
+++ /dev/null
@@ -1,76 +0,0 @@
-// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
-
-import init, {
- source,
- create_hash as createHash,
- update_hash as updateHash,
- digest_hash as digestHash,
- DenoHash,
-} 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";
-
-await init(source);
-
-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);
- }
-
- updateHash(this.#hash, msg);
-
- 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 hex.encodeToString(finalized);
- case "base64":
- return base64.encode(finalized);
- default:
- throw new Error("hash: invalid format");
- }
- }
-}