Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions types/node/crypto.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,24 @@ declare module 'crypto' {
cipher?: string;
passphrase?: string | Buffer;
}
interface JwkKeyExportOptions {
format: 'jwk';
}
interface JsonWebKey {
crv?: string;
d?: string;
dp?: string;
dq?: string;
e?: string;
k?: string;
kty?: string;
n?: string;
p?: string;
q?: string;
qi?: string;
x?: string;
y?: string;
}

class KeyObject {
private constructor();
Expand All @@ -210,6 +228,7 @@ declare module 'crypto' {
asymmetricKeySize?: number;
export(options: KeyExportOptions<'pem'>): string | Buffer;
export(options?: KeyExportOptions<'der'>): Buffer;
export(options?: JwkKeyExportOptions): JsonWebKey;
symmetricKeySize?: number;
type: KeyObjectType;
}
Expand Down Expand Up @@ -333,8 +352,13 @@ declare module 'crypto' {
type?: 'pkcs1' | 'spki';
}

function createPrivateKey(key: PrivateKeyInput | string | Buffer): KeyObject;
function createPublicKey(key: PublicKeyInput | string | Buffer | KeyObject): KeyObject;
interface JsonWebKeyInput {
key: JsonWebKey;
format: 'jwk';
}

function createPrivateKey(key: PrivateKeyInput | string | Buffer | JsonWebKeyInput): KeyObject;
function createPublicKey(key: PublicKeyInput | string | Buffer | KeyObject | JsonWebKeyInput): KeyObject;
function createSecretKey(key: NodeJS.ArrayBufferView): KeyObject;

function createSign(algorithm: string, options?: stream.WritableOptions): Signer;
Expand Down
36 changes: 36 additions & 0 deletions types/node/test/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -968,3 +968,39 @@ import { promisify } from 'node:util';
{
const derivedKey = crypto.hkdfSync("sha256", Buffer.alloc(32, 0xFF), Buffer.alloc(16, 0x00), "SomeInfo", 42);
}

{
const secretKeyObject = crypto.createSecretKey(Buffer.from('secret'));
crypto.generateKeyPair('ec', { namedCurve: 'P-256' }, (err, publicKey, privateKey) => {
for (const keyObject of [publicKey, privateKey, secretKeyObject]) {
const jwk = keyObject.export({ format: 'jwk' });
jwk.crv;
jwk.d;
jwk.dp;
jwk.dq;
jwk.e;
jwk.k;
jwk.kty;
jwk.n;
jwk.p;
jwk.q;
jwk.qi;
jwk.x;
jwk.y;
crypto.createPublicKey({ key: jwk, format: 'jwk' });
crypto.createPrivateKey({ key: jwk, format: 'jwk' });
}
});
}

{
const jwk = {
alg: 'ES256',
crv: 'P-256',
kty: 'EC',
x: 'ySK38C1jBdLwDsNWKzzBHqKYEE5Cgv-qjWvorUXk9fw',
y: '_LeQBw07cf5t57Iavn4j-BqJsAD1dpoz8gokd3sBsOo',
};
crypto.createPublicKey({ key: jwk, format: 'jwk' });
crypto.createPrivateKey({ key: jwk, format: 'jwk' });
}