diff --git a/types/node/crypto.d.ts b/types/node/crypto.d.ts index bd04bf7b69b8e0..5d5311fae8125c 100644 --- a/types/node/crypto.d.ts +++ b/types/node/crypto.d.ts @@ -637,6 +637,12 @@ declare module "crypto" { export(options: KeyExportOptions<"pem">): string | Buffer; export(options?: KeyExportOptions<"der">): Buffer; export(options?: JwkKeyExportOptions): JsonWebKey; + /** + * Returns `true` or `false` depending on whether the keys have exactly the same type, value, and parameters. + * This method is not [constant time](https://en.wikipedia.org/wiki/Timing_attack). + * @since v16.15.0 + */ + equals(otherKeyObject: KeyObject): boolean; /** * For secret keys, this property represents the size of the key in bytes. This * property is `undefined` for asymmetric keys. @@ -2475,6 +2481,10 @@ declare module "crypto" { * Name of the curve to use */ namedCurve: string; + /** + * Must be `'named'` or `'explicit'`. Default: `'named'`. + */ + paramEncoding?: "explicit" | "named" | undefined; } interface RSAKeyPairKeyObjectOptions { /** @@ -2585,11 +2595,7 @@ declare module "crypto" { type: "pkcs8"; }; } - interface ECKeyPairOptions { - /** - * Name of the curve to use. - */ - namedCurve: string; + interface ECKeyPairOptions extends ECKeyPairKeyObjectOptions { publicKeyEncoding: { type: "pkcs1" | "spki"; format: PubF; diff --git a/types/node/test/crypto.ts b/types/node/test/crypto.ts index b8e76922b05e11..1b4fa132f54271 100644 --- a/types/node/test/crypto.ts +++ b/types/node/test/crypto.ts @@ -627,6 +627,22 @@ import { promisify } from "node:util"; type: "pkcs8", }, }); + + const ecExplicit: { + publicKey: string; + privateKey: string; + } = crypto.generateKeyPairSync("ec", { + namedCurve: "curve", + paramEncoding: "explicit", + publicKeyEncoding: { + format: "pem", + type: "pkcs1", + }, + privateKeyEncoding: { + format: "pem", + type: "pkcs8", + }, + }); } { @@ -703,6 +719,25 @@ import { promisify } from "node:util"; (err: NodeJS.ErrnoException | null, publicKey: string, privateKey: string) => {}, ); + crypto.generateKeyPair( + "ec", + { + namedCurve: "curve", + paramEncoding: "explicit", + publicKeyEncoding: { + format: "pem", + type: "pkcs1", + }, + privateKeyEncoding: { + cipher: "some-cipher", + format: "pem", + passphrase: "secret", + type: "pkcs8", + }, + }, + (err: NodeJS.ErrnoException | null, publicKey: string, privateKey: string) => {}, + ); + crypto.generateKeyPair( "ed25519", { @@ -872,6 +907,14 @@ import { promisify } from "node:util"; crypto.createSecretKey("ascii", "ascii"); } +{ + const { privateKey, publicKey } = crypto.generateKeyPairSync("ed25519"); + privateKey; // $ExpectType KeyObject + publicKey; // $ExpectType KeyObject + privateKey.equals(publicKey); // $ExpectType boolean + publicKey.equals(privateKey); // $ExpectType boolean +} + { const { privateKey, publicKey } = crypto.generateKeyPairSync("ec", { namedCurve: "sect239k1", diff --git a/types/node/ts4.8/crypto.d.ts b/types/node/ts4.8/crypto.d.ts index 91d68fe0ad87fe..e25d99140a614d 100644 --- a/types/node/ts4.8/crypto.d.ts +++ b/types/node/ts4.8/crypto.d.ts @@ -637,6 +637,12 @@ declare module "crypto" { export(options: KeyExportOptions<"pem">): string | Buffer; export(options?: KeyExportOptions<"der">): Buffer; export(options?: JwkKeyExportOptions): JsonWebKey; + /** + * Returns `true` or `false` depending on whether the keys have exactly the same type, value, and parameters. + * This method is not [constant time](https://en.wikipedia.org/wiki/Timing_attack). + * @since v16.15.0 + */ + equals(otherKeyObject: KeyObject): boolean; /** * For secret keys, this property represents the size of the key in bytes. This * property is `undefined` for asymmetric keys. @@ -2475,6 +2481,10 @@ declare module "crypto" { * Name of the curve to use */ namedCurve: string; + /** + * Must be `'named'` or `'explicit'`. Default: `'named'`. + */ + paramEncoding?: "explicit" | "named" | undefined; } interface RSAKeyPairKeyObjectOptions { /** @@ -2585,11 +2595,7 @@ declare module "crypto" { type: "pkcs8"; }; } - interface ECKeyPairOptions { - /** - * Name of the curve to use. - */ - namedCurve: string; + interface ECKeyPairOptions extends ECKeyPairKeyObjectOptions { publicKeyEncoding: { type: "pkcs1" | "spki"; format: PubF; diff --git a/types/node/ts4.8/test/crypto.ts b/types/node/ts4.8/test/crypto.ts index 4134a5a16d2a43..c6193a73fbc38b 100644 --- a/types/node/ts4.8/test/crypto.ts +++ b/types/node/ts4.8/test/crypto.ts @@ -627,6 +627,22 @@ import { promisify } from "node:util"; type: "pkcs8", }, }); + + const ecExplicit: { + publicKey: string; + privateKey: string; + } = crypto.generateKeyPairSync("ec", { + namedCurve: "curve", + paramEncoding: "explicit", + publicKeyEncoding: { + format: "pem", + type: "pkcs1", + }, + privateKeyEncoding: { + format: "pem", + type: "pkcs8", + }, + }); } { @@ -703,6 +719,25 @@ import { promisify } from "node:util"; (err: NodeJS.ErrnoException | null, publicKey: string, privateKey: string) => {}, ); + crypto.generateKeyPair( + "ec", + { + namedCurve: "curve", + paramEncoding: "explicit", + publicKeyEncoding: { + format: "pem", + type: "pkcs1", + }, + privateKeyEncoding: { + cipher: "some-cipher", + format: "pem", + passphrase: "secret", + type: "pkcs8", + }, + }, + (err: NodeJS.ErrnoException | null, publicKey: string, privateKey: string) => {}, + ); + crypto.generateKeyPair( "ed25519", { @@ -872,6 +907,14 @@ import { promisify } from "node:util"; crypto.createSecretKey("ascii", "ascii"); } +{ + const { privateKey, publicKey } = crypto.generateKeyPairSync("ed25519"); + privateKey; // $ExpectType KeyObject + publicKey; // $ExpectType KeyObject + privateKey.equals(publicKey); // $ExpectType boolean + publicKey.equals(privateKey); // $ExpectType boolean +} + { const { privateKey, publicKey } = crypto.generateKeyPairSync("ec", { namedCurve: "sect239k1", diff --git a/types/node/v16/crypto.d.ts b/types/node/v16/crypto.d.ts index f61efbe470debe..a5df7f1f8ea743 100644 --- a/types/node/v16/crypto.d.ts +++ b/types/node/v16/crypto.d.ts @@ -628,6 +628,12 @@ declare module "crypto" { export(options: KeyExportOptions<"pem">): string | Buffer; export(options?: KeyExportOptions<"der">): Buffer; export(options?: JwkKeyExportOptions): JsonWebKey; + /** + * Returns `true` or `false` depending on whether the keys have exactly the same type, value, and parameters. + * This method is not [constant time](https://en.wikipedia.org/wiki/Timing_attack). + * @since v16.15.0 + */ + equals(otherKeyObject: KeyObject): boolean; /** * For secret keys, this property represents the size of the key in bytes. This * property is `undefined` for asymmetric keys. @@ -2454,6 +2460,10 @@ declare module "crypto" { * Name of the curve to use */ namedCurve: string; + /** + * Must be `'named'` or `'explicit'`. Default: `'named'`. + */ + paramEncoding?: "explicit" | "named" | undefined; } interface RSAKeyPairKeyObjectOptions { /** @@ -2564,11 +2574,7 @@ declare module "crypto" { type: "pkcs8"; }; } - interface ECKeyPairOptions { - /** - * Name of the curve to use. - */ - namedCurve: string; + interface ECKeyPairOptions extends ECKeyPairKeyObjectOptions { publicKeyEncoding: { type: "pkcs1" | "spki"; format: PubF; diff --git a/types/node/v16/test/crypto.ts b/types/node/v16/test/crypto.ts index 63c98d2745edce..ac9fb247741f7e 100644 --- a/types/node/v16/test/crypto.ts +++ b/types/node/v16/test/crypto.ts @@ -627,6 +627,22 @@ import { promisify } from "node:util"; type: "pkcs8", }, }); + + const ecExplicit: { + publicKey: string; + privateKey: string; + } = crypto.generateKeyPairSync("ec", { + namedCurve: "curve", + paramEncoding: "explicit", + publicKeyEncoding: { + format: "pem", + type: "pkcs1", + }, + privateKeyEncoding: { + format: "pem", + type: "pkcs8", + }, + }); } { @@ -703,6 +719,25 @@ import { promisify } from "node:util"; (err: NodeJS.ErrnoException | null, publicKey: string, privateKey: string) => {}, ); + crypto.generateKeyPair( + "ec", + { + namedCurve: "curve", + paramEncoding: "explicit", + publicKeyEncoding: { + format: "pem", + type: "pkcs1", + }, + privateKeyEncoding: { + cipher: "some-cipher", + format: "pem", + passphrase: "secret", + type: "pkcs8", + }, + }, + (err: NodeJS.ErrnoException | null, publicKey: string, privateKey: string) => {}, + ); + crypto.generateKeyPair( "ed25519", { @@ -872,6 +907,14 @@ import { promisify } from "node:util"; crypto.createSecretKey("ascii", "ascii"); } +{ + const { privateKey, publicKey } = crypto.generateKeyPairSync("ed25519"); + privateKey; // $ExpectType KeyObject + publicKey; // $ExpectType KeyObject + privateKey.equals(publicKey); // $ExpectType boolean + publicKey.equals(privateKey); // $ExpectType boolean +} + { const { privateKey, publicKey } = crypto.generateKeyPairSync("ec", { namedCurve: "sect239k1", diff --git a/types/node/v16/ts4.8/crypto.d.ts b/types/node/v16/ts4.8/crypto.d.ts index 26a24a2d9cbc7b..1bde5bc1286c97 100644 --- a/types/node/v16/ts4.8/crypto.d.ts +++ b/types/node/v16/ts4.8/crypto.d.ts @@ -628,6 +628,12 @@ declare module "crypto" { export(options: KeyExportOptions<"pem">): string | Buffer; export(options?: KeyExportOptions<"der">): Buffer; export(options?: JwkKeyExportOptions): JsonWebKey; + /** + * Returns `true` or `false` depending on whether the keys have exactly the same type, value, and parameters. + * This method is not [constant time](https://en.wikipedia.org/wiki/Timing_attack). + * @since v16.15.0 + */ + equals(otherKeyObject: KeyObject): boolean; /** * For secret keys, this property represents the size of the key in bytes. This * property is `undefined` for asymmetric keys. @@ -2452,6 +2458,10 @@ declare module "crypto" { * Name of the curve to use */ namedCurve: string; + /** + * Must be `'named'` or `'explicit'`. Default: `'named'`. + */ + paramEncoding?: "explicit" | "named" | undefined; } interface RSAKeyPairKeyObjectOptions { /** @@ -2562,11 +2572,7 @@ declare module "crypto" { type: "pkcs8"; }; } - interface ECKeyPairOptions { - /** - * Name of the curve to use. - */ - namedCurve: string; + interface ECKeyPairOptions extends ECKeyPairKeyObjectOptions { publicKeyEncoding: { type: "pkcs1" | "spki"; format: PubF; diff --git a/types/node/v16/ts4.8/test/crypto.ts b/types/node/v16/ts4.8/test/crypto.ts index 4794d0de43a6e6..4c79d7ecea3d12 100644 --- a/types/node/v16/ts4.8/test/crypto.ts +++ b/types/node/v16/ts4.8/test/crypto.ts @@ -627,6 +627,22 @@ import { promisify } from "node:util"; type: "pkcs8", }, }); + + const ecExplicit: { + publicKey: string; + privateKey: string; + } = crypto.generateKeyPairSync("ec", { + namedCurve: "curve", + paramEncoding: "explicit", + publicKeyEncoding: { + format: "pem", + type: "pkcs1", + }, + privateKeyEncoding: { + format: "pem", + type: "pkcs8", + }, + }); } { @@ -703,6 +719,25 @@ import { promisify } from "node:util"; (err: NodeJS.ErrnoException | null, publicKey: string, privateKey: string) => {}, ); + crypto.generateKeyPair( + "ec", + { + namedCurve: "curve", + paramEncoding: "explicit", + publicKeyEncoding: { + format: "pem", + type: "pkcs1", + }, + privateKeyEncoding: { + cipher: "some-cipher", + format: "pem", + passphrase: "secret", + type: "pkcs8", + }, + }, + (err: NodeJS.ErrnoException | null, publicKey: string, privateKey: string) => {}, + ); + crypto.generateKeyPair( "ed25519", { @@ -859,6 +894,14 @@ import { promisify } from "node:util"; crypto.createSecretKey("ascii", "ascii"); } +{ + const { privateKey, publicKey } = crypto.generateKeyPairSync("ed25519"); + privateKey; // $ExpectType KeyObject + publicKey; // $ExpectType KeyObject + privateKey.equals(publicKey); // $ExpectType boolean + publicKey.equals(privateKey); // $ExpectType boolean +} + { const { privateKey, publicKey } = crypto.generateKeyPairSync("ec", { namedCurve: "sect239k1", diff --git a/types/node/v18/crypto.d.ts b/types/node/v18/crypto.d.ts index af9ec03ed80463..64cca4493afba9 100644 --- a/types/node/v18/crypto.d.ts +++ b/types/node/v18/crypto.d.ts @@ -655,6 +655,12 @@ declare module "crypto" { export(options: KeyExportOptions<"pem">): string | Buffer; export(options?: KeyExportOptions<"der">): Buffer; export(options?: JwkKeyExportOptions): JsonWebKey; + /** + * Returns `true` or `false` depending on whether the keys have exactly the same type, value, and parameters. + * This method is not [constant time](https://en.wikipedia.org/wiki/Timing_attack). + * @since v16.15.0 + */ + equals(otherKeyObject: KeyObject): boolean; /** * For secret keys, this property represents the size of the key in bytes. This * property is `undefined` for asymmetric keys. @@ -2493,6 +2499,10 @@ declare module "crypto" { * Name of the curve to use */ namedCurve: string; + /** + * Must be `'named'` or `'explicit'`. Default: `'named'`. + */ + paramEncoding?: "explicit" | "named" | undefined; } interface RSAKeyPairKeyObjectOptions { /** @@ -2603,11 +2613,7 @@ declare module "crypto" { type: "pkcs8"; }; } - interface ECKeyPairOptions { - /** - * Name of the curve to use. - */ - namedCurve: string; + interface ECKeyPairOptions extends ECKeyPairKeyObjectOptions { publicKeyEncoding: { type: "pkcs1" | "spki"; format: PubF; diff --git a/types/node/v18/test/crypto.ts b/types/node/v18/test/crypto.ts index 51f4a86f11795d..5e60b49b994b45 100644 --- a/types/node/v18/test/crypto.ts +++ b/types/node/v18/test/crypto.ts @@ -627,6 +627,22 @@ import { promisify } from "node:util"; type: "pkcs8", }, }); + + const ecExplicit: { + publicKey: string; + privateKey: string; + } = crypto.generateKeyPairSync("ec", { + namedCurve: "curve", + paramEncoding: "explicit", + publicKeyEncoding: { + format: "pem", + type: "pkcs1", + }, + privateKeyEncoding: { + format: "pem", + type: "pkcs8", + }, + }); } { @@ -703,6 +719,25 @@ import { promisify } from "node:util"; (err: NodeJS.ErrnoException | null, publicKey: string, privateKey: string) => {}, ); + crypto.generateKeyPair( + "ec", + { + namedCurve: "curve", + paramEncoding: "explicit", + publicKeyEncoding: { + format: "pem", + type: "pkcs1", + }, + privateKeyEncoding: { + cipher: "some-cipher", + format: "pem", + passphrase: "secret", + type: "pkcs8", + }, + }, + (err: NodeJS.ErrnoException | null, publicKey: string, privateKey: string) => {}, + ); + crypto.generateKeyPair( "ed25519", { @@ -872,6 +907,14 @@ import { promisify } from "node:util"; crypto.createSecretKey("ascii", "ascii"); } +{ + const { privateKey, publicKey } = crypto.generateKeyPairSync("ed25519"); + privateKey; // $ExpectType KeyObject + publicKey; // $ExpectType KeyObject + privateKey.equals(publicKey); // $ExpectType boolean + publicKey.equals(privateKey); // $ExpectType boolean +} + { const { privateKey, publicKey } = crypto.generateKeyPairSync("ec", { namedCurve: "sect239k1", diff --git a/types/node/v18/ts4.8/crypto.d.ts b/types/node/v18/ts4.8/crypto.d.ts index 4b38c8e4eceaae..3a27a20911523c 100644 --- a/types/node/v18/ts4.8/crypto.d.ts +++ b/types/node/v18/ts4.8/crypto.d.ts @@ -655,6 +655,12 @@ declare module "crypto" { export(options: KeyExportOptions<"pem">): string | Buffer; export(options?: KeyExportOptions<"der">): Buffer; export(options?: JwkKeyExportOptions): JsonWebKey; + /** + * Returns `true` or `false` depending on whether the keys have exactly the same type, value, and parameters. + * This method is not [constant time](https://en.wikipedia.org/wiki/Timing_attack). + * @since v16.15.0 + */ + equals(otherKeyObject: KeyObject): boolean; /** * For secret keys, this property represents the size of the key in bytes. This * property is `undefined` for asymmetric keys. @@ -2493,6 +2499,10 @@ declare module "crypto" { * Name of the curve to use */ namedCurve: string; + /** + * Must be `'named'` or `'explicit'`. Default: `'named'`. + */ + paramEncoding?: "explicit" | "named" | undefined; } interface RSAKeyPairKeyObjectOptions { /** @@ -2603,11 +2613,7 @@ declare module "crypto" { type: "pkcs8"; }; } - interface ECKeyPairOptions { - /** - * Name of the curve to use. - */ - namedCurve: string; + interface ECKeyPairOptions extends ECKeyPairKeyObjectOptions { publicKeyEncoding: { type: "pkcs1" | "spki"; format: PubF; diff --git a/types/node/v18/ts4.8/test/crypto.ts b/types/node/v18/ts4.8/test/crypto.ts index da7cc7668f7fc8..852bc39edf0add 100644 --- a/types/node/v18/ts4.8/test/crypto.ts +++ b/types/node/v18/ts4.8/test/crypto.ts @@ -627,6 +627,22 @@ import { promisify } from "node:util"; type: "pkcs8", }, }); + + const ecExplicit: { + publicKey: string; + privateKey: string; + } = crypto.generateKeyPairSync("ec", { + namedCurve: "curve", + paramEncoding: "explicit", + publicKeyEncoding: { + format: "pem", + type: "pkcs1", + }, + privateKeyEncoding: { + format: "pem", + type: "pkcs8", + }, + }); } { @@ -703,6 +719,25 @@ import { promisify } from "node:util"; (err: NodeJS.ErrnoException | null, publicKey: string, privateKey: string) => {}, ); + crypto.generateKeyPair( + "ec", + { + namedCurve: "curve", + paramEncoding: "explicit", + publicKeyEncoding: { + format: "pem", + type: "pkcs1", + }, + privateKeyEncoding: { + cipher: "some-cipher", + format: "pem", + passphrase: "secret", + type: "pkcs8", + }, + }, + (err: NodeJS.ErrnoException | null, publicKey: string, privateKey: string) => {}, + ); + crypto.generateKeyPair( "ed25519", { @@ -872,6 +907,14 @@ import { promisify } from "node:util"; crypto.createSecretKey("ascii", "ascii"); } +{ + const { privateKey, publicKey } = crypto.generateKeyPairSync("ed25519"); + privateKey; // $ExpectType KeyObject + publicKey; // $ExpectType KeyObject + privateKey.equals(publicKey); // $ExpectType boolean + publicKey.equals(privateKey); // $ExpectType boolean +} + { const { privateKey, publicKey } = crypto.generateKeyPairSync("ec", { namedCurve: "sect239k1",