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:
parent
0098fddb10
commit
79fa6028d1
2 changed files with 18 additions and 1 deletions
|
@ -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,
|
||||
|
|
|
@ -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");
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue