diff options
author | Divy Srivastava <dj.srivastava23@gmail.com> | 2023-04-06 18:39:25 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-06 18:39:25 +0530 |
commit | 3b62a58818f83e32fc2644f44e75a5c8465b2003 (patch) | |
tree | cf8f0a6d7995fa0c632247f2679295b8ceea4460 /ext/node/polyfills/internal/crypto/keygen.ts | |
parent | 339165bd9565806374fa842dfc217dcc5ebabac5 (diff) |
fix(ext/node): add symmetric keygen (#18609)
Towards #18455
Diffstat (limited to 'ext/node/polyfills/internal/crypto/keygen.ts')
-rw-r--r-- | ext/node/polyfills/internal/crypto/keygen.ts | 79 |
1 files changed, 66 insertions, 13 deletions
diff --git a/ext/node/polyfills/internal/crypto/keygen.ts b/ext/node/polyfills/internal/crypto/keygen.ts index dadc9c198..b490cedd7 100644 --- a/ext/node/polyfills/internal/crypto/keygen.ts +++ b/ext/node/polyfills/internal/crypto/keygen.ts @@ -2,18 +2,80 @@ // Copyright Joyent, Inc. and Node.js contributors. All rights reserved. MIT license. import { KeyObject } from "ext:deno_node/internal/crypto/keys.ts"; +import { kAesKeyLengths } from "ext:deno_node/internal/crypto/util.ts"; +import { + SecretKeyObject, + setOwnedKey, +} from "ext:deno_node/internal/crypto/keys.ts"; import { notImplemented } from "ext:deno_node/_utils.ts"; +import { ERR_INVALID_ARG_VALUE } from "ext:deno_node/internal/errors.ts"; +import { + validateFunction, + validateInteger, + validateObject, + validateOneOf, + validateString, +} from "ext:deno_node/internal/validators.mjs"; import { Buffer } from "ext:deno_node/buffer.ts"; import { KeyFormat, KeyType } from "ext:deno_node/internal/crypto/types.ts"; +const { core } = globalThis.__bootstrap; +const { ops } = core; + +function validateGenerateKey( + type: "hmac" | "aes", + options: { length: number }, +) { + validateString(type, "type"); + validateObject(options, "options"); + const { length } = options; + switch (type) { + case "hmac": + validateInteger(length, "options.length", 8, 2 ** 31 - 1); + break; + case "aes": + validateOneOf(length, "options.length", kAesKeyLengths); + break; + default: + throw new ERR_INVALID_ARG_VALUE( + "type", + type, + "must be a supported key type", + ); + } +} + +export function generateKeySync( + type: "hmac" | "aes", + options: { + length: number; + }, +): KeyObject { + validateGenerateKey(type, options); + const { length } = options; + + const key = new Uint8Array(Math.floor(length / 8)); + ops.op_node_generate_secret(key); + + return new SecretKeyObject(setOwnedKey(key)); +} + export function generateKey( - _type: "hmac" | "aes", - _options: { + type: "hmac" | "aes", + options: { length: number; }, - _callback: (err: Error | null, key: KeyObject) => void, + callback: (err: Error | null, key: KeyObject) => void, ) { - notImplemented("crypto.generateKey"); + validateGenerateKey(type, options); + validateFunction(callback, "callback"); + const { length } = options; + + core.opAsync("op_node_generate_secret_async", Math.floor(length / 8)).then( + (key) => { + callback(null, new SecretKeyObject(setOwnedKey(key))); + }, + ); } export interface BasePrivateKeyEncodingOptions<T extends KeyFormat> { @@ -662,15 +724,6 @@ export function generateKeyPairSync( notImplemented("crypto.generateKeyPairSync"); } -export function generateKeySync( - _type: "hmac" | "aes", - _options: { - length: number; - }, -): KeyObject { - notImplemented("crypto.generateKeySync"); -} - export default { generateKey, generateKeySync, |