@types/node 16.4.1 → 16.4.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
node/crypto.d.ts CHANGED
@@ -1,7 +1,35 @@
1
+ /**
2
+ * The `crypto` module provides cryptographic functionality that includes a set of
3
+ * wrappers for OpenSSL's hash, HMAC, cipher, decipher, sign, and verify functions.
4
+ *
5
+ * ```js
6
+ * import { createHmac } from 'crypto';
7
+ *
8
+ * const secret = 'abcdefg';
9
+ * const hash = createHmac('sha256', secret)
10
+ * .update('I love cupcakes')
11
+ * .digest('hex');
12
+ * console.log(hash);
13
+ * // Prints:
14
+ * // c0fa1bc00531bd78ef38c628449c5102aeabd49b5dc3a2a516ea6ea959d6658e
15
+ * ```
16
+ *
17
+ * ```js
18
+ * const crypto = require('crypto');
19
+ *
20
+ * const secret = 'abcdefg';
21
+ * const hash = crypto.createHmac('sha256', secret)
22
+ * .update('I love cupcakes')
23
+ * .digest('hex');
24
+ * console.log(hash);
25
+ * // Prints:
26
+ * // c0fa1bc00531bd78ef38c628449c5102aeabd49b5dc3a2a516ea6ea959d6658e
27
+ * ```
28
+ * @see [source](https://github.com/nodejs/node/blob/v16.4.2/lib/crypto.js)
29
+ */
1
30
  declare module 'crypto' {
2
31
  import * as stream from 'node:stream';
3
32
  import { PeerCertificate } from 'node:tls';
4
-
5
33
  interface Certificate {
6
34
  /**
7
35
  * @deprecated
@@ -28,10 +56,9 @@ declare module 'crypto' {
28
56
  }
29
57
  const Certificate: Certificate & {
30
58
  /** @deprecated since v14.9.0 - Use static methods of `crypto.Certificate` instead. */
31
- new(): Certificate;
59
+ new (): Certificate;
32
60
  /** @deprecated since v14.9.0 - Use static methods of `crypto.Certificate` instead. */
33
61
  (): Certificate;
34
-
35
62
  /**
36
63
  * @param spkac
37
64
  * @returns The challenge component of the `spkac` data structure,
@@ -52,11 +79,9 @@ declare module 'crypto' {
52
79
  */
53
80
  verifySpkac(spkac: NodeJS.ArrayBufferView): boolean;
54
81
  };
55
-
56
82
  namespace constants {
57
83
  // https://nodejs.org/dist/latest-v10.x/docs/api/crypto.html#crypto_crypto_constants
58
84
  const OPENSSL_VERSION_NUMBER: number;
59
-
60
85
  /** Applies multiple bug workarounds within OpenSSL. See https://www.openssl.org/docs/man1.0.2/ssl/SSL_CTX_set_options.html for detail. */
61
86
  const SSL_OP_ALL: number;
62
87
  /** 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. */
@@ -106,7 +131,6 @@ declare module 'crypto' {
106
131
  const SSL_OP_TLS_D5_BUG: number;
107
132
  /** Instructs OpenSSL to disable version rollback attack detection. */
108
133
  const SSL_OP_TLS_ROLLBACK_BUG: number;
109
-
110
134
  const ENGINE_METHOD_RSA: number;
111
135
  const ENGINE_METHOD_DSA: number;
112
136
  const ENGINE_METHOD_DH: number;
@@ -118,14 +142,11 @@ declare module 'crypto' {
118
142
  const ENGINE_METHOD_PKEY_ASN1_METHS: number;
119
143
  const ENGINE_METHOD_ALL: number;
120
144
  const ENGINE_METHOD_NONE: number;
121
-
122
145
  const DH_CHECK_P_NOT_SAFE_PRIME: number;
123
146
  const DH_CHECK_P_NOT_PRIME: number;
124
147
  const DH_UNABLE_TO_CHECK_GENERATOR: number;
125
148
  const DH_NOT_SUITABLE_GENERATOR: number;
126
-
127
149
  const ALPN_ENABLED: number;
128
-
129
150
  const RSA_PKCS1_PADDING: number;
130
151
  const RSA_SSLV23_PADDING: number;
131
152
  const RSA_NO_PADDING: number;
@@ -138,17 +159,14 @@ declare module 'crypto' {
138
159
  const RSA_PSS_SALTLEN_MAX_SIGN: number;
139
160
  /** Causes the salt length for RSA_PKCS1_PSS_PADDING to be determined automatically when verifying a signature. */
140
161
  const RSA_PSS_SALTLEN_AUTO: number;
141
-
142
162
  const POINT_CONVERSION_COMPRESSED: number;
143
163
  const POINT_CONVERSION_UNCOMPRESSED: number;
144
164
  const POINT_CONVERSION_HYBRID: number;
145
-
146
165
  /** Specifies the built-in default cipher list used by Node.js (colon-separated values). */
147
166
  const defaultCoreCipherList: string;
148
167
  /** Specifies the active default cipher list used by the current Node.js process (colon-separated values). */
149
168
  const defaultCipherList: string;
150
169
  }
151
-
152
170
  interface HashOptions extends stream.TransformOptions {
153
171
  /**
154
172
  * For XOF hash functions such as `shake256`, the
@@ -156,40 +174,495 @@ declare module 'crypto' {
156
174
  */
157
175
  outputLength?: number | undefined;
158
176
  }
159
-
160
177
  /** @deprecated since v10.0.0 */
161
178
  const fips: boolean;
162
-
179
+ /**
180
+ * Creates and returns a `Hash` object that can be used to generate hash digests
181
+ * using the given `algorithm`. Optional `options` argument controls stream
182
+ * behavior. For XOF hash functions such as `'shake256'`, the `outputLength` option
183
+ * can be used to specify the desired output length in bytes.
184
+ *
185
+ * The `algorithm` is dependent on the available algorithms supported by the
186
+ * version of OpenSSL on the platform. Examples are `'sha256'`, `'sha512'`, etc.
187
+ * On recent releases of OpenSSL, `openssl list -digest-algorithms`(`openssl list-message-digest-algorithms` for older versions of OpenSSL) will
188
+ * display the available digest algorithms.
189
+ *
190
+ * Example: generating the sha256 sum of a file
191
+ *
192
+ * ```js
193
+ * import {
194
+ * createReadStream
195
+ * } from 'fs';
196
+ *
197
+ * const {
198
+ * createHash,
199
+ * } = await import('crypto');
200
+ *
201
+ * const filename = process.argv[2];
202
+ *
203
+ * const hash = createHash('sha256');
204
+ *
205
+ * const input = createReadStream(filename);
206
+ * input.on('readable', () => {
207
+ * // Only one element is going to be produced by the
208
+ * // hash stream.
209
+ * const data = input.read();
210
+ * if (data)
211
+ * hash.update(data);
212
+ * else {
213
+ * console.log(`${hash.digest('hex')} ${filename}`);
214
+ * }
215
+ * });
216
+ * ```
217
+ *
218
+ * ```js
219
+ * const {
220
+ * createReadStream,
221
+ * } = require('fs');
222
+ *
223
+ * const {
224
+ * createHash,
225
+ * } = require('crypto');
226
+ *
227
+ * const filename = process.argv[2];
228
+ *
229
+ * const hash = createHash('sha256');
230
+ *
231
+ * const input = createReadStream(filename);
232
+ * input.on('readable', () => {
233
+ * // Only one element is going to be produced by the
234
+ * // hash stream.
235
+ * const data = input.read();
236
+ * if (data)
237
+ * hash.update(data);
238
+ * else {
239
+ * console.log(`${hash.digest('hex')} ${filename}`);
240
+ * }
241
+ * });
242
+ * ```
243
+ * @since v0.1.92
244
+ * @param options `stream.transform` options
245
+ */
163
246
  function createHash(algorithm: string, options?: HashOptions): Hash;
247
+ /**
248
+ * Creates and returns an `Hmac` object that uses the given `algorithm` and `key`.
249
+ * Optional `options` argument controls stream behavior.
250
+ *
251
+ * The `algorithm` is dependent on the available algorithms supported by the
252
+ * version of OpenSSL on the platform. Examples are `'sha256'`, `'sha512'`, etc.
253
+ * On recent releases of OpenSSL, `openssl list -digest-algorithms`(`openssl list-message-digest-algorithms` for older versions of OpenSSL) will
254
+ * display the available digest algorithms.
255
+ *
256
+ * The `key` is the HMAC key used to generate the cryptographic HMAC hash. If it is
257
+ * a `KeyObject`, its type must be `secret`.
258
+ *
259
+ * Example: generating the sha256 HMAC of a file
260
+ *
261
+ * ```js
262
+ * import {
263
+ * createReadStream
264
+ * } from 'fs';
265
+ *
266
+ * const {
267
+ * createHmac,
268
+ * } = await import('crypto');
269
+ *
270
+ * const filename = process.argv[2];
271
+ *
272
+ * const hmac = createHmac('sha256', 'a secret');
273
+ *
274
+ * const input = createReadStream(filename);
275
+ * input.on('readable', () => {
276
+ * // Only one element is going to be produced by the
277
+ * // hash stream.
278
+ * const data = input.read();
279
+ * if (data)
280
+ * hmac.update(data);
281
+ * else {
282
+ * console.log(`${hmac.digest('hex')} ${filename}`);
283
+ * }
284
+ * });
285
+ * ```
286
+ *
287
+ * ```js
288
+ * const {
289
+ * createReadStream,
290
+ * } = require('fs');
291
+ *
292
+ * const {
293
+ * createHmac,
294
+ * } = require('crypto');
295
+ *
296
+ * const filename = process.argv[2];
297
+ *
298
+ * const hmac = createHmac('sha256', 'a secret');
299
+ *
300
+ * const input = createReadStream(filename);
301
+ * input.on('readable', () => {
302
+ * // Only one element is going to be produced by the
303
+ * // hash stream.
304
+ * const data = input.read();
305
+ * if (data)
306
+ * hmac.update(data);
307
+ * else {
308
+ * console.log(`${hmac.digest('hex')} ${filename}`);
309
+ * }
310
+ * });
311
+ * ```
312
+ * @since v0.1.94
313
+ * @param options `stream.transform` options
314
+ */
164
315
  function createHmac(algorithm: string, key: BinaryLike | KeyObject, options?: stream.TransformOptions): Hmac;
165
-
166
316
  // https://nodejs.org/api/buffer.html#buffer_buffers_and_character_encodings
167
317
  type BinaryToTextEncoding = 'base64' | 'hex';
168
318
  type CharacterEncoding = 'utf8' | 'utf-8' | 'utf16le' | 'latin1';
169
319
  type LegacyCharacterEncoding = 'ascii' | 'binary' | 'ucs2' | 'ucs-2';
170
-
171
320
  type Encoding = BinaryToTextEncoding | CharacterEncoding | LegacyCharacterEncoding;
172
-
173
321
  type ECDHKeyFormat = 'compressed' | 'uncompressed' | 'hybrid';
174
-
322
+ /**
323
+ * * Extends: `<stream.Transform>`
324
+ *
325
+ * The `Hash` class is a utility for creating hash digests of data. It can be
326
+ * used in one of two ways:
327
+ *
328
+ * * As a `stream` that is both readable and writable, where data is written
329
+ * to produce a computed hash digest on the readable side, or
330
+ * * Using the `hash.update()` and `hash.digest()` methods to produce the
331
+ * computed hash.
332
+ *
333
+ * The {@link createHash} method is used to create `Hash` instances. `Hash`objects are not to be created directly using the `new` keyword.
334
+ *
335
+ * Example: Using `Hash` objects as streams:
336
+ *
337
+ * ```js
338
+ * const {
339
+ * createHash,
340
+ * } = await import('crypto');
341
+ *
342
+ * const hash = createHash('sha256');
343
+ *
344
+ * hash.on('readable', () => {
345
+ * // Only one element is going to be produced by the
346
+ * // hash stream.
347
+ * const data = hash.read();
348
+ * if (data) {
349
+ * console.log(data.toString('hex'));
350
+ * // Prints:
351
+ * // 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50
352
+ * }
353
+ * });
354
+ *
355
+ * hash.write('some data to hash');
356
+ * hash.end();
357
+ * ```
358
+ *
359
+ * ```js
360
+ * const {
361
+ * createHash,
362
+ * } = require('crypto');
363
+ *
364
+ * const hash = createHash('sha256');
365
+ *
366
+ * hash.on('readable', () => {
367
+ * // Only one element is going to be produced by the
368
+ * // hash stream.
369
+ * const data = hash.read();
370
+ * if (data) {
371
+ * console.log(data.toString('hex'));
372
+ * // Prints:
373
+ * // 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50
374
+ * }
375
+ * });
376
+ *
377
+ * hash.write('some data to hash');
378
+ * hash.end();
379
+ * ```
380
+ *
381
+ * Example: Using `Hash` and piped streams:
382
+ *
383
+ * ```js
384
+ * import { createReadStream } from 'fs';
385
+ *
386
+ * const {
387
+ * createHash,
388
+ * } = await import('crypto');
389
+ * const hash = createHash('sha256');
390
+ *
391
+ * const input = createReadStream('test.js');
392
+ * input.pipe(hash).setEncoding('hex').pipe(process.stdout);
393
+ * ```
394
+ *
395
+ * ```js
396
+ * const {
397
+ * createReadStream,
398
+ * } = require('fs');
399
+ *
400
+ * const {
401
+ * createHash,
402
+ * } = require('crypto');
403
+ *
404
+ * const hash = createHash('sha256');
405
+ *
406
+ * const input = createReadStream('test.js');
407
+ * input.pipe(hash).setEncoding('hex').pipe(process.stdout);
408
+ * ```
409
+ *
410
+ * Example: Using the `hash.update()` and `hash.digest()` methods:
411
+ *
412
+ * ```js
413
+ * const {
414
+ * createHash,
415
+ * } = await import('crypto');
416
+ *
417
+ * const hash = createHash('sha256');
418
+ *
419
+ * hash.update('some data to hash');
420
+ * console.log(hash.digest('hex'));
421
+ * // Prints:
422
+ * // 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50
423
+ * ```
424
+ *
425
+ * ```js
426
+ * const {
427
+ * createHash,
428
+ * } = require('crypto');
429
+ *
430
+ * const hash = createHash('sha256');
431
+ *
432
+ * hash.update('some data to hash');
433
+ * console.log(hash.digest('hex'));
434
+ * // Prints:
435
+ * // 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50
436
+ * ```
437
+ * @since v0.1.92
438
+ */
175
439
  class Hash extends stream.Transform {
176
440
  private constructor();
177
- copy(): Hash;
441
+ /**
442
+ * Creates a new `Hash` object that contains a deep copy of the internal state
443
+ * of the current `Hash` object.
444
+ *
445
+ * The optional `options` argument controls stream behavior. For XOF hash
446
+ * functions such as `'shake256'`, the `outputLength` option can be used to
447
+ * specify the desired output length in bytes.
448
+ *
449
+ * An error is thrown when an attempt is made to copy the `Hash` object after
450
+ * its `hash.digest()` method has been called.
451
+ *
452
+ * ```js
453
+ * // Calculate a rolling hash.
454
+ * const {
455
+ * createHash,
456
+ * } = await import('crypto');
457
+ *
458
+ * const hash = createHash('sha256');
459
+ *
460
+ * hash.update('one');
461
+ * console.log(hash.copy().digest('hex'));
462
+ *
463
+ * hash.update('two');
464
+ * console.log(hash.copy().digest('hex'));
465
+ *
466
+ * hash.update('three');
467
+ * console.log(hash.copy().digest('hex'));
468
+ *
469
+ * // Etc.
470
+ * ```
471
+ *
472
+ * ```js
473
+ * // Calculate a rolling hash.
474
+ * const {
475
+ * createHash,
476
+ * } = require('crypto');
477
+ *
478
+ * const hash = createHash('sha256');
479
+ *
480
+ * hash.update('one');
481
+ * console.log(hash.copy().digest('hex'));
482
+ *
483
+ * hash.update('two');
484
+ * console.log(hash.copy().digest('hex'));
485
+ *
486
+ * hash.update('three');
487
+ * console.log(hash.copy().digest('hex'));
488
+ *
489
+ * // Etc.
490
+ * ```
491
+ * @since v13.1.0
492
+ * @param options `stream.transform` options
493
+ */
494
+ copy(options?: stream.TransformOptions): Hash;
495
+ /**
496
+ * Updates the hash content with the given `data`, the encoding of which
497
+ * is given in `inputEncoding`.
498
+ * If `encoding` is not provided, and the `data` is a string, an
499
+ * encoding of `'utf8'` is enforced. If `data` is a `Buffer`, `TypedArray`, or`DataView`, then `inputEncoding` is ignored.
500
+ *
501
+ * This can be called many times with new data as it is streamed.
502
+ * @since v0.1.92
503
+ * @param inputEncoding The `encoding` of the `data` string.
504
+ */
178
505
  update(data: BinaryLike): Hash;
179
- update(data: string, input_encoding: Encoding): Hash;
506
+ update(data: string, inputEncoding: Encoding): Hash;
507
+ /**
508
+ * Calculates the digest of all of the data passed to be hashed (using the `hash.update()` method).
509
+ * If `encoding` is provided a string will be returned; otherwise
510
+ * a `Buffer` is returned.
511
+ *
512
+ * The `Hash` object can not be used again after `hash.digest()` method has been
513
+ * called. Multiple calls will cause an error to be thrown.
514
+ * @since v0.1.92
515
+ * @param encoding The `encoding` of the return value.
516
+ */
180
517
  digest(): Buffer;
181
518
  digest(encoding: BinaryToTextEncoding): string;
182
519
  }
520
+ /**
521
+ * * Extends: `<stream.Transform>`
522
+ *
523
+ * The `Hmac` class is a utility for creating cryptographic HMAC digests. It can
524
+ * be used in one of two ways:
525
+ *
526
+ * * As a `stream` that is both readable and writable, where data is written
527
+ * to produce a computed HMAC digest on the readable side, or
528
+ * * Using the `hmac.update()` and `hmac.digest()` methods to produce the
529
+ * computed HMAC digest.
530
+ *
531
+ * The {@link createHmac} method is used to create `Hmac` instances. `Hmac`objects are not to be created directly using the `new` keyword.
532
+ *
533
+ * Example: Using `Hmac` objects as streams:
534
+ *
535
+ * ```js
536
+ * const {
537
+ * createHmac,
538
+ * } = await import('crypto');
539
+ *
540
+ * const hmac = createHmac('sha256', 'a secret');
541
+ *
542
+ * hmac.on('readable', () => {
543
+ * // Only one element is going to be produced by the
544
+ * // hash stream.
545
+ * const data = hmac.read();
546
+ * if (data) {
547
+ * console.log(data.toString('hex'));
548
+ * // Prints:
549
+ * // 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e
550
+ * }
551
+ * });
552
+ *
553
+ * hmac.write('some data to hash');
554
+ * hmac.end();
555
+ * ```
556
+ *
557
+ * ```js
558
+ * const {
559
+ * createHmac,
560
+ * } = require('crypto');
561
+ *
562
+ * const hmac = createHmac('sha256', 'a secret');
563
+ *
564
+ * hmac.on('readable', () => {
565
+ * // Only one element is going to be produced by the
566
+ * // hash stream.
567
+ * const data = hmac.read();
568
+ * if (data) {
569
+ * console.log(data.toString('hex'));
570
+ * // Prints:
571
+ * // 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e
572
+ * }
573
+ * });
574
+ *
575
+ * hmac.write('some data to hash');
576
+ * hmac.end();
577
+ * ```
578
+ *
579
+ * Example: Using `Hmac` and piped streams:
580
+ *
581
+ * ```js
582
+ * import { createReadStream } from 'fs';
583
+ *
584
+ * const {
585
+ * createHmac,
586
+ * } = await import('crypto');
587
+ *
588
+ * const hmac = createHmac('sha256', 'a secret');
589
+ *
590
+ * const input = createReadStream('test.js');
591
+ * input.pipe(hmac).pipe(process.stdout);
592
+ * ```
593
+ *
594
+ * ```js
595
+ * const {
596
+ * createReadStream,
597
+ * } = require('fs');
598
+ *
599
+ * const {
600
+ * createHmac,
601
+ * } = require('crypto');
602
+ *
603
+ * const hmac = createHmac('sha256', 'a secret');
604
+ *
605
+ * const input = createReadStream('test.js');
606
+ * input.pipe(hmac).pipe(process.stdout);
607
+ * ```
608
+ *
609
+ * Example: Using the `hmac.update()` and `hmac.digest()` methods:
610
+ *
611
+ * ```js
612
+ * const {
613
+ * createHmac,
614
+ * } = await import('crypto');
615
+ *
616
+ * const hmac = createHmac('sha256', 'a secret');
617
+ *
618
+ * hmac.update('some data to hash');
619
+ * console.log(hmac.digest('hex'));
620
+ * // Prints:
621
+ * // 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e
622
+ * ```
623
+ *
624
+ * ```js
625
+ * const {
626
+ * createHmac,
627
+ * } = require('crypto');
628
+ *
629
+ * const hmac = createHmac('sha256', 'a secret');
630
+ *
631
+ * hmac.update('some data to hash');
632
+ * console.log(hmac.digest('hex'));
633
+ * // Prints:
634
+ * // 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e
635
+ * ```
636
+ * @since v0.1.94
637
+ */
183
638
  class Hmac extends stream.Transform {
184
639
  private constructor();
640
+ /**
641
+ * Updates the `Hmac` content with the given `data`, the encoding of which
642
+ * is given in `inputEncoding`.
643
+ * If `encoding` is not provided, and the `data` is a string, an
644
+ * encoding of `'utf8'` is enforced. If `data` is a `Buffer`, `TypedArray`, or`DataView`, then `inputEncoding` is ignored.
645
+ *
646
+ * This can be called many times with new data as it is streamed.
647
+ * @since v0.1.94
648
+ * @param inputEncoding The `encoding` of the `data` string.
649
+ */
185
650
  update(data: BinaryLike): Hmac;
186
- update(data: string, input_encoding: Encoding): Hmac;
651
+ update(data: string, inputEncoding: Encoding): Hmac;
652
+ /**
653
+ * Calculates the HMAC digest of all of the data passed using `hmac.update()`.
654
+ * If `encoding` is
655
+ * provided a string is returned; otherwise a `Buffer` is returned;
656
+ *
657
+ * The `Hmac` object can not be used again after `hmac.digest()` has been
658
+ * called. Multiple calls to `hmac.digest()` will result in an error being thrown.
659
+ * @since v0.1.94
660
+ * @param encoding The `encoding` of the return value.
661
+ */
187
662
  digest(): Buffer;
188
663
  digest(encoding: BinaryToTextEncoding): string;
189
664
  }
190
-
191
665
  type KeyObjectType = 'secret' | 'public' | 'private';
192
-
193
666
  interface KeyExportOptions<T extends KeyFormat> {
194
667
  type: 'pkcs1' | 'spki' | 'pkcs8' | 'sec1';
195
668
  format: T;
@@ -215,7 +688,6 @@ declare module 'crypto' {
215
688
  y?: string | undefined;
216
689
  [key: string]: unknown;
217
690
  }
218
-
219
691
  interface AsymmetricKeyDetails {
220
692
  /**
221
693
  * Key size in bits (RSA, DSA).
@@ -234,13 +706,42 @@ declare module 'crypto' {
234
706
  */
235
707
  namedCurve?: string | undefined;
236
708
  }
237
-
238
709
  interface JwkKeyExportOptions {
239
710
  format: 'jwk';
240
711
  }
241
-
712
+ /**
713
+ * Node.js uses a `KeyObject` class to represent a symmetric or asymmetric key,
714
+ * and each kind of key exposes different functions. The {@link createSecretKey}, {@link createPublicKey} and {@link createPrivateKey} methods are used to create `KeyObject`instances. `KeyObject`
715
+ * objects are not to be created directly using the `new`keyword.
716
+ *
717
+ * Most applications should consider using the new `KeyObject` API instead of
718
+ * passing keys as strings or `Buffer`s due to improved security features.
719
+ *
720
+ * `KeyObject` instances can be passed to other threads via `postMessage()`.
721
+ * The receiver obtains a cloned `KeyObject`, and the `KeyObject` does not need to
722
+ * be listed in the `transferList` argument.
723
+ * @since v11.6.0
724
+ */
242
725
  class KeyObject {
243
726
  private constructor();
727
+ /**
728
+ * For asymmetric keys, this property represents the type of the key. Supported key
729
+ * types are:
730
+ *
731
+ * * `'rsa'` (OID 1.2.840.113549.1.1.1)
732
+ * * `'rsa-pss'` (OID 1.2.840.113549.1.1.10)
733
+ * * `'dsa'` (OID 1.2.840.10040.4.1)
734
+ * * `'ec'` (OID 1.2.840.10045.2.1)
735
+ * * `'x25519'` (OID 1.3.101.110)
736
+ * * `'x448'` (OID 1.3.101.111)
737
+ * * `'ed25519'` (OID 1.3.101.112)
738
+ * * `'ed448'` (OID 1.3.101.113)
739
+ * * `'dh'` (OID 1.2.840.113549.1.3.1)
740
+ *
741
+ * This property is `undefined` for unrecognized `KeyObject` types and symmetric
742
+ * keys.
743
+ * @since v11.6.0
744
+ */
244
745
  asymmetricKeyType?: KeyType | undefined;
245
746
  /**
246
747
  * For asymmetric keys, this property represents the size of the embedded key in
@@ -250,151 +751,821 @@ declare module 'crypto' {
250
751
  /**
251
752
  * This property exists only on asymmetric keys. Depending on the type of the key,
252
753
  * this object contains information about the key. None of the information obtained
253
- * through this property can be used to uniquely identify a key or to compromise the
254
- * security of the key.
754
+ * through this property can be used to uniquely identify a key or to compromise
755
+ * the security of the key.
756
+ *
757
+ * RSA-PSS parameters, DH, or any future key type details might be exposed via this
758
+ * API using additional attributes.
759
+ * @since v15.7.0
255
760
  */
256
761
  asymmetricKeyDetails?: AsymmetricKeyDetails | undefined;
762
+ /**
763
+ * For symmetric keys, the following encoding options can be used:
764
+ *
765
+ * For public keys, the following encoding options can be used:
766
+ *
767
+ * For private keys, the following encoding options can be used:
768
+ *
769
+ * The result type depends on the selected encoding format, when PEM the
770
+ * result is a string, when DER it will be a buffer containing the data
771
+ * encoded as DER, when [JWK](https://tools.ietf.org/html/rfc7517) it will be an object.
772
+ *
773
+ * When [JWK](https://tools.ietf.org/html/rfc7517) encoding format was selected, all other encoding options are
774
+ * ignored.
775
+ *
776
+ * PKCS#1, SEC1, and PKCS#8 type keys can be encrypted by using a combination of
777
+ * the `cipher` and `format` options. The PKCS#8 `type` can be used with any`format` to encrypt any key algorithm (RSA, EC, or DH) by specifying a`cipher`. PKCS#1 and SEC1 can only be
778
+ * encrypted by specifying a `cipher`when the PEM `format` is used. For maximum compatibility, use PKCS#8 for
779
+ * encrypted private keys. Since PKCS#8 defines its own
780
+ * encryption mechanism, PEM-level encryption is not supported when encrypting
781
+ * a PKCS#8 key. See [RFC 5208](https://www.rfc-editor.org/rfc/rfc5208.txt) for PKCS#8 encryption and [RFC 1421](https://www.rfc-editor.org/rfc/rfc1421.txt) for
782
+ * PKCS#1 and SEC1 encryption.
783
+ * @since v11.6.0
784
+ */
257
785
  export(options: KeyExportOptions<'pem'>): string | Buffer;
258
786
  export(options?: KeyExportOptions<'der'>): Buffer;
259
787
  export(options?: JwkKeyExportOptions): JsonWebKey;
788
+ /**
789
+ * For secret keys, this property represents the size of the key in bytes. This
790
+ * property is `undefined` for asymmetric keys.
791
+ * @since v11.6.0
792
+ */
260
793
  symmetricKeySize?: number | undefined;
794
+ /**
795
+ * Depending on the type of this `KeyObject`, this property is either`'secret'` for secret (symmetric) keys, `'public'` for public (asymmetric) keys
796
+ * or `'private'` for private (asymmetric) keys.
797
+ * @since v11.6.0
798
+ */
261
799
  type: KeyObjectType;
262
800
  }
263
-
264
801
  type CipherCCMTypes = 'aes-128-ccm' | 'aes-192-ccm' | 'aes-256-ccm' | 'chacha20-poly1305';
265
802
  type CipherGCMTypes = 'aes-128-gcm' | 'aes-192-gcm' | 'aes-256-gcm';
266
-
267
803
  type BinaryLike = string | NodeJS.ArrayBufferView;
268
-
269
804
  type CipherKey = BinaryLike | KeyObject;
270
-
271
805
  interface CipherCCMOptions extends stream.TransformOptions {
272
806
  authTagLength: number;
273
807
  }
274
808
  interface CipherGCMOptions extends stream.TransformOptions {
275
809
  authTagLength?: number | undefined;
276
810
  }
277
- /** @deprecated since v10.0.0 use `createCipheriv()` */
811
+ /**
812
+ * Creates and returns a `Cipher` object that uses the given `algorithm` and`password`.
813
+ *
814
+ * The `options` argument controls stream behavior and is optional except when a
815
+ * cipher in CCM or OCB mode is used (e.g. `'aes-128-ccm'`). In that case, the`authTagLength` option is required and specifies the length of the
816
+ * authentication tag in bytes, see `CCM mode`. In GCM mode, the `authTagLength`option is not required but can be used to set the length of the authentication
817
+ * tag that will be returned by `getAuthTag()` and defaults to 16 bytes.
818
+ *
819
+ * The `algorithm` is dependent on OpenSSL, examples are `'aes192'`, etc. On
820
+ * recent OpenSSL releases, `openssl list -cipher-algorithms`(`openssl list-cipher-algorithms` for older versions of OpenSSL) will
821
+ * display the available cipher algorithms.
822
+ *
823
+ * The `password` is used to derive the cipher key and initialization vector (IV).
824
+ * The value must be either a `'latin1'` encoded string, a `Buffer`, a`TypedArray`, or a `DataView`.
825
+ *
826
+ * The implementation of `crypto.createCipher()` derives keys using the OpenSSL
827
+ * function [`EVP_BytesToKey`](https://www.openssl.org/docs/man1.1.0/crypto/EVP_BytesToKey.html) with the digest algorithm set to MD5, one
828
+ * iteration, and no salt. The lack of salt allows dictionary attacks as the same
829
+ * password always creates the same key. The low iteration count and
830
+ * non-cryptographically secure hash algorithm allow passwords to be tested very
831
+ * rapidly.
832
+ *
833
+ * In line with OpenSSL's recommendation to use a more modern algorithm instead of[`EVP_BytesToKey`](https://www.openssl.org/docs/man1.1.0/crypto/EVP_BytesToKey.html) it is recommended that
834
+ * developers derive a key and IV on
835
+ * their own using {@link scrypt} and to use {@link createCipheriv} to create the `Cipher` object. Users should not use ciphers with counter mode
836
+ * (e.g. CTR, GCM, or CCM) in `crypto.createCipher()`. A warning is emitted when
837
+ * they are used in order to avoid the risk of IV reuse that causes
838
+ * vulnerabilities. For the case when IV is reused in GCM, see [Nonce-Disrespecting Adversaries](https://github.com/nonce-disrespect/nonce-disrespect) for details.
839
+ * @since v0.1.94
840
+ * @deprecated Since v10.0.0 - Use {@link createCipheriv} instead.
841
+ * @param options `stream.transform` options
842
+ */
278
843
  function createCipher(algorithm: CipherCCMTypes, password: BinaryLike, options: CipherCCMOptions): CipherCCM;
279
844
  /** @deprecated since v10.0.0 use `createCipheriv()` */
280
845
  function createCipher(algorithm: CipherGCMTypes, password: BinaryLike, options?: CipherGCMOptions): CipherGCM;
281
846
  /** @deprecated since v10.0.0 use `createCipheriv()` */
282
847
  function createCipher(algorithm: string, password: BinaryLike, options?: stream.TransformOptions): Cipher;
283
-
284
- function createCipheriv(
285
- algorithm: CipherCCMTypes,
286
- key: CipherKey,
287
- iv: BinaryLike | null,
288
- options: CipherCCMOptions,
289
- ): CipherCCM;
290
- function createCipheriv(
291
- algorithm: CipherGCMTypes,
292
- key: CipherKey,
293
- iv: BinaryLike | null,
294
- options?: CipherGCMOptions,
295
- ): CipherGCM;
296
- function createCipheriv(
297
- algorithm: string,
298
- key: CipherKey,
299
- iv: BinaryLike | null,
300
- options?: stream.TransformOptions,
301
- ): Cipher;
302
-
848
+ /**
849
+ * Creates and returns a `Cipher` object, with the given `algorithm`, `key` and
850
+ * initialization vector (`iv`).
851
+ *
852
+ * The `options` argument controls stream behavior and is optional except when a
853
+ * cipher in CCM or OCB mode is used (e.g. `'aes-128-ccm'`). In that case, the`authTagLength` option is required and specifies the length of the
854
+ * authentication tag in bytes, see `CCM mode`. In GCM mode, the `authTagLength`option is not required but can be used to set the length of the authentication
855
+ * tag that will be returned by `getAuthTag()` and defaults to 16 bytes.
856
+ *
857
+ * The `algorithm` is dependent on OpenSSL, examples are `'aes192'`, etc. On
858
+ * recent OpenSSL releases, `openssl list -cipher-algorithms`(`openssl list-cipher-algorithms` for older versions of OpenSSL) will
859
+ * display the available cipher algorithms.
860
+ *
861
+ * The `key` is the raw key used by the `algorithm` and `iv` is an[initialization vector](https://en.wikipedia.org/wiki/Initialization_vector). Both arguments must be `'utf8'` encoded
862
+ * strings,`Buffers`, `TypedArray`, or `DataView`s. The `key` may optionally be
863
+ * a `KeyObject` of type `secret`. If the cipher does not need
864
+ * an initialization vector, `iv` may be `null`.
865
+ *
866
+ * When passing strings for `key` or `iv`, please consider `caveats when using strings as inputs to cryptographic APIs`.
867
+ *
868
+ * Initialization vectors should be unpredictable and unique; ideally, they will be
869
+ * cryptographically random. They do not have to be secret: IVs are typically just
870
+ * added to ciphertext messages unencrypted. It may sound contradictory that
871
+ * something has to be unpredictable and unique, but does not have to be secret;
872
+ * remember that an attacker must not be able to predict ahead of time what a
873
+ * given IV will be.
874
+ * @since v0.1.94
875
+ * @param options `stream.transform` options
876
+ */
877
+ function createCipheriv(algorithm: CipherCCMTypes, key: CipherKey, iv: BinaryLike | null, options: CipherCCMOptions): CipherCCM;
878
+ function createCipheriv(algorithm: CipherGCMTypes, key: CipherKey, iv: BinaryLike | null, options?: CipherGCMOptions): CipherGCM;
879
+ function createCipheriv(algorithm: string, key: CipherKey, iv: BinaryLike | null, options?: stream.TransformOptions): Cipher;
880
+ /**
881
+ * * Extends: `<stream.Transform>`
882
+ *
883
+ * Instances of the `Cipher` class are used to encrypt data. The class can be
884
+ * used in one of two ways:
885
+ *
886
+ * * As a `stream` that is both readable and writable, where plain unencrypted
887
+ * data is written to produce encrypted data on the readable side, or
888
+ * * Using the `cipher.update()` and `cipher.final()` methods to produce
889
+ * the encrypted data.
890
+ *
891
+ * The {@link createCipher} or {@link createCipheriv} methods are
892
+ * used to create `Cipher` instances. `Cipher` objects are not to be created
893
+ * directly using the `new` keyword.
894
+ *
895
+ * Example: Using `Cipher` objects as streams:
896
+ *
897
+ * ```js
898
+ * const {
899
+ * scrypt,
900
+ * randomFill,
901
+ * createCipheriv
902
+ * } = await import('crypto');
903
+ *
904
+ * const algorithm = 'aes-192-cbc';
905
+ * const password = 'Password used to generate key';
906
+ *
907
+ * // First, we'll generate the key. The key length is dependent on the algorithm.
908
+ * // In this case for aes192, it is 24 bytes (192 bits).
909
+ * scrypt(password, 'salt', 24, (err, key) => {
910
+ * if (err) throw err;
911
+ * // Then, we'll generate a random initialization vector
912
+ * randomFill(new Uint8Array(16), (err, iv) => {
913
+ * if (err) throw err;
914
+ *
915
+ * // Once we have the key and iv, we can create and use the cipher...
916
+ * const cipher = createCipheriv(algorithm, key, iv);
917
+ *
918
+ * let encrypted = '';
919
+ * cipher.setEncoding('hex');
920
+ *
921
+ * cipher.on('data', (chunk) => encrypted += chunk);
922
+ * cipher.on('end', () => console.log(encrypted));
923
+ *
924
+ * cipher.write('some clear text data');
925
+ * cipher.end();
926
+ * });
927
+ * });
928
+ * ```
929
+ *
930
+ * ```js
931
+ * const {
932
+ * scrypt,
933
+ * randomFill,
934
+ * createCipheriv
935
+ * } = require('crypto');
936
+ *
937
+ * const algorithm = 'aes-192-cbc';
938
+ * const password = 'Password used to generate key';
939
+ *
940
+ * // First, we'll generate the key. The key length is dependent on the algorithm.
941
+ * // In this case for aes192, it is 24 bytes (192 bits).
942
+ * scrypt(password, 'salt', 24, (err, key) => {
943
+ * if (err) throw err;
944
+ * // Then, we'll generate a random initialization vector
945
+ * randomFill(new Uint8Array(16), (err, iv) => {
946
+ * if (err) throw err;
947
+ *
948
+ * // Once we have the key and iv, we can create and use the cipher...
949
+ * const cipher = createCipheriv(algorithm, key, iv);
950
+ *
951
+ * let encrypted = '';
952
+ * cipher.setEncoding('hex');
953
+ *
954
+ * cipher.on('data', (chunk) => encrypted += chunk);
955
+ * cipher.on('end', () => console.log(encrypted));
956
+ *
957
+ * cipher.write('some clear text data');
958
+ * cipher.end();
959
+ * });
960
+ * });
961
+ * ```
962
+ *
963
+ * Example: Using `Cipher` and piped streams:
964
+ *
965
+ * ```js
966
+ * import {
967
+ * createReadStream,
968
+ * createWriteStream,
969
+ * } from 'fs';
970
+ *
971
+ * import {
972
+ * pipeline
973
+ * } from 'stream';
974
+ *
975
+ * const {
976
+ * scrypt,
977
+ * randomFill,
978
+ * createCipheriv,
979
+ * } = await import('crypto');
980
+ *
981
+ * const algorithm = 'aes-192-cbc';
982
+ * const password = 'Password used to generate key';
983
+ *
984
+ * // First, we'll generate the key. The key length is dependent on the algorithm.
985
+ * // In this case for aes192, it is 24 bytes (192 bits).
986
+ * scrypt(password, 'salt', 24, (err, key) => {
987
+ * if (err) throw err;
988
+ * // Then, we'll generate a random initialization vector
989
+ * randomFill(new Uint8Array(16), (err, iv) => {
990
+ * if (err) throw err;
991
+ *
992
+ * const cipher = createCipheriv(algorithm, key, iv);
993
+ *
994
+ * const input = createReadStream('test.js');
995
+ * const output = createWriteStream('test.enc');
996
+ *
997
+ * pipeline(input, cipher, output, (err) => {
998
+ * if (err) throw err;
999
+ * });
1000
+ * });
1001
+ * });
1002
+ * ```
1003
+ *
1004
+ * ```js
1005
+ * const {
1006
+ * createReadStream,
1007
+ * createWriteStream,
1008
+ * } = require('fs');
1009
+ *
1010
+ * const {
1011
+ * pipeline
1012
+ * } = require('stream');
1013
+ *
1014
+ * const {
1015
+ * scrypt,
1016
+ * randomFill,
1017
+ * createCipheriv,
1018
+ * } = require('crypto');
1019
+ *
1020
+ * const algorithm = 'aes-192-cbc';
1021
+ * const password = 'Password used to generate key';
1022
+ *
1023
+ * // First, we'll generate the key. The key length is dependent on the algorithm.
1024
+ * // In this case for aes192, it is 24 bytes (192 bits).
1025
+ * scrypt(password, 'salt', 24, (err, key) => {
1026
+ * if (err) throw err;
1027
+ * // Then, we'll generate a random initialization vector
1028
+ * randomFill(new Uint8Array(16), (err, iv) => {
1029
+ * if (err) throw err;
1030
+ *
1031
+ * const cipher = createCipheriv(algorithm, key, iv);
1032
+ *
1033
+ * const input = createReadStream('test.js');
1034
+ * const output = createWriteStream('test.enc');
1035
+ *
1036
+ * pipeline(input, cipher, output, (err) => {
1037
+ * if (err) throw err;
1038
+ * });
1039
+ * });
1040
+ * });
1041
+ * ```
1042
+ *
1043
+ * Example: Using the `cipher.update()` and `cipher.final()` methods:
1044
+ *
1045
+ * ```js
1046
+ * const {
1047
+ * scrypt,
1048
+ * randomFill,
1049
+ * createCipheriv,
1050
+ * } = await import('crypto');
1051
+ *
1052
+ * const algorithm = 'aes-192-cbc';
1053
+ * const password = 'Password used to generate key';
1054
+ *
1055
+ * // First, we'll generate the key. The key length is dependent on the algorithm.
1056
+ * // In this case for aes192, it is 24 bytes (192 bits).
1057
+ * scrypt(password, 'salt', 24, (err, key) => {
1058
+ * if (err) throw err;
1059
+ * // Then, we'll generate a random initialization vector
1060
+ * randomFill(new Uint8Array(16), (err, iv) => {
1061
+ * if (err) throw err;
1062
+ *
1063
+ * const cipher = createCipheriv(algorithm, key, iv);
1064
+ *
1065
+ * let encrypted = cipher.update('some clear text data', 'utf8', 'hex');
1066
+ * encrypted += cipher.final('hex');
1067
+ * console.log(encrypted);
1068
+ * });
1069
+ * });
1070
+ * ```
1071
+ *
1072
+ * ```js
1073
+ * const {
1074
+ * scrypt,
1075
+ * randomFill,
1076
+ * createCipheriv,
1077
+ * } = require('crypto');
1078
+ *
1079
+ * const algorithm = 'aes-192-cbc';
1080
+ * const password = 'Password used to generate key';
1081
+ *
1082
+ * // First, we'll generate the key. The key length is dependent on the algorithm.
1083
+ * // In this case for aes192, it is 24 bytes (192 bits).
1084
+ * scrypt(password, 'salt', 24, (err, key) => {
1085
+ * if (err) throw err;
1086
+ * // Then, we'll generate a random initialization vector
1087
+ * randomFill(new Uint8Array(16), (err, iv) => {
1088
+ * if (err) throw err;
1089
+ *
1090
+ * const cipher = createCipheriv(algorithm, key, iv);
1091
+ *
1092
+ * let encrypted = cipher.update('some clear text data', 'utf8', 'hex');
1093
+ * encrypted += cipher.final('hex');
1094
+ * console.log(encrypted);
1095
+ * });
1096
+ * });
1097
+ * ```
1098
+ * @since v0.1.94
1099
+ */
303
1100
  class Cipher extends stream.Transform {
304
1101
  private constructor();
1102
+ /**
1103
+ * Updates the cipher with `data`. If the `inputEncoding` argument is given,
1104
+ * the `data`argument is a string using the specified encoding. If the `inputEncoding`argument is not given, `data` must be a `Buffer`, `TypedArray`, or`DataView`. If `data` is a `Buffer`,
1105
+ * `TypedArray`, or `DataView`, then`inputEncoding` is ignored.
1106
+ *
1107
+ * The `outputEncoding` specifies the output format of the enciphered
1108
+ * data. If the `outputEncoding`is specified, a string using the specified encoding is returned. If no`outputEncoding` is provided, a `Buffer` is returned.
1109
+ *
1110
+ * The `cipher.update()` method can be called multiple times with new data until `cipher.final()` is called. Calling `cipher.update()` after `cipher.final()` will result in an error being
1111
+ * thrown.
1112
+ * @since v0.1.94
1113
+ * @param inputEncoding The `encoding` of the data.
1114
+ * @param outputEncoding The `encoding` of the return value.
1115
+ */
305
1116
  update(data: BinaryLike): Buffer;
306
- update(data: string, input_encoding: Encoding): Buffer;
307
- update(data: NodeJS.ArrayBufferView, input_encoding: undefined, output_encoding: Encoding): string;
308
- update(data: string, input_encoding: Encoding | undefined, output_encoding: Encoding): string;
1117
+ update(data: string, inputEncoding: Encoding): Buffer;
1118
+ update(data: NodeJS.ArrayBufferView, inputEncoding: undefined, outputEncoding: Encoding): string;
1119
+ update(data: string, inputEncoding: Encoding | undefined, outputEncoding: Encoding): string;
1120
+ /**
1121
+ * Once the `cipher.final()` method has been called, the `Cipher` object can no
1122
+ * longer be used to encrypt data. Attempts to call `cipher.final()` more than
1123
+ * once will result in an error being thrown.
1124
+ * @since v0.1.94
1125
+ * @param outputEncoding The `encoding` of the return value.
1126
+ * @return Any remaining enciphered contents. If `outputEncoding` is specified, a string is returned. If an `outputEncoding` is not provided, a {@link Buffer} is returned.
1127
+ */
309
1128
  final(): Buffer;
310
- final(output_encoding: BufferEncoding): string;
311
- setAutoPadding(auto_padding?: boolean): this;
312
- // getAuthTag(): Buffer;
313
- // setAAD(buffer: NodeJS.ArrayBufferView): this;
1129
+ final(outputEncoding: BufferEncoding): string;
1130
+ /**
1131
+ * When using block encryption algorithms, the `Cipher` class will automatically
1132
+ * add padding to the input data to the appropriate block size. To disable the
1133
+ * default padding call `cipher.setAutoPadding(false)`.
1134
+ *
1135
+ * When `autoPadding` is `false`, the length of the entire input data must be a
1136
+ * multiple of the cipher's block size or `cipher.final()` will throw an error.
1137
+ * Disabling automatic padding is useful for non-standard padding, for instance
1138
+ * using `0x0` instead of PKCS padding.
1139
+ *
1140
+ * The `cipher.setAutoPadding()` method must be called before `cipher.final()`.
1141
+ * @since v0.7.1
1142
+ * @return for method chaining.
1143
+ */
1144
+ setAutoPadding(autoPadding?: boolean): this;
314
1145
  }
315
1146
  interface CipherCCM extends Cipher {
316
- setAAD(buffer: NodeJS.ArrayBufferView, options: { plaintextLength: number }): this;
1147
+ setAAD(
1148
+ buffer: NodeJS.ArrayBufferView,
1149
+ options: {
1150
+ plaintextLength: number;
1151
+ }
1152
+ ): this;
317
1153
  getAuthTag(): Buffer;
318
1154
  }
319
1155
  interface CipherGCM extends Cipher {
320
- setAAD(buffer: NodeJS.ArrayBufferView, options?: { plaintextLength: number }): this;
1156
+ setAAD(
1157
+ buffer: NodeJS.ArrayBufferView,
1158
+ options?: {
1159
+ plaintextLength: number;
1160
+ }
1161
+ ): this;
321
1162
  getAuthTag(): Buffer;
322
1163
  }
323
- /** @deprecated since v10.0.0 use `createDecipheriv()` */
1164
+ /**
1165
+ * Creates and returns a `Decipher` object that uses the given `algorithm` and`password` (key).
1166
+ *
1167
+ * The `options` argument controls stream behavior and is optional except when a
1168
+ * cipher in CCM or OCB mode is used (e.g. `'aes-128-ccm'`). In that case, the`authTagLength` option is required and specifies the length of the
1169
+ * authentication tag in bytes, see `CCM mode`.
1170
+ *
1171
+ * The implementation of `crypto.createDecipher()` derives keys using the OpenSSL
1172
+ * function [`EVP_BytesToKey`](https://www.openssl.org/docs/man1.1.0/crypto/EVP_BytesToKey.html) with the digest algorithm set to MD5, one
1173
+ * iteration, and no salt. The lack of salt allows dictionary attacks as the same
1174
+ * password always creates the same key. The low iteration count and
1175
+ * non-cryptographically secure hash algorithm allow passwords to be tested very
1176
+ * rapidly.
1177
+ *
1178
+ * In line with OpenSSL's recommendation to use a more modern algorithm instead of[`EVP_BytesToKey`](https://www.openssl.org/docs/man1.1.0/crypto/EVP_BytesToKey.html) it is recommended that
1179
+ * developers derive a key and IV on
1180
+ * their own using {@link scrypt} and to use {@link createDecipheriv} to create the `Decipher` object.
1181
+ * @since v0.1.94
1182
+ * @deprecated Since v10.0.0 - Use {@link createDecipheriv} instead.
1183
+ * @param options `stream.transform` options
1184
+ */
324
1185
  function createDecipher(algorithm: CipherCCMTypes, password: BinaryLike, options: CipherCCMOptions): DecipherCCM;
325
1186
  /** @deprecated since v10.0.0 use `createDecipheriv()` */
326
1187
  function createDecipher(algorithm: CipherGCMTypes, password: BinaryLike, options?: CipherGCMOptions): DecipherGCM;
327
1188
  /** @deprecated since v10.0.0 use `createDecipheriv()` */
328
1189
  function createDecipher(algorithm: string, password: BinaryLike, options?: stream.TransformOptions): Decipher;
329
-
330
- function createDecipheriv(
331
- algorithm: CipherCCMTypes,
332
- key: CipherKey,
333
- iv: BinaryLike | null,
334
- options: CipherCCMOptions,
335
- ): DecipherCCM;
336
- function createDecipheriv(
337
- algorithm: CipherGCMTypes,
338
- key: CipherKey,
339
- iv: BinaryLike | null,
340
- options?: CipherGCMOptions,
341
- ): DecipherGCM;
342
- function createDecipheriv(
343
- algorithm: string,
344
- key: CipherKey,
345
- iv: BinaryLike | null,
346
- options?: stream.TransformOptions,
347
- ): Decipher;
348
-
1190
+ /**
1191
+ * Creates and returns a `Decipher` object that uses the given `algorithm`, `key`and initialization vector (`iv`).
1192
+ *
1193
+ * The `options` argument controls stream behavior and is optional except when a
1194
+ * cipher in CCM or OCB mode is used (e.g. `'aes-128-ccm'`). In that case, the`authTagLength` option is required and specifies the length of the
1195
+ * authentication tag in bytes, see `CCM mode`. In GCM mode, the `authTagLength`option is not required but can be used to restrict accepted authentication tags
1196
+ * to those with the specified length.
1197
+ *
1198
+ * The `algorithm` is dependent on OpenSSL, examples are `'aes192'`, etc. On
1199
+ * recent OpenSSL releases, `openssl list -cipher-algorithms`(`openssl list-cipher-algorithms` for older versions of OpenSSL) will
1200
+ * display the available cipher algorithms.
1201
+ *
1202
+ * The `key` is the raw key used by the `algorithm` and `iv` is an[initialization vector](https://en.wikipedia.org/wiki/Initialization_vector). Both arguments must be `'utf8'` encoded
1203
+ * strings,`Buffers`, `TypedArray`, or `DataView`s. The `key` may optionally be
1204
+ * a `KeyObject` of type `secret`. If the cipher does not need
1205
+ * an initialization vector, `iv` may be `null`.
1206
+ *
1207
+ * When passing strings for `key` or `iv`, please consider `caveats when using strings as inputs to cryptographic APIs`.
1208
+ *
1209
+ * Initialization vectors should be unpredictable and unique; ideally, they will be
1210
+ * cryptographically random. They do not have to be secret: IVs are typically just
1211
+ * added to ciphertext messages unencrypted. It may sound contradictory that
1212
+ * something has to be unpredictable and unique, but does not have to be secret;
1213
+ * remember that an attacker must not be able to predict ahead of time what a given
1214
+ * IV will be.
1215
+ * @since v0.1.94
1216
+ * @param options `stream.transform` options
1217
+ */
1218
+ function createDecipheriv(algorithm: CipherCCMTypes, key: CipherKey, iv: BinaryLike | null, options: CipherCCMOptions): DecipherCCM;
1219
+ function createDecipheriv(algorithm: CipherGCMTypes, key: CipherKey, iv: BinaryLike | null, options?: CipherGCMOptions): DecipherGCM;
1220
+ function createDecipheriv(algorithm: string, key: CipherKey, iv: BinaryLike | null, options?: stream.TransformOptions): Decipher;
1221
+ /**
1222
+ * * Extends: `<stream.Transform>`
1223
+ *
1224
+ * Instances of the `Decipher` class are used to decrypt data. The class can be
1225
+ * used in one of two ways:
1226
+ *
1227
+ * * As a `stream` that is both readable and writable, where plain encrypted
1228
+ * data is written to produce unencrypted data on the readable side, or
1229
+ * * Using the `decipher.update()` and `decipher.final()` methods to
1230
+ * produce the unencrypted data.
1231
+ *
1232
+ * The {@link createDecipher} or {@link createDecipheriv} methods are
1233
+ * used to create `Decipher` instances. `Decipher` objects are not to be created
1234
+ * directly using the `new` keyword.
1235
+ *
1236
+ * Example: Using `Decipher` objects as streams:
1237
+ *
1238
+ * ```js
1239
+ * const {
1240
+ * scryptSync,
1241
+ * createDecipheriv,
1242
+ * } = await import('crypto');
1243
+ *
1244
+ * const algorithm = 'aes-192-cbc';
1245
+ * const password = 'Password used to generate key';
1246
+ * // Key length is dependent on the algorithm. In this case for aes192, it is
1247
+ * // 24 bytes (192 bits).
1248
+ * // Use the async `crypto.scrypt()` instead.
1249
+ * const key = scryptSync(password, 'salt', 24);
1250
+ * // The IV is usually passed along with the ciphertext.
1251
+ * const iv = Buffer.alloc(16, 0); // Initialization vector.
1252
+ *
1253
+ * const decipher = createDecipheriv(algorithm, key, iv);
1254
+ *
1255
+ * let decrypted = '';
1256
+ * decipher.on('readable', () => {
1257
+ * while (null !== (chunk = decipher.read())) {
1258
+ * decrypted += chunk.toString('utf8');
1259
+ * }
1260
+ * });
1261
+ * decipher.on('end', () => {
1262
+ * console.log(decrypted);
1263
+ * // Prints: some clear text data
1264
+ * });
1265
+ *
1266
+ * // Encrypted with same algorithm, key and iv.
1267
+ * const encrypted =
1268
+ * 'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa';
1269
+ * decipher.write(encrypted, 'hex');
1270
+ * decipher.end();
1271
+ * ```
1272
+ *
1273
+ * ```js
1274
+ * const {
1275
+ * scryptSync,
1276
+ * createDecipheriv,
1277
+ * } = require('crypto');
1278
+ *
1279
+ * const algorithm = 'aes-192-cbc';
1280
+ * const password = 'Password used to generate key';
1281
+ * // Key length is dependent on the algorithm. In this case for aes192, it is
1282
+ * // 24 bytes (192 bits).
1283
+ * // Use the async `crypto.scrypt()` instead.
1284
+ * const key = scryptSync(password, 'salt', 24);
1285
+ * // The IV is usually passed along with the ciphertext.
1286
+ * const iv = Buffer.alloc(16, 0); // Initialization vector.
1287
+ *
1288
+ * const decipher = createDecipheriv(algorithm, key, iv);
1289
+ *
1290
+ * let decrypted = '';
1291
+ * decipher.on('readable', () => {
1292
+ * while (null !== (chunk = decipher.read())) {
1293
+ * decrypted += chunk.toString('utf8');
1294
+ * }
1295
+ * });
1296
+ * decipher.on('end', () => {
1297
+ * console.log(decrypted);
1298
+ * // Prints: some clear text data
1299
+ * });
1300
+ *
1301
+ * // Encrypted with same algorithm, key and iv.
1302
+ * const encrypted =
1303
+ * 'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa';
1304
+ * decipher.write(encrypted, 'hex');
1305
+ * decipher.end();
1306
+ * ```
1307
+ *
1308
+ * Example: Using `Decipher` and piped streams:
1309
+ *
1310
+ * ```js
1311
+ * import {
1312
+ * createReadStream,
1313
+ * createWriteStream,
1314
+ * } from 'fs';
1315
+ *
1316
+ * const {
1317
+ * scryptSync,
1318
+ * createDecipheriv,
1319
+ * } = await import('crypto');
1320
+ *
1321
+ * const algorithm = 'aes-192-cbc';
1322
+ * const password = 'Password used to generate key';
1323
+ * // Use the async `crypto.scrypt()` instead.
1324
+ * const key = scryptSync(password, 'salt', 24);
1325
+ * // The IV is usually passed along with the ciphertext.
1326
+ * const iv = Buffer.alloc(16, 0); // Initialization vector.
1327
+ *
1328
+ * const decipher = createDecipheriv(algorithm, key, iv);
1329
+ *
1330
+ * const input = createReadStream('test.enc');
1331
+ * const output = createWriteStream('test.js');
1332
+ *
1333
+ * input.pipe(decipher).pipe(output);
1334
+ * ```
1335
+ *
1336
+ * ```js
1337
+ * const {
1338
+ * createReadStream,
1339
+ * createWriteStream,
1340
+ * } = require('fs');
1341
+ *
1342
+ * const {
1343
+ * scryptSync,
1344
+ * createDecipheriv,
1345
+ * } = require('crypto');
1346
+ *
1347
+ * const algorithm = 'aes-192-cbc';
1348
+ * const password = 'Password used to generate key';
1349
+ * // Use the async `crypto.scrypt()` instead.
1350
+ * const key = scryptSync(password, 'salt', 24);
1351
+ * // The IV is usually passed along with the ciphertext.
1352
+ * const iv = Buffer.alloc(16, 0); // Initialization vector.
1353
+ *
1354
+ * const decipher = createDecipheriv(algorithm, key, iv);
1355
+ *
1356
+ * const input = createReadStream('test.enc');
1357
+ * const output = createWriteStream('test.js');
1358
+ *
1359
+ * input.pipe(decipher).pipe(output);
1360
+ * ```
1361
+ *
1362
+ * Example: Using the `decipher.update()` and `decipher.final()` methods:
1363
+ *
1364
+ * ```js
1365
+ * const {
1366
+ * scryptSync,
1367
+ * createDecipheriv,
1368
+ * } = await import('crypto');
1369
+ *
1370
+ * const algorithm = 'aes-192-cbc';
1371
+ * const password = 'Password used to generate key';
1372
+ * // Use the async `crypto.scrypt()` instead.
1373
+ * const key = scryptSync(password, 'salt', 24);
1374
+ * // The IV is usually passed along with the ciphertext.
1375
+ * const iv = Buffer.alloc(16, 0); // Initialization vector.
1376
+ *
1377
+ * const decipher = createDecipheriv(algorithm, key, iv);
1378
+ *
1379
+ * // Encrypted using same algorithm, key and iv.
1380
+ * const encrypted =
1381
+ * 'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa';
1382
+ * let decrypted = decipher.update(encrypted, 'hex', 'utf8');
1383
+ * decrypted += decipher.final('utf8');
1384
+ * console.log(decrypted);
1385
+ * // Prints: some clear text data
1386
+ * ```
1387
+ *
1388
+ * ```js
1389
+ * const {
1390
+ * scryptSync,
1391
+ * createDecipheriv,
1392
+ * } = require('crypto');
1393
+ *
1394
+ * const algorithm = 'aes-192-cbc';
1395
+ * const password = 'Password used to generate key';
1396
+ * // Use the async `crypto.scrypt()` instead.
1397
+ * const key = scryptSync(password, 'salt', 24);
1398
+ * // The IV is usually passed along with the ciphertext.
1399
+ * const iv = Buffer.alloc(16, 0); // Initialization vector.
1400
+ *
1401
+ * const decipher = createDecipheriv(algorithm, key, iv);
1402
+ *
1403
+ * // Encrypted using same algorithm, key and iv.
1404
+ * const encrypted =
1405
+ * 'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa';
1406
+ * let decrypted = decipher.update(encrypted, 'hex', 'utf8');
1407
+ * decrypted += decipher.final('utf8');
1408
+ * console.log(decrypted);
1409
+ * // Prints: some clear text data
1410
+ * ```
1411
+ * @since v0.1.94
1412
+ */
349
1413
  class Decipher extends stream.Transform {
350
1414
  private constructor();
1415
+ /**
1416
+ * Updates the decipher with `data`. If the `inputEncoding` argument is given,
1417
+ * the `data`argument is a string using the specified encoding. If the `inputEncoding`argument is not given, `data` must be a `Buffer`. If `data` is a `Buffer` then `inputEncoding` is
1418
+ * ignored.
1419
+ *
1420
+ * The `outputEncoding` specifies the output format of the enciphered
1421
+ * data. If the `outputEncoding`is specified, a string using the specified encoding is returned. If no`outputEncoding` is provided, a `Buffer` is returned.
1422
+ *
1423
+ * The `decipher.update()` method can be called multiple times with new data until `decipher.final()` is called. Calling `decipher.update()` after `decipher.final()` will result in an error
1424
+ * being thrown.
1425
+ * @since v0.1.94
1426
+ * @param inputEncoding The `encoding` of the `data` string.
1427
+ * @param outputEncoding The `encoding` of the return value.
1428
+ */
351
1429
  update(data: NodeJS.ArrayBufferView): Buffer;
352
- update(data: string, input_encoding: Encoding): Buffer;
353
- update(data: NodeJS.ArrayBufferView, input_encoding: undefined, output_encoding: Encoding): string;
354
- update(data: string, input_encoding: Encoding | undefined, output_encoding: Encoding): string;
1430
+ update(data: string, inputEncoding: Encoding): Buffer;
1431
+ update(data: NodeJS.ArrayBufferView, inputEncoding: undefined, outputEncoding: Encoding): string;
1432
+ update(data: string, inputEncoding: Encoding | undefined, outputEncoding: Encoding): string;
1433
+ /**
1434
+ * Once the `decipher.final()` method has been called, the `Decipher` object can
1435
+ * no longer be used to decrypt data. Attempts to call `decipher.final()` more
1436
+ * than once will result in an error being thrown.
1437
+ * @since v0.1.94
1438
+ * @param outputEncoding The `encoding` of the return value.
1439
+ * @return Any remaining deciphered contents. If `outputEncoding` is specified, a string is returned. If an `outputEncoding` is not provided, a {@link Buffer} is returned.
1440
+ */
355
1441
  final(): Buffer;
356
- final(output_encoding: BufferEncoding): string;
1442
+ final(outputEncoding: BufferEncoding): string;
1443
+ /**
1444
+ * When data has been encrypted without standard block padding, calling`decipher.setAutoPadding(false)` will disable automatic padding to prevent `decipher.final()` from checking for and
1445
+ * removing padding.
1446
+ *
1447
+ * Turning auto padding off will only work if the input data's length is a
1448
+ * multiple of the ciphers block size.
1449
+ *
1450
+ * The `decipher.setAutoPadding()` method must be called before `decipher.final()`.
1451
+ * @since v0.7.1
1452
+ * @return for method chaining.
1453
+ */
357
1454
  setAutoPadding(auto_padding?: boolean): this;
358
- // setAuthTag(tag: NodeJS.ArrayBufferView): this;
359
- // setAAD(buffer: NodeJS.ArrayBufferView): this;
360
1455
  }
361
1456
  interface DecipherCCM extends Decipher {
362
1457
  setAuthTag(buffer: NodeJS.ArrayBufferView): this;
363
- setAAD(buffer: NodeJS.ArrayBufferView, options: { plaintextLength: number }): this;
1458
+ setAAD(
1459
+ buffer: NodeJS.ArrayBufferView,
1460
+ options: {
1461
+ plaintextLength: number;
1462
+ }
1463
+ ): this;
364
1464
  }
365
1465
  interface DecipherGCM extends Decipher {
366
1466
  setAuthTag(buffer: NodeJS.ArrayBufferView): this;
367
- setAAD(buffer: NodeJS.ArrayBufferView, options?: { plaintextLength: number }): this;
1467
+ setAAD(
1468
+ buffer: NodeJS.ArrayBufferView,
1469
+ options?: {
1470
+ plaintextLength: number;
1471
+ }
1472
+ ): this;
368
1473
  }
369
-
370
1474
  interface PrivateKeyInput {
371
1475
  key: string | Buffer;
372
1476
  format?: KeyFormat | undefined;
373
1477
  type?: 'pkcs1' | 'pkcs8' | 'sec1' | undefined;
374
1478
  passphrase?: string | Buffer | undefined;
375
1479
  }
376
-
377
1480
  interface PublicKeyInput {
378
1481
  key: string | Buffer;
379
1482
  format?: KeyFormat | undefined;
380
1483
  type?: 'pkcs1' | 'spki' | undefined;
381
1484
  }
382
-
383
- function generateKey(type: 'hmac' | 'aes', options: {length: number}, callback: (err: Error | null, key: KeyObject) => void): void;
384
-
1485
+ /**
1486
+ * Asynchronously generates a new random secret key of the given `length`. The`type` will determine which validations will be performed on the `length`.
1487
+ *
1488
+ * ```js
1489
+ * const {
1490
+ * generateKey,
1491
+ * } = await import('crypto');
1492
+ *
1493
+ * generateKey('hmac', { length: 64 }, (err, key) => {
1494
+ * if (err) throw err;
1495
+ * console.log(key.export().toString('hex')); // 46e..........620
1496
+ * });
1497
+ * ```
1498
+ *
1499
+ * ```js
1500
+ * const {
1501
+ * generateKey,
1502
+ * } = require('crypto');
1503
+ *
1504
+ * generateKey('hmac', { length: 64 }, (err, key) => {
1505
+ * if (err) throw err;
1506
+ * console.log(key.export().toString('hex')); // 46e..........620
1507
+ * });
1508
+ * ```
1509
+ * @since v15.0.0
1510
+ * @param type The intended use of the generated secret key. Currently accepted values are `'hmac'` and `'aes'`.
1511
+ */
1512
+ function generateKey(
1513
+ type: 'hmac' | 'aes',
1514
+ options: {
1515
+ length: number;
1516
+ },
1517
+ callback: (err: Error | null, key: KeyObject) => void
1518
+ ): void;
385
1519
  interface JsonWebKeyInput {
386
1520
  key: JsonWebKey;
387
1521
  format: 'jwk';
388
1522
  }
389
-
1523
+ /**
1524
+ * Creates and returns a new key object containing a private key. If `key` is a
1525
+ * string or `Buffer`, `format` is assumed to be `'pem'`; otherwise, `key`must be an object with the properties described above.
1526
+ *
1527
+ * If the private key is encrypted, a `passphrase` must be specified. The length
1528
+ * of the passphrase is limited to 1024 bytes.
1529
+ * @since v11.6.0
1530
+ */
390
1531
  function createPrivateKey(key: PrivateKeyInput | string | Buffer | JsonWebKeyInput): KeyObject;
1532
+ /**
1533
+ * Creates and returns a new key object containing a public key. If `key` is a
1534
+ * string or `Buffer`, `format` is assumed to be `'pem'`; if `key` is a `KeyObject`with type `'private'`, the public key is derived from the given private key;
1535
+ * otherwise, `key` must be an object with the properties described above.
1536
+ *
1537
+ * If the format is `'pem'`, the `'key'` may also be an X.509 certificate.
1538
+ *
1539
+ * Because public keys can be derived from private keys, a private key may be
1540
+ * passed instead of a public key. In that case, this function behaves as if {@link createPrivateKey} had been called, except that the type of the
1541
+ * returned `KeyObject` will be `'public'` and that the private key cannot be
1542
+ * extracted from the returned `KeyObject`. Similarly, if a `KeyObject` with type`'private'` is given, a new `KeyObject` with type `'public'` will be returned
1543
+ * and it will be impossible to extract the private key from the returned object.
1544
+ * @since v11.6.0
1545
+ */
391
1546
  function createPublicKey(key: PublicKeyInput | string | Buffer | KeyObject | JsonWebKeyInput): KeyObject;
1547
+ /**
1548
+ * Creates and returns a new key object containing a secret key for symmetric
1549
+ * encryption or `Hmac`.
1550
+ * @since v11.6.0
1551
+ * @param encoding The string encoding when `key` is a string.
1552
+ */
392
1553
  function createSecretKey(key: NodeJS.ArrayBufferView): KeyObject;
393
-
1554
+ function createSecretKey(key: string, encoding: BufferEncoding): KeyObject;
1555
+ /**
1556
+ * Creates and returns a `Sign` object that uses the given `algorithm`. Use {@link getHashes} to obtain the names of the available digest algorithms.
1557
+ * Optional `options` argument controls the `stream.Writable` behavior.
1558
+ *
1559
+ * In some cases, a `Sign` instance can be created using the name of a signature
1560
+ * algorithm, such as `'RSA-SHA256'`, instead of a digest algorithm. This will use
1561
+ * the corresponding digest algorithm. This does not work for all signature
1562
+ * algorithms, such as `'ecdsa-with-SHA256'`, so it is best to always use digest
1563
+ * algorithm names.
1564
+ * @since v0.1.92
1565
+ * @param options `stream.Writable` options
1566
+ */
394
1567
  function createSign(algorithm: string, options?: stream.WritableOptions): Sign;
395
-
396
1568
  type DSAEncoding = 'der' | 'ieee-p1363';
397
-
398
1569
  interface SigningOptions {
399
1570
  /**
400
1571
  * @See crypto.constants.RSA_PKCS1_PADDING
@@ -403,132 +1574,933 @@ declare module 'crypto' {
403
1574
  saltLength?: number | undefined;
404
1575
  dsaEncoding?: DSAEncoding | undefined;
405
1576
  }
406
-
407
- interface SignPrivateKeyInput extends PrivateKeyInput, SigningOptions { }
1577
+ interface SignPrivateKeyInput extends PrivateKeyInput, SigningOptions {}
408
1578
  interface SignKeyObjectInput extends SigningOptions {
409
1579
  key: KeyObject;
410
1580
  }
411
- interface VerifyPublicKeyInput extends PublicKeyInput, SigningOptions { }
1581
+ interface VerifyPublicKeyInput extends PublicKeyInput, SigningOptions {}
412
1582
  interface VerifyKeyObjectInput extends SigningOptions {
413
1583
  key: KeyObject;
414
1584
  }
415
-
416
1585
  type KeyLike = string | Buffer | KeyObject;
417
-
1586
+ /**
1587
+ * * Extends: `<stream.Writable>`
1588
+ *
1589
+ * The `Sign` class is a utility for generating signatures. It can be used in one
1590
+ * of two ways:
1591
+ *
1592
+ * * As a writable `stream`, where data to be signed is written and the `sign.sign()` method is used to generate and return the signature, or
1593
+ * * Using the `sign.update()` and `sign.sign()` methods to produce the
1594
+ * signature.
1595
+ *
1596
+ * The {@link createSign} method is used to create `Sign` instances. The
1597
+ * argument is the string name of the hash function to use. `Sign` objects are not
1598
+ * to be created directly using the `new` keyword.
1599
+ *
1600
+ * Example: Using `Sign` and `Verify` objects as streams:
1601
+ *
1602
+ * ```js
1603
+ * const {
1604
+ * generateKeyPairSync,
1605
+ * createSign,
1606
+ * createVerify,
1607
+ * } = await import('crypto');
1608
+ *
1609
+ * const { privateKey, publicKey } = generateKeyPairSync('ec', {
1610
+ * namedCurve: 'sect239k1'
1611
+ * });
1612
+ *
1613
+ * const sign = createSign('SHA256');
1614
+ * sign.write('some data to sign');
1615
+ * sign.end();
1616
+ * const signature = sign.sign(privateKey, 'hex');
1617
+ *
1618
+ * const verify = createVerify('SHA256');
1619
+ * verify.write('some data to sign');
1620
+ * verify.end();
1621
+ * console.log(verify.verify(publicKey, signature, 'hex'));
1622
+ * // Prints: true
1623
+ * ```
1624
+ *
1625
+ * ```js
1626
+ * const {
1627
+ * generateKeyPairSync,
1628
+ * createSign,
1629
+ * createVerify,
1630
+ * } = require('crypto');
1631
+ *
1632
+ * const { privateKey, publicKey } = generateKeyPairSync('ec', {
1633
+ * namedCurve: 'sect239k1'
1634
+ * });
1635
+ *
1636
+ * const sign = createSign('SHA256');
1637
+ * sign.write('some data to sign');
1638
+ * sign.end();
1639
+ * const signature = sign.sign(privateKey, 'hex');
1640
+ *
1641
+ * const verify = createVerify('SHA256');
1642
+ * verify.write('some data to sign');
1643
+ * verify.end();
1644
+ * console.log(verify.verify(publicKey, signature, 'hex'));
1645
+ * // Prints: true
1646
+ * ```
1647
+ *
1648
+ * Example: Using the `sign.update()` and `verify.update()` methods:
1649
+ *
1650
+ * ```js
1651
+ * const {
1652
+ * generateKeyPairSync,
1653
+ * createSign,
1654
+ * createVerify,
1655
+ * } = await import('crypto');
1656
+ *
1657
+ * const { privateKey, publicKey } = generateKeyPairSync('rsa', {
1658
+ * modulusLength: 2048,
1659
+ * });
1660
+ *
1661
+ * const sign = createSign('SHA256');
1662
+ * sign.update('some data to sign');
1663
+ * sign.end();
1664
+ * const signature = sign.sign(privateKey);
1665
+ *
1666
+ * const verify = createVerify('SHA256');
1667
+ * verify.update('some data to sign');
1668
+ * verify.end();
1669
+ * console.log(verify.verify(publicKey, signature));
1670
+ * // Prints: true
1671
+ * ```
1672
+ *
1673
+ * ```js
1674
+ * const {
1675
+ * generateKeyPairSync,
1676
+ * createSign,
1677
+ * createVerify,
1678
+ * } = require('crypto');
1679
+ *
1680
+ * const { privateKey, publicKey } = generateKeyPairSync('rsa', {
1681
+ * modulusLength: 2048,
1682
+ * });
1683
+ *
1684
+ * const sign = createSign('SHA256');
1685
+ * sign.update('some data to sign');
1686
+ * sign.end();
1687
+ * const signature = sign.sign(privateKey);
1688
+ *
1689
+ * const verify = createVerify('SHA256');
1690
+ * verify.update('some data to sign');
1691
+ * verify.end();
1692
+ * console.log(verify.verify(publicKey, signature));
1693
+ * // Prints: true
1694
+ * ```
1695
+ * @since v0.1.92
1696
+ */
418
1697
  class Sign extends stream.Writable {
419
1698
  private constructor();
420
-
1699
+ /**
1700
+ * Updates the `Sign` content with the given `data`, the encoding of which
1701
+ * is given in `inputEncoding`.
1702
+ * If `encoding` is not provided, and the `data` is a string, an
1703
+ * encoding of `'utf8'` is enforced. If `data` is a `Buffer`, `TypedArray`, or`DataView`, then `inputEncoding` is ignored.
1704
+ *
1705
+ * This can be called many times with new data as it is streamed.
1706
+ * @since v0.1.92
1707
+ * @param inputEncoding The `encoding` of the `data` string.
1708
+ */
421
1709
  update(data: BinaryLike): this;
422
- update(data: string, input_encoding: Encoding): this;
423
- sign(private_key: KeyLike | SignKeyObjectInput | SignPrivateKeyInput): Buffer;
424
- sign(
425
- private_key: KeyLike | SignKeyObjectInput | SignPrivateKeyInput,
426
- output_format: BinaryToTextEncoding,
427
- ): string;
1710
+ update(data: string, inputEncoding: Encoding): this;
1711
+ /**
1712
+ * Calculates the signature on all the data passed through using either `sign.update()` or `sign.write()`.
1713
+ *
1714
+ * If `privateKey` is not a `KeyObject`, this function behaves as if`privateKey` had been passed to {@link createPrivateKey}. If it is an
1715
+ * object, the following additional properties can be passed:
1716
+ *
1717
+ * If `outputEncoding` is provided a string is returned; otherwise a `Buffer` is returned.
1718
+ *
1719
+ * The `Sign` object can not be again used after `sign.sign()` method has been
1720
+ * called. Multiple calls to `sign.sign()` will result in an error being thrown.
1721
+ * @since v0.1.92
1722
+ */
1723
+ sign(privateKey: KeyLike | SignKeyObjectInput | SignPrivateKeyInput): Buffer;
1724
+ sign(privateKey: KeyLike | SignKeyObjectInput | SignPrivateKeyInput, outputFormat: BinaryToTextEncoding): string;
428
1725
  }
429
-
1726
+ /**
1727
+ * Creates and returns a `Verify` object that uses the given algorithm.
1728
+ * Use {@link getHashes} to obtain an array of names of the available
1729
+ * signing algorithms. Optional `options` argument controls the`stream.Writable` behavior.
1730
+ *
1731
+ * In some cases, a `Verify` instance can be created using the name of a signature
1732
+ * algorithm, such as `'RSA-SHA256'`, instead of a digest algorithm. This will use
1733
+ * the corresponding digest algorithm. This does not work for all signature
1734
+ * algorithms, such as `'ecdsa-with-SHA256'`, so it is best to always use digest
1735
+ * algorithm names.
1736
+ * @since v0.1.92
1737
+ * @param options `stream.Writable` options
1738
+ */
430
1739
  function createVerify(algorithm: string, options?: stream.WritableOptions): Verify;
1740
+ /**
1741
+ * * Extends: `<stream.Writable>`
1742
+ *
1743
+ * The `Verify` class is a utility for verifying signatures. It can be used in one
1744
+ * of two ways:
1745
+ *
1746
+ * * As a writable `stream` where written data is used to validate against the
1747
+ * supplied signature, or
1748
+ * * Using the `verify.update()` and `verify.verify()` methods to verify
1749
+ * the signature.
1750
+ *
1751
+ * The {@link createVerify} method is used to create `Verify` instances.`Verify` objects are not to be created directly using the `new` keyword.
1752
+ *
1753
+ * See `Sign` for examples.
1754
+ * @since v0.1.92
1755
+ */
431
1756
  class Verify extends stream.Writable {
432
1757
  private constructor();
433
-
1758
+ /**
1759
+ * Updates the `Verify` content with the given `data`, the encoding of which
1760
+ * is given in `inputEncoding`.
1761
+ * If `inputEncoding` is not provided, and the `data` is a string, an
1762
+ * encoding of `'utf8'` is enforced. If `data` is a `Buffer`, `TypedArray`, or`DataView`, then `inputEncoding` is ignored.
1763
+ *
1764
+ * This can be called many times with new data as it is streamed.
1765
+ * @since v0.1.92
1766
+ * @param inputEncoding The `encoding` of the `data` string.
1767
+ */
434
1768
  update(data: BinaryLike): Verify;
435
- update(data: string, input_encoding: Encoding): Verify;
436
- verify(
437
- object: KeyLike | VerifyKeyObjectInput | VerifyPublicKeyInput,
438
- signature: NodeJS.ArrayBufferView,
439
- ): boolean;
440
- verify(
441
- object: KeyLike | VerifyKeyObjectInput | VerifyPublicKeyInput,
442
- signature: string,
443
- signature_format?: BinaryToTextEncoding,
444
- ): boolean;
445
- // https://nodejs.org/api/crypto.html#crypto_verifier_verify_object_signature_signature_format
446
- // The signature field accepts a TypedArray type, but it is only available starting ES2017
1769
+ update(data: string, inputEncoding: Encoding): Verify;
1770
+ /**
1771
+ * Verifies the provided data using the given `object` and `signature`.
1772
+ *
1773
+ * If `object` is not a `KeyObject`, this function behaves as if`object` had been passed to {@link createPublicKey}. If it is an
1774
+ * object, the following additional properties can be passed:
1775
+ *
1776
+ * The `signature` argument is the previously calculated signature for the data, in
1777
+ * the `signatureEncoding`.
1778
+ * If a `signatureEncoding` is specified, the `signature` is expected to be a
1779
+ * string; otherwise `signature` is expected to be a `Buffer`,`TypedArray`, or `DataView`.
1780
+ *
1781
+ * The `verify` object can not be used again after `verify.verify()` has been
1782
+ * called. Multiple calls to `verify.verify()` will result in an error being
1783
+ * thrown.
1784
+ *
1785
+ * Because public keys can be derived from private keys, a private key may
1786
+ * be passed instead of a public key.
1787
+ * @since v0.1.92
1788
+ */
1789
+ verify(object: KeyLike | VerifyKeyObjectInput | VerifyPublicKeyInput, signature: NodeJS.ArrayBufferView): boolean;
1790
+ verify(object: KeyLike | VerifyKeyObjectInput | VerifyPublicKeyInput, signature: string, signature_format?: BinaryToTextEncoding): boolean;
447
1791
  }
448
- function createDiffieHellman(prime_length: number, generator?: number | NodeJS.ArrayBufferView): DiffieHellman;
1792
+ /**
1793
+ * Creates a `DiffieHellman` key exchange object using the supplied `prime` and an
1794
+ * optional specific `generator`.
1795
+ *
1796
+ * The `generator` argument can be a number, string, or `Buffer`. If`generator` is not specified, the value `2` is used.
1797
+ *
1798
+ * If `primeEncoding` is specified, `prime` is expected to be a string; otherwise
1799
+ * a `Buffer`, `TypedArray`, or `DataView` is expected.
1800
+ *
1801
+ * If `generatorEncoding` is specified, `generator` is expected to be a string;
1802
+ * otherwise a number, `Buffer`, `TypedArray`, or `DataView` is expected.
1803
+ * @since v0.11.12
1804
+ * @param primeEncoding The `encoding` of the `prime` string.
1805
+ * @param generatorEncoding The `encoding` of the `generator` string.
1806
+ */
1807
+ function createDiffieHellman(primeLength: number, generator?: number | NodeJS.ArrayBufferView): DiffieHellman;
449
1808
  function createDiffieHellman(prime: NodeJS.ArrayBufferView): DiffieHellman;
450
- function createDiffieHellman(prime: string, prime_encoding: BinaryToTextEncoding): DiffieHellman;
451
- function createDiffieHellman(
452
- prime: string,
453
- prime_encoding: BinaryToTextEncoding,
454
- generator: number | NodeJS.ArrayBufferView,
455
- ): DiffieHellman;
456
- function createDiffieHellman(
457
- prime: string,
458
- prime_encoding: BinaryToTextEncoding,
459
- generator: string,
460
- generator_encoding: BinaryToTextEncoding,
461
- ): DiffieHellman;
1809
+ function createDiffieHellman(prime: string, primeEncoding: BinaryToTextEncoding): DiffieHellman;
1810
+ function createDiffieHellman(prime: string, primeEncoding: BinaryToTextEncoding, generator: number | NodeJS.ArrayBufferView): DiffieHellman;
1811
+ function createDiffieHellman(prime: string, primeEncoding: BinaryToTextEncoding, generator: string, generatorEncoding: BinaryToTextEncoding): DiffieHellman;
1812
+ /**
1813
+ * The `DiffieHellman` class is a utility for creating Diffie-Hellman key
1814
+ * exchanges.
1815
+ *
1816
+ * Instances of the `DiffieHellman` class can be created using the {@link createDiffieHellman} function.
1817
+ *
1818
+ * ```js
1819
+ * import assert from 'assert';
1820
+ *
1821
+ * const {
1822
+ * createDiffieHellman,
1823
+ * } = await import('crypto');
1824
+ *
1825
+ * // Generate Alice's keys...
1826
+ * const alice = createDiffieHellman(2048);
1827
+ * const aliceKey = alice.generateKeys();
1828
+ *
1829
+ * // Generate Bob's keys...
1830
+ * const bob = createDiffieHellman(alice.getPrime(), alice.getGenerator());
1831
+ * const bobKey = bob.generateKeys();
1832
+ *
1833
+ * // Exchange and generate the secret...
1834
+ * const aliceSecret = alice.computeSecret(bobKey);
1835
+ * const bobSecret = bob.computeSecret(aliceKey);
1836
+ *
1837
+ * // OK
1838
+ * assert.strictEqual(aliceSecret.toString('hex'), bobSecret.toString('hex'));
1839
+ * ```
1840
+ *
1841
+ * ```js
1842
+ * const assert = require('assert');
1843
+ *
1844
+ * const {
1845
+ * createDiffieHellman,
1846
+ * } = require('crypto');
1847
+ *
1848
+ * // Generate Alice's keys...
1849
+ * const alice = createDiffieHellman(2048);
1850
+ * const aliceKey = alice.generateKeys();
1851
+ *
1852
+ * // Generate Bob's keys...
1853
+ * const bob = createDiffieHellman(alice.getPrime(), alice.getGenerator());
1854
+ * const bobKey = bob.generateKeys();
1855
+ *
1856
+ * // Exchange and generate the secret...
1857
+ * const aliceSecret = alice.computeSecret(bobKey);
1858
+ * const bobSecret = bob.computeSecret(aliceKey);
1859
+ *
1860
+ * // OK
1861
+ * assert.strictEqual(aliceSecret.toString('hex'), bobSecret.toString('hex'));
1862
+ * ```
1863
+ * @since v0.5.0
1864
+ */
462
1865
  class DiffieHellman {
463
1866
  private constructor();
1867
+ /**
1868
+ * Generates private and public Diffie-Hellman key values, and returns
1869
+ * the public key in the specified `encoding`. This key should be
1870
+ * transferred to the other party.
1871
+ * If `encoding` is provided a string is returned; otherwise a `Buffer` is returned.
1872
+ * @since v0.5.0
1873
+ * @param encoding The `encoding` of the return value.
1874
+ */
464
1875
  generateKeys(): Buffer;
465
1876
  generateKeys(encoding: BinaryToTextEncoding): string;
466
- computeSecret(other_public_key: NodeJS.ArrayBufferView): Buffer;
467
- computeSecret(other_public_key: string, input_encoding: BinaryToTextEncoding): Buffer;
468
- computeSecret(other_public_key: NodeJS.ArrayBufferView, output_encoding: BinaryToTextEncoding): string;
469
- computeSecret(
470
- other_public_key: string,
471
- input_encoding: BinaryToTextEncoding,
472
- output_encoding: BinaryToTextEncoding,
473
- ): string;
1877
+ /**
1878
+ * Computes the shared secret using `otherPublicKey` as the other
1879
+ * party's public key and returns the computed shared secret. The supplied
1880
+ * key is interpreted using the specified `inputEncoding`, and secret is
1881
+ * encoded using specified `outputEncoding`.
1882
+ * If the `inputEncoding` is not
1883
+ * provided, `otherPublicKey` is expected to be a `Buffer`,`TypedArray`, or `DataView`.
1884
+ *
1885
+ * If `outputEncoding` is given a string is returned; otherwise, a `Buffer` is returned.
1886
+ * @since v0.5.0
1887
+ * @param inputEncoding The `encoding` of an `otherPublicKey` string.
1888
+ * @param outputEncoding The `encoding` of the return value.
1889
+ */
1890
+ computeSecret(otherPublicKey: NodeJS.ArrayBufferView): Buffer;
1891
+ computeSecret(otherPublicKey: string, inputEncoding: BinaryToTextEncoding): Buffer;
1892
+ computeSecret(otherPublicKey: NodeJS.ArrayBufferView, outputEncoding: BinaryToTextEncoding): string;
1893
+ computeSecret(otherPublicKey: string, inputEncoding: BinaryToTextEncoding, outputEncoding: BinaryToTextEncoding): string;
1894
+ /**
1895
+ * Returns the Diffie-Hellman prime in the specified `encoding`.
1896
+ * If `encoding` is provided a string is
1897
+ * returned; otherwise a `Buffer` is returned.
1898
+ * @since v0.5.0
1899
+ * @param encoding The `encoding` of the return value.
1900
+ */
474
1901
  getPrime(): Buffer;
475
1902
  getPrime(encoding: BinaryToTextEncoding): string;
1903
+ /**
1904
+ * Returns the Diffie-Hellman generator in the specified `encoding`.
1905
+ * If `encoding` is provided a string is
1906
+ * returned; otherwise a `Buffer` is returned.
1907
+ * @since v0.5.0
1908
+ * @param encoding The `encoding` of the return value.
1909
+ */
476
1910
  getGenerator(): Buffer;
477
1911
  getGenerator(encoding: BinaryToTextEncoding): string;
1912
+ /**
1913
+ * Returns the Diffie-Hellman public key in the specified `encoding`.
1914
+ * If `encoding` is provided a
1915
+ * string is returned; otherwise a `Buffer` is returned.
1916
+ * @since v0.5.0
1917
+ * @param encoding The `encoding` of the return value.
1918
+ */
478
1919
  getPublicKey(): Buffer;
479
1920
  getPublicKey(encoding: BinaryToTextEncoding): string;
1921
+ /**
1922
+ * Returns the Diffie-Hellman private key in the specified `encoding`.
1923
+ * If `encoding` is provided a
1924
+ * string is returned; otherwise a `Buffer` is returned.
1925
+ * @since v0.5.0
1926
+ * @param encoding The `encoding` of the return value.
1927
+ */
480
1928
  getPrivateKey(): Buffer;
481
1929
  getPrivateKey(encoding: BinaryToTextEncoding): string;
482
- setPublicKey(public_key: NodeJS.ArrayBufferView): void;
483
- setPublicKey(public_key: string, encoding: BufferEncoding): void;
484
- setPrivateKey(private_key: NodeJS.ArrayBufferView): void;
485
- setPrivateKey(private_key: string, encoding: BufferEncoding): void;
1930
+ /**
1931
+ * Sets the Diffie-Hellman public key. If the `encoding` argument is provided,`publicKey` is expected
1932
+ * to be a string. If no `encoding` is provided, `publicKey` is expected
1933
+ * to be a `Buffer`, `TypedArray`, or `DataView`.
1934
+ * @since v0.5.0
1935
+ * @param encoding The `encoding` of the `publicKey` string.
1936
+ */
1937
+ setPublicKey(publicKey: NodeJS.ArrayBufferView): void;
1938
+ setPublicKey(publicKey: string, encoding: BufferEncoding): void;
1939
+ /**
1940
+ * Sets the Diffie-Hellman private key. If the `encoding` argument is provided,`privateKey` is expected
1941
+ * to be a string. If no `encoding` is provided, `privateKey` is expected
1942
+ * to be a `Buffer`, `TypedArray`, or `DataView`.
1943
+ * @since v0.5.0
1944
+ * @param encoding The `encoding` of the `privateKey` string.
1945
+ */
1946
+ setPrivateKey(privateKey: NodeJS.ArrayBufferView): void;
1947
+ setPrivateKey(privateKey: string, encoding: BufferEncoding): void;
1948
+ /**
1949
+ * A bit field containing any warnings and/or errors resulting from a check
1950
+ * performed during initialization of the `DiffieHellman` object.
1951
+ *
1952
+ * The following values are valid for this property (as defined in `constants`module):
1953
+ *
1954
+ * * `DH_CHECK_P_NOT_SAFE_PRIME`
1955
+ * * `DH_CHECK_P_NOT_PRIME`
1956
+ * * `DH_UNABLE_TO_CHECK_GENERATOR`
1957
+ * * `DH_NOT_SUITABLE_GENERATOR`
1958
+ * @since v0.11.12
1959
+ */
486
1960
  verifyError: number;
487
1961
  }
488
- function getDiffieHellman(group_name: string): DiffieHellman;
489
- function pbkdf2(
490
- password: BinaryLike,
491
- salt: BinaryLike,
492
- iterations: number,
493
- keylen: number,
494
- digest: string,
495
- callback: (err: Error | null, derivedKey: Buffer) => void,
496
- ): void;
497
- function pbkdf2Sync(
498
- password: BinaryLike,
499
- salt: BinaryLike,
500
- iterations: number,
501
- keylen: number,
502
- digest: string,
503
- ): Buffer;
504
-
1962
+ /**
1963
+ * Creates a predefined `DiffieHellmanGroup` key exchange object. The
1964
+ * supported groups are: `'modp1'`, `'modp2'`, `'modp5'` (defined in[RFC 2412](https://www.rfc-editor.org/rfc/rfc2412.txt), but see `Caveats`) and `'modp14'`, `'modp15'`,`'modp16'`, `'modp17'`,
1965
+ * `'modp18'` (defined in [RFC 3526](https://www.rfc-editor.org/rfc/rfc3526.txt)). The
1966
+ * returned object mimics the interface of objects created by {@link createDiffieHellman}, but will not allow changing
1967
+ * the keys (with `diffieHellman.setPublicKey()`, for example). The
1968
+ * advantage of using this method is that the parties do not have to
1969
+ * generate nor exchange a group modulus beforehand, saving both processor
1970
+ * and communication time.
1971
+ *
1972
+ * Example (obtaining a shared secret):
1973
+ *
1974
+ * ```js
1975
+ * const {
1976
+ * getDiffieHellman,
1977
+ * } = await import('crypto');
1978
+ * const alice = getDiffieHellman('modp14');
1979
+ * const bob = getDiffieHellman('modp14');
1980
+ *
1981
+ * alice.generateKeys();
1982
+ * bob.generateKeys();
1983
+ *
1984
+ * const aliceSecret = alice.computeSecret(bob.getPublicKey(), null, 'hex');
1985
+ * const bobSecret = bob.computeSecret(alice.getPublicKey(), null, 'hex');
1986
+ *
1987
+ * // aliceSecret and bobSecret should be the same
1988
+ * console.log(aliceSecret === bobSecret);
1989
+ * ```
1990
+ *
1991
+ * ```js
1992
+ * const {
1993
+ * getDiffieHellman,
1994
+ * } = require('crypto');
1995
+ *
1996
+ * const alice = getDiffieHellman('modp14');
1997
+ * const bob = getDiffieHellman('modp14');
1998
+ *
1999
+ * alice.generateKeys();
2000
+ * bob.generateKeys();
2001
+ *
2002
+ * const aliceSecret = alice.computeSecret(bob.getPublicKey(), null, 'hex');
2003
+ * const bobSecret = bob.computeSecret(alice.getPublicKey(), null, 'hex');
2004
+ *
2005
+ * // aliceSecret and bobSecret should be the same
2006
+ * console.log(aliceSecret === bobSecret);
2007
+ * ```
2008
+ * @since v0.7.5
2009
+ */
2010
+ function getDiffieHellman(groupName: string): DiffieHellman;
2011
+ /**
2012
+ * Provides an asynchronous Password-Based Key Derivation Function 2 (PBKDF2)
2013
+ * implementation. A selected HMAC digest algorithm specified by `digest` is
2014
+ * applied to derive a key of the requested byte length (`keylen`) from the`password`, `salt` and `iterations`.
2015
+ *
2016
+ * The supplied `callback` function is called with two arguments: `err` and`derivedKey`. If an error occurs while deriving the key, `err` will be set;
2017
+ * otherwise `err` will be `null`. By default, the successfully generated`derivedKey` will be passed to the callback as a `Buffer`. An error will be
2018
+ * thrown if any of the input arguments specify invalid values or types.
2019
+ *
2020
+ * If `digest` is `null`, `'sha1'` will be used. This behavior is deprecated,
2021
+ * please specify a `digest` explicitly.
2022
+ *
2023
+ * The `iterations` argument must be a number set as high as possible. The
2024
+ * higher the number of iterations, the more secure the derived key will be,
2025
+ * but will take a longer amount of time to complete.
2026
+ *
2027
+ * The `salt` should be as unique as possible. It is recommended that a salt is
2028
+ * random and at least 16 bytes long. See [NIST SP 800-132](https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf) for details.
2029
+ *
2030
+ * When passing strings for `password` or `salt`, please consider `caveats when using strings as inputs to cryptographic APIs`.
2031
+ *
2032
+ * ```js
2033
+ * const {
2034
+ * pbkdf2,
2035
+ * } = await import('crypto');
2036
+ *
2037
+ * pbkdf2('secret', 'salt', 100000, 64, 'sha512', (err, derivedKey) => {
2038
+ * if (err) throw err;
2039
+ * console.log(derivedKey.toString('hex')); // '3745e48...08d59ae'
2040
+ * });
2041
+ * ```
2042
+ *
2043
+ * ```js
2044
+ * const {
2045
+ * pbkdf2,
2046
+ * } = require('crypto');
2047
+ *
2048
+ * pbkdf2('secret', 'salt', 100000, 64, 'sha512', (err, derivedKey) => {
2049
+ * if (err) throw err;
2050
+ * console.log(derivedKey.toString('hex')); // '3745e48...08d59ae'
2051
+ * });
2052
+ * ```
2053
+ *
2054
+ * The `crypto.DEFAULT_ENCODING` property can be used to change the way the`derivedKey` is passed to the callback. This property, however, has been
2055
+ * deprecated and use should be avoided.
2056
+ *
2057
+ * ```js
2058
+ * const crypto = await import('crypto');
2059
+ * crypto.DEFAULT_ENCODING = 'hex';
2060
+ * crypto.pbkdf2('secret', 'salt', 100000, 512, 'sha512', (err, derivedKey) => {
2061
+ * if (err) throw err;
2062
+ * console.log(derivedKey); // '3745e48...aa39b34'
2063
+ * });
2064
+ * ```
2065
+ *
2066
+ * ```js
2067
+ * const crypto = require('crypto');
2068
+ * crypto.DEFAULT_ENCODING = 'hex';
2069
+ * crypto.pbkdf2('secret', 'salt', 100000, 512, 'sha512', (err, derivedKey) => {
2070
+ * if (err) throw err;
2071
+ * console.log(derivedKey); // '3745e48...aa39b34'
2072
+ * });
2073
+ * ```
2074
+ *
2075
+ * An array of supported digest functions can be retrieved using {@link getHashes}.
2076
+ *
2077
+ * This API uses libuv's threadpool, which can have surprising and
2078
+ * negative performance implications for some applications; see the `UV_THREADPOOL_SIZE` documentation for more information.
2079
+ * @since v0.5.5
2080
+ */
2081
+ function pbkdf2(password: BinaryLike, salt: BinaryLike, iterations: number, keylen: number, digest: string, callback: (err: Error | null, derivedKey: Buffer) => void): void;
2082
+ /**
2083
+ * Provides a synchronous Password-Based Key Derivation Function 2 (PBKDF2)
2084
+ * implementation. A selected HMAC digest algorithm specified by `digest` is
2085
+ * applied to derive a key of the requested byte length (`keylen`) from the`password`, `salt` and `iterations`.
2086
+ *
2087
+ * If an error occurs an `Error` will be thrown, otherwise the derived key will be
2088
+ * returned as a `Buffer`.
2089
+ *
2090
+ * If `digest` is `null`, `'sha1'` will be used. This behavior is deprecated,
2091
+ * please specify a `digest` explicitly.
2092
+ *
2093
+ * The `iterations` argument must be a number set as high as possible. The
2094
+ * higher the number of iterations, the more secure the derived key will be,
2095
+ * but will take a longer amount of time to complete.
2096
+ *
2097
+ * The `salt` should be as unique as possible. It is recommended that a salt is
2098
+ * random and at least 16 bytes long. See [NIST SP 800-132](https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf) for details.
2099
+ *
2100
+ * When passing strings for `password` or `salt`, please consider `caveats when using strings as inputs to cryptographic APIs`.
2101
+ *
2102
+ * ```js
2103
+ * const {
2104
+ * pbkdf2Sync,
2105
+ * } = await import('crypto');
2106
+ *
2107
+ * const key = pbkdf2Sync('secret', 'salt', 100000, 64, 'sha512');
2108
+ * console.log(key.toString('hex')); // '3745e48...08d59ae'
2109
+ * ```
2110
+ *
2111
+ * ```js
2112
+ * const {
2113
+ * pbkdf2Sync,
2114
+ * } = require('crypto');
2115
+ *
2116
+ * const key = pbkdf2Sync('secret', 'salt', 100000, 64, 'sha512');
2117
+ * console.log(key.toString('hex')); // '3745e48...08d59ae'
2118
+ * ```
2119
+ *
2120
+ * The `crypto.DEFAULT_ENCODING` property may be used to change the way the`derivedKey` is returned. This property, however, is deprecated and use
2121
+ * should be avoided.
2122
+ *
2123
+ * ```js
2124
+ * const crypto = await import('crypto');
2125
+ * crypto.DEFAULT_ENCODING = 'hex';
2126
+ * const key = crypto.pbkdf2Sync('secret', 'salt', 100000, 512, 'sha512');
2127
+ * console.log(key); // '3745e48...aa39b34'
2128
+ * ```
2129
+ *
2130
+ * ```js
2131
+ * const crypto = require('crypto');
2132
+ * crypto.DEFAULT_ENCODING = 'hex';
2133
+ * const key = crypto.pbkdf2Sync('secret', 'salt', 100000, 512, 'sha512');
2134
+ * console.log(key); // '3745e48...aa39b34'
2135
+ * ```
2136
+ *
2137
+ * An array of supported digest functions can be retrieved using {@link getHashes}.
2138
+ * @since v0.9.3
2139
+ */
2140
+ function pbkdf2Sync(password: BinaryLike, salt: BinaryLike, iterations: number, keylen: number, digest: string): Buffer;
2141
+ /**
2142
+ * Generates cryptographically strong pseudorandom data. The `size` argument
2143
+ * is a number indicating the number of bytes to generate.
2144
+ *
2145
+ * If a `callback` function is provided, the bytes are generated asynchronously
2146
+ * and the `callback` function is invoked with two arguments: `err` and `buf`.
2147
+ * If an error occurs, `err` will be an `Error` object; otherwise it is `null`. The`buf` argument is a `Buffer` containing the generated bytes.
2148
+ *
2149
+ * ```js
2150
+ * // Asynchronous
2151
+ * const {
2152
+ * randomBytes,
2153
+ * } = await import('crypto');
2154
+ *
2155
+ * randomBytes(256, (err, buf) => {
2156
+ * if (err) throw err;
2157
+ * console.log(`${buf.length} bytes of random data: ${buf.toString('hex')}`);
2158
+ * });
2159
+ * ```
2160
+ *
2161
+ * ```js
2162
+ * // Asynchronous
2163
+ * const {
2164
+ * randomBytes,
2165
+ * } = require('crypto');
2166
+ *
2167
+ * randomBytes(256, (err, buf) => {
2168
+ * if (err) throw err;
2169
+ * console.log(`${buf.length} bytes of random data: ${buf.toString('hex')}`);
2170
+ * });
2171
+ * ```
2172
+ *
2173
+ * If the `callback` function is not provided, the random bytes are generated
2174
+ * synchronously and returned as a `Buffer`. An error will be thrown if
2175
+ * there is a problem generating the bytes.
2176
+ *
2177
+ * ```js
2178
+ * // Synchronous
2179
+ * const {
2180
+ * randomBytes,
2181
+ * } = await import('crypto');
2182
+ *
2183
+ * const buf = randomBytes(256);
2184
+ * console.log(
2185
+ * `${buf.length} bytes of random data: ${buf.toString('hex')}`);
2186
+ * ```
2187
+ *
2188
+ * ```js
2189
+ * // Synchronous
2190
+ * const {
2191
+ * randomBytes,
2192
+ * } = require('crypto');
2193
+ *
2194
+ * const buf = randomBytes(256);
2195
+ * console.log(
2196
+ * `${buf.length} bytes of random data: ${buf.toString('hex')}`);
2197
+ * ```
2198
+ *
2199
+ * The `crypto.randomBytes()` method will not complete until there is
2200
+ * sufficient entropy available.
2201
+ * This should normally never take longer than a few milliseconds. The only time
2202
+ * when generating the random bytes may conceivably block for a longer period of
2203
+ * time is right after boot, when the whole system is still low on entropy.
2204
+ *
2205
+ * This API uses libuv's threadpool, which can have surprising and
2206
+ * negative performance implications for some applications; see the `UV_THREADPOOL_SIZE` documentation for more information.
2207
+ *
2208
+ * The asynchronous version of `crypto.randomBytes()` is carried out in a single
2209
+ * threadpool request. To minimize threadpool task length variation, partition
2210
+ * large `randomBytes` requests when doing so as part of fulfilling a client
2211
+ * request.
2212
+ * @since v0.5.8
2213
+ * @param size The number of bytes to generate. The `size` must not be larger than `2**31 - 1`.
2214
+ * @return if the `callback` function is not provided.
2215
+ */
505
2216
  function randomBytes(size: number): Buffer;
506
2217
  function randomBytes(size: number, callback: (err: Error | null, buf: Buffer) => void): void;
507
2218
  function pseudoRandomBytes(size: number): Buffer;
508
2219
  function pseudoRandomBytes(size: number, callback: (err: Error | null, buf: Buffer) => void): void;
509
-
2220
+ /**
2221
+ * Return a random integer `n` such that `min <= n < max`. This
2222
+ * implementation avoids [modulo bias](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#Modulo_bias).
2223
+ *
2224
+ * The range (`max - min`) must be less than 248. `min` and `max` must
2225
+ * be [safe integers](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isSafeInteger).
2226
+ *
2227
+ * If the `callback` function is not provided, the random integer is
2228
+ * generated synchronously.
2229
+ *
2230
+ * ```js
2231
+ * // Asynchronous
2232
+ * const {
2233
+ * randomInt,
2234
+ * } = await import('crypto');
2235
+ *
2236
+ * randomInt(3, (err, n) => {
2237
+ * if (err) throw err;
2238
+ * console.log(`Random number chosen from (0, 1, 2): ${n}`);
2239
+ * });
2240
+ * ```
2241
+ *
2242
+ * ```js
2243
+ * // Asynchronous
2244
+ * const {
2245
+ * randomInt,
2246
+ * } = require('crypto');
2247
+ *
2248
+ * randomInt(3, (err, n) => {
2249
+ * if (err) throw err;
2250
+ * console.log(`Random number chosen from (0, 1, 2): ${n}`);
2251
+ * });
2252
+ * ```
2253
+ *
2254
+ * ```js
2255
+ * // Synchronous
2256
+ * const {
2257
+ * randomInt,
2258
+ * } = await import('crypto');
2259
+ *
2260
+ * const n = randomInt(3);
2261
+ * console.log(`Random number chosen from (0, 1, 2): ${n}`);
2262
+ * ```
2263
+ *
2264
+ * ```js
2265
+ * // Synchronous
2266
+ * const {
2267
+ * randomInt,
2268
+ * } = require('crypto');
2269
+ *
2270
+ * const n = randomInt(3);
2271
+ * console.log(`Random number chosen from (0, 1, 2): ${n}`);
2272
+ * ```
2273
+ *
2274
+ * ```js
2275
+ * // With `min` argument
2276
+ * const {
2277
+ * randomInt,
2278
+ * } = await import('crypto');
2279
+ *
2280
+ * const n = randomInt(1, 7);
2281
+ * console.log(`The dice rolled: ${n}`);
2282
+ * ```
2283
+ *
2284
+ * ```js
2285
+ * // With `min` argument
2286
+ * const {
2287
+ * randomInt,
2288
+ * } = require('crypto');
2289
+ *
2290
+ * const n = randomInt(1, 7);
2291
+ * console.log(`The dice rolled: ${n}`);
2292
+ * ```
2293
+ * @since v14.10.0, v12.19.0
2294
+ * @param min Start of random range (inclusive).
2295
+ * @param max End of random range (exclusive).
2296
+ * @param callback `function(err, n) {}`.
2297
+ */
510
2298
  function randomInt(max: number): number;
511
2299
  function randomInt(min: number, max: number): number;
512
2300
  function randomInt(max: number, callback: (err: Error | null, value: number) => void): void;
513
2301
  function randomInt(min: number, max: number, callback: (err: Error | null, value: number) => void): void;
514
-
515
- function randomFillSync<T extends NodeJS.ArrayBufferView>(buffer: T, offset?: number, size?: number): T;
516
- function randomFill<T extends NodeJS.ArrayBufferView>(
517
- buffer: T,
518
- callback: (err: Error | null, buf: T) => void,
519
- ): void;
520
- function randomFill<T extends NodeJS.ArrayBufferView>(
521
- buffer: T,
522
- offset: number,
523
- callback: (err: Error | null, buf: T) => void,
524
- ): void;
525
- function randomFill<T extends NodeJS.ArrayBufferView>(
526
- buffer: T,
527
- offset: number,
528
- size: number,
529
- callback: (err: Error | null, buf: T) => void,
530
- ): void;
531
-
2302
+ /**
2303
+ * Synchronous version of {@link randomFill}.
2304
+ *
2305
+ * ```js
2306
+ * const {
2307
+ * randomFillSync,
2308
+ * } = await import('crypto');
2309
+ *
2310
+ * const buf = Buffer.alloc(10);
2311
+ * console.log(randomFillSync(buf).toString('hex'));
2312
+ *
2313
+ * randomFillSync(buf, 5);
2314
+ * console.log(buf.toString('hex'));
2315
+ *
2316
+ * // The above is equivalent to the following:
2317
+ * randomFillSync(buf, 5, 5);
2318
+ * console.log(buf.toString('hex'));
2319
+ * ```
2320
+ *
2321
+ * ```js
2322
+ * const {
2323
+ * randomFillSync,
2324
+ * } = require('crypto');
2325
+ *
2326
+ * const buf = Buffer.alloc(10);
2327
+ * console.log(randomFillSync(buf).toString('hex'));
2328
+ *
2329
+ * randomFillSync(buf, 5);
2330
+ * console.log(buf.toString('hex'));
2331
+ *
2332
+ * // The above is equivalent to the following:
2333
+ * randomFillSync(buf, 5, 5);
2334
+ * console.log(buf.toString('hex'));
2335
+ * ```
2336
+ *
2337
+ * Any `ArrayBuffer`, `TypedArray` or `DataView` instance may be passed as`buffer`.
2338
+ *
2339
+ * ```js
2340
+ * const {
2341
+ * randomFillSync,
2342
+ * } = await import('crypto');
2343
+ *
2344
+ * const a = new Uint32Array(10);
2345
+ * console.log(Buffer.from(randomFillSync(a).buffer,
2346
+ * a.byteOffset, a.byteLength).toString('hex'));
2347
+ *
2348
+ * const b = new DataView(new ArrayBuffer(10));
2349
+ * console.log(Buffer.from(randomFillSync(b).buffer,
2350
+ * b.byteOffset, b.byteLength).toString('hex'));
2351
+ *
2352
+ * const c = new ArrayBuffer(10);
2353
+ * console.log(Buffer.from(randomFillSync(c)).toString('hex'));
2354
+ * ```
2355
+ *
2356
+ * ```js
2357
+ * const {
2358
+ * randomFillSync,
2359
+ * } = require('crypto');
2360
+ *
2361
+ * const a = new Uint32Array(10);
2362
+ * console.log(Buffer.from(randomFillSync(a).buffer,
2363
+ * a.byteOffset, a.byteLength).toString('hex'));
2364
+ *
2365
+ * const b = new DataView(new ArrayBuffer(10));
2366
+ * console.log(Buffer.from(randomFillSync(b).buffer,
2367
+ * b.byteOffset, b.byteLength).toString('hex'));
2368
+ *
2369
+ * const c = new ArrayBuffer(10);
2370
+ * console.log(Buffer.from(randomFillSync(c)).toString('hex'));
2371
+ * ```
2372
+ * @since v7.10.0, v6.13.0
2373
+ * @param buffer Must be supplied. The size of the provided `buffer` must not be larger than `2**31 - 1`.
2374
+ * @return The object passed as `buffer` argument.
2375
+ */
2376
+ function randomFillSync<T extends NodeJS.ArrayBufferView>(buffer: T, offset?: number, size?: number): T;
2377
+ /**
2378
+ * This function is similar to {@link randomBytes} but requires the first
2379
+ * argument to be a `Buffer` that will be filled. It also
2380
+ * requires that a callback is passed in.
2381
+ *
2382
+ * If the `callback` function is not provided, an error will be thrown.
2383
+ *
2384
+ * ```js
2385
+ * const {
2386
+ * randomFill,
2387
+ * } = await import('crypto');
2388
+ *
2389
+ * const buf = Buffer.alloc(10);
2390
+ * randomFill(buf, (err, buf) => {
2391
+ * if (err) throw err;
2392
+ * console.log(buf.toString('hex'));
2393
+ * });
2394
+ *
2395
+ * randomFill(buf, 5, (err, buf) => {
2396
+ * if (err) throw err;
2397
+ * console.log(buf.toString('hex'));
2398
+ * });
2399
+ *
2400
+ * // The above is equivalent to the following:
2401
+ * randomFill(buf, 5, 5, (err, buf) => {
2402
+ * if (err) throw err;
2403
+ * console.log(buf.toString('hex'));
2404
+ * });
2405
+ * ```
2406
+ *
2407
+ * ```js
2408
+ * const {
2409
+ * randomFill,
2410
+ * } = require('crypto');
2411
+ *
2412
+ * const buf = Buffer.alloc(10);
2413
+ * randomFill(buf, (err, buf) => {
2414
+ * if (err) throw err;
2415
+ * console.log(buf.toString('hex'));
2416
+ * });
2417
+ *
2418
+ * randomFill(buf, 5, (err, buf) => {
2419
+ * if (err) throw err;
2420
+ * console.log(buf.toString('hex'));
2421
+ * });
2422
+ *
2423
+ * // The above is equivalent to the following:
2424
+ * randomFill(buf, 5, 5, (err, buf) => {
2425
+ * if (err) throw err;
2426
+ * console.log(buf.toString('hex'));
2427
+ * });
2428
+ * ```
2429
+ *
2430
+ * Any `ArrayBuffer`, `TypedArray`, or `DataView` instance may be passed as`buffer`.
2431
+ *
2432
+ * While this includes instances of `Float32Array` and `Float64Array`, this
2433
+ * function should not be used to generate random floating-point numbers. The
2434
+ * result may contain `+Infinity`, `-Infinity`, and `NaN`, and even if the array
2435
+ * contains finite numbers only, they are not drawn from a uniform random
2436
+ * distribution and have no meaningful lower or upper bounds.
2437
+ *
2438
+ * ```js
2439
+ * const {
2440
+ * randomFill,
2441
+ * } = await import('crypto');
2442
+ *
2443
+ * const a = new Uint32Array(10);
2444
+ * randomFill(a, (err, buf) => {
2445
+ * if (err) throw err;
2446
+ * console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
2447
+ * .toString('hex'));
2448
+ * });
2449
+ *
2450
+ * const b = new DataView(new ArrayBuffer(10));
2451
+ * randomFill(b, (err, buf) => {
2452
+ * if (err) throw err;
2453
+ * console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
2454
+ * .toString('hex'));
2455
+ * });
2456
+ *
2457
+ * const c = new ArrayBuffer(10);
2458
+ * randomFill(c, (err, buf) => {
2459
+ * if (err) throw err;
2460
+ * console.log(Buffer.from(buf).toString('hex'));
2461
+ * });
2462
+ * ```
2463
+ *
2464
+ * ```js
2465
+ * const {
2466
+ * randomFill,
2467
+ * } = require('crypto');
2468
+ *
2469
+ * const a = new Uint32Array(10);
2470
+ * randomFill(a, (err, buf) => {
2471
+ * if (err) throw err;
2472
+ * console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
2473
+ * .toString('hex'));
2474
+ * });
2475
+ *
2476
+ * const b = new DataView(new ArrayBuffer(10));
2477
+ * randomFill(b, (err, buf) => {
2478
+ * if (err) throw err;
2479
+ * console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
2480
+ * .toString('hex'));
2481
+ * });
2482
+ *
2483
+ * const c = new ArrayBuffer(10);
2484
+ * randomFill(c, (err, buf) => {
2485
+ * if (err) throw err;
2486
+ * console.log(Buffer.from(buf).toString('hex'));
2487
+ * });
2488
+ * ```
2489
+ *
2490
+ * This API uses libuv's threadpool, which can have surprising and
2491
+ * negative performance implications for some applications; see the `UV_THREADPOOL_SIZE` documentation for more information.
2492
+ *
2493
+ * The asynchronous version of `crypto.randomFill()` is carried out in a single
2494
+ * threadpool request. To minimize threadpool task length variation, partition
2495
+ * large `randomFill` requests when doing so as part of fulfilling a client
2496
+ * request.
2497
+ * @since v7.10.0, v6.13.0
2498
+ * @param buffer Must be supplied. The size of the provided `buffer` must not be larger than `2**31 - 1`.
2499
+ * @param callback `function(err, buf) {}`.
2500
+ */
2501
+ function randomFill<T extends NodeJS.ArrayBufferView>(buffer: T, callback: (err: Error | null, buf: T) => void): void;
2502
+ function randomFill<T extends NodeJS.ArrayBufferView>(buffer: T, offset: number, callback: (err: Error | null, buf: T) => void): void;
2503
+ function randomFill<T extends NodeJS.ArrayBufferView>(buffer: T, offset: number, size: number, callback: (err: Error | null, buf: T) => void): void;
532
2504
  interface ScryptOptions {
533
2505
  cost?: number | undefined;
534
2506
  blockSize?: number | undefined;
@@ -538,21 +2510,103 @@ declare module 'crypto' {
538
2510
  p?: number | undefined;
539
2511
  maxmem?: number | undefined;
540
2512
  }
541
- function scrypt(
542
- password: BinaryLike,
543
- salt: BinaryLike,
544
- keylen: number,
545
- callback: (err: Error | null, derivedKey: Buffer) => void,
546
- ): void;
547
- function scrypt(
548
- password: BinaryLike,
549
- salt: BinaryLike,
550
- keylen: number,
551
- options: ScryptOptions,
552
- callback: (err: Error | null, derivedKey: Buffer) => void,
553
- ): void;
2513
+ /**
2514
+ * Provides an asynchronous [scrypt](https://en.wikipedia.org/wiki/Scrypt) implementation. Scrypt is a password-based
2515
+ * key derivation function that is designed to be expensive computationally and
2516
+ * memory-wise in order to make brute-force attacks unrewarding.
2517
+ *
2518
+ * The `salt` should be as unique as possible. It is recommended that a salt is
2519
+ * random and at least 16 bytes long. See [NIST SP 800-132](https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf) for details.
2520
+ *
2521
+ * When passing strings for `password` or `salt`, please consider `caveats when using strings as inputs to cryptographic APIs`.
2522
+ *
2523
+ * The `callback` function is called with two arguments: `err` and `derivedKey`.`err` is an exception object when key derivation fails, otherwise `err` is`null`. `derivedKey` is passed to the
2524
+ * callback as a `Buffer`.
2525
+ *
2526
+ * An exception is thrown when any of the input arguments specify invalid values
2527
+ * or types.
2528
+ *
2529
+ * ```js
2530
+ * const {
2531
+ * scrypt,
2532
+ * } = await import('crypto');
2533
+ *
2534
+ * // Using the factory defaults.
2535
+ * scrypt('password', 'salt', 64, (err, derivedKey) => {
2536
+ * if (err) throw err;
2537
+ * console.log(derivedKey.toString('hex')); // '3745e48...08d59ae'
2538
+ * });
2539
+ * // Using a custom N parameter. Must be a power of two.
2540
+ * scrypt('password', 'salt', 64, { N: 1024 }, (err, derivedKey) => {
2541
+ * if (err) throw err;
2542
+ * console.log(derivedKey.toString('hex')); // '3745e48...aa39b34'
2543
+ * });
2544
+ * ```
2545
+ *
2546
+ * ```js
2547
+ * const {
2548
+ * scrypt,
2549
+ * } = require('crypto');
2550
+ *
2551
+ * // Using the factory defaults.
2552
+ * scrypt('password', 'salt', 64, (err, derivedKey) => {
2553
+ * if (err) throw err;
2554
+ * console.log(derivedKey.toString('hex')); // '3745e48...08d59ae'
2555
+ * });
2556
+ * // Using a custom N parameter. Must be a power of two.
2557
+ * scrypt('password', 'salt', 64, { N: 1024 }, (err, derivedKey) => {
2558
+ * if (err) throw err;
2559
+ * console.log(derivedKey.toString('hex')); // '3745e48...aa39b34'
2560
+ * });
2561
+ * ```
2562
+ * @since v10.5.0
2563
+ */
2564
+ function scrypt(password: BinaryLike, salt: BinaryLike, keylen: number, callback: (err: Error | null, derivedKey: Buffer) => void): void;
2565
+ function scrypt(password: BinaryLike, salt: BinaryLike, keylen: number, options: ScryptOptions, callback: (err: Error | null, derivedKey: Buffer) => void): void;
2566
+ /**
2567
+ * Provides a synchronous [scrypt](https://en.wikipedia.org/wiki/Scrypt) implementation. Scrypt is a password-based
2568
+ * key derivation function that is designed to be expensive computationally and
2569
+ * memory-wise in order to make brute-force attacks unrewarding.
2570
+ *
2571
+ * The `salt` should be as unique as possible. It is recommended that a salt is
2572
+ * random and at least 16 bytes long. See [NIST SP 800-132](https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf) for details.
2573
+ *
2574
+ * When passing strings for `password` or `salt`, please consider `caveats when using strings as inputs to cryptographic APIs`.
2575
+ *
2576
+ * An exception is thrown when key derivation fails, otherwise the derived key is
2577
+ * returned as a `Buffer`.
2578
+ *
2579
+ * An exception is thrown when any of the input arguments specify invalid values
2580
+ * or types.
2581
+ *
2582
+ * ```js
2583
+ * const {
2584
+ * scryptSync,
2585
+ * } = await import('crypto');
2586
+ * // Using the factory defaults.
2587
+ *
2588
+ * const key1 = scryptSync('password', 'salt', 64);
2589
+ * console.log(key1.toString('hex')); // '3745e48...08d59ae'
2590
+ * // Using a custom N parameter. Must be a power of two.
2591
+ * const key2 = scryptSync('password', 'salt', 64, { N: 1024 });
2592
+ * console.log(key2.toString('hex')); // '3745e48...aa39b34'
2593
+ * ```
2594
+ *
2595
+ * ```js
2596
+ * const {
2597
+ * scryptSync,
2598
+ * } = require('crypto');
2599
+ * // Using the factory defaults.
2600
+ *
2601
+ * const key1 = scryptSync('password', 'salt', 64);
2602
+ * console.log(key1.toString('hex')); // '3745e48...08d59ae'
2603
+ * // Using a custom N parameter. Must be a power of two.
2604
+ * const key2 = scryptSync('password', 'salt', 64, { N: 1024 });
2605
+ * console.log(key2.toString('hex')); // '3745e48...aa39b34'
2606
+ * ```
2607
+ * @since v10.5.0
2608
+ */
554
2609
  function scryptSync(password: BinaryLike, salt: BinaryLike, keylen: number, options?: ScryptOptions): Buffer;
555
-
556
2610
  interface RsaPublicKey {
557
2611
  key: KeyLike;
558
2612
  padding?: number | undefined;
@@ -567,114 +2621,375 @@ declare module 'crypto' {
567
2621
  oaepLabel?: NodeJS.TypedArray | undefined;
568
2622
  padding?: number | undefined;
569
2623
  }
2624
+ /**
2625
+ * Encrypts the content of `buffer` with `key` and returns a new `Buffer` with encrypted content. The returned data can be decrypted using
2626
+ * the corresponding private key, for example using {@link privateDecrypt}.
2627
+ *
2628
+ * If `key` is not a `KeyObject`, this function behaves as if`key` had been passed to {@link createPublicKey}. If it is an
2629
+ * object, the `padding` property can be passed. Otherwise, this function uses`RSA_PKCS1_OAEP_PADDING`.
2630
+ *
2631
+ * Because RSA public keys can be derived from private keys, a private key may
2632
+ * be passed instead of a public key.
2633
+ * @since v0.11.14
2634
+ */
570
2635
  function publicEncrypt(key: RsaPublicKey | RsaPrivateKey | KeyLike, buffer: NodeJS.ArrayBufferView): Buffer;
2636
+ /**
2637
+ * Decrypts `buffer` with `key`.`buffer` was previously encrypted using
2638
+ * the corresponding private key, for example using {@link privateEncrypt}.
2639
+ *
2640
+ * If `key` is not a `KeyObject`, this function behaves as if`key` had been passed to {@link createPublicKey}. If it is an
2641
+ * object, the `padding` property can be passed. Otherwise, this function uses`RSA_PKCS1_PADDING`.
2642
+ *
2643
+ * Because RSA public keys can be derived from private keys, a private key may
2644
+ * be passed instead of a public key.
2645
+ * @since v1.1.0
2646
+ */
571
2647
  function publicDecrypt(key: RsaPublicKey | RsaPrivateKey | KeyLike, buffer: NodeJS.ArrayBufferView): Buffer;
572
- function privateDecrypt(private_key: RsaPrivateKey | KeyLike, buffer: NodeJS.ArrayBufferView): Buffer;
573
- function privateEncrypt(private_key: RsaPrivateKey | KeyLike, buffer: NodeJS.ArrayBufferView): Buffer;
2648
+ /**
2649
+ * Decrypts `buffer` with `privateKey`. `buffer` was previously encrypted using
2650
+ * the corresponding public key, for example using {@link publicEncrypt}.
2651
+ *
2652
+ * If `privateKey` is not a `KeyObject`, this function behaves as if`privateKey` had been passed to {@link createPrivateKey}. If it is an
2653
+ * object, the `padding` property can be passed. Otherwise, this function uses`RSA_PKCS1_OAEP_PADDING`.
2654
+ * @since v0.11.14
2655
+ */
2656
+ function privateDecrypt(privateKey: RsaPrivateKey | KeyLike, buffer: NodeJS.ArrayBufferView): Buffer;
2657
+ /**
2658
+ * Encrypts `buffer` with `privateKey`. The returned data can be decrypted using
2659
+ * the corresponding public key, for example using {@link publicDecrypt}.
2660
+ *
2661
+ * If `privateKey` is not a `KeyObject`, this function behaves as if`privateKey` had been passed to {@link createPrivateKey}. If it is an
2662
+ * object, the `padding` property can be passed. Otherwise, this function uses`RSA_PKCS1_PADDING`.
2663
+ * @since v1.1.0
2664
+ */
2665
+ function privateEncrypt(privateKey: RsaPrivateKey | KeyLike, buffer: NodeJS.ArrayBufferView): Buffer;
2666
+ /**
2667
+ * ```js
2668
+ * const {
2669
+ * getCiphers,
2670
+ * } = await import('crypto');
2671
+ *
2672
+ * console.log(getCiphers()); // ['aes-128-cbc', 'aes-128-ccm', ...]
2673
+ * ```
2674
+ *
2675
+ * ```js
2676
+ * const {
2677
+ * getCiphers,
2678
+ * } = require('crypto');
2679
+ *
2680
+ * console.log(getCiphers()); // ['aes-128-cbc', 'aes-128-ccm', ...]
2681
+ * ```
2682
+ * @since v0.9.3
2683
+ * @return An array with the names of the supported cipher algorithms.
2684
+ */
574
2685
  function getCiphers(): string[];
2686
+ /**
2687
+ * ```js
2688
+ * const {
2689
+ * getCurves,
2690
+ * } = await import('crypto');
2691
+ *
2692
+ * console.log(getCurves()); // ['Oakley-EC2N-3', 'Oakley-EC2N-4', ...]
2693
+ * ```
2694
+ *
2695
+ * ```js
2696
+ * const {
2697
+ * getCurves,
2698
+ * } = require('crypto');
2699
+ *
2700
+ * console.log(getCurves()); // ['Oakley-EC2N-3', 'Oakley-EC2N-4', ...]
2701
+ * ```
2702
+ * @since v2.3.0
2703
+ * @return An array with the names of the supported elliptic curves.
2704
+ */
575
2705
  function getCurves(): string[];
2706
+ /**
2707
+ * @since v10.0.0
2708
+ * @return `1` if and only if a FIPS compliant crypto provider is currently in use, `0` otherwise. A future semver-major release may change the return type of this API to a {boolean}.
2709
+ */
576
2710
  function getFips(): 1 | 0;
2711
+ /**
2712
+ * ```js
2713
+ * const {
2714
+ * getHashes,
2715
+ * } = await import('crypto');
2716
+ *
2717
+ * console.log(getHashes()); // ['DSA', 'DSA-SHA', 'DSA-SHA1', ...]
2718
+ * ```
2719
+ *
2720
+ * ```js
2721
+ * const {
2722
+ * getHashes,
2723
+ * } = require('crypto');
2724
+ *
2725
+ * console.log(getHashes()); // ['DSA', 'DSA-SHA', 'DSA-SHA1', ...]
2726
+ * ```
2727
+ * @since v0.9.3
2728
+ * @return An array of the names of the supported hash algorithms, such as `'RSA-SHA256'`. Hash algorithms are also called "digest" algorithms.
2729
+ */
577
2730
  function getHashes(): string[];
2731
+ /**
2732
+ * The `ECDH` class is a utility for creating Elliptic Curve Diffie-Hellman (ECDH)
2733
+ * key exchanges.
2734
+ *
2735
+ * Instances of the `ECDH` class can be created using the {@link createECDH} function.
2736
+ *
2737
+ * ```js
2738
+ * import assert from 'assert';
2739
+ *
2740
+ * const {
2741
+ * createECDH,
2742
+ * } = await import('crypto');
2743
+ *
2744
+ * // Generate Alice's keys...
2745
+ * const alice = createECDH('secp521r1');
2746
+ * const aliceKey = alice.generateKeys();
2747
+ *
2748
+ * // Generate Bob's keys...
2749
+ * const bob = createECDH('secp521r1');
2750
+ * const bobKey = bob.generateKeys();
2751
+ *
2752
+ * // Exchange and generate the secret...
2753
+ * const aliceSecret = alice.computeSecret(bobKey);
2754
+ * const bobSecret = bob.computeSecret(aliceKey);
2755
+ *
2756
+ * assert.strictEqual(aliceSecret.toString('hex'), bobSecret.toString('hex'));
2757
+ * // OK
2758
+ * ```
2759
+ *
2760
+ * ```js
2761
+ * const assert = require('assert');
2762
+ *
2763
+ * const {
2764
+ * createECDH,
2765
+ * } = require('crypto');
2766
+ *
2767
+ * // Generate Alice's keys...
2768
+ * const alice = createECDH('secp521r1');
2769
+ * const aliceKey = alice.generateKeys();
2770
+ *
2771
+ * // Generate Bob's keys...
2772
+ * const bob = createECDH('secp521r1');
2773
+ * const bobKey = bob.generateKeys();
2774
+ *
2775
+ * // Exchange and generate the secret...
2776
+ * const aliceSecret = alice.computeSecret(bobKey);
2777
+ * const bobSecret = bob.computeSecret(aliceKey);
2778
+ *
2779
+ * assert.strictEqual(aliceSecret.toString('hex'), bobSecret.toString('hex'));
2780
+ * // OK
2781
+ * ```
2782
+ * @since v0.11.14
2783
+ */
578
2784
  class ECDH {
579
2785
  private constructor();
2786
+ /**
2787
+ * Converts the EC Diffie-Hellman public key specified by `key` and `curve` to the
2788
+ * format specified by `format`. The `format` argument specifies point encoding
2789
+ * and can be `'compressed'`, `'uncompressed'` or `'hybrid'`. The supplied key is
2790
+ * interpreted using the specified `inputEncoding`, and the returned key is encoded
2791
+ * using the specified `outputEncoding`.
2792
+ *
2793
+ * Use {@link getCurves} to obtain a list of available curve names.
2794
+ * On recent OpenSSL releases, `openssl ecparam -list_curves` will also display
2795
+ * the name and description of each available elliptic curve.
2796
+ *
2797
+ * If `format` is not specified the point will be returned in `'uncompressed'`format.
2798
+ *
2799
+ * If the `inputEncoding` is not provided, `key` is expected to be a `Buffer`,`TypedArray`, or `DataView`.
2800
+ *
2801
+ * Example (uncompressing a key):
2802
+ *
2803
+ * ```js
2804
+ * const {
2805
+ * createECDH,
2806
+ * ECDH,
2807
+ * } = await import('crypto');
2808
+ *
2809
+ * const ecdh = createECDH('secp256k1');
2810
+ * ecdh.generateKeys();
2811
+ *
2812
+ * const compressedKey = ecdh.getPublicKey('hex', 'compressed');
2813
+ *
2814
+ * const uncompressedKey = ECDH.convertKey(compressedKey,
2815
+ * 'secp256k1',
2816
+ * 'hex',
2817
+ * 'hex',
2818
+ * 'uncompressed');
2819
+ *
2820
+ * // The converted key and the uncompressed public key should be the same
2821
+ * console.log(uncompressedKey === ecdh.getPublicKey('hex'));
2822
+ * ```
2823
+ *
2824
+ * ```js
2825
+ * const {
2826
+ * createECDH,
2827
+ * ECDH,
2828
+ * } = require('crypto');
2829
+ *
2830
+ * const ecdh = createECDH('secp256k1');
2831
+ * ecdh.generateKeys();
2832
+ *
2833
+ * const compressedKey = ecdh.getPublicKey('hex', 'compressed');
2834
+ *
2835
+ * const uncompressedKey = ECDH.convertKey(compressedKey,
2836
+ * 'secp256k1',
2837
+ * 'hex',
2838
+ * 'hex',
2839
+ * 'uncompressed');
2840
+ *
2841
+ * // The converted key and the uncompressed public key should be the same
2842
+ * console.log(uncompressedKey === ecdh.getPublicKey('hex'));
2843
+ * ```
2844
+ * @since v10.0.0
2845
+ * @param inputEncoding The `encoding` of the `key` string.
2846
+ * @param outputEncoding The `encoding` of the return value.
2847
+ */
580
2848
  static convertKey(
581
2849
  key: BinaryLike,
582
2850
  curve: string,
583
2851
  inputEncoding?: BinaryToTextEncoding,
584
2852
  outputEncoding?: 'latin1' | 'hex' | 'base64',
585
- format?: 'uncompressed' | 'compressed' | 'hybrid',
2853
+ format?: 'uncompressed' | 'compressed' | 'hybrid'
586
2854
  ): Buffer | string;
2855
+ /**
2856
+ * Generates private and public EC Diffie-Hellman key values, and returns
2857
+ * the public key in the specified `format` and `encoding`. This key should be
2858
+ * transferred to the other party.
2859
+ *
2860
+ * The `format` argument specifies point encoding and can be `'compressed'` or`'uncompressed'`. If `format` is not specified, the point will be returned in`'uncompressed'` format.
2861
+ *
2862
+ * If `encoding` is provided a string is returned; otherwise a `Buffer` is returned.
2863
+ * @since v0.11.14
2864
+ * @param encoding The `encoding` of the return value.
2865
+ */
587
2866
  generateKeys(): Buffer;
588
2867
  generateKeys(encoding: BinaryToTextEncoding, format?: ECDHKeyFormat): string;
589
- computeSecret(other_public_key: NodeJS.ArrayBufferView): Buffer;
590
- computeSecret(other_public_key: string, input_encoding: BinaryToTextEncoding): Buffer;
591
- computeSecret(other_public_key: NodeJS.ArrayBufferView, output_encoding: BinaryToTextEncoding): string;
592
- computeSecret(
593
- other_public_key: string,
594
- input_encoding: BinaryToTextEncoding,
595
- output_encoding: BinaryToTextEncoding,
596
- ): string;
2868
+ /**
2869
+ * Computes the shared secret using `otherPublicKey` as the other
2870
+ * party's public key and returns the computed shared secret. The supplied
2871
+ * key is interpreted using specified `inputEncoding`, and the returned secret
2872
+ * is encoded using the specified `outputEncoding`.
2873
+ * If the `inputEncoding` is not
2874
+ * provided, `otherPublicKey` is expected to be a `Buffer`, `TypedArray`, or`DataView`.
2875
+ *
2876
+ * If `outputEncoding` is given a string will be returned; otherwise a `Buffer` is returned.
2877
+ *
2878
+ * `ecdh.computeSecret` will throw an`ERR_CRYPTO_ECDH_INVALID_PUBLIC_KEY` error when `otherPublicKey`lies outside of the elliptic curve. Since `otherPublicKey` is
2879
+ * usually supplied from a remote user over an insecure network,
2880
+ * be sure to handle this exception accordingly.
2881
+ * @since v0.11.14
2882
+ * @param inputEncoding The `encoding` of the `otherPublicKey` string.
2883
+ * @param outputEncoding The `encoding` of the return value.
2884
+ */
2885
+ computeSecret(otherPublicKey: NodeJS.ArrayBufferView): Buffer;
2886
+ computeSecret(otherPublicKey: string, inputEncoding: BinaryToTextEncoding): Buffer;
2887
+ computeSecret(otherPublicKey: NodeJS.ArrayBufferView, outputEncoding: BinaryToTextEncoding): string;
2888
+ computeSecret(otherPublicKey: string, inputEncoding: BinaryToTextEncoding, outputEncoding: BinaryToTextEncoding): string;
2889
+ /**
2890
+ * If `encoding` is specified, a string is returned; otherwise a `Buffer` is
2891
+ * returned.
2892
+ * @since v0.11.14
2893
+ * @param encoding The `encoding` of the return value.
2894
+ * @return The EC Diffie-Hellman in the specified `encoding`.
2895
+ */
597
2896
  getPrivateKey(): Buffer;
598
2897
  getPrivateKey(encoding: BinaryToTextEncoding): string;
2898
+ /**
2899
+ * The `format` argument specifies point encoding and can be `'compressed'` or`'uncompressed'`. If `format` is not specified the point will be returned in`'uncompressed'` format.
2900
+ *
2901
+ * If `encoding` is specified, a string is returned; otherwise a `Buffer` is
2902
+ * returned.
2903
+ * @since v0.11.14
2904
+ * @param encoding The `encoding` of the return value.
2905
+ * @return The EC Diffie-Hellman public key in the specified `encoding` and `format`.
2906
+ */
599
2907
  getPublicKey(): Buffer;
600
2908
  getPublicKey(encoding: BinaryToTextEncoding, format?: ECDHKeyFormat): string;
601
- setPrivateKey(private_key: NodeJS.ArrayBufferView): void;
602
- setPrivateKey(private_key: string, encoding: BinaryToTextEncoding): void;
2909
+ /**
2910
+ * Sets the EC Diffie-Hellman private key.
2911
+ * If `encoding` is provided, `privateKey` is expected
2912
+ * to be a string; otherwise `privateKey` is expected to be a `Buffer`,`TypedArray`, or `DataView`.
2913
+ *
2914
+ * If `privateKey` is not valid for the curve specified when the `ECDH` object was
2915
+ * created, an error is thrown. Upon setting the private key, the associated
2916
+ * public point (key) is also generated and set in the `ECDH` object.
2917
+ * @since v0.11.14
2918
+ * @param encoding The `encoding` of the `privateKey` string.
2919
+ */
2920
+ setPrivateKey(privateKey: NodeJS.ArrayBufferView): void;
2921
+ setPrivateKey(privateKey: string, encoding: BinaryToTextEncoding): void;
603
2922
  }
604
- function createECDH(curve_name: string): ECDH;
2923
+ /**
2924
+ * Creates an Elliptic Curve Diffie-Hellman (`ECDH`) key exchange object using a
2925
+ * predefined curve specified by the `curveName` string. Use {@link getCurves} to obtain a list of available curve names. On recent
2926
+ * OpenSSL releases, `openssl ecparam -list_curves` will also display the name
2927
+ * and description of each available elliptic curve.
2928
+ * @since v0.11.14
2929
+ */
2930
+ function createECDH(curveName: string): ECDH;
2931
+ /**
2932
+ * This function is based on a constant-time algorithm.
2933
+ * Returns true if `a` is equal to `b`, without leaking timing information that
2934
+ * would allow an attacker to guess one of the values. This is suitable for
2935
+ * comparing HMAC digests or secret values like authentication cookies or[capability urls](https://www.w3.org/TR/capability-urls/).
2936
+ *
2937
+ * `a` and `b` must both be `Buffer`s, `TypedArray`s, or `DataView`s, and they
2938
+ * must have the same byte length.
2939
+ *
2940
+ * If at least one of `a` and `b` is a `TypedArray` with more than one byte per
2941
+ * entry, such as `Uint16Array`, the result will be computed using the platform
2942
+ * byte order.
2943
+ *
2944
+ * Use of `crypto.timingSafeEqual` does not guarantee that the _surrounding_ code
2945
+ * is timing-safe. Care should be taken to ensure that the surrounding code does
2946
+ * not introduce timing vulnerabilities.
2947
+ * @since v6.6.0
2948
+ */
605
2949
  function timingSafeEqual(a: NodeJS.ArrayBufferView, b: NodeJS.ArrayBufferView): boolean;
606
2950
  /** @deprecated since v10.0.0 */
607
2951
  const DEFAULT_ENCODING: BufferEncoding;
608
-
609
2952
  type KeyType = 'rsa' | 'dsa' | 'ec' | 'ed25519' | 'ed448' | 'x25519' | 'x448';
610
2953
  type KeyFormat = 'pem' | 'der';
611
-
612
2954
  interface BasePrivateKeyEncodingOptions<T extends KeyFormat> {
613
2955
  format: T;
614
2956
  cipher?: string | undefined;
615
2957
  passphrase?: string | undefined;
616
2958
  }
617
-
618
2959
  interface KeyPairKeyObjectResult {
619
2960
  publicKey: KeyObject;
620
2961
  privateKey: KeyObject;
621
2962
  }
622
-
623
- interface ED25519KeyPairKeyObjectOptions {
624
- /**
625
- * No options.
626
- */
627
- }
628
-
629
- interface ED448KeyPairKeyObjectOptions {
630
- /**
631
- * No options.
632
- */
633
- }
634
-
635
- interface X25519KeyPairKeyObjectOptions {
636
- /**
637
- * No options.
638
- */
639
- }
640
-
641
- interface X448KeyPairKeyObjectOptions {
642
- /**
643
- * No options.
644
- */
645
- }
646
-
2963
+ interface ED25519KeyPairKeyObjectOptions {}
2964
+ interface ED448KeyPairKeyObjectOptions {}
2965
+ interface X25519KeyPairKeyObjectOptions {}
2966
+ interface X448KeyPairKeyObjectOptions {}
647
2967
  interface ECKeyPairKeyObjectOptions {
648
2968
  /**
649
2969
  * Name of the curve to use.
650
2970
  */
651
2971
  namedCurve: string;
652
2972
  }
653
-
654
2973
  interface RSAKeyPairKeyObjectOptions {
655
2974
  /**
656
2975
  * Key size in bits
657
2976
  */
658
2977
  modulusLength: number;
659
-
660
2978
  /**
661
2979
  * @default 0x10001
662
2980
  */
663
2981
  publicExponent?: number | undefined;
664
2982
  }
665
-
666
2983
  interface DSAKeyPairKeyObjectOptions {
667
2984
  /**
668
2985
  * Key size in bits
669
2986
  */
670
2987
  modulusLength: number;
671
-
672
2988
  /**
673
2989
  * Size of q in bits
674
2990
  */
675
2991
  divisorLength: number;
676
2992
  }
677
-
678
2993
  interface RSAKeyPairOptions<PubF extends KeyFormat, PrivF extends KeyFormat> {
679
2994
  /**
680
2995
  * Key size in bits
@@ -684,7 +2999,6 @@ declare module 'crypto' {
684
2999
  * @default 0x10001
685
3000
  */
686
3001
  publicExponent?: number | undefined;
687
-
688
3002
  publicKeyEncoding: {
689
3003
  type: 'pkcs1' | 'spki';
690
3004
  format: PubF;
@@ -693,7 +3007,6 @@ declare module 'crypto' {
693
3007
  type: 'pkcs1' | 'pkcs8';
694
3008
  };
695
3009
  }
696
-
697
3010
  interface DSAKeyPairOptions<PubF extends KeyFormat, PrivF extends KeyFormat> {
698
3011
  /**
699
3012
  * Key size in bits
@@ -703,7 +3016,6 @@ declare module 'crypto' {
703
3016
  * Size of q in bits
704
3017
  */
705
3018
  divisorLength: number;
706
-
707
3019
  publicKeyEncoding: {
708
3020
  type: 'spki';
709
3021
  format: PubF;
@@ -712,13 +3024,11 @@ declare module 'crypto' {
712
3024
  type: 'pkcs8';
713
3025
  };
714
3026
  }
715
-
716
3027
  interface ECKeyPairOptions<PubF extends KeyFormat, PrivF extends KeyFormat> {
717
3028
  /**
718
3029
  * Name of the curve to use.
719
3030
  */
720
3031
  namedCurve: string;
721
-
722
3032
  publicKeyEncoding: {
723
3033
  type: 'pkcs1' | 'spki';
724
3034
  format: PubF;
@@ -727,7 +3037,6 @@ declare module 'crypto' {
727
3037
  type: 'sec1' | 'pkcs8';
728
3038
  };
729
3039
  }
730
-
731
3040
  interface ED25519KeyPairOptions<PubF extends KeyFormat, PrivF extends KeyFormat> {
732
3041
  publicKeyEncoding: {
733
3042
  type: 'spki';
@@ -737,7 +3046,6 @@ declare module 'crypto' {
737
3046
  type: 'pkcs8';
738
3047
  };
739
3048
  }
740
-
741
3049
  interface ED448KeyPairOptions<PubF extends KeyFormat, PrivF extends KeyFormat> {
742
3050
  publicKeyEncoding: {
743
3051
  type: 'spki';
@@ -747,7 +3055,6 @@ declare module 'crypto' {
747
3055
  type: 'pkcs8';
748
3056
  };
749
3057
  }
750
-
751
3058
  interface X25519KeyPairOptions<PubF extends KeyFormat, PrivF extends KeyFormat> {
752
3059
  publicKeyEncoding: {
753
3060
  type: 'spki';
@@ -757,7 +3064,6 @@ declare module 'crypto' {
757
3064
  type: 'pkcs8';
758
3065
  };
759
3066
  }
760
-
761
3067
  interface X448KeyPairOptions<PubF extends KeyFormat, PrivF extends KeyFormat> {
762
3068
  publicKeyEncoding: {
763
3069
  type: 'spki';
@@ -767,488 +3073,446 @@ declare module 'crypto' {
767
3073
  type: 'pkcs8';
768
3074
  };
769
3075
  }
770
-
771
3076
  interface KeyPairSyncResult<T1 extends string | Buffer, T2 extends string | Buffer> {
772
3077
  publicKey: T1;
773
3078
  privateKey: T2;
774
3079
  }
775
-
776
- function generateKeyPairSync(
777
- type: 'rsa',
778
- options: RSAKeyPairOptions<'pem', 'pem'>,
779
- ): KeyPairSyncResult<string, string>;
780
- function generateKeyPairSync(
781
- type: 'rsa',
782
- options: RSAKeyPairOptions<'pem', 'der'>,
783
- ): KeyPairSyncResult<string, Buffer>;
784
- function generateKeyPairSync(
785
- type: 'rsa',
786
- options: RSAKeyPairOptions<'der', 'pem'>,
787
- ): KeyPairSyncResult<Buffer, string>;
788
- function generateKeyPairSync(
789
- type: 'rsa',
790
- options: RSAKeyPairOptions<'der', 'der'>,
791
- ): KeyPairSyncResult<Buffer, Buffer>;
3080
+ /**
3081
+ * Generates a new asymmetric key pair of the given `type`. RSA, DSA, EC, Ed25519,
3082
+ * Ed448, X25519, X448, and DH are currently supported.
3083
+ *
3084
+ * If a `publicKeyEncoding` or `privateKeyEncoding` was specified, this function
3085
+ * behaves as if `keyObject.export()` had been called on its result. Otherwise,
3086
+ * the respective part of the key is returned as a `KeyObject`.
3087
+ *
3088
+ * When encoding public keys, it is recommended to use `'spki'`. When encoding
3089
+ * private keys, it is recommended to use `'pkcs8'` with a strong passphrase,
3090
+ * and to keep the passphrase confidential.
3091
+ *
3092
+ * ```js
3093
+ * const {
3094
+ * generateKeyPairSync,
3095
+ * } = await import('crypto');
3096
+ *
3097
+ * const {
3098
+ * publicKey,
3099
+ * privateKey,
3100
+ * } = generateKeyPairSync('rsa', {
3101
+ * modulusLength: 4096,
3102
+ * publicKeyEncoding: {
3103
+ * type: 'spki',
3104
+ * format: 'pem'
3105
+ * },
3106
+ * privateKeyEncoding: {
3107
+ * type: 'pkcs8',
3108
+ * format: 'pem',
3109
+ * cipher: 'aes-256-cbc',
3110
+ * passphrase: 'top secret'
3111
+ * }
3112
+ * });
3113
+ * ```
3114
+ *
3115
+ * ```js
3116
+ * const {
3117
+ * generateKeyPairSync,
3118
+ * } = require('crypto');
3119
+ *
3120
+ * const {
3121
+ * publicKey,
3122
+ * privateKey,
3123
+ * } = generateKeyPairSync('rsa', {
3124
+ * modulusLength: 4096,
3125
+ * publicKeyEncoding: {
3126
+ * type: 'spki',
3127
+ * format: 'pem'
3128
+ * },
3129
+ * privateKeyEncoding: {
3130
+ * type: 'pkcs8',
3131
+ * format: 'pem',
3132
+ * cipher: 'aes-256-cbc',
3133
+ * passphrase: 'top secret'
3134
+ * }
3135
+ * });
3136
+ * ```
3137
+ *
3138
+ * The return value `{ publicKey, privateKey }` represents the generated key pair.
3139
+ * When PEM encoding was selected, the respective key will be a string, otherwise
3140
+ * it will be a buffer containing the data encoded as DER.
3141
+ * @since v10.12.0
3142
+ * @param type Must be `'rsa'`, `'dsa'`, `'ec'`, `'ed25519'`, `'ed448'`, `'x25519'`, `'x448'`, or `'dh'`.
3143
+ */
3144
+ function generateKeyPairSync(type: 'rsa', options: RSAKeyPairOptions<'pem', 'pem'>): KeyPairSyncResult<string, string>;
3145
+ function generateKeyPairSync(type: 'rsa', options: RSAKeyPairOptions<'pem', 'der'>): KeyPairSyncResult<string, Buffer>;
3146
+ function generateKeyPairSync(type: 'rsa', options: RSAKeyPairOptions<'der', 'pem'>): KeyPairSyncResult<Buffer, string>;
3147
+ function generateKeyPairSync(type: 'rsa', options: RSAKeyPairOptions<'der', 'der'>): KeyPairSyncResult<Buffer, Buffer>;
792
3148
  function generateKeyPairSync(type: 'rsa', options: RSAKeyPairKeyObjectOptions): KeyPairKeyObjectResult;
793
-
794
- function generateKeyPairSync(
795
- type: 'dsa',
796
- options: DSAKeyPairOptions<'pem', 'pem'>,
797
- ): KeyPairSyncResult<string, string>;
798
- function generateKeyPairSync(
799
- type: 'dsa',
800
- options: DSAKeyPairOptions<'pem', 'der'>,
801
- ): KeyPairSyncResult<string, Buffer>;
802
- function generateKeyPairSync(
803
- type: 'dsa',
804
- options: DSAKeyPairOptions<'der', 'pem'>,
805
- ): KeyPairSyncResult<Buffer, string>;
806
- function generateKeyPairSync(
807
- type: 'dsa',
808
- options: DSAKeyPairOptions<'der', 'der'>,
809
- ): KeyPairSyncResult<Buffer, Buffer>;
3149
+ function generateKeyPairSync(type: 'dsa', options: DSAKeyPairOptions<'pem', 'pem'>): KeyPairSyncResult<string, string>;
3150
+ function generateKeyPairSync(type: 'dsa', options: DSAKeyPairOptions<'pem', 'der'>): KeyPairSyncResult<string, Buffer>;
3151
+ function generateKeyPairSync(type: 'dsa', options: DSAKeyPairOptions<'der', 'pem'>): KeyPairSyncResult<Buffer, string>;
3152
+ function generateKeyPairSync(type: 'dsa', options: DSAKeyPairOptions<'der', 'der'>): KeyPairSyncResult<Buffer, Buffer>;
810
3153
  function generateKeyPairSync(type: 'dsa', options: DSAKeyPairKeyObjectOptions): KeyPairKeyObjectResult;
811
-
812
- function generateKeyPairSync(
813
- type: 'ec',
814
- options: ECKeyPairOptions<'pem', 'pem'>,
815
- ): KeyPairSyncResult<string, string>;
816
- function generateKeyPairSync(
817
- type: 'ec',
818
- options: ECKeyPairOptions<'pem', 'der'>,
819
- ): KeyPairSyncResult<string, Buffer>;
820
- function generateKeyPairSync(
821
- type: 'ec',
822
- options: ECKeyPairOptions<'der', 'pem'>,
823
- ): KeyPairSyncResult<Buffer, string>;
824
- function generateKeyPairSync(
825
- type: 'ec',
826
- options: ECKeyPairOptions<'der', 'der'>,
827
- ): KeyPairSyncResult<Buffer, Buffer>;
3154
+ function generateKeyPairSync(type: 'ec', options: ECKeyPairOptions<'pem', 'pem'>): KeyPairSyncResult<string, string>;
3155
+ function generateKeyPairSync(type: 'ec', options: ECKeyPairOptions<'pem', 'der'>): KeyPairSyncResult<string, Buffer>;
3156
+ function generateKeyPairSync(type: 'ec', options: ECKeyPairOptions<'der', 'pem'>): KeyPairSyncResult<Buffer, string>;
3157
+ function generateKeyPairSync(type: 'ec', options: ECKeyPairOptions<'der', 'der'>): KeyPairSyncResult<Buffer, Buffer>;
828
3158
  function generateKeyPairSync(type: 'ec', options: ECKeyPairKeyObjectOptions): KeyPairKeyObjectResult;
829
-
830
- function generateKeyPairSync(
831
- type: 'ed25519',
832
- options: ED25519KeyPairOptions<'pem', 'pem'>,
833
- ): KeyPairSyncResult<string, string>;
834
- function generateKeyPairSync(
835
- type: 'ed25519',
836
- options: ED25519KeyPairOptions<'pem', 'der'>,
837
- ): KeyPairSyncResult<string, Buffer>;
838
- function generateKeyPairSync(
839
- type: 'ed25519',
840
- options: ED25519KeyPairOptions<'der', 'pem'>,
841
- ): KeyPairSyncResult<Buffer, string>;
842
- function generateKeyPairSync(
843
- type: 'ed25519',
844
- options: ED25519KeyPairOptions<'der', 'der'>,
845
- ): KeyPairSyncResult<Buffer, Buffer>;
3159
+ function generateKeyPairSync(type: 'ed25519', options: ED25519KeyPairOptions<'pem', 'pem'>): KeyPairSyncResult<string, string>;
3160
+ function generateKeyPairSync(type: 'ed25519', options: ED25519KeyPairOptions<'pem', 'der'>): KeyPairSyncResult<string, Buffer>;
3161
+ function generateKeyPairSync(type: 'ed25519', options: ED25519KeyPairOptions<'der', 'pem'>): KeyPairSyncResult<Buffer, string>;
3162
+ function generateKeyPairSync(type: 'ed25519', options: ED25519KeyPairOptions<'der', 'der'>): KeyPairSyncResult<Buffer, Buffer>;
846
3163
  function generateKeyPairSync(type: 'ed25519', options?: ED25519KeyPairKeyObjectOptions): KeyPairKeyObjectResult;
847
-
848
- function generateKeyPairSync(
849
- type: 'ed448',
850
- options: ED448KeyPairOptions<'pem', 'pem'>,
851
- ): KeyPairSyncResult<string, string>;
852
- function generateKeyPairSync(
853
- type: 'ed448',
854
- options: ED448KeyPairOptions<'pem', 'der'>,
855
- ): KeyPairSyncResult<string, Buffer>;
856
- function generateKeyPairSync(
857
- type: 'ed448',
858
- options: ED448KeyPairOptions<'der', 'pem'>,
859
- ): KeyPairSyncResult<Buffer, string>;
860
- function generateKeyPairSync(
861
- type: 'ed448',
862
- options: ED448KeyPairOptions<'der', 'der'>,
863
- ): KeyPairSyncResult<Buffer, Buffer>;
3164
+ function generateKeyPairSync(type: 'ed448', options: ED448KeyPairOptions<'pem', 'pem'>): KeyPairSyncResult<string, string>;
3165
+ function generateKeyPairSync(type: 'ed448', options: ED448KeyPairOptions<'pem', 'der'>): KeyPairSyncResult<string, Buffer>;
3166
+ function generateKeyPairSync(type: 'ed448', options: ED448KeyPairOptions<'der', 'pem'>): KeyPairSyncResult<Buffer, string>;
3167
+ function generateKeyPairSync(type: 'ed448', options: ED448KeyPairOptions<'der', 'der'>): KeyPairSyncResult<Buffer, Buffer>;
864
3168
  function generateKeyPairSync(type: 'ed448', options?: ED448KeyPairKeyObjectOptions): KeyPairKeyObjectResult;
865
-
866
- function generateKeyPairSync(
867
- type: 'x25519',
868
- options: X25519KeyPairOptions<'pem', 'pem'>,
869
- ): KeyPairSyncResult<string, string>;
870
- function generateKeyPairSync(
871
- type: 'x25519',
872
- options: X25519KeyPairOptions<'pem', 'der'>,
873
- ): KeyPairSyncResult<string, Buffer>;
874
- function generateKeyPairSync(
875
- type: 'x25519',
876
- options: X25519KeyPairOptions<'der', 'pem'>,
877
- ): KeyPairSyncResult<Buffer, string>;
878
- function generateKeyPairSync(
879
- type: 'x25519',
880
- options: X25519KeyPairOptions<'der', 'der'>,
881
- ): KeyPairSyncResult<Buffer, Buffer>;
3169
+ function generateKeyPairSync(type: 'x25519', options: X25519KeyPairOptions<'pem', 'pem'>): KeyPairSyncResult<string, string>;
3170
+ function generateKeyPairSync(type: 'x25519', options: X25519KeyPairOptions<'pem', 'der'>): KeyPairSyncResult<string, Buffer>;
3171
+ function generateKeyPairSync(type: 'x25519', options: X25519KeyPairOptions<'der', 'pem'>): KeyPairSyncResult<Buffer, string>;
3172
+ function generateKeyPairSync(type: 'x25519', options: X25519KeyPairOptions<'der', 'der'>): KeyPairSyncResult<Buffer, Buffer>;
882
3173
  function generateKeyPairSync(type: 'x25519', options?: X25519KeyPairKeyObjectOptions): KeyPairKeyObjectResult;
883
-
884
- function generateKeyPairSync(
885
- type: 'x448',
886
- options: X448KeyPairOptions<'pem', 'pem'>,
887
- ): KeyPairSyncResult<string, string>;
888
- function generateKeyPairSync(
889
- type: 'x448',
890
- options: X448KeyPairOptions<'pem', 'der'>,
891
- ): KeyPairSyncResult<string, Buffer>;
892
- function generateKeyPairSync(
893
- type: 'x448',
894
- options: X448KeyPairOptions<'der', 'pem'>,
895
- ): KeyPairSyncResult<Buffer, string>;
896
- function generateKeyPairSync(
897
- type: 'x448',
898
- options: X448KeyPairOptions<'der', 'der'>,
899
- ): KeyPairSyncResult<Buffer, Buffer>;
3174
+ function generateKeyPairSync(type: 'x448', options: X448KeyPairOptions<'pem', 'pem'>): KeyPairSyncResult<string, string>;
3175
+ function generateKeyPairSync(type: 'x448', options: X448KeyPairOptions<'pem', 'der'>): KeyPairSyncResult<string, Buffer>;
3176
+ function generateKeyPairSync(type: 'x448', options: X448KeyPairOptions<'der', 'pem'>): KeyPairSyncResult<Buffer, string>;
3177
+ function generateKeyPairSync(type: 'x448', options: X448KeyPairOptions<'der', 'der'>): KeyPairSyncResult<Buffer, Buffer>;
900
3178
  function generateKeyPairSync(type: 'x448', options?: X448KeyPairKeyObjectOptions): KeyPairKeyObjectResult;
901
-
902
- function generateKeyPair(
903
- type: 'rsa',
904
- options: RSAKeyPairOptions<'pem', 'pem'>,
905
- callback: (err: Error | null, publicKey: string, privateKey: string) => void,
906
- ): void;
907
- function generateKeyPair(
908
- type: 'rsa',
909
- options: RSAKeyPairOptions<'pem', 'der'>,
910
- callback: (err: Error | null, publicKey: string, privateKey: Buffer) => void,
911
- ): void;
912
- function generateKeyPair(
913
- type: 'rsa',
914
- options: RSAKeyPairOptions<'der', 'pem'>,
915
- callback: (err: Error | null, publicKey: Buffer, privateKey: string) => void,
916
- ): void;
917
- function generateKeyPair(
918
- type: 'rsa',
919
- options: RSAKeyPairOptions<'der', 'der'>,
920
- callback: (err: Error | null, publicKey: Buffer, privateKey: Buffer) => void,
921
- ): void;
922
- function generateKeyPair(
923
- type: 'rsa',
924
- options: RSAKeyPairKeyObjectOptions,
925
- callback: (err: Error | null, publicKey: KeyObject, privateKey: KeyObject) => void,
926
- ): void;
927
-
928
- function generateKeyPair(
929
- type: 'dsa',
930
- options: DSAKeyPairOptions<'pem', 'pem'>,
931
- callback: (err: Error | null, publicKey: string, privateKey: string) => void,
932
- ): void;
933
- function generateKeyPair(
934
- type: 'dsa',
935
- options: DSAKeyPairOptions<'pem', 'der'>,
936
- callback: (err: Error | null, publicKey: string, privateKey: Buffer) => void,
937
- ): void;
938
- function generateKeyPair(
939
- type: 'dsa',
940
- options: DSAKeyPairOptions<'der', 'pem'>,
941
- callback: (err: Error | null, publicKey: Buffer, privateKey: string) => void,
942
- ): void;
943
- function generateKeyPair(
944
- type: 'dsa',
945
- options: DSAKeyPairOptions<'der', 'der'>,
946
- callback: (err: Error | null, publicKey: Buffer, privateKey: Buffer) => void,
947
- ): void;
948
- function generateKeyPair(
949
- type: 'dsa',
950
- options: DSAKeyPairKeyObjectOptions,
951
- callback: (err: Error | null, publicKey: KeyObject, privateKey: KeyObject) => void,
952
- ): void;
953
-
954
- function generateKeyPair(
955
- type: 'ec',
956
- options: ECKeyPairOptions<'pem', 'pem'>,
957
- callback: (err: Error | null, publicKey: string, privateKey: string) => void,
958
- ): void;
959
- function generateKeyPair(
960
- type: 'ec',
961
- options: ECKeyPairOptions<'pem', 'der'>,
962
- callback: (err: Error | null, publicKey: string, privateKey: Buffer) => void,
963
- ): void;
964
- function generateKeyPair(
965
- type: 'ec',
966
- options: ECKeyPairOptions<'der', 'pem'>,
967
- callback: (err: Error | null, publicKey: Buffer, privateKey: string) => void,
968
- ): void;
969
- function generateKeyPair(
970
- type: 'ec',
971
- options: ECKeyPairOptions<'der', 'der'>,
972
- callback: (err: Error | null, publicKey: Buffer, privateKey: Buffer) => void,
973
- ): void;
974
- function generateKeyPair(
975
- type: 'ec',
976
- options: ECKeyPairKeyObjectOptions,
977
- callback: (err: Error | null, publicKey: KeyObject, privateKey: KeyObject) => void,
978
- ): void;
979
-
980
- function generateKeyPair(
981
- type: 'ed25519',
982
- options: ED25519KeyPairOptions<'pem', 'pem'>,
983
- callback: (err: Error | null, publicKey: string, privateKey: string) => void,
984
- ): void;
985
- function generateKeyPair(
986
- type: 'ed25519',
987
- options: ED25519KeyPairOptions<'pem', 'der'>,
988
- callback: (err: Error | null, publicKey: string, privateKey: Buffer) => void,
989
- ): void;
990
- function generateKeyPair(
991
- type: 'ed25519',
992
- options: ED25519KeyPairOptions<'der', 'pem'>,
993
- callback: (err: Error | null, publicKey: Buffer, privateKey: string) => void,
994
- ): void;
995
- function generateKeyPair(
996
- type: 'ed25519',
997
- options: ED25519KeyPairOptions<'der', 'der'>,
998
- callback: (err: Error | null, publicKey: Buffer, privateKey: Buffer) => void,
999
- ): void;
1000
- function generateKeyPair(
1001
- type: 'ed25519',
1002
- options: ED25519KeyPairKeyObjectOptions | undefined,
1003
- callback: (err: Error | null, publicKey: KeyObject, privateKey: KeyObject) => void,
1004
- ): void;
1005
-
1006
- function generateKeyPair(
1007
- type: 'ed448',
1008
- options: ED448KeyPairOptions<'pem', 'pem'>,
1009
- callback: (err: Error | null, publicKey: string, privateKey: string) => void,
1010
- ): void;
1011
- function generateKeyPair(
1012
- type: 'ed448',
1013
- options: ED448KeyPairOptions<'pem', 'der'>,
1014
- callback: (err: Error | null, publicKey: string, privateKey: Buffer) => void,
1015
- ): void;
1016
- function generateKeyPair(
1017
- type: 'ed448',
1018
- options: ED448KeyPairOptions<'der', 'pem'>,
1019
- callback: (err: Error | null, publicKey: Buffer, privateKey: string) => void,
1020
- ): void;
1021
- function generateKeyPair(
1022
- type: 'ed448',
1023
- options: ED448KeyPairOptions<'der', 'der'>,
1024
- callback: (err: Error | null, publicKey: Buffer, privateKey: Buffer) => void,
1025
- ): void;
1026
- function generateKeyPair(
1027
- type: 'ed448',
1028
- options: ED448KeyPairKeyObjectOptions | undefined,
1029
- callback: (err: Error | null, publicKey: KeyObject, privateKey: KeyObject) => void,
1030
- ): void;
1031
-
1032
- function generateKeyPair(
1033
- type: 'x25519',
1034
- options: X25519KeyPairOptions<'pem', 'pem'>,
1035
- callback: (err: Error | null, publicKey: string, privateKey: string) => void,
1036
- ): void;
1037
- function generateKeyPair(
1038
- type: 'x25519',
1039
- options: X25519KeyPairOptions<'pem', 'der'>,
1040
- callback: (err: Error | null, publicKey: string, privateKey: Buffer) => void,
1041
- ): void;
1042
- function generateKeyPair(
1043
- type: 'x25519',
1044
- options: X25519KeyPairOptions<'der', 'pem'>,
1045
- callback: (err: Error | null, publicKey: Buffer, privateKey: string) => void,
1046
- ): void;
1047
- function generateKeyPair(
1048
- type: 'x25519',
1049
- options: X25519KeyPairOptions<'der', 'der'>,
1050
- callback: (err: Error | null, publicKey: Buffer, privateKey: Buffer) => void,
1051
- ): void;
1052
- function generateKeyPair(
1053
- type: 'x25519',
1054
- options: X25519KeyPairKeyObjectOptions | undefined,
1055
- callback: (err: Error | null, publicKey: KeyObject, privateKey: KeyObject) => void,
1056
- ): void;
1057
-
1058
- function generateKeyPair(
1059
- type: 'x448',
1060
- options: X448KeyPairOptions<'pem', 'pem'>,
1061
- callback: (err: Error | null, publicKey: string, privateKey: string) => void,
1062
- ): void;
1063
- function generateKeyPair(
1064
- type: 'x448',
1065
- options: X448KeyPairOptions<'pem', 'der'>,
1066
- callback: (err: Error | null, publicKey: string, privateKey: Buffer) => void,
1067
- ): void;
1068
- function generateKeyPair(
1069
- type: 'x448',
1070
- options: X448KeyPairOptions<'der', 'pem'>,
1071
- callback: (err: Error | null, publicKey: Buffer, privateKey: string) => void,
1072
- ): void;
1073
- function generateKeyPair(
1074
- type: 'x448',
1075
- options: X448KeyPairOptions<'der', 'der'>,
1076
- callback: (err: Error | null, publicKey: Buffer, privateKey: Buffer) => void,
1077
- ): void;
1078
- function generateKeyPair(
1079
- type: 'x448',
1080
- options: X448KeyPairKeyObjectOptions | undefined,
1081
- callback: (err: Error | null, publicKey: KeyObject, privateKey: KeyObject) => void,
1082
- ): void;
1083
-
3179
+ /**
3180
+ * Generates a new asymmetric key pair of the given `type`. RSA, DSA, EC, Ed25519,
3181
+ * Ed448, X25519, X448, and DH are currently supported.
3182
+ *
3183
+ * If a `publicKeyEncoding` or `privateKeyEncoding` was specified, this function
3184
+ * behaves as if `keyObject.export()` had been called on its result. Otherwise,
3185
+ * the respective part of the key is returned as a `KeyObject`.
3186
+ *
3187
+ * It is recommended to encode public keys as `'spki'` and private keys as`'pkcs8'` with encryption for long-term storage:
3188
+ *
3189
+ * ```js
3190
+ * const {
3191
+ * generateKeyPair,
3192
+ * } = await import('crypto');
3193
+ *
3194
+ * generateKeyPair('rsa', {
3195
+ * modulusLength: 4096,
3196
+ * publicKeyEncoding: {
3197
+ * type: 'spki',
3198
+ * format: 'pem'
3199
+ * },
3200
+ * privateKeyEncoding: {
3201
+ * type: 'pkcs8',
3202
+ * format: 'pem',
3203
+ * cipher: 'aes-256-cbc',
3204
+ * passphrase: 'top secret'
3205
+ * }
3206
+ * }, (err, publicKey, privateKey) => {
3207
+ * // Handle errors and use the generated key pair.
3208
+ * });
3209
+ * ```
3210
+ *
3211
+ * ```js
3212
+ * const {
3213
+ * generateKeyPair,
3214
+ * } = require('crypto');
3215
+ *
3216
+ * generateKeyPair('rsa', {
3217
+ * modulusLength: 4096,
3218
+ * publicKeyEncoding: {
3219
+ * type: 'spki',
3220
+ * format: 'pem'
3221
+ * },
3222
+ * privateKeyEncoding: {
3223
+ * type: 'pkcs8',
3224
+ * format: 'pem',
3225
+ * cipher: 'aes-256-cbc',
3226
+ * passphrase: 'top secret'
3227
+ * }
3228
+ * }, (err, publicKey, privateKey) => {
3229
+ * // Handle errors and use the generated key pair.
3230
+ * });
3231
+ * ```
3232
+ *
3233
+ * On completion, `callback` will be called with `err` set to `undefined` and`publicKey` / `privateKey` representing the generated key pair.
3234
+ *
3235
+ * If this method is invoked as its `util.promisify()` ed version, it returns
3236
+ * a `Promise` for an `Object` with `publicKey` and `privateKey` properties.
3237
+ * @since v10.12.0
3238
+ * @param type Must be `'rsa'`, `'dsa'`, `'ec'`, `'ed25519'`, `'ed448'`, `'x25519'`, `'x448'`, or `'dh'`.
3239
+ */
3240
+ function generateKeyPair(type: 'rsa', options: RSAKeyPairOptions<'pem', 'pem'>, callback: (err: Error | null, publicKey: string, privateKey: string) => void): void;
3241
+ function generateKeyPair(type: 'rsa', options: RSAKeyPairOptions<'pem', 'der'>, callback: (err: Error | null, publicKey: string, privateKey: Buffer) => void): void;
3242
+ function generateKeyPair(type: 'rsa', options: RSAKeyPairOptions<'der', 'pem'>, callback: (err: Error | null, publicKey: Buffer, privateKey: string) => void): void;
3243
+ function generateKeyPair(type: 'rsa', options: RSAKeyPairOptions<'der', 'der'>, callback: (err: Error | null, publicKey: Buffer, privateKey: Buffer) => void): void;
3244
+ function generateKeyPair(type: 'rsa', options: RSAKeyPairKeyObjectOptions, callback: (err: Error | null, publicKey: KeyObject, privateKey: KeyObject) => void): void;
3245
+ function generateKeyPair(type: 'dsa', options: DSAKeyPairOptions<'pem', 'pem'>, callback: (err: Error | null, publicKey: string, privateKey: string) => void): void;
3246
+ function generateKeyPair(type: 'dsa', options: DSAKeyPairOptions<'pem', 'der'>, callback: (err: Error | null, publicKey: string, privateKey: Buffer) => void): void;
3247
+ function generateKeyPair(type: 'dsa', options: DSAKeyPairOptions<'der', 'pem'>, callback: (err: Error | null, publicKey: Buffer, privateKey: string) => void): void;
3248
+ function generateKeyPair(type: 'dsa', options: DSAKeyPairOptions<'der', 'der'>, callback: (err: Error | null, publicKey: Buffer, privateKey: Buffer) => void): void;
3249
+ function generateKeyPair(type: 'dsa', options: DSAKeyPairKeyObjectOptions, callback: (err: Error | null, publicKey: KeyObject, privateKey: KeyObject) => void): void;
3250
+ function generateKeyPair(type: 'ec', options: ECKeyPairOptions<'pem', 'pem'>, callback: (err: Error | null, publicKey: string, privateKey: string) => void): void;
3251
+ function generateKeyPair(type: 'ec', options: ECKeyPairOptions<'pem', 'der'>, callback: (err: Error | null, publicKey: string, privateKey: Buffer) => void): void;
3252
+ function generateKeyPair(type: 'ec', options: ECKeyPairOptions<'der', 'pem'>, callback: (err: Error | null, publicKey: Buffer, privateKey: string) => void): void;
3253
+ function generateKeyPair(type: 'ec', options: ECKeyPairOptions<'der', 'der'>, callback: (err: Error | null, publicKey: Buffer, privateKey: Buffer) => void): void;
3254
+ function generateKeyPair(type: 'ec', options: ECKeyPairKeyObjectOptions, callback: (err: Error | null, publicKey: KeyObject, privateKey: KeyObject) => void): void;
3255
+ function generateKeyPair(type: 'ed25519', options: ED25519KeyPairOptions<'pem', 'pem'>, callback: (err: Error | null, publicKey: string, privateKey: string) => void): void;
3256
+ function generateKeyPair(type: 'ed25519', options: ED25519KeyPairOptions<'pem', 'der'>, callback: (err: Error | null, publicKey: string, privateKey: Buffer) => void): void;
3257
+ function generateKeyPair(type: 'ed25519', options: ED25519KeyPairOptions<'der', 'pem'>, callback: (err: Error | null, publicKey: Buffer, privateKey: string) => void): void;
3258
+ function generateKeyPair(type: 'ed25519', options: ED25519KeyPairOptions<'der', 'der'>, callback: (err: Error | null, publicKey: Buffer, privateKey: Buffer) => void): void;
3259
+ function generateKeyPair(type: 'ed25519', options: ED25519KeyPairKeyObjectOptions | undefined, callback: (err: Error | null, publicKey: KeyObject, privateKey: KeyObject) => void): void;
3260
+ function generateKeyPair(type: 'ed448', options: ED448KeyPairOptions<'pem', 'pem'>, callback: (err: Error | null, publicKey: string, privateKey: string) => void): void;
3261
+ function generateKeyPair(type: 'ed448', options: ED448KeyPairOptions<'pem', 'der'>, callback: (err: Error | null, publicKey: string, privateKey: Buffer) => void): void;
3262
+ function generateKeyPair(type: 'ed448', options: ED448KeyPairOptions<'der', 'pem'>, callback: (err: Error | null, publicKey: Buffer, privateKey: string) => void): void;
3263
+ function generateKeyPair(type: 'ed448', options: ED448KeyPairOptions<'der', 'der'>, callback: (err: Error | null, publicKey: Buffer, privateKey: Buffer) => void): void;
3264
+ function generateKeyPair(type: 'ed448', options: ED448KeyPairKeyObjectOptions | undefined, callback: (err: Error | null, publicKey: KeyObject, privateKey: KeyObject) => void): void;
3265
+ function generateKeyPair(type: 'x25519', options: X25519KeyPairOptions<'pem', 'pem'>, callback: (err: Error | null, publicKey: string, privateKey: string) => void): void;
3266
+ function generateKeyPair(type: 'x25519', options: X25519KeyPairOptions<'pem', 'der'>, callback: (err: Error | null, publicKey: string, privateKey: Buffer) => void): void;
3267
+ function generateKeyPair(type: 'x25519', options: X25519KeyPairOptions<'der', 'pem'>, callback: (err: Error | null, publicKey: Buffer, privateKey: string) => void): void;
3268
+ function generateKeyPair(type: 'x25519', options: X25519KeyPairOptions<'der', 'der'>, callback: (err: Error | null, publicKey: Buffer, privateKey: Buffer) => void): void;
3269
+ function generateKeyPair(type: 'x25519', options: X25519KeyPairKeyObjectOptions | undefined, callback: (err: Error | null, publicKey: KeyObject, privateKey: KeyObject) => void): void;
3270
+ function generateKeyPair(type: 'x448', options: X448KeyPairOptions<'pem', 'pem'>, callback: (err: Error | null, publicKey: string, privateKey: string) => void): void;
3271
+ function generateKeyPair(type: 'x448', options: X448KeyPairOptions<'pem', 'der'>, callback: (err: Error | null, publicKey: string, privateKey: Buffer) => void): void;
3272
+ function generateKeyPair(type: 'x448', options: X448KeyPairOptions<'der', 'pem'>, callback: (err: Error | null, publicKey: Buffer, privateKey: string) => void): void;
3273
+ function generateKeyPair(type: 'x448', options: X448KeyPairOptions<'der', 'der'>, callback: (err: Error | null, publicKey: Buffer, privateKey: Buffer) => void): void;
3274
+ function generateKeyPair(type: 'x448', options: X448KeyPairKeyObjectOptions | undefined, callback: (err: Error | null, publicKey: KeyObject, privateKey: KeyObject) => void): void;
1084
3275
  namespace generateKeyPair {
1085
3276
  function __promisify__(
1086
3277
  type: 'rsa',
1087
- options: RSAKeyPairOptions<'pem', 'pem'>,
1088
- ): Promise<{ publicKey: string; privateKey: string }>;
3278
+ options: RSAKeyPairOptions<'pem', 'pem'>
3279
+ ): Promise<{
3280
+ publicKey: string;
3281
+ privateKey: string;
3282
+ }>;
1089
3283
  function __promisify__(
1090
3284
  type: 'rsa',
1091
- options: RSAKeyPairOptions<'pem', 'der'>,
1092
- ): Promise<{ publicKey: string; privateKey: Buffer }>;
3285
+ options: RSAKeyPairOptions<'pem', 'der'>
3286
+ ): Promise<{
3287
+ publicKey: string;
3288
+ privateKey: Buffer;
3289
+ }>;
1093
3290
  function __promisify__(
1094
3291
  type: 'rsa',
1095
- options: RSAKeyPairOptions<'der', 'pem'>,
1096
- ): Promise<{ publicKey: Buffer; privateKey: string }>;
3292
+ options: RSAKeyPairOptions<'der', 'pem'>
3293
+ ): Promise<{
3294
+ publicKey: Buffer;
3295
+ privateKey: string;
3296
+ }>;
1097
3297
  function __promisify__(
1098
3298
  type: 'rsa',
1099
- options: RSAKeyPairOptions<'der', 'der'>,
1100
- ): Promise<{ publicKey: Buffer; privateKey: Buffer }>;
3299
+ options: RSAKeyPairOptions<'der', 'der'>
3300
+ ): Promise<{
3301
+ publicKey: Buffer;
3302
+ privateKey: Buffer;
3303
+ }>;
1101
3304
  function __promisify__(type: 'rsa', options: RSAKeyPairKeyObjectOptions): Promise<KeyPairKeyObjectResult>;
1102
-
1103
3305
  function __promisify__(
1104
3306
  type: 'dsa',
1105
- options: DSAKeyPairOptions<'pem', 'pem'>,
1106
- ): Promise<{ publicKey: string; privateKey: string }>;
3307
+ options: DSAKeyPairOptions<'pem', 'pem'>
3308
+ ): Promise<{
3309
+ publicKey: string;
3310
+ privateKey: string;
3311
+ }>;
1107
3312
  function __promisify__(
1108
3313
  type: 'dsa',
1109
- options: DSAKeyPairOptions<'pem', 'der'>,
1110
- ): Promise<{ publicKey: string; privateKey: Buffer }>;
3314
+ options: DSAKeyPairOptions<'pem', 'der'>
3315
+ ): Promise<{
3316
+ publicKey: string;
3317
+ privateKey: Buffer;
3318
+ }>;
1111
3319
  function __promisify__(
1112
3320
  type: 'dsa',
1113
- options: DSAKeyPairOptions<'der', 'pem'>,
1114
- ): Promise<{ publicKey: Buffer; privateKey: string }>;
3321
+ options: DSAKeyPairOptions<'der', 'pem'>
3322
+ ): Promise<{
3323
+ publicKey: Buffer;
3324
+ privateKey: string;
3325
+ }>;
1115
3326
  function __promisify__(
1116
3327
  type: 'dsa',
1117
- options: DSAKeyPairOptions<'der', 'der'>,
1118
- ): Promise<{ publicKey: Buffer; privateKey: Buffer }>;
3328
+ options: DSAKeyPairOptions<'der', 'der'>
3329
+ ): Promise<{
3330
+ publicKey: Buffer;
3331
+ privateKey: Buffer;
3332
+ }>;
1119
3333
  function __promisify__(type: 'dsa', options: DSAKeyPairKeyObjectOptions): Promise<KeyPairKeyObjectResult>;
1120
-
1121
3334
  function __promisify__(
1122
3335
  type: 'ec',
1123
- options: ECKeyPairOptions<'pem', 'pem'>,
1124
- ): Promise<{ publicKey: string; privateKey: string }>;
3336
+ options: ECKeyPairOptions<'pem', 'pem'>
3337
+ ): Promise<{
3338
+ publicKey: string;
3339
+ privateKey: string;
3340
+ }>;
1125
3341
  function __promisify__(
1126
3342
  type: 'ec',
1127
- options: ECKeyPairOptions<'pem', 'der'>,
1128
- ): Promise<{ publicKey: string; privateKey: Buffer }>;
3343
+ options: ECKeyPairOptions<'pem', 'der'>
3344
+ ): Promise<{
3345
+ publicKey: string;
3346
+ privateKey: Buffer;
3347
+ }>;
1129
3348
  function __promisify__(
1130
3349
  type: 'ec',
1131
- options: ECKeyPairOptions<'der', 'pem'>,
1132
- ): Promise<{ publicKey: Buffer; privateKey: string }>;
3350
+ options: ECKeyPairOptions<'der', 'pem'>
3351
+ ): Promise<{
3352
+ publicKey: Buffer;
3353
+ privateKey: string;
3354
+ }>;
1133
3355
  function __promisify__(
1134
3356
  type: 'ec',
1135
- options: ECKeyPairOptions<'der', 'der'>,
1136
- ): Promise<{ publicKey: Buffer; privateKey: Buffer }>;
3357
+ options: ECKeyPairOptions<'der', 'der'>
3358
+ ): Promise<{
3359
+ publicKey: Buffer;
3360
+ privateKey: Buffer;
3361
+ }>;
1137
3362
  function __promisify__(type: 'ec', options: ECKeyPairKeyObjectOptions): Promise<KeyPairKeyObjectResult>;
1138
-
1139
- function __promisify__(
1140
- type: 'ed25519',
1141
- options: ED25519KeyPairOptions<'pem', 'pem'>,
1142
- ): Promise<{ publicKey: string; privateKey: string }>;
1143
3363
  function __promisify__(
1144
3364
  type: 'ed25519',
1145
- options: ED25519KeyPairOptions<'pem', 'der'>,
1146
- ): Promise<{ publicKey: string; privateKey: Buffer }>;
3365
+ options: ED25519KeyPairOptions<'pem', 'pem'>
3366
+ ): Promise<{
3367
+ publicKey: string;
3368
+ privateKey: string;
3369
+ }>;
1147
3370
  function __promisify__(
1148
3371
  type: 'ed25519',
1149
- options: ED25519KeyPairOptions<'der', 'pem'>,
1150
- ): Promise<{ publicKey: Buffer; privateKey: string }>;
3372
+ options: ED25519KeyPairOptions<'pem', 'der'>
3373
+ ): Promise<{
3374
+ publicKey: string;
3375
+ privateKey: Buffer;
3376
+ }>;
1151
3377
  function __promisify__(
1152
3378
  type: 'ed25519',
1153
- options: ED25519KeyPairOptions<'der', 'der'>,
1154
- ): Promise<{ publicKey: Buffer; privateKey: Buffer }>;
3379
+ options: ED25519KeyPairOptions<'der', 'pem'>
3380
+ ): Promise<{
3381
+ publicKey: Buffer;
3382
+ privateKey: string;
3383
+ }>;
1155
3384
  function __promisify__(
1156
3385
  type: 'ed25519',
1157
- options?: ED25519KeyPairKeyObjectOptions,
1158
- ): Promise<KeyPairKeyObjectResult>;
1159
-
3386
+ options: ED25519KeyPairOptions<'der', 'der'>
3387
+ ): Promise<{
3388
+ publicKey: Buffer;
3389
+ privateKey: Buffer;
3390
+ }>;
3391
+ function __promisify__(type: 'ed25519', options?: ED25519KeyPairKeyObjectOptions): Promise<KeyPairKeyObjectResult>;
1160
3392
  function __promisify__(
1161
3393
  type: 'ed448',
1162
- options: ED448KeyPairOptions<'pem', 'pem'>,
1163
- ): Promise<{ publicKey: string; privateKey: string }>;
3394
+ options: ED448KeyPairOptions<'pem', 'pem'>
3395
+ ): Promise<{
3396
+ publicKey: string;
3397
+ privateKey: string;
3398
+ }>;
1164
3399
  function __promisify__(
1165
3400
  type: 'ed448',
1166
- options: ED448KeyPairOptions<'pem', 'der'>,
1167
- ): Promise<{ publicKey: string; privateKey: Buffer }>;
3401
+ options: ED448KeyPairOptions<'pem', 'der'>
3402
+ ): Promise<{
3403
+ publicKey: string;
3404
+ privateKey: Buffer;
3405
+ }>;
1168
3406
  function __promisify__(
1169
3407
  type: 'ed448',
1170
- options: ED448KeyPairOptions<'der', 'pem'>,
1171
- ): Promise<{ publicKey: Buffer; privateKey: string }>;
3408
+ options: ED448KeyPairOptions<'der', 'pem'>
3409
+ ): Promise<{
3410
+ publicKey: Buffer;
3411
+ privateKey: string;
3412
+ }>;
1172
3413
  function __promisify__(
1173
3414
  type: 'ed448',
1174
- options: ED448KeyPairOptions<'der', 'der'>,
1175
- ): Promise<{ publicKey: Buffer; privateKey: Buffer }>;
3415
+ options: ED448KeyPairOptions<'der', 'der'>
3416
+ ): Promise<{
3417
+ publicKey: Buffer;
3418
+ privateKey: Buffer;
3419
+ }>;
1176
3420
  function __promisify__(type: 'ed448', options?: ED448KeyPairKeyObjectOptions): Promise<KeyPairKeyObjectResult>;
1177
-
1178
3421
  function __promisify__(
1179
3422
  type: 'x25519',
1180
- options: X25519KeyPairOptions<'pem', 'pem'>,
1181
- ): Promise<{ publicKey: string; privateKey: string }>;
3423
+ options: X25519KeyPairOptions<'pem', 'pem'>
3424
+ ): Promise<{
3425
+ publicKey: string;
3426
+ privateKey: string;
3427
+ }>;
1182
3428
  function __promisify__(
1183
3429
  type: 'x25519',
1184
- options: X25519KeyPairOptions<'pem', 'der'>,
1185
- ): Promise<{ publicKey: string; privateKey: Buffer }>;
3430
+ options: X25519KeyPairOptions<'pem', 'der'>
3431
+ ): Promise<{
3432
+ publicKey: string;
3433
+ privateKey: Buffer;
3434
+ }>;
1186
3435
  function __promisify__(
1187
3436
  type: 'x25519',
1188
- options: X25519KeyPairOptions<'der', 'pem'>,
1189
- ): Promise<{ publicKey: Buffer; privateKey: string }>;
3437
+ options: X25519KeyPairOptions<'der', 'pem'>
3438
+ ): Promise<{
3439
+ publicKey: Buffer;
3440
+ privateKey: string;
3441
+ }>;
1190
3442
  function __promisify__(
1191
3443
  type: 'x25519',
1192
- options: X25519KeyPairOptions<'der', 'der'>,
1193
- ): Promise<{ publicKey: Buffer; privateKey: Buffer }>;
1194
- function __promisify__(
1195
- type: 'x25519',
1196
- options?: X25519KeyPairKeyObjectOptions,
1197
- ): Promise<KeyPairKeyObjectResult>;
1198
-
3444
+ options: X25519KeyPairOptions<'der', 'der'>
3445
+ ): Promise<{
3446
+ publicKey: Buffer;
3447
+ privateKey: Buffer;
3448
+ }>;
3449
+ function __promisify__(type: 'x25519', options?: X25519KeyPairKeyObjectOptions): Promise<KeyPairKeyObjectResult>;
1199
3450
  function __promisify__(
1200
3451
  type: 'x448',
1201
- options: X448KeyPairOptions<'pem', 'pem'>,
1202
- ): Promise<{ publicKey: string; privateKey: string }>;
3452
+ options: X448KeyPairOptions<'pem', 'pem'>
3453
+ ): Promise<{
3454
+ publicKey: string;
3455
+ privateKey: string;
3456
+ }>;
1203
3457
  function __promisify__(
1204
3458
  type: 'x448',
1205
- options: X448KeyPairOptions<'pem', 'der'>,
1206
- ): Promise<{ publicKey: string; privateKey: Buffer }>;
3459
+ options: X448KeyPairOptions<'pem', 'der'>
3460
+ ): Promise<{
3461
+ publicKey: string;
3462
+ privateKey: Buffer;
3463
+ }>;
1207
3464
  function __promisify__(
1208
3465
  type: 'x448',
1209
- options: X448KeyPairOptions<'der', 'pem'>,
1210
- ): Promise<{ publicKey: Buffer; privateKey: string }>;
3466
+ options: X448KeyPairOptions<'der', 'pem'>
3467
+ ): Promise<{
3468
+ publicKey: Buffer;
3469
+ privateKey: string;
3470
+ }>;
1211
3471
  function __promisify__(
1212
3472
  type: 'x448',
1213
- options: X448KeyPairOptions<'der', 'der'>,
1214
- ): Promise<{ publicKey: Buffer; privateKey: Buffer }>;
3473
+ options: X448KeyPairOptions<'der', 'der'>
3474
+ ): Promise<{
3475
+ publicKey: Buffer;
3476
+ privateKey: Buffer;
3477
+ }>;
1215
3478
  function __promisify__(type: 'x448', options?: X448KeyPairKeyObjectOptions): Promise<KeyPairKeyObjectResult>;
1216
3479
  }
1217
-
1218
3480
  /**
1219
3481
  * Calculates and returns the signature for `data` using the given private key and
1220
3482
  * algorithm. If `algorithm` is `null` or `undefined`, then the algorithm is
1221
3483
  * dependent upon the key type (especially Ed25519 and Ed448).
1222
3484
  *
1223
3485
  * If `key` is not a `KeyObject`, this function behaves as if `key` had been
1224
- * passed to `crypto.createPrivateKey().
3486
+ * passed to {@link createPrivateKey}. If it is an object, the following
3487
+ * additional properties can be passed:
3488
+ *
3489
+ * If the `callback` function is provided this function uses libuv's threadpool.
3490
+ * @since v12.0.0
1225
3491
  */
1226
- function sign(
1227
- algorithm: string | null | undefined,
1228
- data: NodeJS.ArrayBufferView,
1229
- key: KeyLike | SignKeyObjectInput | SignPrivateKeyInput,
1230
- ): Buffer;
3492
+ function sign(algorithm: string | null | undefined, data: NodeJS.ArrayBufferView, key: KeyLike | SignKeyObjectInput | SignPrivateKeyInput): Buffer;
1231
3493
  function sign(
1232
3494
  algorithm: string | null | undefined,
1233
3495
  data: NodeJS.ArrayBufferView,
1234
3496
  key: KeyLike | SignKeyObjectInput | SignPrivateKeyInput,
1235
3497
  callback: (error: Error | null, data: Buffer) => void
1236
3498
  ): void;
1237
-
1238
3499
  /**
1239
- * Calculates and returns the signature for `data` using the given private key and
1240
- * algorithm. If `algorithm` is `null` or `undefined`, then the algorithm is
1241
- * dependent upon the key type (especially Ed25519 and Ed448).
3500
+ * Verifies the given signature for `data` using the given key and algorithm. If`algorithm` is `null` or `undefined`, then the algorithm is dependent upon the
3501
+ * key type (especially Ed25519 and Ed448).
1242
3502
  *
1243
3503
  * If `key` is not a `KeyObject`, this function behaves as if `key` had been
1244
- * passed to `crypto.createPublicKey()`.
3504
+ * passed to {@link createPublicKey}. If it is an object, the following
3505
+ * additional properties can be passed:
3506
+ *
3507
+ * The `signature` argument is the previously calculated signature for the `data`.
3508
+ *
3509
+ * Because public keys can be derived from private keys, a private key or a public
3510
+ * key may be passed for `key`.
3511
+ *
3512
+ * If the `callback` function is provided this function uses libuv's threadpool.
3513
+ * @since v12.0.0
1245
3514
  */
1246
- function verify(
1247
- algorithm: string | null | undefined,
1248
- data: NodeJS.ArrayBufferView,
1249
- key: KeyLike | VerifyKeyObjectInput | VerifyPublicKeyInput,
1250
- signature: NodeJS.ArrayBufferView,
1251
- ): boolean;
3515
+ function verify(algorithm: string | null | undefined, data: NodeJS.ArrayBufferView, key: KeyLike | VerifyKeyObjectInput | VerifyPublicKeyInput, signature: NodeJS.ArrayBufferView): boolean;
1252
3516
  function verify(
1253
3517
  algorithm: string | null | undefined,
1254
3518
  data: NodeJS.ArrayBufferView,
@@ -1256,16 +3520,13 @@ declare module 'crypto' {
1256
3520
  signature: NodeJS.ArrayBufferView,
1257
3521
  callback: (error: Error | null, result: boolean) => void
1258
3522
  ): void;
1259
-
1260
3523
  /**
1261
- * Computes the Diffie-Hellman secret based on a privateKey and a publicKey.
1262
- * Both keys must have the same asymmetricKeyType, which must be one of
1263
- * 'dh' (for Diffie-Hellman), 'ec' (for ECDH), 'x448', or 'x25519' (for ECDH-ES).
3524
+ * Computes the Diffie-Hellman secret based on a `privateKey` and a `publicKey`.
3525
+ * Both keys must have the same `asymmetricKeyType`, which must be one of `'dh'`(for Diffie-Hellman), `'ec'` (for ECDH), `'x448'`, or `'x25519'` (for ECDH-ES).
3526
+ * @since v13.9.0, v12.17.0
1264
3527
  */
1265
3528
  function diffieHellman(options: { privateKey: KeyObject; publicKey: KeyObject }): Buffer;
1266
-
1267
3529
  type CipherMode = 'cbc' | 'ccm' | 'cfb' | 'ctr' | 'ecb' | 'gcm' | 'ocb' | 'ofb' | 'stream' | 'wrap' | 'xts';
1268
-
1269
3530
  interface CipherInfoOptions {
1270
3531
  /**
1271
3532
  * A test key length.
@@ -1276,7 +3537,6 @@ declare module 'crypto' {
1276
3537
  */
1277
3538
  ivLength?: number | undefined;
1278
3539
  }
1279
-
1280
3540
  interface CipherInfo {
1281
3541
  /**
1282
3542
  * The name of the cipher.
@@ -1305,64 +3565,113 @@ declare module 'crypto' {
1305
3565
  */
1306
3566
  mode: CipherMode;
1307
3567
  }
1308
-
1309
3568
  /**
1310
3569
  * Returns information about a given cipher.
1311
3570
  *
1312
- * Some ciphers accept variable length keys and initialization vectors.
1313
- * By default, the `crypto.getCipherInfo()` method will return the default
1314
- * values for these ciphers. To test if a given key length or iv length
1315
- * is acceptable for given cipher, use the `keyLenth` and `ivLenth` options.
1316
- * If the given values are unacceptable, `undefined` will be returned.
3571
+ * Some ciphers accept variable length keys and initialization vectors. By default,
3572
+ * the `crypto.getCipherInfo()` method will return the default values for these
3573
+ * ciphers. To test if a given key length or iv length is acceptable for given
3574
+ * cipher, use the `keyLength` and `ivLength` options. If the given values are
3575
+ * unacceptable, `undefined` will be returned.
3576
+ * @since v15.0.0
1317
3577
  * @param nameOrNid The name or nid of the cipher to query.
1318
3578
  */
1319
3579
  function getCipherInfo(nameOrNid: string | number, options?: CipherInfoOptions): CipherInfo | undefined;
1320
-
1321
3580
  /**
1322
- * HKDF is a simple key derivation function defined in RFC 5869.
1323
- * The given `key`, `salt` and `info` are used with the `digest` to derive a key of `keylen` bytes.
3581
+ * HKDF is a simple key derivation function defined in RFC 5869\. The given `key`,`salt` and `info` are used with the `digest` to derive a key of `keylen` bytes.
3582
+ *
3583
+ * The supplied `callback` function is called with two arguments: `err` and`derivedKey`. If an errors occurs while deriving the key, `err` will be set;
3584
+ * otherwise `err` will be `null`. The successfully generated `derivedKey` will
3585
+ * be passed to the callback as an [&lt;ArrayBuffer&gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer). An error will be thrown if any
3586
+ * of the input arguments specify invalid values or types.
3587
+ *
3588
+ * ```js
3589
+ * const {
3590
+ * hkdf,
3591
+ * } = await import('crypto');
1324
3592
  *
1325
- * The supplied `callback` function is called with two arguments: `err` and `derivedKey`.
1326
- * If an errors occurs while deriving the key, `err` will be set; otherwise `err` will be `null`.
1327
- * The successfully generated `derivedKey` will be passed to the callback as an `ArrayBuffer`.
1328
- * An error will be thrown if any of the input aguments specify invalid values or types.
3593
+ * hkdf('sha512', 'key', 'salt', 'info', 64, (err, derivedKey) => {
3594
+ * if (err) throw err;
3595
+ * console.log(Buffer.from(derivedKey).toString('hex')); // '24156e2...5391653'
3596
+ * });
3597
+ * ```
3598
+ *
3599
+ * ```js
3600
+ * const {
3601
+ * hkdf,
3602
+ * } = require('crypto');
3603
+ *
3604
+ * hkdf('sha512', 'key', 'salt', 'info', 64, (err, derivedKey) => {
3605
+ * if (err) throw err;
3606
+ * console.log(Buffer.from(derivedKey).toString('hex')); // '24156e2...5391653'
3607
+ * });
3608
+ * ```
3609
+ * @since v15.0.0
3610
+ * @param digest The digest algorithm to use.
3611
+ * @param key The secret key. It must be at least one byte in length.
3612
+ * @param salt The salt value. Must be provided but can be zero-length.
3613
+ * @param info Additional info value. Must be provided but can be zero-length, and cannot be more than 1024 bytes.
3614
+ * @param keylen The length of the key to generate. Must be greater than 0. The maximum allowable value is `255` times the number of bytes produced by the selected digest function (e.g. `sha512`
3615
+ * generates 64-byte hashes, making the maximum HKDF output 16320 bytes).
1329
3616
  */
1330
3617
  function hkdf(digest: string, key: BinaryLike | KeyObject, salt: BinaryLike, info: BinaryLike, keylen: number, callback: (err: Error | null, derivedKey: ArrayBuffer) => void): void;
1331
-
1332
3618
  /**
1333
- * Provides a synchronous HKDF key derivation function as defined in RFC 5869.
1334
- * The given `key`, `salt` and `info` are used with the `digest` to derive a key of `keylen` bytes.
3619
+ * Provides a synchronous HKDF key derivation function as defined in RFC 5869\. The
3620
+ * given `key`, `salt` and `info` are used with the `digest` to derive a key of`keylen` bytes.
3621
+ *
3622
+ * The successfully generated `derivedKey` will be returned as an [&lt;ArrayBuffer&gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer).
3623
+ *
3624
+ * An error will be thrown if any of the input arguments specify invalid values or
3625
+ * types, or if the derived key cannot be generated.
1335
3626
  *
1336
- * The successfully generated `derivedKey` will be returned as an `ArrayBuffer`.
1337
- * An error will be thrown if any of the input aguments specify invalid values or types,
1338
- * or if the derived key cannot be generated.
3627
+ * ```js
3628
+ * const {
3629
+ * hkdfSync,
3630
+ * } = await import('crypto');
3631
+ *
3632
+ * const derivedKey = hkdfSync('sha512', 'key', 'salt', 'info', 64);
3633
+ * console.log(Buffer.from(derivedKey).toString('hex')); // '24156e2...5391653'
3634
+ * ```
3635
+ *
3636
+ * ```js
3637
+ * const {
3638
+ * hkdfSync,
3639
+ * } = require('crypto');
3640
+ *
3641
+ * const derivedKey = hkdfSync('sha512', 'key', 'salt', 'info', 64);
3642
+ * console.log(Buffer.from(derivedKey).toString('hex')); // '24156e2...5391653'
3643
+ * ```
3644
+ * @since v15.0.0
3645
+ * @param digest The digest algorithm to use.
3646
+ * @param key The secret key. It must be at least one byte in length.
3647
+ * @param salt The salt value. Must be provided but can be zero-length.
3648
+ * @param info Additional info value. Must be provided but can be zero-length, and cannot be more than 1024 bytes.
3649
+ * @param keylen The length of the key to generate. Must be greater than 0. The maximum allowable value is `255` times the number of bytes produced by the selected digest function (e.g. `sha512`
3650
+ * generates 64-byte hashes, making the maximum HKDF output 16320 bytes).
1339
3651
  */
1340
3652
  function hkdfSync(digest: string, key: BinaryLike | KeyObject, salt: BinaryLike, info: BinaryLike, keylen: number): ArrayBuffer;
1341
-
1342
3653
  interface SecureHeapUsage {
1343
3654
  /**
1344
3655
  * The total allocated secure heap size as specified using the `--secure-heap=n` command-line flag.
1345
3656
  */
1346
3657
  total: number;
1347
-
1348
3658
  /**
1349
3659
  * The minimum allocation from the secure heap as specified using the `--secure-heap-min` command-line flag.
1350
3660
  */
1351
3661
  min: number;
1352
-
1353
3662
  /**
1354
3663
  * The total number of bytes currently allocated from the secure heap.
1355
3664
  */
1356
3665
  used: number;
1357
-
1358
3666
  /**
1359
3667
  * The calculated ratio of `used` to `total` allocated bytes.
1360
3668
  */
1361
3669
  utilization: number;
1362
3670
  }
1363
-
3671
+ /**
3672
+ * @since v15.6.0
3673
+ */
1364
3674
  function secureHeapUsed(): SecureHeapUsage;
1365
-
1366
3675
  interface RandomUUIDOptions {
1367
3676
  /**
1368
3677
  * By default, to improve performance,
@@ -1374,166 +3683,183 @@ declare module 'crypto' {
1374
3683
  */
1375
3684
  disableEntropyCache?: boolean | undefined;
1376
3685
  }
1377
-
3686
+ /**
3687
+ * Generates a random [RFC 4122](https://www.rfc-editor.org/rfc/rfc4122.txt) Version 4 UUID. The UUID is generated using a
3688
+ * cryptographic pseudorandom number generator.
3689
+ * @since v15.6.0
3690
+ */
1378
3691
  function randomUUID(options?: RandomUUIDOptions): string;
1379
-
1380
3692
  interface X509CheckOptions {
1381
3693
  /**
1382
3694
  * @default 'always'
1383
3695
  */
1384
3696
  subject: 'always' | 'never';
1385
-
1386
3697
  /**
1387
3698
  * @default true
1388
3699
  */
1389
3700
  wildcards: boolean;
1390
-
1391
3701
  /**
1392
3702
  * @default true
1393
3703
  */
1394
3704
  partialWildcards: boolean;
1395
-
1396
3705
  /**
1397
3706
  * @default false
1398
3707
  */
1399
3708
  multiLabelWildcards: boolean;
1400
-
1401
3709
  /**
1402
3710
  * @default false
1403
3711
  */
1404
3712
  singleLabelSubdomains: boolean;
1405
3713
  }
1406
-
3714
+ /**
3715
+ * Encapsulates an X509 certificate and provides read-only access to
3716
+ * its information.
3717
+ *
3718
+ * ```js
3719
+ * const { X509Certificate } = await import('crypto');
3720
+ *
3721
+ * const x509 = new X509Certificate('{... pem encoded cert ...}');
3722
+ *
3723
+ * console.log(x509.subject);
3724
+ * ```
3725
+ *
3726
+ * ```js
3727
+ * const { X509Certificate } = require('crypto');
3728
+ *
3729
+ * const x509 = new X509Certificate('{... pem encoded cert ...}');
3730
+ *
3731
+ * console.log(x509.subject);
3732
+ * ```
3733
+ * @since v15.6.0
3734
+ */
1407
3735
  class X509Certificate {
1408
3736
  /**
1409
- * Will be `true` if this is a Certificate Authority (ca) certificate.
3737
+ * Will be \`true\` if this is a Certificate Authority (ca) certificate.
3738
+ * @since v15.6.0
1410
3739
  */
1411
3740
  readonly ca: boolean;
1412
-
1413
3741
  /**
1414
3742
  * The SHA-1 fingerprint of this certificate.
3743
+ * @since v15.6.0
1415
3744
  */
1416
3745
  readonly fingerprint: string;
1417
-
1418
3746
  /**
1419
3747
  * The SHA-256 fingerprint of this certificate.
3748
+ * @since v15.6.0
1420
3749
  */
1421
3750
  readonly fingerprint256: string;
1422
-
1423
3751
  /**
1424
3752
  * The complete subject of this certificate.
3753
+ * @since v15.6.0
1425
3754
  */
1426
3755
  readonly subject: string;
1427
-
1428
3756
  /**
1429
3757
  * The subject alternative name specified for this certificate.
3758
+ * @since v15.6.0
1430
3759
  */
1431
3760
  readonly subjectAltName: string;
1432
-
1433
3761
  /**
1434
3762
  * The information access content of this certificate.
3763
+ * @since v15.6.0
1435
3764
  */
1436
3765
  readonly infoAccess: string;
1437
-
1438
3766
  /**
1439
3767
  * An array detailing the key usages for this certificate.
3768
+ * @since v15.6.0
1440
3769
  */
1441
3770
  readonly keyUsage: string[];
1442
-
1443
3771
  /**
1444
3772
  * The issuer identification included in this certificate.
3773
+ * @since v15.6.0
1445
3774
  */
1446
3775
  readonly issuer: string;
1447
-
1448
3776
  /**
1449
- * The issuer certificate or `undefined` if the issuer certificate is not available.
3777
+ * The issuer certificate or `undefined` if the issuer certificate is not
3778
+ * available.
3779
+ * @since v15.9.0
1450
3780
  */
1451
3781
  readonly issuerCertificate?: X509Certificate | undefined;
1452
-
1453
3782
  /**
1454
- * The public key for this certificate.
3783
+ * The public key `<KeyObject>` for this certificate.
3784
+ * @since v15.6.0
1455
3785
  */
1456
3786
  readonly publicKey: KeyObject;
1457
-
1458
3787
  /**
1459
3788
  * A `Buffer` containing the DER encoding of this certificate.
3789
+ * @since v15.6.0
1460
3790
  */
1461
3791
  readonly raw: Buffer;
1462
-
1463
3792
  /**
1464
3793
  * The serial number of this certificate.
3794
+ * @since v15.6.0
1465
3795
  */
1466
3796
  readonly serialNumber: string;
1467
-
1468
3797
  /**
1469
- * Returns the PEM-encoded certificate.
3798
+ * The date/time from which this certificate is considered valid.
3799
+ * @since v15.6.0
1470
3800
  */
1471
3801
  readonly validFrom: string;
1472
-
1473
3802
  /**
1474
- * The date/time from which this certificate is considered valid.
3803
+ * The date/time until which this certificate is considered valid.
3804
+ * @since v15.6.0
1475
3805
  */
1476
3806
  readonly validTo: string;
1477
-
1478
3807
  constructor(buffer: BinaryLike);
1479
-
1480
3808
  /**
1481
3809
  * Checks whether the certificate matches the given email address.
1482
- *
1483
- * Returns `email` if the certificate matches,`undefined` if it does not.
3810
+ * @since v15.6.0
3811
+ * @return Returns `email` if the certificate matches, `undefined` if it does not.
1484
3812
  */
1485
3813
  checkEmail(email: string, options?: X509CheckOptions): string | undefined;
1486
-
1487
3814
  /**
1488
3815
  * Checks whether the certificate matches the given host name.
1489
- *
1490
- * Returns `name` if the certificate matches, `undefined` if it does not.
3816
+ * @since v15.6.0
3817
+ * @return Returns `name` if the certificate matches, `undefined` if it does not.
1491
3818
  */
1492
3819
  checkHost(name: string, options?: X509CheckOptions): string | undefined;
1493
-
1494
3820
  /**
1495
3821
  * Checks whether the certificate matches the given IP address (IPv4 or IPv6).
1496
- *
1497
- * Returns `ip` if the certificate matches, `undefined` if it does not.
3822
+ * @since v15.6.0
3823
+ * @return Returns `ip` if the certificate matches, `undefined` if it does not.
1498
3824
  */
1499
3825
  checkIP(ip: string, options?: X509CheckOptions): string | undefined;
1500
-
1501
3826
  /**
1502
3827
  * Checks whether this certificate was issued by the given `otherCert`.
3828
+ * @since v15.6.0
1503
3829
  */
1504
3830
  checkIssued(otherCert: X509Certificate): boolean;
1505
-
1506
3831
  /**
1507
- * Checks whether this certificate was issued by the given `otherCert`.
3832
+ * Checks whether the public key for this certificate is consistent with
3833
+ * the given private key.
3834
+ * @since v15.6.0
3835
+ * @param privateKey A private key.
1508
3836
  */
1509
3837
  checkPrivateKey(privateKey: KeyObject): boolean;
1510
-
1511
3838
  /**
1512
- * There is no standard JSON encoding for X509 certificates. The
1513
- * `toJSON()` method returns a string containing the PEM encoded
3839
+ * There is no standard JSON encoding for X509 certificates. The`toJSON()` method returns a string containing the PEM encoded
1514
3840
  * certificate.
3841
+ * @since v15.6.0
1515
3842
  */
1516
3843
  toJSON(): string;
1517
-
1518
3844
  /**
1519
- * Returns information about this certificate using the legacy certificate object encoding.
3845
+ * Returns information about this certificate using the legacy `certificate object` encoding.
3846
+ * @since v15.6.0
1520
3847
  */
1521
3848
  toLegacyObject(): PeerCertificate;
1522
-
1523
3849
  /**
1524
3850
  * Returns the PEM-encoded certificate.
3851
+ * @since v15.6.0
1525
3852
  */
1526
3853
  toString(): string;
1527
-
1528
3854
  /**
1529
3855
  * Verifies that this certificate was signed by the given public key.
1530
3856
  * Does not perform any other validation checks on the certificate.
3857
+ * @since v15.6.0
3858
+ * @param publicKey A public key.
1531
3859
  */
1532
3860
  verify(publicKey: KeyObject): boolean;
1533
3861
  }
1534
-
1535
3862
  type LargeNumberLike = NodeJS.ArrayBufferView | SharedArrayBuffer | ArrayBuffer | bigint;
1536
-
1537
3863
  interface GeneratePrimeOptions {
1538
3864
  add?: LargeNumberLike | undefined;
1539
3865
  rem?: LargeNumberLike | undefined;
@@ -1543,25 +3869,74 @@ declare module 'crypto' {
1543
3869
  safe?: boolean | undefined;
1544
3870
  bigint?: boolean | undefined;
1545
3871
  }
1546
-
1547
3872
  interface GeneratePrimeOptionsBigInt extends GeneratePrimeOptions {
1548
3873
  bigint: true;
1549
3874
  }
1550
-
1551
3875
  interface GeneratePrimeOptionsArrayBuffer extends GeneratePrimeOptions {
1552
3876
  bigint?: false | undefined;
1553
3877
  }
1554
-
3878
+ /**
3879
+ * Generates a pseudorandom prime of `size` bits.
3880
+ *
3881
+ * If `options.safe` is `true`, the prime will be a safe prime -- that is,`(prime - 1) / 2` will also be a prime.
3882
+ *
3883
+ * The `options.add` and `options.rem` parameters can be used to enforce additional
3884
+ * requirements, e.g., for Diffie-Hellman:
3885
+ *
3886
+ * * If `options.add` and `options.rem` are both set, the prime will satisfy the
3887
+ * condition that `prime % add = rem`.
3888
+ * * If only `options.add` is set and `options.safe` is not `true`, the prime will
3889
+ * satisfy the condition that `prime % add = 1`.
3890
+ * * If only `options.add` is set and `options.safe` is set to `true`, the prime
3891
+ * will instead satisfy the condition that `prime % add = 3`. This is necessary
3892
+ * because `prime % add = 1` for `options.add > 2` would contradict the condition
3893
+ * enforced by `options.safe`.
3894
+ * * `options.rem` is ignored if `options.add` is not given.
3895
+ *
3896
+ * Both `options.add` and `options.rem` must be encoded as big-endian sequences
3897
+ * if given as an `ArrayBuffer`, `SharedArrayBuffer`, `TypedArray`, `Buffer`, or`DataView`.
3898
+ *
3899
+ * By default, the prime is encoded as a big-endian sequence of octets
3900
+ * in an [&lt;ArrayBuffer&gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer). If the `bigint` option is `true`, then a
3901
+ * [&lt;bigint&gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt)is provided.
3902
+ * @since v15.8.0
3903
+ * @param size The size (in bits) of the prime to generate.
3904
+ */
1555
3905
  function generatePrime(size: number, callback: (err: Error | null, prime: ArrayBuffer) => void): void;
1556
3906
  function generatePrime(size: number, options: GeneratePrimeOptionsBigInt, callback: (err: Error | null, prime: bigint) => void): void;
1557
3907
  function generatePrime(size: number, options: GeneratePrimeOptionsArrayBuffer, callback: (err: Error | null, prime: ArrayBuffer) => void): void;
1558
3908
  function generatePrime(size: number, options: GeneratePrimeOptions, callback: (err: Error | null, prime: ArrayBuffer | bigint) => void): void;
1559
-
3909
+ /**
3910
+ * Generates a pseudorandom prime of `size` bits.
3911
+ *
3912
+ * If `options.safe` is `true`, the prime will be a safe prime -- that is,`(prime - 1) / 2` will also be a prime.
3913
+ *
3914
+ * The `options.add` and `options.rem` parameters can be used to enforce additional
3915
+ * requirements, e.g., for Diffie-Hellman:
3916
+ *
3917
+ * * If `options.add` and `options.rem` are both set, the prime will satisfy the
3918
+ * condition that `prime % add = rem`.
3919
+ * * If only `options.add` is set and `options.safe` is not `true`, the prime will
3920
+ * satisfy the condition that `prime % add = 1`.
3921
+ * * If only `options.add` is set and `options.safe` is set to `true`, the prime
3922
+ * will instead satisfy the condition that `prime % add = 3`. This is necessary
3923
+ * because `prime % add = 1` for `options.add > 2` would contradict the condition
3924
+ * enforced by `options.safe`.
3925
+ * * `options.rem` is ignored if `options.add` is not given.
3926
+ *
3927
+ * Both `options.add` and `options.rem` must be encoded as big-endian sequences
3928
+ * if given as an `ArrayBuffer`, `SharedArrayBuffer`, `TypedArray`, `Buffer`, or`DataView`.
3929
+ *
3930
+ * By default, the prime is encoded as a big-endian sequence of octets
3931
+ * in an [&lt;ArrayBuffer&gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer). If the `bigint` option is `true`, then a
3932
+ * [&lt;bigint&gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt)is provided.
3933
+ * @since v15.8.0
3934
+ * @param size The size (in bits) of the prime to generate.
3935
+ */
1560
3936
  function generatePrimeSync(size: number): ArrayBuffer;
1561
3937
  function generatePrimeSync(size: number, options: GeneratePrimeOptionsBigInt): bigint;
1562
3938
  function generatePrimeSync(size: number, options: GeneratePrimeOptionsArrayBuffer): ArrayBuffer;
1563
3939
  function generatePrimeSync(size: number, options: GeneratePrimeOptions): ArrayBuffer | bigint;
1564
-
1565
3940
  interface CheckPrimeOptions {
1566
3941
  /**
1567
3942
  * The number of Miller-Rabin probabilistic primality iterations to perform.
@@ -1573,23 +3948,24 @@ declare module 'crypto' {
1573
3948
  */
1574
3949
  checks?: number | undefined;
1575
3950
  }
1576
-
1577
3951
  /**
1578
- * Checks the primality of the candidate.
3952
+ * Checks the primality of the `candidate`.
3953
+ * @since v15.8.0
3954
+ * @param candidate A possible prime encoded as a sequence of big endian octets of arbitrary length.
1579
3955
  */
1580
3956
  function checkPrime(value: LargeNumberLike, callback: (err: Error | null, result: boolean) => void): void;
1581
3957
  function checkPrime(value: LargeNumberLike, options: CheckPrimeOptions, callback: (err: Error | null, result: boolean) => void): void;
1582
-
1583
3958
  /**
1584
- * Checks the primality of the candidate.
3959
+ * Checks the primality of the `candidate`.
3960
+ * @since v15.8.0
3961
+ * @param candidate A possible prime encoded as a sequence of big endian octets of arbitrary length.
3962
+ * @return `true` if the candidate is a prime with an error probability less than `0.25 ** options.checks`.
1585
3963
  */
1586
- function checkPrimeSync(value: LargeNumberLike, options?: CheckPrimeOptions): boolean;
1587
-
3964
+ function checkPrimeSync(candidate: LargeNumberLike, options?: CheckPrimeOptions): boolean;
1588
3965
  namespace webcrypto {
1589
3966
  class CryptoKey {} // placeholder
1590
3967
  }
1591
3968
  }
1592
-
1593
3969
  declare module 'node:crypto' {
1594
3970
  export * from 'crypto';
1595
3971
  }