diff options
author | Divy Srivastava <dj.srivastava23@gmail.com> | 2023-04-06 22:26:56 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-06 16:56:56 +0000 |
commit | df72420d723affb780c0c388b7d704985288f801 (patch) | |
tree | 6e6da7d1a93930c6742c98c838eeae55678e144e /ext/node/polyfills | |
parent | 2d0a9ffbccf9d8a4773eb6efa48ddc6978af6455 (diff) |
fix(ext/node): implement hkdf-expand (#18612)
Towards https://github.com/denoland/deno/issues/18455
Diffstat (limited to 'ext/node/polyfills')
-rw-r--r-- | ext/node/polyfills/internal/crypto/hkdf.ts | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/ext/node/polyfills/internal/crypto/hkdf.ts b/ext/node/polyfills/internal/crypto/hkdf.ts index deeba102f..fb26053df 100644 --- a/ext/node/polyfills/internal/crypto/hkdf.ts +++ b/ext/node/polyfills/internal/crypto/hkdf.ts @@ -7,6 +7,7 @@ import { validateString, } from "ext:deno_node/internal/validators.mjs"; import { + ERR_CRYPTO_INVALID_DIGEST, ERR_INVALID_ARG_TYPE, ERR_OUT_OF_RANGE, hideStackFrames, @@ -26,17 +27,19 @@ import { isAnyArrayBuffer, isArrayBufferView, } from "ext:deno_node/internal/util/types.ts"; -import { notImplemented } from "ext:deno_node/_utils.ts"; -const validateParameters = hideStackFrames((hash, key, salt, info, length) => { - key = prepareKey(key); - salt = toBuf(salt); - info = toBuf(info); +const { core } = globalThis.__bootstrap; +const { ops } = core; +const validateParameters = hideStackFrames((hash, key, salt, info, length) => { validateString(hash, "digest"); + key = new Uint8Array(prepareKey(key)); validateByteSource(salt, "salt"); validateByteSource(info, "info"); + salt = new Uint8Array(toBuf(salt)); + info = new Uint8Array(toBuf(info)); + validateInteger(length, "length", 0, kMaxLength); if (info.byteLength > 1024) { @@ -91,7 +94,7 @@ export function hkdf( salt: BinaryLike, info: BinaryLike, length: number, - callback: (err: Error | null, derivedKey: ArrayBuffer) => void, + callback: (err: Error | null, derivedKey: ArrayBuffer | undefined) => void, ) { ({ hash, key, salt, info, length } = validateParameters( hash, @@ -103,7 +106,9 @@ export function hkdf( validateFunction(callback, "callback"); - notImplemented("crypto.hkdf"); + core.opAsync("op_node_hkdf_async", hash, key, salt, info, length) + .then((okm) => callback(null, okm.buffer)) + .catch((err) => callback(new ERR_CRYPTO_INVALID_DIGEST(err), undefined)); } export function hkdfSync( @@ -121,7 +126,14 @@ export function hkdfSync( length, )); - notImplemented("crypto.hkdfSync"); + const okm = new Uint8Array(length); + try { + ops.op_node_hkdf(hash, key, salt, info, okm); + } catch (e) { + throw new ERR_CRYPTO_INVALID_DIGEST(e); + } + + return okm.buffer; } export default { |