UNPKG

30.6 kBTypeScriptView Raw
1declare module "crypto" {
2 import * as stream from "stream";
3
4 interface Certificate {
5 exportChallenge(spkac: BinaryLike): Buffer;
6 exportPublicKey(spkac: BinaryLike): Buffer;
7 verifySpkac(spkac: NodeJS.ArrayBufferView): boolean;
8 }
9 const Certificate: {
10 new(): Certificate;
11 (): Certificate;
12 };
13
14 namespace constants { // https://nodejs.org/dist/latest-v10.x/docs/api/crypto.html#crypto_crypto_constants
15 const OPENSSL_VERSION_NUMBER: number;
16
17 /** Applies multiple bug workarounds within OpenSSL. See https://www.openssl.org/docs/man1.0.2/ssl/SSL_CTX_set_options.html for detail. */
18 const SSL_OP_ALL: number;
19 /** Allows legacy insecure renegotiation between OpenSSL and unpatched clients or servers. See https://www.openssl.org/docs/man1.0.2/ssl/SSL_CTX_set_options.html. */
20 const SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION: number;
21 /** Attempts to use the server's preferences instead of the client's when selecting a cipher. See https://www.openssl.org/docs/man1.0.2/ssl/SSL_CTX_set_options.html. */
22 const SSL_OP_CIPHER_SERVER_PREFERENCE: number;
23 /** Instructs OpenSSL to use Cisco's "speshul" version of DTLS_BAD_VER. */
24 const SSL_OP_CISCO_ANYCONNECT: number;
25 /** Instructs OpenSSL to turn on cookie exchange. */
26 const SSL_OP_COOKIE_EXCHANGE: number;
27 /** Instructs OpenSSL to add server-hello extension from an early version of the cryptopro draft. */
28 const SSL_OP_CRYPTOPRO_TLSEXT_BUG: number;
29 /** Instructs OpenSSL to disable a SSL 3.0/TLS 1.0 vulnerability workaround added in OpenSSL 0.9.6d. */
30 const SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS: number;
31 /** Instructs OpenSSL to always use the tmp_rsa key when performing RSA operations. */
32 const SSL_OP_EPHEMERAL_RSA: number;
33 /** Allows initial connection to servers that do not support RI. */
34 const SSL_OP_LEGACY_SERVER_CONNECT: number;
35 const SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER: number;
36 const SSL_OP_MICROSOFT_SESS_ID_BUG: number;
37 /** Instructs OpenSSL to disable the workaround for a man-in-the-middle protocol-version vulnerability in the SSL 2.0 server implementation. */
38 const SSL_OP_MSIE_SSLV2_RSA_PADDING: number;
39 const SSL_OP_NETSCAPE_CA_DN_BUG: number;
40 const SSL_OP_NETSCAPE_CHALLENGE_BUG: number;
41 const SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG: number;
42 const SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG: number;
43 /** Instructs OpenSSL to disable support for SSL/TLS compression. */
44 const SSL_OP_NO_COMPRESSION: number;
45 const SSL_OP_NO_QUERY_MTU: number;
46 /** Instructs OpenSSL to always start a new session when performing renegotiation. */
47 const SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION: number;
48 const SSL_OP_NO_SSLv2: number;
49 const SSL_OP_NO_SSLv3: number;
50 const SSL_OP_NO_TICKET: number;
51 const SSL_OP_NO_TLSv1: number;
52 const SSL_OP_NO_TLSv1_1: number;
53 const SSL_OP_NO_TLSv1_2: number;
54 const SSL_OP_PKCS1_CHECK_1: number;
55 const SSL_OP_PKCS1_CHECK_2: number;
56 /** Instructs OpenSSL to always create a new key when using temporary/ephemeral DH parameters. */
57 const SSL_OP_SINGLE_DH_USE: number;
58 /** Instructs OpenSSL to always create a new key when using temporary/ephemeral ECDH parameters. */
59 const SSL_OP_SINGLE_ECDH_USE: number;
60 const SSL_OP_SSLEAY_080_CLIENT_DH_BUG: number;
61 const SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG: number;
62 const SSL_OP_TLS_BLOCK_PADDING_BUG: number;
63 const SSL_OP_TLS_D5_BUG: number;
64 /** Instructs OpenSSL to disable version rollback attack detection. */
65 const SSL_OP_TLS_ROLLBACK_BUG: number;
66
67 const ENGINE_METHOD_RSA: number;
68 const ENGINE_METHOD_DSA: number;
69 const ENGINE_METHOD_DH: number;
70 const ENGINE_METHOD_RAND: number;
71 const ENGINE_METHOD_EC: number;
72 const ENGINE_METHOD_CIPHERS: number;
73 const ENGINE_METHOD_DIGESTS: number;
74 const ENGINE_METHOD_PKEY_METHS: number;
75 const ENGINE_METHOD_PKEY_ASN1_METHS: number;
76 const ENGINE_METHOD_ALL: number;
77 const ENGINE_METHOD_NONE: number;
78
79 const DH_CHECK_P_NOT_SAFE_PRIME: number;
80 const DH_CHECK_P_NOT_PRIME: number;
81 const DH_UNABLE_TO_CHECK_GENERATOR: number;
82 const DH_NOT_SUITABLE_GENERATOR: number;
83
84 const ALPN_ENABLED: number;
85
86 const RSA_PKCS1_PADDING: number;
87 const RSA_SSLV23_PADDING: number;
88 const RSA_NO_PADDING: number;
89 const RSA_PKCS1_OAEP_PADDING: number;
90 const RSA_X931_PADDING: number;
91 const RSA_PKCS1_PSS_PADDING: number;
92 /** Sets the salt length for RSA_PKCS1_PSS_PADDING to the digest size when signing or verifying. */
93 const RSA_PSS_SALTLEN_DIGEST: number;
94 /** Sets the salt length for RSA_PKCS1_PSS_PADDING to the maximum permissible value when signing data. */
95 const RSA_PSS_SALTLEN_MAX_SIGN: number;
96 /** Causes the salt length for RSA_PKCS1_PSS_PADDING to be determined automatically when verifying a signature. */
97 const RSA_PSS_SALTLEN_AUTO: number;
98
99 const POINT_CONVERSION_COMPRESSED: number;
100 const POINT_CONVERSION_UNCOMPRESSED: number;
101 const POINT_CONVERSION_HYBRID: number;
102
103 /** Specifies the built-in default cipher list used by Node.js (colon-separated values). */
104 const defaultCoreCipherList: string;
105 /** Specifies the active default cipher list used by the current Node.js process (colon-separated values). */
106 const defaultCipherList: string;
107 }
108
109 interface HashOptions extends stream.TransformOptions {
110 /**
111 * For XOF hash functions such as `shake256`, the
112 * outputLength option can be used to specify the desired output length in bytes.
113 */
114 outputLength?: number;
115 }
116
117 /** @deprecated since v10.0.0 */
118 const fips: boolean;
119
120 function createHash(algorithm: string, options?: HashOptions): Hash;
121 function createHmac(algorithm: string, key: BinaryLike, options?: stream.TransformOptions): Hmac;
122
123 type Utf8AsciiLatin1Encoding = "utf8" | "ascii" | "latin1";
124 type HexBase64Latin1Encoding = "latin1" | "hex" | "base64";
125 type Utf8AsciiBinaryEncoding = "utf8" | "ascii" | "binary";
126 type HexBase64BinaryEncoding = "binary" | "base64" | "hex";
127 type ECDHKeyFormat = "compressed" | "uncompressed" | "hybrid";
128
129 class Hash extends stream.Transform {
130 private constructor();
131 copy(): Hash;
132 update(data: BinaryLike): Hash;
133 update(data: string, input_encoding: Utf8AsciiLatin1Encoding): Hash;
134 digest(): Buffer;
135 digest(encoding: HexBase64Latin1Encoding): string;
136 }
137 class Hmac extends stream.Transform {
138 private constructor();
139 update(data: BinaryLike): Hmac;
140 update(data: string, input_encoding: Utf8AsciiLatin1Encoding): Hmac;
141 digest(): Buffer;
142 digest(encoding: HexBase64Latin1Encoding): string;
143 }
144
145 type KeyObjectType = 'secret' | 'public' | 'private';
146
147 interface KeyExportOptions<T extends KeyFormat> {
148 type: 'pkcs1' | 'spki' | 'pkcs8' | 'sec1';
149 format: T;
150 cipher?: string;
151 passphrase?: string | Buffer;
152 }
153
154 class KeyObject {
155 private constructor();
156 asymmetricKeyType?: KeyType;
157 /**
158 * For asymmetric keys, this property represents the size of the embedded key in
159 * bytes. This property is `undefined` for symmetric keys.
160 */
161 asymmetricKeySize?: number;
162 export(options: KeyExportOptions<'pem'>): string | Buffer;
163 export(options?: KeyExportOptions<'der'>): Buffer;
164 symmetricSize?: number;
165 type: KeyObjectType;
166 }
167
168 type CipherCCMTypes = 'aes-128-ccm' | 'aes-192-ccm' | 'aes-256-ccm' | 'chacha20-poly1305';
169 type CipherGCMTypes = 'aes-128-gcm' | 'aes-192-gcm' | 'aes-256-gcm';
170
171 type BinaryLike = string | NodeJS.ArrayBufferView;
172
173 type CipherKey = BinaryLike | KeyObject;
174
175 interface CipherCCMOptions extends stream.TransformOptions {
176 authTagLength: number;
177 }
178 interface CipherGCMOptions extends stream.TransformOptions {
179 authTagLength?: number;
180 }
181 /** @deprecated since v10.0.0 use createCipheriv() */
182 function createCipher(algorithm: CipherCCMTypes, password: BinaryLike, options: CipherCCMOptions): CipherCCM;
183 /** @deprecated since v10.0.0 use createCipheriv() */
184 function createCipher(algorithm: CipherGCMTypes, password: BinaryLike, options?: CipherGCMOptions): CipherGCM;
185 /** @deprecated since v10.0.0 use createCipheriv() */
186 function createCipher(algorithm: string, password: BinaryLike, options?: stream.TransformOptions): Cipher;
187
188 function createCipheriv(
189 algorithm: CipherCCMTypes,
190 key: CipherKey,
191 iv: BinaryLike | null,
192 options: CipherCCMOptions
193 ): CipherCCM;
194 function createCipheriv(
195 algorithm: CipherGCMTypes,
196 key: CipherKey,
197 iv: BinaryLike | null,
198 options?: CipherGCMOptions
199 ): CipherGCM;
200 function createCipheriv(
201 algorithm: string, key: CipherKey, iv: BinaryLike | null, options?: stream.TransformOptions
202 ): Cipher;
203
204 class Cipher extends stream.Transform {
205 private constructor();
206 update(data: BinaryLike): Buffer;
207 update(data: string, input_encoding: Utf8AsciiBinaryEncoding): Buffer;
208 update(data: NodeJS.ArrayBufferView, input_encoding: undefined, output_encoding: HexBase64BinaryEncoding): string;
209 update(data: string, input_encoding: Utf8AsciiBinaryEncoding | undefined, output_encoding: HexBase64BinaryEncoding): string;
210 final(): Buffer;
211 final(output_encoding: string): string;
212 setAutoPadding(auto_padding?: boolean): this;
213 // getAuthTag(): Buffer;
214 // setAAD(buffer: Buffer): this; // docs only say buffer
215 }
216 interface CipherCCM extends Cipher {
217 setAAD(buffer: Buffer, options: { plaintextLength: number }): this;
218 getAuthTag(): Buffer;
219 }
220 interface CipherGCM extends Cipher {
221 setAAD(buffer: Buffer, options?: { plaintextLength: number }): this;
222 getAuthTag(): Buffer;
223 }
224 /** @deprecated since v10.0.0 use createDecipheriv() */
225 function createDecipher(algorithm: CipherCCMTypes, password: BinaryLike, options: CipherCCMOptions): DecipherCCM;
226 /** @deprecated since v10.0.0 use createDecipheriv() */
227 function createDecipher(algorithm: CipherGCMTypes, password: BinaryLike, options?: CipherGCMOptions): DecipherGCM;
228 /** @deprecated since v10.0.0 use createDecipheriv() */
229 function createDecipher(algorithm: string, password: BinaryLike, options?: stream.TransformOptions): Decipher;
230
231 function createDecipheriv(
232 algorithm: CipherCCMTypes,
233 key: BinaryLike,
234 iv: BinaryLike | null,
235 options: CipherCCMOptions,
236 ): DecipherCCM;
237 function createDecipheriv(
238 algorithm: CipherGCMTypes,
239 key: BinaryLike,
240 iv: BinaryLike | null,
241 options?: CipherGCMOptions,
242 ): DecipherGCM;
243 function createDecipheriv(algorithm: string, key: BinaryLike, iv: BinaryLike | null, options?: stream.TransformOptions): Decipher;
244
245 class Decipher extends stream.Transform {
246 private constructor();
247 update(data: NodeJS.ArrayBufferView): Buffer;
248 update(data: string, input_encoding: HexBase64BinaryEncoding): Buffer;
249 update(data: NodeJS.ArrayBufferView, input_encoding: HexBase64BinaryEncoding | undefined, output_encoding: Utf8AsciiBinaryEncoding): string;
250 update(data: string, input_encoding: HexBase64BinaryEncoding | undefined, output_encoding: Utf8AsciiBinaryEncoding): string;
251 final(): Buffer;
252 final(output_encoding: string): string;
253 setAutoPadding(auto_padding?: boolean): this;
254 // setAuthTag(tag: NodeJS.ArrayBufferView): this;
255 // setAAD(buffer: NodeJS.ArrayBufferView): this;
256 }
257 interface DecipherCCM extends Decipher {
258 setAuthTag(buffer: NodeJS.ArrayBufferView): this;
259 setAAD(buffer: NodeJS.ArrayBufferView, options: { plaintextLength: number }): this;
260 }
261 interface DecipherGCM extends Decipher {
262 setAuthTag(buffer: NodeJS.ArrayBufferView): this;
263 setAAD(buffer: NodeJS.ArrayBufferView, options?: { plaintextLength: number }): this;
264 }
265
266 interface PrivateKeyInput {
267 key: string | Buffer;
268 format?: KeyFormat;
269 type?: 'pkcs1' | 'pkcs8' | 'sec1';
270 passphrase?: string | Buffer;
271 }
272
273 interface PublicKeyInput {
274 key: string | Buffer;
275 format?: KeyFormat;
276 type?: 'pkcs1' | 'spki';
277 }
278
279 function createPrivateKey(key: PrivateKeyInput | string | Buffer): KeyObject;
280 function createPublicKey(key: PublicKeyInput | string | Buffer | KeyObject): KeyObject;
281 function createSecretKey(key: Buffer): KeyObject;
282
283 function createSign(algorithm: string, options?: stream.WritableOptions): Signer;
284
285 interface SigningOptions {
286 /**
287 * @See crypto.constants.RSA_PKCS1_PADDING
288 */
289 padding?: number;
290 saltLength?: number;
291 }
292
293 interface SignPrivateKeyInput extends PrivateKeyInput, SigningOptions {
294 }
295
296 type KeyLike = string | Buffer | KeyObject;
297
298 class Signer extends stream.Writable {
299 private constructor();
300
301 update(data: BinaryLike): Signer;
302 update(data: string, input_encoding: Utf8AsciiLatin1Encoding): Signer;
303 sign(private_key: SignPrivateKeyInput | KeyLike): Buffer;
304 sign(private_key: SignPrivateKeyInput | KeyLike, output_format: HexBase64Latin1Encoding): string;
305 }
306
307 function createVerify(algorithm: string, options?: stream.WritableOptions): Verify;
308 class Verify extends stream.Writable {
309 private constructor();
310
311 update(data: BinaryLike): Verify;
312 update(data: string, input_encoding: Utf8AsciiLatin1Encoding): Verify;
313 verify(object: object | KeyLike, signature: NodeJS.ArrayBufferView): boolean;
314 verify(object: object | KeyLike, signature: string, signature_format?: HexBase64Latin1Encoding): boolean;
315 // https://nodejs.org/api/crypto.html#crypto_verifier_verify_object_signature_signature_format
316 // The signature field accepts a TypedArray type, but it is only available starting ES2017
317 }
318 function createDiffieHellman(prime_length: number, generator?: number | NodeJS.ArrayBufferView): DiffieHellman;
319 function createDiffieHellman(prime: NodeJS.ArrayBufferView): DiffieHellman;
320 function createDiffieHellman(prime: string, prime_encoding: HexBase64Latin1Encoding): DiffieHellman;
321 function createDiffieHellman(prime: string, prime_encoding: HexBase64Latin1Encoding, generator: number | NodeJS.ArrayBufferView): DiffieHellman;
322 function createDiffieHellman(prime: string, prime_encoding: HexBase64Latin1Encoding, generator: string, generator_encoding: HexBase64Latin1Encoding): DiffieHellman;
323 class DiffieHellman {
324 private constructor();
325 generateKeys(): Buffer;
326 generateKeys(encoding: HexBase64Latin1Encoding): string;
327 computeSecret(other_public_key: NodeJS.ArrayBufferView): Buffer;
328 computeSecret(other_public_key: string, input_encoding: HexBase64Latin1Encoding): Buffer;
329 computeSecret(other_public_key: NodeJS.ArrayBufferView, output_encoding: HexBase64Latin1Encoding): string;
330 computeSecret(other_public_key: string, input_encoding: HexBase64Latin1Encoding, output_encoding: HexBase64Latin1Encoding): string;
331 getPrime(): Buffer;
332 getPrime(encoding: HexBase64Latin1Encoding): string;
333 getGenerator(): Buffer;
334 getGenerator(encoding: HexBase64Latin1Encoding): string;
335 getPublicKey(): Buffer;
336 getPublicKey(encoding: HexBase64Latin1Encoding): string;
337 getPrivateKey(): Buffer;
338 getPrivateKey(encoding: HexBase64Latin1Encoding): string;
339 setPublicKey(public_key: NodeJS.ArrayBufferView): void;
340 setPublicKey(public_key: string, encoding: string): void;
341 setPrivateKey(private_key: NodeJS.ArrayBufferView): void;
342 setPrivateKey(private_key: string, encoding: string): void;
343 verifyError: number;
344 }
345 function getDiffieHellman(group_name: string): DiffieHellman;
346 function pbkdf2(
347 password: BinaryLike,
348 salt: BinaryLike,
349 iterations: number,
350 keylen: number,
351 digest: string,
352 callback: (err: Error | null, derivedKey: Buffer) => any,
353 ): void;
354 function pbkdf2Sync(password: BinaryLike, salt: BinaryLike, iterations: number, keylen: number, digest: string): Buffer;
355
356 function randomBytes(size: number): Buffer;
357 function randomBytes(size: number, callback: (err: Error | null, buf: Buffer) => void): void;
358 function pseudoRandomBytes(size: number): Buffer;
359 function pseudoRandomBytes(size: number, callback: (err: Error | null, buf: Buffer) => void): void;
360
361 function randomFillSync<T extends NodeJS.ArrayBufferView>(buffer: T, offset?: number, size?: number): T;
362 function randomFill<T extends NodeJS.ArrayBufferView>(buffer: T, callback: (err: Error | null, buf: T) => void): void;
363 function randomFill<T extends NodeJS.ArrayBufferView>(buffer: T, offset: number, callback: (err: Error | null, buf: T) => void): void;
364 function randomFill<T extends NodeJS.ArrayBufferView>(buffer: T, offset: number, size: number, callback: (err: Error | null, buf: T) => void): void;
365
366 interface ScryptOptions {
367 N?: number;
368 r?: number;
369 p?: number;
370 maxmem?: number;
371 }
372 function scrypt(
373 password: BinaryLike,
374 salt: BinaryLike,
375 keylen: number, callback: (err: Error | null, derivedKey: Buffer) => void,
376 ): void;
377 function scrypt(
378 password: BinaryLike,
379 salt: BinaryLike,
380 keylen: number,
381 options: ScryptOptions,
382 callback: (err: Error | null, derivedKey: Buffer) => void,
383 ): void;
384 function scryptSync(password: BinaryLike, salt: BinaryLike, keylen: number, options?: ScryptOptions): Buffer;
385
386 interface RsaPublicKey {
387 key: KeyLike;
388 padding?: number;
389 }
390 interface RsaPrivateKey {
391 key: KeyLike;
392 passphrase?: string;
393 /**
394 * @default 'sha1'
395 */
396 oaepHash?: string;
397 oaepLabel?: NodeJS.TypedArray;
398 padding?: number;
399 }
400 function publicEncrypt(key: RsaPublicKey | RsaPrivateKey | KeyLike, buffer: NodeJS.ArrayBufferView): Buffer;
401 function publicDecrypt(key: RsaPublicKey | RsaPrivateKey | KeyLike, buffer: NodeJS.ArrayBufferView): Buffer;
402 function privateDecrypt(private_key: RsaPrivateKey | KeyLike, buffer: NodeJS.ArrayBufferView): Buffer;
403 function privateEncrypt(private_key: RsaPrivateKey | KeyLike, buffer: NodeJS.ArrayBufferView): Buffer;
404 function getCiphers(): string[];
405 function getCurves(): string[];
406 function getHashes(): string[];
407 class ECDH {
408 private constructor();
409 static convertKey(
410 key: BinaryLike,
411 curve: string,
412 inputEncoding?: HexBase64Latin1Encoding,
413 outputEncoding?: "latin1" | "hex" | "base64",
414 format?: "uncompressed" | "compressed" | "hybrid",
415 ): Buffer | string;
416 generateKeys(): Buffer;
417 generateKeys(encoding: HexBase64Latin1Encoding, format?: ECDHKeyFormat): string;
418 computeSecret(other_public_key: NodeJS.ArrayBufferView): Buffer;
419 computeSecret(other_public_key: string, input_encoding: HexBase64Latin1Encoding): Buffer;
420 computeSecret(other_public_key: NodeJS.ArrayBufferView, output_encoding: HexBase64Latin1Encoding): string;
421 computeSecret(other_public_key: string, input_encoding: HexBase64Latin1Encoding, output_encoding: HexBase64Latin1Encoding): string;
422 getPrivateKey(): Buffer;
423 getPrivateKey(encoding: HexBase64Latin1Encoding): string;
424 getPublicKey(): Buffer;
425 getPublicKey(encoding: HexBase64Latin1Encoding, format?: ECDHKeyFormat): string;
426 setPrivateKey(private_key: NodeJS.ArrayBufferView): void;
427 setPrivateKey(private_key: string, encoding: HexBase64Latin1Encoding): void;
428 }
429 function createECDH(curve_name: string): ECDH;
430 function timingSafeEqual(a: NodeJS.ArrayBufferView, b: NodeJS.ArrayBufferView): boolean;
431 /** @deprecated since v10.0.0 */
432 const DEFAULT_ENCODING: string;
433
434 type KeyType = 'rsa' | 'dsa' | 'ec';
435 type KeyFormat = 'pem' | 'der';
436
437 interface BasePrivateKeyEncodingOptions<T extends KeyFormat> {
438 format: T;
439 cipher?: string;
440 passphrase?: string;
441 }
442
443 interface KeyPairKeyObjectResult {
444 publicKey: KeyObject;
445 privateKey: KeyObject;
446 }
447
448 interface ECKeyPairKeyObjectOptions {
449 /**
450 * Name of the curve to use.
451 */
452 namedCurve: string;
453 }
454
455 interface RSAKeyPairKeyObjectOptions {
456 /**
457 * Key size in bits
458 */
459 modulusLength: number;
460
461 /**
462 * @default 0x10001
463 */
464 publicExponent?: number;
465 }
466
467 interface DSAKeyPairKeyObjectOptions {
468 /**
469 * Key size in bits
470 */
471 modulusLength: number;
472
473 /**
474 * Size of q in bits
475 */
476 divisorLength: number;
477 }
478
479 interface RSAKeyPairOptions<PubF extends KeyFormat, PrivF extends KeyFormat> {
480 /**
481 * Key size in bits
482 */
483 modulusLength: number;
484 /**
485 * @default 0x10001
486 */
487 publicExponent?: number;
488
489 publicKeyEncoding: {
490 type: 'pkcs1' | 'spki';
491 format: PubF;
492 };
493 privateKeyEncoding: BasePrivateKeyEncodingOptions<PrivF> & {
494 type: 'pkcs1' | 'pkcs8';
495 };
496 }
497
498 interface DSAKeyPairOptions<PubF extends KeyFormat, PrivF extends KeyFormat> {
499 /**
500 * Key size in bits
501 */
502 modulusLength: number;
503 /**
504 * Size of q in bits
505 */
506 divisorLength: number;
507
508 publicKeyEncoding: {
509 type: 'spki';
510 format: PubF;
511 };
512 privateKeyEncoding: BasePrivateKeyEncodingOptions<PrivF> & {
513 type: 'pkcs8';
514 };
515 }
516
517 interface ECKeyPairOptions<PubF extends KeyFormat, PrivF extends KeyFormat> {
518 /**
519 * Name of the curve to use.
520 */
521 namedCurve: string;
522
523 publicKeyEncoding: {
524 type: 'pkcs1' | 'spki';
525 format: PubF;
526 };
527 privateKeyEncoding: BasePrivateKeyEncodingOptions<PrivF> & {
528 type: 'sec1' | 'pkcs8';
529 };
530 }
531
532 interface KeyPairSyncResult<T1 extends string | Buffer, T2 extends string | Buffer> {
533 publicKey: T1;
534 privateKey: T2;
535 }
536
537 function generateKeyPairSync(type: 'rsa', options: RSAKeyPairOptions<'pem', 'pem'>): KeyPairSyncResult<string, string>;
538 function generateKeyPairSync(type: 'rsa', options: RSAKeyPairOptions<'pem', 'der'>): KeyPairSyncResult<string, Buffer>;
539 function generateKeyPairSync(type: 'rsa', options: RSAKeyPairOptions<'der', 'pem'>): KeyPairSyncResult<Buffer, string>;
540 function generateKeyPairSync(type: 'rsa', options: RSAKeyPairOptions<'der', 'der'>): KeyPairSyncResult<Buffer, Buffer>;
541 function generateKeyPairSync(type: 'rsa', options: RSAKeyPairKeyObjectOptions): KeyPairKeyObjectResult;
542
543 function generateKeyPairSync(type: 'dsa', options: DSAKeyPairOptions<'pem', 'pem'>): KeyPairSyncResult<string, string>;
544 function generateKeyPairSync(type: 'dsa', options: DSAKeyPairOptions<'pem', 'der'>): KeyPairSyncResult<string, Buffer>;
545 function generateKeyPairSync(type: 'dsa', options: DSAKeyPairOptions<'der', 'pem'>): KeyPairSyncResult<Buffer, string>;
546 function generateKeyPairSync(type: 'dsa', options: DSAKeyPairOptions<'der', 'der'>): KeyPairSyncResult<Buffer, Buffer>;
547 function generateKeyPairSync(type: 'dsa', options: DSAKeyPairKeyObjectOptions): KeyPairKeyObjectResult;
548
549 function generateKeyPairSync(type: 'ec', options: ECKeyPairOptions<'pem', 'pem'>): KeyPairSyncResult<string, string>;
550 function generateKeyPairSync(type: 'ec', options: ECKeyPairOptions<'pem', 'der'>): KeyPairSyncResult<string, Buffer>;
551 function generateKeyPairSync(type: 'ec', options: ECKeyPairOptions<'der', 'pem'>): KeyPairSyncResult<Buffer, string>;
552 function generateKeyPairSync(type: 'ec', options: ECKeyPairOptions<'der', 'der'>): KeyPairSyncResult<Buffer, Buffer>;
553 function generateKeyPairSync(type: 'ec', options: ECKeyPairKeyObjectOptions): KeyPairKeyObjectResult;
554
555 function generateKeyPair(type: 'rsa', options: RSAKeyPairOptions<'pem', 'pem'>, callback: (err: Error | null, publicKey: string, privateKey: string) => void): void;
556 function generateKeyPair(type: 'rsa', options: RSAKeyPairOptions<'pem', 'der'>, callback: (err: Error | null, publicKey: string, privateKey: Buffer) => void): void;
557 function generateKeyPair(type: 'rsa', options: RSAKeyPairOptions<'der', 'pem'>, callback: (err: Error | null, publicKey: Buffer, privateKey: string) => void): void;
558 function generateKeyPair(type: 'rsa', options: RSAKeyPairOptions<'der', 'der'>, callback: (err: Error | null, publicKey: Buffer, privateKey: Buffer) => void): void;
559 function generateKeyPair(type: 'rsa', options: RSAKeyPairKeyObjectOptions, callback: (err: Error | null, publicKey: KeyObject, privateKey: KeyObject) => void): void;
560
561 function generateKeyPair(type: 'dsa', options: DSAKeyPairOptions<'pem', 'pem'>, callback: (err: Error | null, publicKey: string, privateKey: string) => void): void;
562 function generateKeyPair(type: 'dsa', options: DSAKeyPairOptions<'pem', 'der'>, callback: (err: Error | null, publicKey: string, privateKey: Buffer) => void): void;
563 function generateKeyPair(type: 'dsa', options: DSAKeyPairOptions<'der', 'pem'>, callback: (err: Error | null, publicKey: Buffer, privateKey: string) => void): void;
564 function generateKeyPair(type: 'dsa', options: DSAKeyPairOptions<'der', 'der'>, callback: (err: Error | null, publicKey: Buffer, privateKey: Buffer) => void): void;
565 function generateKeyPair(type: 'dsa', options: DSAKeyPairKeyObjectOptions, callback: (err: Error | null, publicKey: KeyObject, privateKey: KeyObject) => void): void;
566
567 function generateKeyPair(type: 'ec', options: ECKeyPairOptions<'pem', 'pem'>, callback: (err: Error | null, publicKey: string, privateKey: string) => void): void;
568 function generateKeyPair(type: 'ec', options: ECKeyPairOptions<'pem', 'der'>, callback: (err: Error | null, publicKey: string, privateKey: Buffer) => void): void;
569 function generateKeyPair(type: 'ec', options: ECKeyPairOptions<'der', 'pem'>, callback: (err: Error | null, publicKey: Buffer, privateKey: string) => void): void;
570 function generateKeyPair(type: 'ec', options: ECKeyPairOptions<'der', 'der'>, callback: (err: Error | null, publicKey: Buffer, privateKey: Buffer) => void): void;
571 function generateKeyPair(type: 'ec', options: ECKeyPairKeyObjectOptions, callback: (err: Error | null, publicKey: KeyObject, privateKey: KeyObject) => void): void;
572
573 namespace generateKeyPair {
574 function __promisify__(type: "rsa", options: RSAKeyPairOptions<'pem', 'pem'>): Promise<{ publicKey: string, privateKey: string }>;
575 function __promisify__(type: "rsa", options: RSAKeyPairOptions<'pem', 'der'>): Promise<{ publicKey: string, privateKey: Buffer }>;
576 function __promisify__(type: "rsa", options: RSAKeyPairOptions<'der', 'pem'>): Promise<{ publicKey: Buffer, privateKey: string }>;
577 function __promisify__(type: "rsa", options: RSAKeyPairOptions<'der', 'der'>): Promise<{ publicKey: Buffer, privateKey: Buffer }>;
578 function __promisify__(type: "rsa", options: RSAKeyPairKeyObjectOptions): Promise<KeyPairKeyObjectResult>;
579
580 function __promisify__(type: "dsa", options: DSAKeyPairOptions<'pem', 'pem'>): Promise<{ publicKey: string, privateKey: string }>;
581 function __promisify__(type: "dsa", options: DSAKeyPairOptions<'pem', 'der'>): Promise<{ publicKey: string, privateKey: Buffer }>;
582 function __promisify__(type: "dsa", options: DSAKeyPairOptions<'der', 'pem'>): Promise<{ publicKey: Buffer, privateKey: string }>;
583 function __promisify__(type: "dsa", options: DSAKeyPairOptions<'der', 'der'>): Promise<{ publicKey: Buffer, privateKey: Buffer }>;
584 function __promisify__(type: "dsa", options: DSAKeyPairKeyObjectOptions): Promise<KeyPairKeyObjectResult>;
585
586 function __promisify__(type: "ec", options: ECKeyPairOptions<'pem', 'pem'>): Promise<{ publicKey: string, privateKey: string }>;
587 function __promisify__(type: "ec", options: ECKeyPairOptions<'pem', 'der'>): Promise<{ publicKey: string, privateKey: Buffer }>;
588 function __promisify__(type: "ec", options: ECKeyPairOptions<'der', 'pem'>): Promise<{ publicKey: Buffer, privateKey: string }>;
589 function __promisify__(type: "ec", options: ECKeyPairOptions<'der', 'der'>): Promise<{ publicKey: Buffer, privateKey: Buffer }>;
590 function __promisify__(type: "ec", options: ECKeyPairKeyObjectOptions): Promise<KeyPairKeyObjectResult>;
591 }
592
593 /**
594 * Calculates and returns the signature for `data` using the given private key and
595 * algorithm. If `algorithm` is `null` or `undefined`, then the algorithm is
596 * dependent upon the key type (especially Ed25519 and Ed448).
597 *
598 * If `key` is not a [`KeyObject`][], this function behaves as if `key` had been
599 * passed to [`crypto.createPrivateKey()`][].
600 */
601 function sign(algorithm: string | null | undefined, data: NodeJS.ArrayBufferView, key: KeyLike | SignPrivateKeyInput): Buffer;
602
603 interface VerifyKeyWithOptions extends KeyObject, SigningOptions {
604 }
605
606 /**
607 * Calculates and returns the signature for `data` using the given private key and
608 * algorithm. If `algorithm` is `null` or `undefined`, then the algorithm is
609 * dependent upon the key type (especially Ed25519 and Ed448).
610 *
611 * If `key` is not a [`KeyObject`][], this function behaves as if `key` had been
612 * passed to [`crypto.createPublicKey()`][].
613 */
614 function verify(algorithm: string | null | undefined, data: NodeJS.ArrayBufferView, key: KeyLike | VerifyKeyWithOptions, signature: NodeJS.ArrayBufferView): boolean;
615}