Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[node:crypto]: Add missing paramEncoding option #67404

Merged
merged 6 commits into from Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 11 additions & 5 deletions types/node/crypto.d.ts
Expand Up @@ -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.
Expand Down Expand Up @@ -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";
vansergen marked this conversation as resolved.
Show resolved Hide resolved
vansergen marked this conversation as resolved.
Show resolved Hide resolved
}
interface RSAKeyPairKeyObjectOptions {
/**
Expand Down Expand Up @@ -2585,11 +2595,7 @@ declare module "crypto" {
type: "pkcs8";
};
}
interface ECKeyPairOptions<PubF extends KeyFormat, PrivF extends KeyFormat> {
/**
* Name of the curve to use.
*/
namedCurve: string;
interface ECKeyPairOptions<PubF extends KeyFormat, PrivF extends KeyFormat> extends ECKeyPairKeyObjectOptions {
publicKeyEncoding: {
type: "pkcs1" | "spki";
format: PubF;
Expand Down
43 changes: 43 additions & 0 deletions types/node/test/crypto.ts
Expand Up @@ -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",
},
});
}

{
Expand Down Expand Up @@ -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",
{
Expand Down Expand Up @@ -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",
Expand Down
16 changes: 11 additions & 5 deletions types/node/ts4.8/crypto.d.ts
Expand Up @@ -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.
Expand Down Expand Up @@ -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";
}
interface RSAKeyPairKeyObjectOptions {
/**
Expand Down Expand Up @@ -2585,11 +2595,7 @@ declare module "crypto" {
type: "pkcs8";
};
}
interface ECKeyPairOptions<PubF extends KeyFormat, PrivF extends KeyFormat> {
/**
* Name of the curve to use.
*/
namedCurve: string;
interface ECKeyPairOptions<PubF extends KeyFormat, PrivF extends KeyFormat> extends ECKeyPairKeyObjectOptions {
publicKeyEncoding: {
type: "pkcs1" | "spki";
format: PubF;
Expand Down
43 changes: 43 additions & 0 deletions types/node/ts4.8/test/crypto.ts
Expand Up @@ -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",
},
});
}

{
Expand Down Expand Up @@ -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",
{
Expand Down Expand Up @@ -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",
Expand Down
16 changes: 11 additions & 5 deletions types/node/v16/crypto.d.ts
Expand Up @@ -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.
Expand Down Expand Up @@ -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";
}
interface RSAKeyPairKeyObjectOptions {
/**
Expand Down Expand Up @@ -2564,11 +2574,7 @@ declare module "crypto" {
type: "pkcs8";
};
}
interface ECKeyPairOptions<PubF extends KeyFormat, PrivF extends KeyFormat> {
/**
* Name of the curve to use.
*/
namedCurve: string;
interface ECKeyPairOptions<PubF extends KeyFormat, PrivF extends KeyFormat> extends ECKeyPairKeyObjectOptions {
publicKeyEncoding: {
type: "pkcs1" | "spki";
format: PubF;
Expand Down
43 changes: 43 additions & 0 deletions types/node/v16/test/crypto.ts
Expand Up @@ -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",
},
});
}

{
Expand Down Expand Up @@ -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",
{
Expand Down Expand Up @@ -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",
Expand Down
16 changes: 11 additions & 5 deletions types/node/v16/ts4.8/crypto.d.ts
Expand Up @@ -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.
Expand Down Expand Up @@ -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";
}
interface RSAKeyPairKeyObjectOptions {
/**
Expand Down Expand Up @@ -2562,11 +2572,7 @@ declare module "crypto" {
type: "pkcs8";
};
}
interface ECKeyPairOptions<PubF extends KeyFormat, PrivF extends KeyFormat> {
/**
* Name of the curve to use.
*/
namedCurve: string;
interface ECKeyPairOptions<PubF extends KeyFormat, PrivF extends KeyFormat> extends ECKeyPairKeyObjectOptions {
publicKeyEncoding: {
type: "pkcs1" | "spki";
format: PubF;
Expand Down