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
67 changes: 65 additions & 2 deletions types/node/crypto.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,43 @@ 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;
}

interface AsymmetricKeyDetails {
/**
* Key size in bits (RSA, DSA).
*/
modulusLength?: number;
/**
* Public exponent (RSA).
*/
publicExponent?: bigint;
/**
* Size of q in bits (DSA).
*/
divisorLength?: number;
/**
* Name of the curve (EC).
*/
namedCurve?: string;
}

class KeyObject {
private constructor();
Expand All @@ -208,8 +245,16 @@ declare module 'crypto' {
* bytes. This property is `undefined` for symmetric keys.
*/
asymmetricKeySize?: number;
/**
* This property exists only on asymmetric keys. Depending on the type of the key,
* this object contains information about the key. None of the information obtained
* through this property can be used to uniquely identify a key or to compromise the
* security of the key.
*/
asymmetricKeyDetails?: AsymmetricKeyDetails;
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 +378,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 Expand Up @@ -1174,6 +1224,12 @@ declare module 'crypto' {
data: NodeJS.ArrayBufferView,
key: KeyLike | SignKeyObjectInput | SignPrivateKeyInput,
): Buffer;
function sign(
algorithm: string | null | undefined,
data: NodeJS.ArrayBufferView,
key: KeyLike | SignKeyObjectInput | SignPrivateKeyInput,
callback: (err: Error | null, signature: Buffer) => void,
): void;

/**
* Calculates and returns the signature for `data` using the given private key and
Expand All @@ -1189,6 +1245,13 @@ declare module 'crypto' {
key: KeyLike | VerifyKeyObjectInput | VerifyPublicKeyInput,
signature: NodeJS.ArrayBufferView,
): boolean;
function verify(
algorithm: string | null | undefined,
data: NodeJS.ArrayBufferView,
key: KeyLike | VerifyKeyObjectInput | VerifyPublicKeyInput,
signature: NodeJS.ArrayBufferView,
callback: (err: Error | null, result: boolean) => void,
): void;

/**
* Computes the Diffie-Hellman secret based on a privateKey and a publicKey.
Expand Down
86 changes: 86 additions & 0 deletions types/node/test/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,7 @@ import { promisify } from 'node:util';
}

{
const callback = (error: Error | null, signature: Buffer): void => {};
const key = crypto.createPrivateKey('pkey');
crypto.sign('sha256', Buffer.from('asd'), {
key: Buffer.from('keylike'),
Expand All @@ -872,13 +873,22 @@ import { promisify } from 'node:util';
key,
dsaEncoding: 'der',
});
crypto.sign('sha256', Buffer.from('asd'), {
key,
dsaEncoding: 'der',
}, callback);
promisify(crypto.sign)('sha256', Buffer.from('asd'), {
key,
dsaEncoding: 'der',
}).then((signature: Buffer) => {});
crypto.createSign('sha256').update(Buffer.from('asd')).sign({
key,
dsaEncoding: 'der',
});
}

{
const callback = (error: Error | null, result: boolean): void => {};
const key = crypto.createPublicKey('pkey');
crypto.verify(
'sha256',
Expand Down Expand Up @@ -908,6 +918,25 @@ import { promisify } from 'node:util';
},
Buffer.from('sig'),
);
crypto.verify(
'sha256',
Buffer.from('asd'),
{
key,
dsaEncoding: 'der',
},
Buffer.from('sig'),
callback
);
promisify(crypto.verify)(
'sha256',
Buffer.from('asd'),
{
key,
dsaEncoding: 'der',
},
Buffer.from('sig'),
).then((result: boolean) => {});
crypto.createVerify('sha256').update(Buffer.from('asd')).verify(
{
key,
Expand Down Expand Up @@ -968,3 +997,60 @@ 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' });
}

{
crypto.generateKeyPair('ec', { namedCurve: 'P-256' }, (err, publicKey, privateKey) => {
for (const keyObject of [publicKey, privateKey]) {
if (keyObject.asymmetricKeyDetails) {
if (keyObject.asymmetricKeyDetails.modulusLength) {
const modulusLength: number = keyObject.asymmetricKeyDetails.modulusLength;
}
if (keyObject.asymmetricKeyDetails.publicExponent) {
const publicExponent: bigint = keyObject.asymmetricKeyDetails.publicExponent;
}
if (keyObject.asymmetricKeyDetails.divisorLength) {
const divisorLength: number = keyObject.asymmetricKeyDetails.divisorLength;
}
if (keyObject.asymmetricKeyDetails.namedCurve) {
const namedCurve: string = keyObject.asymmetricKeyDetails.namedCurve;
}
}
}
});
}