UNPKG

30.5 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 update(data: BinaryLike): Hash;
132 update(data: string, input_encoding: Utf8AsciiLatin1Encoding): Hash;
133 digest(): Buffer;
134 digest(encoding: HexBase64Latin1Encoding): string;
135 }
136 class Hmac extends stream.Transform {
137 private constructor();
138 update(data: BinaryLike): Hmac;
139 update(data: string, input_encoding: Utf8AsciiLatin1Encoding): Hmac;
140 digest(): Buffer;
141 digest(encoding: HexBase64Latin1Encoding): string;
142 }
143
144 type KeyObjectType = 'secret' | 'public' | 'private';
145
146 interface KeyExportOptions<T extends KeyFormat> {
147 type: 'pkcs1' | 'spki' | 'pkcs8' | 'sec1';
148 format: T;
149 cipher?: string;
150 passphrase?: string | Buffer;
151 }
152
153 class KeyObject {
154 private constructor();
155 asymmetricKeyType?: KeyType;
156 /**
157 * For asymmetric keys, this property represents the size of the embedded key in
158 * bytes. This property is `undefined` for symmetric keys.
159 */
160 asymmetricKeySize?: number;
161 export(options: KeyExportOptions<'pem'>): string | Buffer;
162 export(options?: KeyExportOptions<'der'>): Buffer;
163 symmetricSize?: number;
164 type: KeyObjectType;
165 }
166
167 type CipherCCMTypes = 'aes-128-ccm' | 'aes-192-ccm' | 'aes-256-ccm';
168 type CipherGCMTypes = 'aes-128-gcm' | 'aes-192-gcm' | 'aes-256-gcm';
169
170 type BinaryLike = string | NodeJS.ArrayBufferView;
171
172 type CipherKey = BinaryLike | KeyObject;
173
174 interface CipherCCMOptions extends stream.TransformOptions {
175 authTagLength: number;
176 }
177 interface CipherGCMOptions extends stream.TransformOptions {
178 authTagLength?: number;
179 }
180 /** @deprecated since v10.0.0 use createCipheriv() */
181 function createCipher(algorithm: CipherCCMTypes, password: BinaryLike, options: CipherCCMOptions): CipherCCM;
182 /** @deprecated since v10.0.0 use createCipheriv() */
183 function createCipher(algorithm: CipherGCMTypes, password: BinaryLike, options?: CipherGCMOptions): CipherGCM;
184 /** @deprecated since v10.0.0 use createCipheriv() */
185 function createCipher(algorithm: string, password: BinaryLike, options?: stream.TransformOptions): Cipher;
186
187 function createCipheriv(
188 algorithm: CipherCCMTypes,
189 key: CipherKey,
190 iv: BinaryLike | null,
191 options: CipherCCMOptions
192 ): CipherCCM;
193 function createCipheriv(
194 algorithm: CipherGCMTypes,
195 key: CipherKey,
196 iv: BinaryLike | null,
197 options?: CipherGCMOptions
198 ): CipherGCM;
199 function createCipheriv(
200 algorithm: string, key: CipherKey, iv: BinaryLike | null, options?: stream.TransformOptions
201 ): Cipher;
202
203 class Cipher extends stream.Transform {
204 private constructor();
205 update(data: BinaryLike): Buffer;
206 update(data: string, input_encoding: Utf8AsciiBinaryEncoding): Buffer;
207 update(data: NodeJS.ArrayBufferView, input_encoding: undefined, output_encoding: HexBase64BinaryEncoding): string;
208 update(data: string, input_encoding: Utf8AsciiBinaryEncoding | undefined, output_encoding: HexBase64BinaryEncoding): string;
209 final(): Buffer;
210 final(output_encoding: string): string;
211 setAutoPadding(auto_padding?: boolean): this;
212 // getAuthTag(): Buffer;
213 // setAAD(buffer: Buffer): this; // docs only say buffer
214 }
215 interface CipherCCM extends Cipher {
216 setAAD(buffer: Buffer, options: { plaintextLength: number }): this;
217 getAuthTag(): Buffer;
218 }
219 interface CipherGCM extends Cipher {
220 setAAD(buffer: Buffer, options?: { plaintextLength: number }): this;
221 getAuthTag(): Buffer;
222 }
223 /** @deprecated since v10.0.0 use createCipheriv() */
224 function createDecipher(algorithm: CipherCCMTypes, password: BinaryLike, options: CipherCCMOptions): DecipherCCM;
225 /** @deprecated since v10.0.0 use createCipheriv() */
226 function createDecipher(algorithm: CipherGCMTypes, password: BinaryLike, options?: CipherGCMOptions): DecipherGCM;
227 /** @deprecated since v10.0.0 use createCipheriv() */
228 function createDecipher(algorithm: string, password: BinaryLike, options?: stream.TransformOptions): Decipher;
229
230 function createDecipheriv(
231 algorithm: CipherCCMTypes,
232 key: BinaryLike,
233 iv: BinaryLike | null,
234 options: CipherCCMOptions,
235 ): DecipherCCM;
236 function createDecipheriv(
237 algorithm: CipherGCMTypes,
238 key: BinaryLike,
239 iv: BinaryLike | null,
240 options?: CipherGCMOptions,
241 ): DecipherGCM;
242 function createDecipheriv(algorithm: string, key: BinaryLike, iv: BinaryLike | null, options?: stream.TransformOptions): Decipher;
243
244 class Decipher extends stream.Transform {
245 private constructor();
246 update(data: NodeJS.ArrayBufferView): Buffer;
247 update(data: string, input_encoding: HexBase64BinaryEncoding): Buffer;
248 update(data: NodeJS.ArrayBufferView, input_encoding: undefined, output_encoding: Utf8AsciiBinaryEncoding): string;
249 update(data: string, input_encoding: HexBase64BinaryEncoding | undefined, output_encoding: Utf8AsciiBinaryEncoding): string;
250 final(): Buffer;
251 final(output_encoding: string): string;
252 setAutoPadding(auto_padding?: boolean): this;
253 // setAuthTag(tag: NodeJS.ArrayBufferView): this;
254 // setAAD(buffer: NodeJS.ArrayBufferView): this;
255 }
256 interface DecipherCCM extends Decipher {
257 setAuthTag(buffer: NodeJS.ArrayBufferView): this;
258 setAAD(buffer: NodeJS.ArrayBufferView, options: { plaintextLength: number }): this;
259 }
260 interface DecipherGCM extends Decipher {
261 setAuthTag(buffer: NodeJS.ArrayBufferView): this;
262 setAAD(buffer: NodeJS.ArrayBufferView, options?: { plaintextLength: number }): this;
263 }
264
265 interface PrivateKeyInput {
266 key: string | Buffer;
267 format?: KeyFormat;
268 type?: 'pkcs1' | 'pkcs8' | 'sec1';
269 passphrase?: string | Buffer;
270 }
271
272 interface PublicKeyInput {
273 key: string | Buffer;
274 format?: KeyFormat;
275 type?: 'pkcs1' | 'spki';
276 }
277
278 function createPrivateKey(key: PrivateKeyInput | string | Buffer): KeyObject;
279 function createPublicKey(key: PublicKeyInput | string | Buffer | KeyObject): KeyObject;
280 function createSecretKey(key: Buffer): KeyObject;
281
282 function createSign(algorithm: string, options?: stream.WritableOptions): Signer;
283
284 interface SigningOptions {
285 /**
286 * @See crypto.constants.RSA_PKCS1_PADDING
287 */
288 padding?: number;
289 saltLength?: number;
290 }
291
292 interface SignPrivateKeyInput extends PrivateKeyInput, SigningOptions {
293 }
294
295 type KeyLike = string | Buffer | KeyObject;
296
297 class Signer extends stream.Writable {
298 private constructor();
299
300 update(data: BinaryLike): Signer;
301 update(data: string, input_encoding: Utf8AsciiLatin1Encoding): Signer;
302 sign(private_key: SignPrivateKeyInput | KeyLike): Buffer;
303 sign(private_key: SignPrivateKeyInput | KeyLike, output_format: HexBase64Latin1Encoding): string;
304 }
305
306 function createVerify(algorithm: string, options?: stream.WritableOptions): Verify;
307 class Verify extends stream.Writable {
308 private constructor();
309
310 update(data: BinaryLike): Verify;
311 update(data: string, input_encoding: Utf8AsciiLatin1Encoding): Verify;
312 verify(object: Object | KeyLike, signature: NodeJS.ArrayBufferView): boolean;
313 verify(object: Object | KeyLike, signature: string, signature_format?: HexBase64Latin1Encoding): boolean;
314 // https://nodejs.org/api/crypto.html#crypto_verifier_verify_object_signature_signature_format
315 // The signature field accepts a TypedArray type, but it is only available starting ES2017
316 }
317 function createDiffieHellman(prime_length: number, generator?: number | NodeJS.ArrayBufferView): DiffieHellman;
318 function createDiffieHellman(prime: NodeJS.ArrayBufferView): DiffieHellman;
319 function createDiffieHellman(prime: string, prime_encoding: HexBase64Latin1Encoding): DiffieHellman;
320 function createDiffieHellman(prime: string, prime_encoding: HexBase64Latin1Encoding, generator: number | NodeJS.ArrayBufferView): DiffieHellman;
321 function createDiffieHellman(prime: string, prime_encoding: HexBase64Latin1Encoding, generator: string, generator_encoding: HexBase64Latin1Encoding): DiffieHellman;
322 class DiffieHellman {
323 private constructor();
324 generateKeys(): Buffer;
325 generateKeys(encoding: HexBase64Latin1Encoding): string;
326 computeSecret(other_public_key: NodeJS.ArrayBufferView): Buffer;
327 computeSecret(other_public_key: string, input_encoding: HexBase64Latin1Encoding): Buffer;
328 computeSecret(other_public_key: NodeJS.ArrayBufferView, output_encoding: HexBase64Latin1Encoding): string;
329 computeSecret(other_public_key: string, input_encoding: HexBase64Latin1Encoding, output_encoding: HexBase64Latin1Encoding): string;
330 getPrime(): Buffer;
331 getPrime(encoding: HexBase64Latin1Encoding): string;
332 getGenerator(): Buffer;
333 getGenerator(encoding: HexBase64Latin1Encoding): string;
334 getPublicKey(): Buffer;
335 getPublicKey(encoding: HexBase64Latin1Encoding): string;
336 getPrivateKey(): Buffer;
337 getPrivateKey(encoding: HexBase64Latin1Encoding): string;
338 setPublicKey(public_key: NodeJS.ArrayBufferView): void;
339 setPublicKey(public_key: string, encoding: string): void;
340 setPrivateKey(private_key: NodeJS.ArrayBufferView): void;
341 setPrivateKey(private_key: string, encoding: string): void;
342 verifyError: number;
343 }
344 function getDiffieHellman(group_name: string): DiffieHellman;
345 function pbkdf2(
346 password: BinaryLike,
347 salt: BinaryLike,
348 iterations: number,
349 keylen: number,
350 digest: string,
351 callback: (err: Error | null, derivedKey: Buffer) => any,
352 ): void;
353 function pbkdf2Sync(password: BinaryLike, salt: BinaryLike, iterations: number, keylen: number, digest: string): Buffer;
354
355 function randomBytes(size: number): Buffer;
356 function randomBytes(size: number, callback: (err: Error | null, buf: Buffer) => void): void;
357 function pseudoRandomBytes(size: number): Buffer;
358 function pseudoRandomBytes(size: number, callback: (err: Error | null, buf: Buffer) => void): void;
359
360 function randomFillSync<T extends NodeJS.ArrayBufferView>(buffer: T, offset?: number, size?: number): T;
361 function randomFill<T extends NodeJS.ArrayBufferView>(buffer: T, callback: (err: Error | null, buf: T) => void): void;
362 function randomFill<T extends NodeJS.ArrayBufferView>(buffer: T, offset: number, callback: (err: Error | null, buf: T) => void): void;
363 function randomFill<T extends NodeJS.ArrayBufferView>(buffer: T, offset: number, size: number, callback: (err: Error | null, buf: T) => void): void;
364
365 interface ScryptOptions {
366 N?: number;
367 r?: number;
368 p?: number;
369 maxmem?: number;
370 }
371 function scrypt(
372 password: BinaryLike,
373 salt: BinaryLike,
374 keylen: number, callback: (err: Error | null, derivedKey: Buffer) => void,
375 ): void;
376 function scrypt(
377 password: BinaryLike,
378 salt: BinaryLike,
379 keylen: number,
380 options: ScryptOptions,
381 callback: (err: Error | null, derivedKey: Buffer) => void,
382 ): void;
383 function scryptSync(password: BinaryLike, salt: BinaryLike, keylen: number, options?: ScryptOptions): Buffer;
384
385 interface RsaPublicKey {
386 key: KeyLike;
387 padding?: number;
388 }
389 interface RsaPrivateKey {
390 key: KeyLike;
391 passphrase?: string;
392 /**
393 * @default 'sha1'
394 */
395 oaepHash?: string;
396 oaepLabel?: NodeJS.TypedArray;
397 padding?: number;
398 }
399 function publicEncrypt(key: RsaPublicKey | RsaPrivateKey | KeyLike, buffer: NodeJS.ArrayBufferView): Buffer;
400 function publicDecrypt(key: RsaPublicKey | RsaPrivateKey | KeyLike, buffer: NodeJS.ArrayBufferView): Buffer;
401 function privateDecrypt(private_key: RsaPrivateKey | KeyLike, buffer: NodeJS.ArrayBufferView): Buffer;
402 function privateEncrypt(private_key: RsaPrivateKey | KeyLike, buffer: NodeJS.ArrayBufferView): Buffer;
403 function getCiphers(): string[];
404 function getCurves(): string[];
405 function getHashes(): string[];
406 class ECDH {
407 private constructor();
408 static convertKey(
409 key: BinaryLike,
410 curve: string,
411 inputEncoding?: HexBase64Latin1Encoding,
412 outputEncoding?: "latin1" | "hex" | "base64",
413 format?: "uncompressed" | "compressed" | "hybrid",
414 ): Buffer | string;
415 generateKeys(): Buffer;
416 generateKeys(encoding: HexBase64Latin1Encoding, format?: ECDHKeyFormat): string;
417 computeSecret(other_public_key: NodeJS.ArrayBufferView): Buffer;
418 computeSecret(other_public_key: string, input_encoding: HexBase64Latin1Encoding): Buffer;
419 computeSecret(other_public_key: NodeJS.ArrayBufferView, output_encoding: HexBase64Latin1Encoding): string;
420 computeSecret(other_public_key: string, input_encoding: HexBase64Latin1Encoding, output_encoding: HexBase64Latin1Encoding): string;
421 getPrivateKey(): Buffer;
422 getPrivateKey(encoding: HexBase64Latin1Encoding): string;
423 getPublicKey(): Buffer;
424 getPublicKey(encoding: HexBase64Latin1Encoding, format?: ECDHKeyFormat): string;
425 setPrivateKey(private_key: NodeJS.ArrayBufferView): void;
426 setPrivateKey(private_key: string, encoding: HexBase64Latin1Encoding): void;
427 }
428 function createECDH(curve_name: string): ECDH;
429 function timingSafeEqual(a: NodeJS.ArrayBufferView, b: NodeJS.ArrayBufferView): boolean;
430 /** @deprecated since v10.0.0 */
431 const DEFAULT_ENCODING: string;
432
433 type KeyType = 'rsa' | 'dsa' | 'ec';
434 type KeyFormat = 'pem' | 'der';
435
436 interface BasePrivateKeyEncodingOptions<T extends KeyFormat> {
437 format: T;
438 cipher?: string;
439 passphrase?: string;
440 }
441
442 interface KeyPairKeyObjectResult {
443 publicKey: KeyObject;
444 privateKey: KeyObject;
445 }
446
447 interface ECKeyPairKeyObjectOptions {
448 /**
449 * Name of the curve to use.
450 */
451 namedCurve: string;
452 }
453
454 interface RSAKeyPairKeyObjectOptions {
455 /**
456 * Key size in bits
457 */
458 modulusLength: number;
459
460 /**
461 * @default 0x10001
462 */
463 publicExponent?: number;
464 }
465
466 interface DSAKeyPairKeyObjectOptions {
467 /**
468 * Key size in bits
469 */
470 modulusLength: number;
471
472 /**
473 * Size of q in bits
474 */
475 divisorLength: number;
476 }
477
478 interface RSAKeyPairOptions<PubF extends KeyFormat, PrivF extends KeyFormat> {
479 /**
480 * Key size in bits
481 */
482 modulusLength: number;
483 /**
484 * @default 0x10001
485 */
486 publicExponent?: number;
487
488 publicKeyEncoding: {
489 type: 'pkcs1' | 'spki';
490 format: PubF;
491 };
492 privateKeyEncoding: BasePrivateKeyEncodingOptions<PrivF> & {
493 type: 'pkcs1' | 'pkcs8';
494 };
495 }
496
497 interface DSAKeyPairOptions<PubF extends KeyFormat, PrivF extends KeyFormat> {
498 /**
499 * Key size in bits
500 */
501 modulusLength: number;
502 /**
503 * Size of q in bits
504 */
505 divisorLength: number;
506
507 publicKeyEncoding: {
508 type: 'spki';
509 format: PubF;
510 };
511 privateKeyEncoding: BasePrivateKeyEncodingOptions<PrivF> & {
512 type: 'pkcs8';
513 };
514 }
515
516 interface ECKeyPairOptions<PubF extends KeyFormat, PrivF extends KeyFormat> {
517 /**
518 * Name of the curve to use.
519 */
520 namedCurve: string;
521
522 publicKeyEncoding: {
523 type: 'pkcs1' | 'spki';
524 format: PubF;
525 };
526 privateKeyEncoding: BasePrivateKeyEncodingOptions<PrivF> & {
527 type: 'sec1' | 'pkcs8';
528 };
529 }
530
531 interface KeyPairSyncResult<T1 extends string | Buffer, T2 extends string | Buffer> {
532 publicKey: T1;
533 privateKey: T2;
534 }
535
536 function generateKeyPairSync(type: 'rsa', options: RSAKeyPairOptions<'pem', 'pem'>): KeyPairSyncResult<string, string>;
537 function generateKeyPairSync(type: 'rsa', options: RSAKeyPairOptions<'pem', 'der'>): KeyPairSyncResult<string, Buffer>;
538 function generateKeyPairSync(type: 'rsa', options: RSAKeyPairOptions<'der', 'pem'>): KeyPairSyncResult<Buffer, string>;
539 function generateKeyPairSync(type: 'rsa', options: RSAKeyPairOptions<'der', 'der'>): KeyPairSyncResult<Buffer, Buffer>;
540 function generateKeyPairSync(type: 'rsa', options: RSAKeyPairKeyObjectOptions): KeyPairKeyObjectResult;
541
542 function generateKeyPairSync(type: 'dsa', options: DSAKeyPairOptions<'pem', 'pem'>): KeyPairSyncResult<string, string>;
543 function generateKeyPairSync(type: 'dsa', options: DSAKeyPairOptions<'pem', 'der'>): KeyPairSyncResult<string, Buffer>;
544 function generateKeyPairSync(type: 'dsa', options: DSAKeyPairOptions<'der', 'pem'>): KeyPairSyncResult<Buffer, string>;
545 function generateKeyPairSync(type: 'dsa', options: DSAKeyPairOptions<'der', 'der'>): KeyPairSyncResult<Buffer, Buffer>;
546 function generateKeyPairSync(type: 'dsa', options: DSAKeyPairKeyObjectOptions): KeyPairKeyObjectResult;
547
548 function generateKeyPairSync(type: 'ec', options: ECKeyPairOptions<'pem', 'pem'>): KeyPairSyncResult<string, string>;
549 function generateKeyPairSync(type: 'ec', options: ECKeyPairOptions<'pem', 'der'>): KeyPairSyncResult<string, Buffer>;
550 function generateKeyPairSync(type: 'ec', options: ECKeyPairOptions<'der', 'pem'>): KeyPairSyncResult<Buffer, string>;
551 function generateKeyPairSync(type: 'ec', options: ECKeyPairOptions<'der', 'der'>): KeyPairSyncResult<Buffer, Buffer>;
552 function generateKeyPairSync(type: 'ec', options: ECKeyPairKeyObjectOptions): KeyPairKeyObjectResult;
553
554 function generateKeyPair(type: 'rsa', options: RSAKeyPairOptions<'pem', 'pem'>, callback: (err: Error | null, publicKey: string, privateKey: string) => void): void;
555 function generateKeyPair(type: 'rsa', options: RSAKeyPairOptions<'pem', 'der'>, callback: (err: Error | null, publicKey: string, privateKey: Buffer) => void): void;
556 function generateKeyPair(type: 'rsa', options: RSAKeyPairOptions<'der', 'pem'>, callback: (err: Error | null, publicKey: Buffer, privateKey: string) => void): void;
557 function generateKeyPair(type: 'rsa', options: RSAKeyPairOptions<'der', 'der'>, callback: (err: Error | null, publicKey: Buffer, privateKey: Buffer) => void): void;
558 function generateKeyPair(type: 'rsa', options: RSAKeyPairKeyObjectOptions, callback: (err: Error | null, publicKey: KeyObject, privateKey: KeyObject) => void): void;
559
560 function generateKeyPair(type: 'dsa', options: DSAKeyPairOptions<'pem', 'pem'>, callback: (err: Error | null, publicKey: string, privateKey: string) => void): void;
561 function generateKeyPair(type: 'dsa', options: DSAKeyPairOptions<'pem', 'der'>, callback: (err: Error | null, publicKey: string, privateKey: Buffer) => void): void;
562 function generateKeyPair(type: 'dsa', options: DSAKeyPairOptions<'der', 'pem'>, callback: (err: Error | null, publicKey: Buffer, privateKey: string) => void): void;
563 function generateKeyPair(type: 'dsa', options: DSAKeyPairOptions<'der', 'der'>, callback: (err: Error | null, publicKey: Buffer, privateKey: Buffer) => void): void;
564 function generateKeyPair(type: 'dsa', options: DSAKeyPairKeyObjectOptions, callback: (err: Error | null, publicKey: KeyObject, privateKey: KeyObject) => void): void;
565
566 function generateKeyPair(type: 'ec', options: ECKeyPairOptions<'pem', 'pem'>, callback: (err: Error | null, publicKey: string, privateKey: string) => void): void;
567 function generateKeyPair(type: 'ec', options: ECKeyPairOptions<'pem', 'der'>, callback: (err: Error | null, publicKey: string, privateKey: Buffer) => void): void;
568 function generateKeyPair(type: 'ec', options: ECKeyPairOptions<'der', 'pem'>, callback: (err: Error | null, publicKey: Buffer, privateKey: string) => void): void;
569 function generateKeyPair(type: 'ec', options: ECKeyPairOptions<'der', 'der'>, callback: (err: Error | null, publicKey: Buffer, privateKey: Buffer) => void): void;
570 function generateKeyPair(type: 'ec', options: ECKeyPairKeyObjectOptions, callback: (err: Error | null, publicKey: KeyObject, privateKey: KeyObject) => void): void;
571
572 namespace generateKeyPair {
573 function __promisify__(type: "rsa", options: RSAKeyPairOptions<'pem', 'pem'>): Promise<{ publicKey: string, privateKey: string }>;
574 function __promisify__(type: "rsa", options: RSAKeyPairOptions<'pem', 'der'>): Promise<{ publicKey: string, privateKey: Buffer }>;
575 function __promisify__(type: "rsa", options: RSAKeyPairOptions<'der', 'pem'>): Promise<{ publicKey: Buffer, privateKey: string }>;
576 function __promisify__(type: "rsa", options: RSAKeyPairOptions<'der', 'der'>): Promise<{ publicKey: Buffer, privateKey: Buffer }>;
577 function __promisify__(type: "rsa", options: RSAKeyPairKeyObjectOptions): Promise<KeyPairKeyObjectResult>;
578
579 function __promisify__(type: "dsa", options: DSAKeyPairOptions<'pem', 'pem'>): Promise<{ publicKey: string, privateKey: string }>;
580 function __promisify__(type: "dsa", options: DSAKeyPairOptions<'pem', 'der'>): Promise<{ publicKey: string, privateKey: Buffer }>;
581 function __promisify__(type: "dsa", options: DSAKeyPairOptions<'der', 'pem'>): Promise<{ publicKey: Buffer, privateKey: string }>;
582 function __promisify__(type: "dsa", options: DSAKeyPairOptions<'der', 'der'>): Promise<{ publicKey: Buffer, privateKey: Buffer }>;
583 function __promisify__(type: "dsa", options: DSAKeyPairKeyObjectOptions): Promise<KeyPairKeyObjectResult>;
584
585 function __promisify__(type: "ec", options: ECKeyPairOptions<'pem', 'pem'>): Promise<{ publicKey: string, privateKey: string }>;
586 function __promisify__(type: "ec", options: ECKeyPairOptions<'pem', 'der'>): Promise<{ publicKey: string, privateKey: Buffer }>;
587 function __promisify__(type: "ec", options: ECKeyPairOptions<'der', 'pem'>): Promise<{ publicKey: Buffer, privateKey: string }>;
588 function __promisify__(type: "ec", options: ECKeyPairOptions<'der', 'der'>): Promise<{ publicKey: Buffer, privateKey: Buffer }>;
589 function __promisify__(type: "ec", options: ECKeyPairKeyObjectOptions): Promise<KeyPairKeyObjectResult>;
590 }
591
592 /**
593 * Calculates and returns the signature for `data` using the given private key and
594 * algorithm. If `algorithm` is `null` or `undefined`, then the algorithm is
595 * dependent upon the key type (especially Ed25519 and Ed448).
596 *
597 * If `key` is not a [`KeyObject`][], this function behaves as if `key` had been
598 * passed to [`crypto.createPrivateKey()`][].
599 */
600 function sign(algorithm: string | null | undefined, data: NodeJS.ArrayBufferView, key: KeyLike | SignPrivateKeyInput): Buffer;
601
602 interface VerifyKeyWithOptions extends KeyObject, SigningOptions {
603 }
604
605 /**
606 * Calculates and returns the signature for `data` using the given private key and
607 * algorithm. If `algorithm` is `null` or `undefined`, then the algorithm is
608 * dependent upon the key type (especially Ed25519 and Ed448).
609 *
610 * If `key` is not a [`KeyObject`][], this function behaves as if `key` had been
611 * passed to [`crypto.createPublicKey()`][].
612 */
613 function verify(algorithm: string | null | undefined, data: NodeJS.ArrayBufferView, key: KeyLike | VerifyKeyWithOptions, signature: NodeJS.ArrayBufferView): Buffer;
614}