Repository: denoland/deno (language: rust)

Issue:
Implement crypto.hash
Stability 1 in Node

https://nodejs.org/api/crypto.html#cryptodiffiehellmanoptions


Discussion / hints:

Fix patch:
diff --git a/ext/node/polyfills/crypto.ts b/ext/node/polyfills/crypto.ts
index ff3bdc7752da6b..acea5c8629797c 100644
--- a/ext/node/polyfills/crypto.ts
+++ b/ext/node/polyfills/crypto.ts
@@ -169,6 +169,16 @@ function getRandomValues(typedArray) {
   return webcrypto.getRandomValues(typedArray);
 }
 
+function hash(
+  algorithm: string,
+  data: BinaryLike,
+  outputEncoding: BinaryToTextEncoding = "hex",
+) {
+  const hash = createHash(algorithm);
+  hash.update(data);
+  return hash.digest(outputEncoding);
+}
+
 function createCipheriv(
   algorithm: CipherCCMTypes,
   key: CipherKey,
@@ -350,6 +360,7 @@ export default {
   getDiffieHellman,
   getFips,
   getHashes,
+  hash,
   Hash,
   hkdf,
   hkdfSync,
@@ -489,6 +500,7 @@ export {
   getHashes,
   getRandomValues,
   Hash,
+  hash,
   hkdf,
   hkdfSync,
   Hmac,


Test patch:
diff --git a/tests/unit_node/crypto/crypto_hash_test.ts b/tests/unit_node/crypto/crypto_hash_test.ts
index 0a3fd2245ecd12..5c0421a9eb7f7e 100644
--- a/tests/unit_node/crypto/crypto_hash_test.ts
+++ b/tests/unit_node/crypto/crypto_hash_test.ts
@@ -1,5 +1,5 @@
 // Copyright 2018-2025 the Deno authors. MIT license.
-import { createHash, createHmac, getHashes } from "node:crypto";
+import { createHash, createHmac, getHashes, hash } from "node:crypto";
 import { Buffer } from "node:buffer";
 import { Readable } from "node:stream";
 import { assert, assertEquals } from "@std/assert";
@@ -127,3 +127,8 @@ Deno.test("[node/crypto.hash] does not leak", () => {
   const hasher = createHash("sha1");
   hasher.update("abc");
 });
+
+Deno.test("[node/crypto.hash] oneshot hash API", () => {
+  const d = hash("sha1", "Node.js");
+  assertEquals(d, "10b3493287f831e81a438811a1ffba01f8cec4b7");
+});