summaryrefslogtreecommitdiff
path: root/cli/tests/unit_node/crypto/crypto_hash_test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'cli/tests/unit_node/crypto/crypto_hash_test.ts')
-rw-r--r--cli/tests/unit_node/crypto/crypto_hash_test.ts110
1 files changed, 106 insertions, 4 deletions
diff --git a/cli/tests/unit_node/crypto/crypto_hash_test.ts b/cli/tests/unit_node/crypto/crypto_hash_test.ts
index 679577770..e140765ec 100644
--- a/cli/tests/unit_node/crypto/crypto_hash_test.ts
+++ b/cli/tests/unit_node/crypto/crypto_hash_test.ts
@@ -1,10 +1,15 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
-import { createHash, createHmac } from "node:crypto";
-import { assertEquals } from "../../../../test_util/std/testing/asserts.ts";
+import { createHash, createHmac, getHashes, randomUUID } from "node:crypto";
+import { Buffer } from "node:buffer";
+import { Readable } from "node:stream";
+import {
+ assert,
+ assertEquals,
+} from "../../../../test_util/std/testing/asserts.ts";
// https://github.com/denoland/deno/issues/18140
Deno.test({
- name: "createHmac digest",
+ name: "[node/crypto] createHmac digest",
fn() {
assertEquals(
createHmac("sha256", "secret").update("hello").digest("hex"),
@@ -14,7 +19,7 @@ Deno.test({
});
Deno.test({
- name: "createHash digest",
+ name: "[node/crypto] createHash digest",
fn() {
assertEquals(
createHash("sha256").update("hello").digest("hex"),
@@ -22,3 +27,100 @@ Deno.test({
);
},
});
+
+Deno.test("[node/crypto.Hash] basic usage - buffer output", () => {
+ const d = createHash("sha1").update("abc").update("def").digest();
+ assertEquals(
+ d,
+ Buffer.from([
+ 0x1f,
+ 0x8a,
+ 0xc1,
+ 0xf,
+ 0x23,
+ 0xc5,
+ 0xb5,
+ 0xbc,
+ 0x11,
+ 0x67,
+ 0xbd,
+ 0xa8,
+ 0x4b,
+ 0x83,
+ 0x3e,
+ 0x5c,
+ 0x5,
+ 0x7a,
+ 0x77,
+ 0xd2,
+ ]),
+ );
+});
+
+Deno.test("[node/crypto.Hash] basic usage - hex output", () => {
+ const d = createHash("sha1").update("abc").update("def").digest("hex");
+ assertEquals(d, "1f8ac10f23c5b5bc1167bda84b833e5c057a77d2");
+});
+
+Deno.test("[node/crypto.Hash] basic usage - base64 output", () => {
+ const d = createHash("sha1").update("abc").update("def").digest("base64");
+ assertEquals(d, "H4rBDyPFtbwRZ72oS4M+XAV6d9I=");
+});
+
+Deno.test("[node/crypto.Hash] basic usage - base64url output", () => {
+ const d = createHash("sha1").update("abc").update("def").digest("base64url");
+ assertEquals(d, "H4rBDyPFtbwRZ72oS4M-XAV6d9I");
+});
+
+Deno.test("[node/crypto.Hash] streaming usage", async () => {
+ const source = Readable.from(["abc", "def"]);
+ const hash = createHash("sha1");
+ const dest = source.pipe(hash);
+ const result = await new Promise((resolve, _) => {
+ let buffer = Buffer.from([]);
+ dest.on("data", (data) => {
+ buffer = Buffer.concat([buffer, data]);
+ });
+ dest.on("end", () => {
+ resolve(buffer);
+ });
+ });
+ assertEquals(
+ result,
+ Buffer.from([
+ 0x1f,
+ 0x8a,
+ 0xc1,
+ 0xf,
+ 0x23,
+ 0xc5,
+ 0xb5,
+ 0xbc,
+ 0x11,
+ 0x67,
+ 0xbd,
+ 0xa8,
+ 0x4b,
+ 0x83,
+ 0x3e,
+ 0x5c,
+ 0x5,
+ 0x7a,
+ 0x77,
+ 0xd2,
+ ]),
+ );
+});
+
+Deno.test("[node/crypto.getHashes]", () => {
+ for (const algorithm of getHashes()) {
+ const d = createHash(algorithm).update("abc").digest();
+ assert(d instanceof Buffer);
+ assert(d.length > 0);
+ }
+});
+
+Deno.test("[node/crypto.getRandomUUID] works the same way as Web Crypto API", () => {
+ assertEquals(randomUUID().length, crypto.randomUUID().length);
+ assertEquals(typeof randomUUID(), typeof crypto.randomUUID());
+});