0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-02-01 12:16:11 -05:00

fix(ext/node): implement crypto.hash (#27858)

Implement
[`crypto.hash`](https://nodejs.org/api/crypto.html#cryptohashalgorithm-data-outputencoding)
- one-shot version of `createHash`

Fixes #24945
This commit is contained in:
Divy Srivastava 2025-01-29 20:49:43 +05:30 committed by GitHub
parent 0098fddb10
commit 79fa6028d1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 1 deletions

View file

@ -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,

View file

@ -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");
});