@types/node 16.3.3 → 16.4.3

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();
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
+ */
177
494
  copy(): 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
506
  update(data: string, input_encoding: 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
651
  update(data: string, input_encoding: 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,820 @@ 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
1117
  update(data: string, input_encoding: Encoding): Buffer;
307
1118
  update(data: NodeJS.ArrayBufferView, input_encoding: undefined, output_encoding: Encoding): string;
308
1119
  update(data: string, input_encoding: Encoding | undefined, output_encoding: 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
1129
  final(output_encoding: 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
+ */
311
1144
  setAutoPadding(auto_padding?: boolean): this;
312
- // getAuthTag(): Buffer;
313
- // setAAD(buffer: NodeJS.ArrayBufferView): 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
1430
  update(data: string, input_encoding: Encoding): Buffer;
353
1431
  update(data: NodeJS.ArrayBufferView, input_encoding: undefined, output_encoding: Encoding): string;
354
1432
  update(data: string, input_encoding: Encoding | undefined, output_encoding: 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
1442
  final(output_encoding: 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
+ /**
1555
+ * Creates and returns a `Sign` object that uses the given `algorithm`. Use {@link getHashes} to obtain the names of the available digest algorithms.
1556
+ * Optional `options` argument controls the `stream.Writable` behavior.
1557
+ *
1558
+ * In some cases, a `Sign` instance can be created using the name of a signature
1559
+ * algorithm, such as `'RSA-SHA256'`, instead of a digest algorithm. This will use
1560
+ * the corresponding digest algorithm. This does not work for all signature
1561
+ * algorithms, such as `'ecdsa-with-SHA256'`, so it is best to always use digest
1562
+ * algorithm names.
1563
+ * @since v0.1.92
1564
+ * @param options `stream.Writable` options
1565
+ */
394
1566
  function createSign(algorithm: string, options?: stream.WritableOptions): Sign;
395
-
396
1567
  type DSAEncoding = 'der' | 'ieee-p1363';
397
-
398
1568
  interface SigningOptions {
399
1569
  /**
400
1570
  * @See crypto.constants.RSA_PKCS1_PADDING
@@ -403,132 +1573,933 @@ declare module 'crypto' {
403
1573
  saltLength?: number | undefined;
404
1574
  dsaEncoding?: DSAEncoding | undefined;
405
1575
  }
406
-
407
- interface SignPrivateKeyInput extends PrivateKeyInput, SigningOptions { }
1576
+ interface SignPrivateKeyInput extends PrivateKeyInput, SigningOptions {}
408
1577
  interface SignKeyObjectInput extends SigningOptions {
409
1578
  key: KeyObject;
410
1579
  }
411
- interface VerifyPublicKeyInput extends PublicKeyInput, SigningOptions { }
1580
+ interface VerifyPublicKeyInput extends PublicKeyInput, SigningOptions {}
412
1581
  interface VerifyKeyObjectInput extends SigningOptions {
413
1582
  key: KeyObject;
414
1583
  }
415
-
416
1584
  type KeyLike = string | Buffer | KeyObject;
417
-
1585
+ /**
1586
+ * * Extends: `<stream.Writable>`
1587
+ *
1588
+ * The `Sign` class is a utility for generating signatures. It can be used in one
1589
+ * of two ways:
1590
+ *
1591
+ * * 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
1592
+ * * Using the `sign.update()` and `sign.sign()` methods to produce the
1593
+ * signature.
1594
+ *
1595
+ * The {@link createSign} method is used to create `Sign` instances. The
1596
+ * argument is the string name of the hash function to use. `Sign` objects are not
1597
+ * to be created directly using the `new` keyword.
1598
+ *
1599
+ * Example: Using `Sign` and `Verify` objects as streams:
1600
+ *
1601
+ * ```js
1602
+ * const {
1603
+ * generateKeyPairSync,
1604
+ * createSign,
1605
+ * createVerify,
1606
+ * } = await import('crypto');
1607
+ *
1608
+ * const { privateKey, publicKey } = generateKeyPairSync('ec', {
1609
+ * namedCurve: 'sect239k1'
1610
+ * });
1611
+ *
1612
+ * const sign = createSign('SHA256');
1613
+ * sign.write('some data to sign');
1614
+ * sign.end();
1615
+ * const signature = sign.sign(privateKey, 'hex');
1616
+ *
1617
+ * const verify = createVerify('SHA256');
1618
+ * verify.write('some data to sign');
1619
+ * verify.end();
1620
+ * console.log(verify.verify(publicKey, signature, 'hex'));
1621
+ * // Prints: true
1622
+ * ```
1623
+ *
1624
+ * ```js
1625
+ * const {
1626
+ * generateKeyPairSync,
1627
+ * createSign,
1628
+ * createVerify,
1629
+ * } = require('crypto');
1630
+ *
1631
+ * const { privateKey, publicKey } = generateKeyPairSync('ec', {
1632
+ * namedCurve: 'sect239k1'
1633
+ * });
1634
+ *
1635
+ * const sign = createSign('SHA256');
1636
+ * sign.write('some data to sign');
1637
+ * sign.end();
1638
+ * const signature = sign.sign(privateKey, 'hex');
1639
+ *
1640
+ * const verify = createVerify('SHA256');
1641
+ * verify.write('some data to sign');
1642
+ * verify.end();
1643
+ * console.log(verify.verify(publicKey, signature, 'hex'));
1644
+ * // Prints: true
1645
+ * ```
1646
+ *
1647
+ * Example: Using the `sign.update()` and `verify.update()` methods:
1648
+ *
1649
+ * ```js
1650
+ * const {
1651
+ * generateKeyPairSync,
1652
+ * createSign,
1653
+ * createVerify,
1654
+ * } = await import('crypto');
1655
+ *
1656
+ * const { privateKey, publicKey } = generateKeyPairSync('rsa', {
1657
+ * modulusLength: 2048,
1658
+ * });
1659
+ *
1660
+ * const sign = createSign('SHA256');
1661
+ * sign.update('some data to sign');
1662
+ * sign.end();
1663
+ * const signature = sign.sign(privateKey);
1664
+ *
1665
+ * const verify = createVerify('SHA256');
1666
+ * verify.update('some data to sign');
1667
+ * verify.end();
1668
+ * console.log(verify.verify(publicKey, signature));
1669
+ * // Prints: true
1670
+ * ```
1671
+ *
1672
+ * ```js
1673
+ * const {
1674
+ * generateKeyPairSync,
1675
+ * createSign,
1676
+ * createVerify,
1677
+ * } = require('crypto');
1678
+ *
1679
+ * const { privateKey, publicKey } = generateKeyPairSync('rsa', {
1680
+ * modulusLength: 2048,
1681
+ * });
1682
+ *
1683
+ * const sign = createSign('SHA256');
1684
+ * sign.update('some data to sign');
1685
+ * sign.end();
1686
+ * const signature = sign.sign(privateKey);
1687
+ *
1688
+ * const verify = createVerify('SHA256');
1689
+ * verify.update('some data to sign');
1690
+ * verify.end();
1691
+ * console.log(verify.verify(publicKey, signature));
1692
+ * // Prints: true
1693
+ * ```
1694
+ * @since v0.1.92
1695
+ */
418
1696
  class Sign extends stream.Writable {
419
1697
  private constructor();
420
-
1698
+ /**
1699
+ * Updates the `Sign` content with the given `data`, the encoding of which
1700
+ * is given in `inputEncoding`.
1701
+ * If `encoding` is not provided, and the `data` is a string, an
1702
+ * encoding of `'utf8'` is enforced. If `data` is a `Buffer`, `TypedArray`, or`DataView`, then `inputEncoding` is ignored.
1703
+ *
1704
+ * This can be called many times with new data as it is streamed.
1705
+ * @since v0.1.92
1706
+ * @param inputEncoding The `encoding` of the `data` string.
1707
+ */
421
1708
  update(data: BinaryLike): this;
422
1709
  update(data: string, input_encoding: Encoding): this;
1710
+ /**
1711
+ * Calculates the signature on all the data passed through using either `sign.update()` or `sign.write()`.
1712
+ *
1713
+ * If `privateKey` is not a `KeyObject`, this function behaves as if`privateKey` had been passed to {@link createPrivateKey}. If it is an
1714
+ * object, the following additional properties can be passed:
1715
+ *
1716
+ * If `outputEncoding` is provided a string is returned; otherwise a `Buffer` is returned.
1717
+ *
1718
+ * The `Sign` object can not be again used after `sign.sign()` method has been
1719
+ * called. Multiple calls to `sign.sign()` will result in an error being thrown.
1720
+ * @since v0.1.92
1721
+ */
423
1722
  sign(private_key: KeyLike | SignKeyObjectInput | SignPrivateKeyInput): Buffer;
424
- sign(
425
- private_key: KeyLike | SignKeyObjectInput | SignPrivateKeyInput,
426
- output_format: BinaryToTextEncoding,
427
- ): string;
1723
+ sign(private_key: KeyLike | SignKeyObjectInput | SignPrivateKeyInput, output_format: BinaryToTextEncoding): string;
428
1724
  }
429
-
1725
+ /**
1726
+ * Creates and returns a `Verify` object that uses the given algorithm.
1727
+ * Use {@link getHashes} to obtain an array of names of the available
1728
+ * signing algorithms. Optional `options` argument controls the`stream.Writable` behavior.
1729
+ *
1730
+ * In some cases, a `Verify` instance can be created using the name of a signature
1731
+ * algorithm, such as `'RSA-SHA256'`, instead of a digest algorithm. This will use
1732
+ * the corresponding digest algorithm. This does not work for all signature
1733
+ * algorithms, such as `'ecdsa-with-SHA256'`, so it is best to always use digest
1734
+ * algorithm names.
1735
+ * @since v0.1.92
1736
+ * @param options `stream.Writable` options
1737
+ */
430
1738
  function createVerify(algorithm: string, options?: stream.WritableOptions): Verify;
1739
+ /**
1740
+ * * Extends: `<stream.Writable>`
1741
+ *
1742
+ * The `Verify` class is a utility for verifying signatures. It can be used in one
1743
+ * of two ways:
1744
+ *
1745
+ * * As a writable `stream` where written data is used to validate against the
1746
+ * supplied signature, or
1747
+ * * Using the `verify.update()` and `verify.verify()` methods to verify
1748
+ * the signature.
1749
+ *
1750
+ * The {@link createVerify} method is used to create `Verify` instances.`Verify` objects are not to be created directly using the `new` keyword.
1751
+ *
1752
+ * See `Sign` for examples.
1753
+ * @since v0.1.92
1754
+ */
431
1755
  class Verify extends stream.Writable {
432
1756
  private constructor();
433
-
1757
+ /**
1758
+ * Updates the `Verify` content with the given `data`, the encoding of which
1759
+ * is given in `inputEncoding`.
1760
+ * If `inputEncoding` is not provided, and the `data` is a string, an
1761
+ * encoding of `'utf8'` is enforced. If `data` is a `Buffer`, `TypedArray`, or`DataView`, then `inputEncoding` is ignored.
1762
+ *
1763
+ * This can be called many times with new data as it is streamed.
1764
+ * @since v0.1.92
1765
+ * @param inputEncoding The `encoding` of the `data` string.
1766
+ */
434
1767
  update(data: BinaryLike): Verify;
435
1768
  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
+ /**
1770
+ * Verifies the provided data using the given `object` and `signature`.
1771
+ *
1772
+ * If `object` is not a `KeyObject`, this function behaves as if`object` had been passed to {@link createPublicKey}. If it is an
1773
+ * object, the following additional properties can be passed:
1774
+ *
1775
+ * The `signature` argument is the previously calculated signature for the data, in
1776
+ * the `signatureEncoding`.
1777
+ * If a `signatureEncoding` is specified, the `signature` is expected to be a
1778
+ * string; otherwise `signature` is expected to be a `Buffer`,`TypedArray`, or `DataView`.
1779
+ *
1780
+ * The `verify` object can not be used again after `verify.verify()` has been
1781
+ * called. Multiple calls to `verify.verify()` will result in an error being
1782
+ * thrown.
1783
+ *
1784
+ * Because public keys can be derived from private keys, a private key may
1785
+ * be passed instead of a public key.
1786
+ * @since v0.1.92
1787
+ */
1788
+ verify(object: KeyLike | VerifyKeyObjectInput | VerifyPublicKeyInput, signature: NodeJS.ArrayBufferView): boolean;
1789
+ verify(object: KeyLike | VerifyKeyObjectInput | VerifyPublicKeyInput, signature: string, signature_format?: BinaryToTextEncoding): boolean;
447
1790
  }
1791
+ /**
1792
+ * Creates a `DiffieHellman` key exchange object using the supplied `prime` and an
1793
+ * optional specific `generator`.
1794
+ *
1795
+ * The `generator` argument can be a number, string, or `Buffer`. If`generator` is not specified, the value `2` is used.
1796
+ *
1797
+ * If `primeEncoding` is specified, `prime` is expected to be a string; otherwise
1798
+ * a `Buffer`, `TypedArray`, or `DataView` is expected.
1799
+ *
1800
+ * If `generatorEncoding` is specified, `generator` is expected to be a string;
1801
+ * otherwise a number, `Buffer`, `TypedArray`, or `DataView` is expected.
1802
+ * @since v0.11.12
1803
+ * @param primeEncoding The `encoding` of the `prime` string.
1804
+ * @param generatorEncoding The `encoding` of the `generator` string.
1805
+ */
448
1806
  function createDiffieHellman(prime_length: number, generator?: number | NodeJS.ArrayBufferView): DiffieHellman;
449
1807
  function createDiffieHellman(prime: NodeJS.ArrayBufferView): DiffieHellman;
450
1808
  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, prime_encoding: BinaryToTextEncoding, generator: number | NodeJS.ArrayBufferView): DiffieHellman;
1810
+ function createDiffieHellman(prime: string, prime_encoding: BinaryToTextEncoding, generator: string, generator_encoding: BinaryToTextEncoding): DiffieHellman;
1811
+ /**
1812
+ * The `DiffieHellman` class is a utility for creating Diffie-Hellman key
1813
+ * exchanges.
1814
+ *
1815
+ * Instances of the `DiffieHellman` class can be created using the {@link createDiffieHellman} function.
1816
+ *
1817
+ * ```js
1818
+ * import assert from 'assert';
1819
+ *
1820
+ * const {
1821
+ * createDiffieHellman,
1822
+ * } = await import('crypto');
1823
+ *
1824
+ * // Generate Alice's keys...
1825
+ * const alice = createDiffieHellman(2048);
1826
+ * const aliceKey = alice.generateKeys();
1827
+ *
1828
+ * // Generate Bob's keys...
1829
+ * const bob = createDiffieHellman(alice.getPrime(), alice.getGenerator());
1830
+ * const bobKey = bob.generateKeys();
1831
+ *
1832
+ * // Exchange and generate the secret...
1833
+ * const aliceSecret = alice.computeSecret(bobKey);
1834
+ * const bobSecret = bob.computeSecret(aliceKey);
1835
+ *
1836
+ * // OK
1837
+ * assert.strictEqual(aliceSecret.toString('hex'), bobSecret.toString('hex'));
1838
+ * ```
1839
+ *
1840
+ * ```js
1841
+ * const assert = require('assert');
1842
+ *
1843
+ * const {
1844
+ * createDiffieHellman,
1845
+ * } = require('crypto');
1846
+ *
1847
+ * // Generate Alice's keys...
1848
+ * const alice = createDiffieHellman(2048);
1849
+ * const aliceKey = alice.generateKeys();
1850
+ *
1851
+ * // Generate Bob's keys...
1852
+ * const bob = createDiffieHellman(alice.getPrime(), alice.getGenerator());
1853
+ * const bobKey = bob.generateKeys();
1854
+ *
1855
+ * // Exchange and generate the secret...
1856
+ * const aliceSecret = alice.computeSecret(bobKey);
1857
+ * const bobSecret = bob.computeSecret(aliceKey);
1858
+ *
1859
+ * // OK
1860
+ * assert.strictEqual(aliceSecret.toString('hex'), bobSecret.toString('hex'));
1861
+ * ```
1862
+ * @since v0.5.0
1863
+ */
462
1864
  class DiffieHellman {
463
1865
  private constructor();
1866
+ /**
1867
+ * Generates private and public Diffie-Hellman key values, and returns
1868
+ * the public key in the specified `encoding`. This key should be
1869
+ * transferred to the other party.
1870
+ * If `encoding` is provided a string is returned; otherwise a `Buffer` is returned.
1871
+ * @since v0.5.0
1872
+ * @param encoding The `encoding` of the return value.
1873
+ */
464
1874
  generateKeys(): Buffer;
465
1875
  generateKeys(encoding: BinaryToTextEncoding): string;
1876
+ /**
1877
+ * Computes the shared secret using `otherPublicKey` as the other
1878
+ * party's public key and returns the computed shared secret. The supplied
1879
+ * key is interpreted using the specified `inputEncoding`, and secret is
1880
+ * encoded using specified `outputEncoding`.
1881
+ * If the `inputEncoding` is not
1882
+ * provided, `otherPublicKey` is expected to be a `Buffer`,`TypedArray`, or `DataView`.
1883
+ *
1884
+ * If `outputEncoding` is given a string is returned; otherwise, a `Buffer` is returned.
1885
+ * @since v0.5.0
1886
+ * @param inputEncoding The `encoding` of an `otherPublicKey` string.
1887
+ * @param outputEncoding The `encoding` of the return value.
1888
+ */
466
1889
  computeSecret(other_public_key: NodeJS.ArrayBufferView): Buffer;
467
1890
  computeSecret(other_public_key: string, input_encoding: BinaryToTextEncoding): Buffer;
468
1891
  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;
1892
+ computeSecret(other_public_key: string, input_encoding: BinaryToTextEncoding, output_encoding: BinaryToTextEncoding): string;
1893
+ /**
1894
+ * Returns the Diffie-Hellman prime in the specified `encoding`.
1895
+ * If `encoding` is provided a string is
1896
+ * returned; otherwise a `Buffer` is returned.
1897
+ * @since v0.5.0
1898
+ * @param encoding The `encoding` of the return value.
1899
+ */
474
1900
  getPrime(): Buffer;
475
1901
  getPrime(encoding: BinaryToTextEncoding): string;
1902
+ /**
1903
+ * Returns the Diffie-Hellman generator in the specified `encoding`.
1904
+ * If `encoding` is provided a string is
1905
+ * returned; otherwise a `Buffer` is returned.
1906
+ * @since v0.5.0
1907
+ * @param encoding The `encoding` of the return value.
1908
+ */
476
1909
  getGenerator(): Buffer;
477
1910
  getGenerator(encoding: BinaryToTextEncoding): string;
1911
+ /**
1912
+ * Returns the Diffie-Hellman public key in the specified `encoding`.
1913
+ * If `encoding` is provided a
1914
+ * string is returned; otherwise a `Buffer` is returned.
1915
+ * @since v0.5.0
1916
+ * @param encoding The `encoding` of the return value.
1917
+ */
478
1918
  getPublicKey(): Buffer;
479
1919
  getPublicKey(encoding: BinaryToTextEncoding): string;
1920
+ /**
1921
+ * Returns the Diffie-Hellman private key in the specified `encoding`.
1922
+ * If `encoding` is provided a
1923
+ * string is returned; otherwise a `Buffer` is returned.
1924
+ * @since v0.5.0
1925
+ * @param encoding The `encoding` of the return value.
1926
+ */
480
1927
  getPrivateKey(): Buffer;
481
1928
  getPrivateKey(encoding: BinaryToTextEncoding): string;
1929
+ /**
1930
+ * Sets the Diffie-Hellman public key. If the `encoding` argument is provided,`publicKey` is expected
1931
+ * to be a string. If no `encoding` is provided, `publicKey` is expected
1932
+ * to be a `Buffer`, `TypedArray`, or `DataView`.
1933
+ * @since v0.5.0
1934
+ * @param encoding The `encoding` of the `publicKey` string.
1935
+ */
482
1936
  setPublicKey(public_key: NodeJS.ArrayBufferView): void;
483
1937
  setPublicKey(public_key: string, encoding: BufferEncoding): void;
1938
+ /**
1939
+ * Sets the Diffie-Hellman private key. If the `encoding` argument is provided,`privateKey` is expected
1940
+ * to be a string. If no `encoding` is provided, `privateKey` is expected
1941
+ * to be a `Buffer`, `TypedArray`, or `DataView`.
1942
+ * @since v0.5.0
1943
+ * @param encoding The `encoding` of the `privateKey` string.
1944
+ */
484
1945
  setPrivateKey(private_key: NodeJS.ArrayBufferView): void;
485
1946
  setPrivateKey(private_key: string, encoding: BufferEncoding): void;
1947
+ /**
1948
+ * A bit field containing any warnings and/or errors resulting from a check
1949
+ * performed during initialization of the `DiffieHellman` object.
1950
+ *
1951
+ * The following values are valid for this property (as defined in `constants`module):
1952
+ *
1953
+ * * `DH_CHECK_P_NOT_SAFE_PRIME`
1954
+ * * `DH_CHECK_P_NOT_PRIME`
1955
+ * * `DH_UNABLE_TO_CHECK_GENERATOR`
1956
+ * * `DH_NOT_SUITABLE_GENERATOR`
1957
+ * @since v0.11.12
1958
+ */
486
1959
  verifyError: number;
487
1960
  }
1961
+ /**
1962
+ * Creates a predefined `DiffieHellmanGroup` key exchange object. The
1963
+ * 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'`,
1964
+ * `'modp18'` (defined in [RFC 3526](https://www.rfc-editor.org/rfc/rfc3526.txt)). The
1965
+ * returned object mimics the interface of objects created by {@link createDiffieHellman}, but will not allow changing
1966
+ * the keys (with `diffieHellman.setPublicKey()`, for example). The
1967
+ * advantage of using this method is that the parties do not have to
1968
+ * generate nor exchange a group modulus beforehand, saving both processor
1969
+ * and communication time.
1970
+ *
1971
+ * Example (obtaining a shared secret):
1972
+ *
1973
+ * ```js
1974
+ * const {
1975
+ * getDiffieHellman,
1976
+ * } = await import('crypto');
1977
+ * const alice = getDiffieHellman('modp14');
1978
+ * const bob = getDiffieHellman('modp14');
1979
+ *
1980
+ * alice.generateKeys();
1981
+ * bob.generateKeys();
1982
+ *
1983
+ * const aliceSecret = alice.computeSecret(bob.getPublicKey(), null, 'hex');
1984
+ * const bobSecret = bob.computeSecret(alice.getPublicKey(), null, 'hex');
1985
+ *
1986
+ * // aliceSecret and bobSecret should be the same
1987
+ * console.log(aliceSecret === bobSecret);
1988
+ * ```
1989
+ *
1990
+ * ```js
1991
+ * const {
1992
+ * getDiffieHellman,
1993
+ * } = require('crypto');
1994
+ *
1995
+ * const alice = getDiffieHellman('modp14');
1996
+ * const bob = getDiffieHellman('modp14');
1997
+ *
1998
+ * alice.generateKeys();
1999
+ * bob.generateKeys();
2000
+ *
2001
+ * const aliceSecret = alice.computeSecret(bob.getPublicKey(), null, 'hex');
2002
+ * const bobSecret = bob.computeSecret(alice.getPublicKey(), null, 'hex');
2003
+ *
2004
+ * // aliceSecret and bobSecret should be the same
2005
+ * console.log(aliceSecret === bobSecret);
2006
+ * ```
2007
+ * @since v0.7.5
2008
+ */
488
2009
  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
-
2010
+ /**
2011
+ * Provides an asynchronous Password-Based Key Derivation Function 2 (PBKDF2)
2012
+ * implementation. A selected HMAC digest algorithm specified by `digest` is
2013
+ * applied to derive a key of the requested byte length (`keylen`) from the`password`, `salt` and `iterations`.
2014
+ *
2015
+ * The supplied `callback` function is called with two arguments: `err` and`derivedKey`. If an error occurs while deriving the key, `err` will be set;
2016
+ * otherwise `err` will be `null`. By default, the successfully generated`derivedKey` will be passed to the callback as a `Buffer`. An error will be
2017
+ * thrown if any of the input arguments specify invalid values or types.
2018
+ *
2019
+ * If `digest` is `null`, `'sha1'` will be used. This behavior is deprecated,
2020
+ * please specify a `digest` explicitly.
2021
+ *
2022
+ * The `iterations` argument must be a number set as high as possible. The
2023
+ * higher the number of iterations, the more secure the derived key will be,
2024
+ * but will take a longer amount of time to complete.
2025
+ *
2026
+ * The `salt` should be as unique as possible. It is recommended that a salt is
2027
+ * random and at least 16 bytes long. See [NIST SP 800-132](https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf) for details.
2028
+ *
2029
+ * When passing strings for `password` or `salt`, please consider `caveats when using strings as inputs to cryptographic APIs`.
2030
+ *
2031
+ * ```js
2032
+ * const {
2033
+ * pbkdf2,
2034
+ * } = await import('crypto');
2035
+ *
2036
+ * pbkdf2('secret', 'salt', 100000, 64, 'sha512', (err, derivedKey) => {
2037
+ * if (err) throw err;
2038
+ * console.log(derivedKey.toString('hex')); // '3745e48...08d59ae'
2039
+ * });
2040
+ * ```
2041
+ *
2042
+ * ```js
2043
+ * const {
2044
+ * pbkdf2,
2045
+ * } = require('crypto');
2046
+ *
2047
+ * pbkdf2('secret', 'salt', 100000, 64, 'sha512', (err, derivedKey) => {
2048
+ * if (err) throw err;
2049
+ * console.log(derivedKey.toString('hex')); // '3745e48...08d59ae'
2050
+ * });
2051
+ * ```
2052
+ *
2053
+ * The `crypto.DEFAULT_ENCODING` property can be used to change the way the`derivedKey` is passed to the callback. This property, however, has been
2054
+ * deprecated and use should be avoided.
2055
+ *
2056
+ * ```js
2057
+ * const crypto = await import('crypto');
2058
+ * crypto.DEFAULT_ENCODING = 'hex';
2059
+ * crypto.pbkdf2('secret', 'salt', 100000, 512, 'sha512', (err, derivedKey) => {
2060
+ * if (err) throw err;
2061
+ * console.log(derivedKey); // '3745e48...aa39b34'
2062
+ * });
2063
+ * ```
2064
+ *
2065
+ * ```js
2066
+ * const crypto = require('crypto');
2067
+ * crypto.DEFAULT_ENCODING = 'hex';
2068
+ * crypto.pbkdf2('secret', 'salt', 100000, 512, 'sha512', (err, derivedKey) => {
2069
+ * if (err) throw err;
2070
+ * console.log(derivedKey); // '3745e48...aa39b34'
2071
+ * });
2072
+ * ```
2073
+ *
2074
+ * An array of supported digest functions can be retrieved using {@link getHashes}.
2075
+ *
2076
+ * This API uses libuv's threadpool, which can have surprising and
2077
+ * negative performance implications for some applications; see the `UV_THREADPOOL_SIZE` documentation for more information.
2078
+ * @since v0.5.5
2079
+ */
2080
+ function pbkdf2(password: BinaryLike, salt: BinaryLike, iterations: number, keylen: number, digest: string, callback: (err: Error | null, derivedKey: Buffer) => void): void;
2081
+ /**
2082
+ * Provides a synchronous Password-Based Key Derivation Function 2 (PBKDF2)
2083
+ * implementation. A selected HMAC digest algorithm specified by `digest` is
2084
+ * applied to derive a key of the requested byte length (`keylen`) from the`password`, `salt` and `iterations`.
2085
+ *
2086
+ * If an error occurs an `Error` will be thrown, otherwise the derived key will be
2087
+ * returned as a `Buffer`.
2088
+ *
2089
+ * If `digest` is `null`, `'sha1'` will be used. This behavior is deprecated,
2090
+ * please specify a `digest` explicitly.
2091
+ *
2092
+ * The `iterations` argument must be a number set as high as possible. The
2093
+ * higher the number of iterations, the more secure the derived key will be,
2094
+ * but will take a longer amount of time to complete.
2095
+ *
2096
+ * The `salt` should be as unique as possible. It is recommended that a salt is
2097
+ * random and at least 16 bytes long. See [NIST SP 800-132](https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf) for details.
2098
+ *
2099
+ * When passing strings for `password` or `salt`, please consider `caveats when using strings as inputs to cryptographic APIs`.
2100
+ *
2101
+ * ```js
2102
+ * const {
2103
+ * pbkdf2Sync,
2104
+ * } = await import('crypto');
2105
+ *
2106
+ * const key = pbkdf2Sync('secret', 'salt', 100000, 64, 'sha512');
2107
+ * console.log(key.toString('hex')); // '3745e48...08d59ae'
2108
+ * ```
2109
+ *
2110
+ * ```js
2111
+ * const {
2112
+ * pbkdf2Sync,
2113
+ * } = require('crypto');
2114
+ *
2115
+ * const key = pbkdf2Sync('secret', 'salt', 100000, 64, 'sha512');
2116
+ * console.log(key.toString('hex')); // '3745e48...08d59ae'
2117
+ * ```
2118
+ *
2119
+ * The `crypto.DEFAULT_ENCODING` property may be used to change the way the`derivedKey` is returned. This property, however, is deprecated and use
2120
+ * should be avoided.
2121
+ *
2122
+ * ```js
2123
+ * const crypto = await import('crypto');
2124
+ * crypto.DEFAULT_ENCODING = 'hex';
2125
+ * const key = crypto.pbkdf2Sync('secret', 'salt', 100000, 512, 'sha512');
2126
+ * console.log(key); // '3745e48...aa39b34'
2127
+ * ```
2128
+ *
2129
+ * ```js
2130
+ * const crypto = require('crypto');
2131
+ * crypto.DEFAULT_ENCODING = 'hex';
2132
+ * const key = crypto.pbkdf2Sync('secret', 'salt', 100000, 512, 'sha512');
2133
+ * console.log(key); // '3745e48...aa39b34'
2134
+ * ```
2135
+ *
2136
+ * An array of supported digest functions can be retrieved using {@link getHashes}.
2137
+ * @since v0.9.3
2138
+ */
2139
+ function pbkdf2Sync(password: BinaryLike, salt: BinaryLike, iterations: number, keylen: number, digest: string): Buffer;
2140
+ /**
2141
+ * Generates cryptographically strong pseudorandom data. The `size` argument
2142
+ * is a number indicating the number of bytes to generate.
2143
+ *
2144
+ * If a `callback` function is provided, the bytes are generated asynchronously
2145
+ * and the `callback` function is invoked with two arguments: `err` and `buf`.
2146
+ * If an error occurs, `err` will be an `Error` object; otherwise it is `null`. The`buf` argument is a `Buffer` containing the generated bytes.
2147
+ *
2148
+ * ```js
2149
+ * // Asynchronous
2150
+ * const {
2151
+ * randomBytes,
2152
+ * } = await import('crypto');
2153
+ *
2154
+ * randomBytes(256, (err, buf) => {
2155
+ * if (err) throw err;
2156
+ * console.log(`${buf.length} bytes of random data: ${buf.toString('hex')}`);
2157
+ * });
2158
+ * ```
2159
+ *
2160
+ * ```js
2161
+ * // Asynchronous
2162
+ * const {
2163
+ * randomBytes,
2164
+ * } = require('crypto');
2165
+ *
2166
+ * randomBytes(256, (err, buf) => {
2167
+ * if (err) throw err;
2168
+ * console.log(`${buf.length} bytes of random data: ${buf.toString('hex')}`);
2169
+ * });
2170
+ * ```
2171
+ *
2172
+ * If the `callback` function is not provided, the random bytes are generated
2173
+ * synchronously and returned as a `Buffer`. An error will be thrown if
2174
+ * there is a problem generating the bytes.
2175
+ *
2176
+ * ```js
2177
+ * // Synchronous
2178
+ * const {
2179
+ * randomBytes,
2180
+ * } = await import('crypto');
2181
+ *
2182
+ * const buf = randomBytes(256);
2183
+ * console.log(
2184
+ * `${buf.length} bytes of random data: ${buf.toString('hex')}`);
2185
+ * ```
2186
+ *
2187
+ * ```js
2188
+ * // Synchronous
2189
+ * const {
2190
+ * randomBytes,
2191
+ * } = require('crypto');
2192
+ *
2193
+ * const buf = randomBytes(256);
2194
+ * console.log(
2195
+ * `${buf.length} bytes of random data: ${buf.toString('hex')}`);
2196
+ * ```
2197
+ *
2198
+ * The `crypto.randomBytes()` method will not complete until there is
2199
+ * sufficient entropy available.
2200
+ * This should normally never take longer than a few milliseconds. The only time
2201
+ * when generating the random bytes may conceivably block for a longer period of
2202
+ * time is right after boot, when the whole system is still low on entropy.
2203
+ *
2204
+ * This API uses libuv's threadpool, which can have surprising and
2205
+ * negative performance implications for some applications; see the `UV_THREADPOOL_SIZE` documentation for more information.
2206
+ *
2207
+ * The asynchronous version of `crypto.randomBytes()` is carried out in a single
2208
+ * threadpool request. To minimize threadpool task length variation, partition
2209
+ * large `randomBytes` requests when doing so as part of fulfilling a client
2210
+ * request.
2211
+ * @since v0.5.8
2212
+ * @param size The number of bytes to generate. The `size` must not be larger than `2**31 - 1`.
2213
+ * @return if the `callback` function is not provided.
2214
+ */
505
2215
  function randomBytes(size: number): Buffer;
506
2216
  function randomBytes(size: number, callback: (err: Error | null, buf: Buffer) => void): void;
507
2217
  function pseudoRandomBytes(size: number): Buffer;
508
2218
  function pseudoRandomBytes(size: number, callback: (err: Error | null, buf: Buffer) => void): void;
509
-
2219
+ /**
2220
+ * Return a random integer `n` such that `min <= n < max`. This
2221
+ * implementation avoids [modulo bias](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#Modulo_bias).
2222
+ *
2223
+ * The range (`max - min`) must be less than 248. `min` and `max` must
2224
+ * be [safe integers](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isSafeInteger).
2225
+ *
2226
+ * If the `callback` function is not provided, the random integer is
2227
+ * generated synchronously.
2228
+ *
2229
+ * ```js
2230
+ * // Asynchronous
2231
+ * const {
2232
+ * randomInt,
2233
+ * } = await import('crypto');
2234
+ *
2235
+ * randomInt(3, (err, n) => {
2236
+ * if (err) throw err;
2237
+ * console.log(`Random number chosen from (0, 1, 2): ${n}`);
2238
+ * });
2239
+ * ```
2240
+ *
2241
+ * ```js
2242
+ * // Asynchronous
2243
+ * const {
2244
+ * randomInt,
2245
+ * } = require('crypto');
2246
+ *
2247
+ * randomInt(3, (err, n) => {
2248
+ * if (err) throw err;
2249
+ * console.log(`Random number chosen from (0, 1, 2): ${n}`);
2250
+ * });
2251
+ * ```
2252
+ *
2253
+ * ```js
2254
+ * // Synchronous
2255
+ * const {
2256
+ * randomInt,
2257
+ * } = await import('crypto');
2258
+ *
2259
+ * const n = randomInt(3);
2260
+ * console.log(`Random number chosen from (0, 1, 2): ${n}`);
2261
+ * ```
2262
+ *
2263
+ * ```js
2264
+ * // Synchronous
2265
+ * const {
2266
+ * randomInt,
2267
+ * } = require('crypto');
2268
+ *
2269
+ * const n = randomInt(3);
2270
+ * console.log(`Random number chosen from (0, 1, 2): ${n}`);
2271
+ * ```
2272
+ *
2273
+ * ```js
2274
+ * // With `min` argument
2275
+ * const {
2276
+ * randomInt,
2277
+ * } = await import('crypto');
2278
+ *
2279
+ * const n = randomInt(1, 7);
2280
+ * console.log(`The dice rolled: ${n}`);
2281
+ * ```
2282
+ *
2283
+ * ```js
2284
+ * // With `min` argument
2285
+ * const {
2286
+ * randomInt,
2287
+ * } = require('crypto');
2288
+ *
2289
+ * const n = randomInt(1, 7);
2290
+ * console.log(`The dice rolled: ${n}`);
2291
+ * ```
2292
+ * @since v14.10.0, v12.19.0
2293
+ * @param min Start of random range (inclusive).
2294
+ * @param max End of random range (exclusive).
2295
+ * @param callback `function(err, n) {}`.
2296
+ */
510
2297
  function randomInt(max: number): number;
511
2298
  function randomInt(min: number, max: number): number;
512
2299
  function randomInt(max: number, callback: (err: Error | null, value: number) => void): void;
513
2300
  function randomInt(min: number, max: number, callback: (err: Error | null, value: number) => void): void;
514
-
2301
+ /**
2302
+ * Synchronous version of {@link randomFill}.
2303
+ *
2304
+ * ```js
2305
+ * const {
2306
+ * randomFillSync,
2307
+ * } = await import('crypto');
2308
+ *
2309
+ * const buf = Buffer.alloc(10);
2310
+ * console.log(randomFillSync(buf).toString('hex'));
2311
+ *
2312
+ * randomFillSync(buf, 5);
2313
+ * console.log(buf.toString('hex'));
2314
+ *
2315
+ * // The above is equivalent to the following:
2316
+ * randomFillSync(buf, 5, 5);
2317
+ * console.log(buf.toString('hex'));
2318
+ * ```
2319
+ *
2320
+ * ```js
2321
+ * const {
2322
+ * randomFillSync,
2323
+ * } = require('crypto');
2324
+ *
2325
+ * const buf = Buffer.alloc(10);
2326
+ * console.log(randomFillSync(buf).toString('hex'));
2327
+ *
2328
+ * randomFillSync(buf, 5);
2329
+ * console.log(buf.toString('hex'));
2330
+ *
2331
+ * // The above is equivalent to the following:
2332
+ * randomFillSync(buf, 5, 5);
2333
+ * console.log(buf.toString('hex'));
2334
+ * ```
2335
+ *
2336
+ * Any `ArrayBuffer`, `TypedArray` or `DataView` instance may be passed as`buffer`.
2337
+ *
2338
+ * ```js
2339
+ * const {
2340
+ * randomFillSync,
2341
+ * } = await import('crypto');
2342
+ *
2343
+ * const a = new Uint32Array(10);
2344
+ * console.log(Buffer.from(randomFillSync(a).buffer,
2345
+ * a.byteOffset, a.byteLength).toString('hex'));
2346
+ *
2347
+ * const b = new DataView(new ArrayBuffer(10));
2348
+ * console.log(Buffer.from(randomFillSync(b).buffer,
2349
+ * b.byteOffset, b.byteLength).toString('hex'));
2350
+ *
2351
+ * const c = new ArrayBuffer(10);
2352
+ * console.log(Buffer.from(randomFillSync(c)).toString('hex'));
2353
+ * ```
2354
+ *
2355
+ * ```js
2356
+ * const {
2357
+ * randomFillSync,
2358
+ * } = require('crypto');
2359
+ *
2360
+ * const a = new Uint32Array(10);
2361
+ * console.log(Buffer.from(randomFillSync(a).buffer,
2362
+ * a.byteOffset, a.byteLength).toString('hex'));
2363
+ *
2364
+ * const b = new DataView(new ArrayBuffer(10));
2365
+ * console.log(Buffer.from(randomFillSync(b).buffer,
2366
+ * b.byteOffset, b.byteLength).toString('hex'));
2367
+ *
2368
+ * const c = new ArrayBuffer(10);
2369
+ * console.log(Buffer.from(randomFillSync(c)).toString('hex'));
2370
+ * ```
2371
+ * @since v7.10.0, v6.13.0
2372
+ * @param buffer Must be supplied. The size of the provided `buffer` must not be larger than `2**31 - 1`.
2373
+ * @return The object passed as `buffer` argument.
2374
+ */
515
2375
  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
-
2376
+ /**
2377
+ * This function is similar to {@link randomBytes} but requires the first
2378
+ * argument to be a `Buffer` that will be filled. It also
2379
+ * requires that a callback is passed in.
2380
+ *
2381
+ * If the `callback` function is not provided, an error will be thrown.
2382
+ *
2383
+ * ```js
2384
+ * const {
2385
+ * randomFill,
2386
+ * } = await import('crypto');
2387
+ *
2388
+ * const buf = Buffer.alloc(10);
2389
+ * randomFill(buf, (err, buf) => {
2390
+ * if (err) throw err;
2391
+ * console.log(buf.toString('hex'));
2392
+ * });
2393
+ *
2394
+ * randomFill(buf, 5, (err, buf) => {
2395
+ * if (err) throw err;
2396
+ * console.log(buf.toString('hex'));
2397
+ * });
2398
+ *
2399
+ * // The above is equivalent to the following:
2400
+ * randomFill(buf, 5, 5, (err, buf) => {
2401
+ * if (err) throw err;
2402
+ * console.log(buf.toString('hex'));
2403
+ * });
2404
+ * ```
2405
+ *
2406
+ * ```js
2407
+ * const {
2408
+ * randomFill,
2409
+ * } = require('crypto');
2410
+ *
2411
+ * const buf = Buffer.alloc(10);
2412
+ * randomFill(buf, (err, buf) => {
2413
+ * if (err) throw err;
2414
+ * console.log(buf.toString('hex'));
2415
+ * });
2416
+ *
2417
+ * randomFill(buf, 5, (err, buf) => {
2418
+ * if (err) throw err;
2419
+ * console.log(buf.toString('hex'));
2420
+ * });
2421
+ *
2422
+ * // The above is equivalent to the following:
2423
+ * randomFill(buf, 5, 5, (err, buf) => {
2424
+ * if (err) throw err;
2425
+ * console.log(buf.toString('hex'));
2426
+ * });
2427
+ * ```
2428
+ *
2429
+ * Any `ArrayBuffer`, `TypedArray`, or `DataView` instance may be passed as`buffer`.
2430
+ *
2431
+ * While this includes instances of `Float32Array` and `Float64Array`, this
2432
+ * function should not be used to generate random floating-point numbers. The
2433
+ * result may contain `+Infinity`, `-Infinity`, and `NaN`, and even if the array
2434
+ * contains finite numbers only, they are not drawn from a uniform random
2435
+ * distribution and have no meaningful lower or upper bounds.
2436
+ *
2437
+ * ```js
2438
+ * const {
2439
+ * randomFill,
2440
+ * } = await import('crypto');
2441
+ *
2442
+ * const a = new Uint32Array(10);
2443
+ * randomFill(a, (err, buf) => {
2444
+ * if (err) throw err;
2445
+ * console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
2446
+ * .toString('hex'));
2447
+ * });
2448
+ *
2449
+ * const b = new DataView(new ArrayBuffer(10));
2450
+ * randomFill(b, (err, buf) => {
2451
+ * if (err) throw err;
2452
+ * console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
2453
+ * .toString('hex'));
2454
+ * });
2455
+ *
2456
+ * const c = new ArrayBuffer(10);
2457
+ * randomFill(c, (err, buf) => {
2458
+ * if (err) throw err;
2459
+ * console.log(Buffer.from(buf).toString('hex'));
2460
+ * });
2461
+ * ```
2462
+ *
2463
+ * ```js
2464
+ * const {
2465
+ * randomFill,
2466
+ * } = require('crypto');
2467
+ *
2468
+ * const a = new Uint32Array(10);
2469
+ * randomFill(a, (err, buf) => {
2470
+ * if (err) throw err;
2471
+ * console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
2472
+ * .toString('hex'));
2473
+ * });
2474
+ *
2475
+ * const b = new DataView(new ArrayBuffer(10));
2476
+ * randomFill(b, (err, buf) => {
2477
+ * if (err) throw err;
2478
+ * console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
2479
+ * .toString('hex'));
2480
+ * });
2481
+ *
2482
+ * const c = new ArrayBuffer(10);
2483
+ * randomFill(c, (err, buf) => {
2484
+ * if (err) throw err;
2485
+ * console.log(Buffer.from(buf).toString('hex'));
2486
+ * });
2487
+ * ```
2488
+ *
2489
+ * This API uses libuv's threadpool, which can have surprising and
2490
+ * negative performance implications for some applications; see the `UV_THREADPOOL_SIZE` documentation for more information.
2491
+ *
2492
+ * The asynchronous version of `crypto.randomFill()` is carried out in a single
2493
+ * threadpool request. To minimize threadpool task length variation, partition
2494
+ * large `randomFill` requests when doing so as part of fulfilling a client
2495
+ * request.
2496
+ * @since v7.10.0, v6.13.0
2497
+ * @param buffer Must be supplied. The size of the provided `buffer` must not be larger than `2**31 - 1`.
2498
+ * @param callback `function(err, buf) {}`.
2499
+ */
2500
+ function randomFill<T extends NodeJS.ArrayBufferView>(buffer: T, callback: (err: Error | null, buf: T) => void): void;
2501
+ function randomFill<T extends NodeJS.ArrayBufferView>(buffer: T, offset: number, callback: (err: Error | null, buf: T) => void): void;
2502
+ function randomFill<T extends NodeJS.ArrayBufferView>(buffer: T, offset: number, size: number, callback: (err: Error | null, buf: T) => void): void;
532
2503
  interface ScryptOptions {
533
2504
  cost?: number | undefined;
534
2505
  blockSize?: number | undefined;
@@ -538,21 +2509,103 @@ declare module 'crypto' {
538
2509
  p?: number | undefined;
539
2510
  maxmem?: number | undefined;
540
2511
  }
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;
2512
+ /**
2513
+ * Provides an asynchronous [scrypt](https://en.wikipedia.org/wiki/Scrypt) implementation. Scrypt is a password-based
2514
+ * key derivation function that is designed to be expensive computationally and
2515
+ * memory-wise in order to make brute-force attacks unrewarding.
2516
+ *
2517
+ * The `salt` should be as unique as possible. It is recommended that a salt is
2518
+ * random and at least 16 bytes long. See [NIST SP 800-132](https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf) for details.
2519
+ *
2520
+ * When passing strings for `password` or `salt`, please consider `caveats when using strings as inputs to cryptographic APIs`.
2521
+ *
2522
+ * 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
2523
+ * callback as a `Buffer`.
2524
+ *
2525
+ * An exception is thrown when any of the input arguments specify invalid values
2526
+ * or types.
2527
+ *
2528
+ * ```js
2529
+ * const {
2530
+ * scrypt,
2531
+ * } = await import('crypto');
2532
+ *
2533
+ * // Using the factory defaults.
2534
+ * scrypt('password', 'salt', 64, (err, derivedKey) => {
2535
+ * if (err) throw err;
2536
+ * console.log(derivedKey.toString('hex')); // '3745e48...08d59ae'
2537
+ * });
2538
+ * // Using a custom N parameter. Must be a power of two.
2539
+ * scrypt('password', 'salt', 64, { N: 1024 }, (err, derivedKey) => {
2540
+ * if (err) throw err;
2541
+ * console.log(derivedKey.toString('hex')); // '3745e48...aa39b34'
2542
+ * });
2543
+ * ```
2544
+ *
2545
+ * ```js
2546
+ * const {
2547
+ * scrypt,
2548
+ * } = require('crypto');
2549
+ *
2550
+ * // Using the factory defaults.
2551
+ * scrypt('password', 'salt', 64, (err, derivedKey) => {
2552
+ * if (err) throw err;
2553
+ * console.log(derivedKey.toString('hex')); // '3745e48...08d59ae'
2554
+ * });
2555
+ * // Using a custom N parameter. Must be a power of two.
2556
+ * scrypt('password', 'salt', 64, { N: 1024 }, (err, derivedKey) => {
2557
+ * if (err) throw err;
2558
+ * console.log(derivedKey.toString('hex')); // '3745e48...aa39b34'
2559
+ * });
2560
+ * ```
2561
+ * @since v10.5.0
2562
+ */
2563
+ function scrypt(password: BinaryLike, salt: BinaryLike, keylen: number, callback: (err: Error | null, derivedKey: Buffer) => void): void;
2564
+ function scrypt(password: BinaryLike, salt: BinaryLike, keylen: number, options: ScryptOptions, callback: (err: Error | null, derivedKey: Buffer) => void): void;
2565
+ /**
2566
+ * Provides a synchronous [scrypt](https://en.wikipedia.org/wiki/Scrypt) implementation. Scrypt is a password-based
2567
+ * key derivation function that is designed to be expensive computationally and
2568
+ * memory-wise in order to make brute-force attacks unrewarding.
2569
+ *
2570
+ * The `salt` should be as unique as possible. It is recommended that a salt is
2571
+ * random and at least 16 bytes long. See [NIST SP 800-132](https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf) for details.
2572
+ *
2573
+ * When passing strings for `password` or `salt`, please consider `caveats when using strings as inputs to cryptographic APIs`.
2574
+ *
2575
+ * An exception is thrown when key derivation fails, otherwise the derived key is
2576
+ * returned as a `Buffer`.
2577
+ *
2578
+ * An exception is thrown when any of the input arguments specify invalid values
2579
+ * or types.
2580
+ *
2581
+ * ```js
2582
+ * const {
2583
+ * scryptSync,
2584
+ * } = await import('crypto');
2585
+ * // Using the factory defaults.
2586
+ *
2587
+ * const key1 = scryptSync('password', 'salt', 64);
2588
+ * console.log(key1.toString('hex')); // '3745e48...08d59ae'
2589
+ * // Using a custom N parameter. Must be a power of two.
2590
+ * const key2 = scryptSync('password', 'salt', 64, { N: 1024 });
2591
+ * console.log(key2.toString('hex')); // '3745e48...aa39b34'
2592
+ * ```
2593
+ *
2594
+ * ```js
2595
+ * const {
2596
+ * scryptSync,
2597
+ * } = require('crypto');
2598
+ * // Using the factory defaults.
2599
+ *
2600
+ * const key1 = scryptSync('password', 'salt', 64);
2601
+ * console.log(key1.toString('hex')); // '3745e48...08d59ae'
2602
+ * // Using a custom N parameter. Must be a power of two.
2603
+ * const key2 = scryptSync('password', 'salt', 64, { N: 1024 });
2604
+ * console.log(key2.toString('hex')); // '3745e48...aa39b34'
2605
+ * ```
2606
+ * @since v10.5.0
2607
+ */
554
2608
  function scryptSync(password: BinaryLike, salt: BinaryLike, keylen: number, options?: ScryptOptions): Buffer;
555
-
556
2609
  interface RsaPublicKey {
557
2610
  key: KeyLike;
558
2611
  padding?: number | undefined;
@@ -567,114 +2620,375 @@ declare module 'crypto' {
567
2620
  oaepLabel?: NodeJS.TypedArray | undefined;
568
2621
  padding?: number | undefined;
569
2622
  }
2623
+ /**
2624
+ * Encrypts the content of `buffer` with `key` and returns a new `Buffer` with encrypted content. The returned data can be decrypted using
2625
+ * the corresponding private key, for example using {@link privateDecrypt}.
2626
+ *
2627
+ * If `key` is not a `KeyObject`, this function behaves as if`key` had been passed to {@link createPublicKey}. If it is an
2628
+ * object, the `padding` property can be passed. Otherwise, this function uses`RSA_PKCS1_OAEP_PADDING`.
2629
+ *
2630
+ * Because RSA public keys can be derived from private keys, a private key may
2631
+ * be passed instead of a public key.
2632
+ * @since v0.11.14
2633
+ */
570
2634
  function publicEncrypt(key: RsaPublicKey | RsaPrivateKey | KeyLike, buffer: NodeJS.ArrayBufferView): Buffer;
2635
+ /**
2636
+ * Decrypts `buffer` with `key`.`buffer` was previously encrypted using
2637
+ * the corresponding private key, for example using {@link privateEncrypt}.
2638
+ *
2639
+ * If `key` is not a `KeyObject`, this function behaves as if`key` had been passed to {@link createPublicKey}. If it is an
2640
+ * object, the `padding` property can be passed. Otherwise, this function uses`RSA_PKCS1_PADDING`.
2641
+ *
2642
+ * Because RSA public keys can be derived from private keys, a private key may
2643
+ * be passed instead of a public key.
2644
+ * @since v1.1.0
2645
+ */
571
2646
  function publicDecrypt(key: RsaPublicKey | RsaPrivateKey | KeyLike, buffer: NodeJS.ArrayBufferView): Buffer;
2647
+ /**
2648
+ * Decrypts `buffer` with `privateKey`. `buffer` was previously encrypted using
2649
+ * the corresponding public key, for example using {@link publicEncrypt}.
2650
+ *
2651
+ * If `privateKey` is not a `KeyObject`, this function behaves as if`privateKey` had been passed to {@link createPrivateKey}. If it is an
2652
+ * object, the `padding` property can be passed. Otherwise, this function uses`RSA_PKCS1_OAEP_PADDING`.
2653
+ * @since v0.11.14
2654
+ */
572
2655
  function privateDecrypt(private_key: RsaPrivateKey | KeyLike, buffer: NodeJS.ArrayBufferView): Buffer;
2656
+ /**
2657
+ * Encrypts `buffer` with `privateKey`. The returned data can be decrypted using
2658
+ * the corresponding public key, for example using {@link publicDecrypt}.
2659
+ *
2660
+ * If `privateKey` is not a `KeyObject`, this function behaves as if`privateKey` had been passed to {@link createPrivateKey}. If it is an
2661
+ * object, the `padding` property can be passed. Otherwise, this function uses`RSA_PKCS1_PADDING`.
2662
+ * @since v1.1.0
2663
+ */
573
2664
  function privateEncrypt(private_key: RsaPrivateKey | KeyLike, buffer: NodeJS.ArrayBufferView): Buffer;
2665
+ /**
2666
+ * ```js
2667
+ * const {
2668
+ * getCiphers,
2669
+ * } = await import('crypto');
2670
+ *
2671
+ * console.log(getCiphers()); // ['aes-128-cbc', 'aes-128-ccm', ...]
2672
+ * ```
2673
+ *
2674
+ * ```js
2675
+ * const {
2676
+ * getCiphers,
2677
+ * } = require('crypto');
2678
+ *
2679
+ * console.log(getCiphers()); // ['aes-128-cbc', 'aes-128-ccm', ...]
2680
+ * ```
2681
+ * @since v0.9.3
2682
+ * @return An array with the names of the supported cipher algorithms.
2683
+ */
574
2684
  function getCiphers(): string[];
2685
+ /**
2686
+ * ```js
2687
+ * const {
2688
+ * getCurves,
2689
+ * } = await import('crypto');
2690
+ *
2691
+ * console.log(getCurves()); // ['Oakley-EC2N-3', 'Oakley-EC2N-4', ...]
2692
+ * ```
2693
+ *
2694
+ * ```js
2695
+ * const {
2696
+ * getCurves,
2697
+ * } = require('crypto');
2698
+ *
2699
+ * console.log(getCurves()); // ['Oakley-EC2N-3', 'Oakley-EC2N-4', ...]
2700
+ * ```
2701
+ * @since v2.3.0
2702
+ * @return An array with the names of the supported elliptic curves.
2703
+ */
575
2704
  function getCurves(): string[];
2705
+ /**
2706
+ * @since v10.0.0
2707
+ * @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}.
2708
+ */
576
2709
  function getFips(): 1 | 0;
2710
+ /**
2711
+ * ```js
2712
+ * const {
2713
+ * getHashes,
2714
+ * } = await import('crypto');
2715
+ *
2716
+ * console.log(getHashes()); // ['DSA', 'DSA-SHA', 'DSA-SHA1', ...]
2717
+ * ```
2718
+ *
2719
+ * ```js
2720
+ * const {
2721
+ * getHashes,
2722
+ * } = require('crypto');
2723
+ *
2724
+ * console.log(getHashes()); // ['DSA', 'DSA-SHA', 'DSA-SHA1', ...]
2725
+ * ```
2726
+ * @since v0.9.3
2727
+ * @return An array of the names of the supported hash algorithms, such as `'RSA-SHA256'`. Hash algorithms are also called "digest" algorithms.
2728
+ */
577
2729
  function getHashes(): string[];
2730
+ /**
2731
+ * The `ECDH` class is a utility for creating Elliptic Curve Diffie-Hellman (ECDH)
2732
+ * key exchanges.
2733
+ *
2734
+ * Instances of the `ECDH` class can be created using the {@link createECDH} function.
2735
+ *
2736
+ * ```js
2737
+ * import assert from 'assert';
2738
+ *
2739
+ * const {
2740
+ * createECDH,
2741
+ * } = await import('crypto');
2742
+ *
2743
+ * // Generate Alice's keys...
2744
+ * const alice = createECDH('secp521r1');
2745
+ * const aliceKey = alice.generateKeys();
2746
+ *
2747
+ * // Generate Bob's keys...
2748
+ * const bob = createECDH('secp521r1');
2749
+ * const bobKey = bob.generateKeys();
2750
+ *
2751
+ * // Exchange and generate the secret...
2752
+ * const aliceSecret = alice.computeSecret(bobKey);
2753
+ * const bobSecret = bob.computeSecret(aliceKey);
2754
+ *
2755
+ * assert.strictEqual(aliceSecret.toString('hex'), bobSecret.toString('hex'));
2756
+ * // OK
2757
+ * ```
2758
+ *
2759
+ * ```js
2760
+ * const assert = require('assert');
2761
+ *
2762
+ * const {
2763
+ * createECDH,
2764
+ * } = require('crypto');
2765
+ *
2766
+ * // Generate Alice's keys...
2767
+ * const alice = createECDH('secp521r1');
2768
+ * const aliceKey = alice.generateKeys();
2769
+ *
2770
+ * // Generate Bob's keys...
2771
+ * const bob = createECDH('secp521r1');
2772
+ * const bobKey = bob.generateKeys();
2773
+ *
2774
+ * // Exchange and generate the secret...
2775
+ * const aliceSecret = alice.computeSecret(bobKey);
2776
+ * const bobSecret = bob.computeSecret(aliceKey);
2777
+ *
2778
+ * assert.strictEqual(aliceSecret.toString('hex'), bobSecret.toString('hex'));
2779
+ * // OK
2780
+ * ```
2781
+ * @since v0.11.14
2782
+ */
578
2783
  class ECDH {
579
2784
  private constructor();
2785
+ /**
2786
+ * Converts the EC Diffie-Hellman public key specified by `key` and `curve` to the
2787
+ * format specified by `format`. The `format` argument specifies point encoding
2788
+ * and can be `'compressed'`, `'uncompressed'` or `'hybrid'`. The supplied key is
2789
+ * interpreted using the specified `inputEncoding`, and the returned key is encoded
2790
+ * using the specified `outputEncoding`.
2791
+ *
2792
+ * Use {@link getCurves} to obtain a list of available curve names.
2793
+ * On recent OpenSSL releases, `openssl ecparam -list_curves` will also display
2794
+ * the name and description of each available elliptic curve.
2795
+ *
2796
+ * If `format` is not specified the point will be returned in `'uncompressed'`format.
2797
+ *
2798
+ * If the `inputEncoding` is not provided, `key` is expected to be a `Buffer`,`TypedArray`, or `DataView`.
2799
+ *
2800
+ * Example (uncompressing a key):
2801
+ *
2802
+ * ```js
2803
+ * const {
2804
+ * createECDH,
2805
+ * ECDH,
2806
+ * } = await import('crypto');
2807
+ *
2808
+ * const ecdh = createECDH('secp256k1');
2809
+ * ecdh.generateKeys();
2810
+ *
2811
+ * const compressedKey = ecdh.getPublicKey('hex', 'compressed');
2812
+ *
2813
+ * const uncompressedKey = ECDH.convertKey(compressedKey,
2814
+ * 'secp256k1',
2815
+ * 'hex',
2816
+ * 'hex',
2817
+ * 'uncompressed');
2818
+ *
2819
+ * // The converted key and the uncompressed public key should be the same
2820
+ * console.log(uncompressedKey === ecdh.getPublicKey('hex'));
2821
+ * ```
2822
+ *
2823
+ * ```js
2824
+ * const {
2825
+ * createECDH,
2826
+ * ECDH,
2827
+ * } = require('crypto');
2828
+ *
2829
+ * const ecdh = createECDH('secp256k1');
2830
+ * ecdh.generateKeys();
2831
+ *
2832
+ * const compressedKey = ecdh.getPublicKey('hex', 'compressed');
2833
+ *
2834
+ * const uncompressedKey = ECDH.convertKey(compressedKey,
2835
+ * 'secp256k1',
2836
+ * 'hex',
2837
+ * 'hex',
2838
+ * 'uncompressed');
2839
+ *
2840
+ * // The converted key and the uncompressed public key should be the same
2841
+ * console.log(uncompressedKey === ecdh.getPublicKey('hex'));
2842
+ * ```
2843
+ * @since v10.0.0
2844
+ * @param inputEncoding The `encoding` of the `key` string.
2845
+ * @param outputEncoding The `encoding` of the return value.
2846
+ */
580
2847
  static convertKey(
581
2848
  key: BinaryLike,
582
2849
  curve: string,
583
2850
  inputEncoding?: BinaryToTextEncoding,
584
2851
  outputEncoding?: 'latin1' | 'hex' | 'base64',
585
- format?: 'uncompressed' | 'compressed' | 'hybrid',
2852
+ format?: 'uncompressed' | 'compressed' | 'hybrid'
586
2853
  ): Buffer | string;
2854
+ /**
2855
+ * Generates private and public EC Diffie-Hellman key values, and returns
2856
+ * the public key in the specified `format` and `encoding`. This key should be
2857
+ * transferred to the other party.
2858
+ *
2859
+ * 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.
2860
+ *
2861
+ * If `encoding` is provided a string is returned; otherwise a `Buffer` is returned.
2862
+ * @since v0.11.14
2863
+ * @param encoding The `encoding` of the return value.
2864
+ */
587
2865
  generateKeys(): Buffer;
588
2866
  generateKeys(encoding: BinaryToTextEncoding, format?: ECDHKeyFormat): string;
2867
+ /**
2868
+ * Computes the shared secret using `otherPublicKey` as the other
2869
+ * party's public key and returns the computed shared secret. The supplied
2870
+ * key is interpreted using specified `inputEncoding`, and the returned secret
2871
+ * is encoded using the specified `outputEncoding`.
2872
+ * If the `inputEncoding` is not
2873
+ * provided, `otherPublicKey` is expected to be a `Buffer`, `TypedArray`, or`DataView`.
2874
+ *
2875
+ * If `outputEncoding` is given a string will be returned; otherwise a `Buffer` is returned.
2876
+ *
2877
+ * `ecdh.computeSecret` will throw an`ERR_CRYPTO_ECDH_INVALID_PUBLIC_KEY` error when `otherPublicKey`lies outside of the elliptic curve. Since `otherPublicKey` is
2878
+ * usually supplied from a remote user over an insecure network,
2879
+ * be sure to handle this exception accordingly.
2880
+ * @since v0.11.14
2881
+ * @param inputEncoding The `encoding` of the `otherPublicKey` string.
2882
+ * @param outputEncoding The `encoding` of the return value.
2883
+ */
589
2884
  computeSecret(other_public_key: NodeJS.ArrayBufferView): Buffer;
590
2885
  computeSecret(other_public_key: string, input_encoding: BinaryToTextEncoding): Buffer;
591
2886
  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;
2887
+ computeSecret(other_public_key: string, input_encoding: BinaryToTextEncoding, output_encoding: BinaryToTextEncoding): string;
2888
+ /**
2889
+ * If `encoding` is specified, a string is returned; otherwise a `Buffer` is
2890
+ * returned.
2891
+ * @since v0.11.14
2892
+ * @param encoding The `encoding` of the return value.
2893
+ * @return The EC Diffie-Hellman in the specified `encoding`.
2894
+ */
597
2895
  getPrivateKey(): Buffer;
598
2896
  getPrivateKey(encoding: BinaryToTextEncoding): string;
2897
+ /**
2898
+ * 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.
2899
+ *
2900
+ * If `encoding` is specified, a string is returned; otherwise a `Buffer` is
2901
+ * returned.
2902
+ * @since v0.11.14
2903
+ * @param encoding The `encoding` of the return value.
2904
+ * @return The EC Diffie-Hellman public key in the specified `encoding` and `format`.
2905
+ */
599
2906
  getPublicKey(): Buffer;
600
2907
  getPublicKey(encoding: BinaryToTextEncoding, format?: ECDHKeyFormat): string;
2908
+ /**
2909
+ * Sets the EC Diffie-Hellman private key.
2910
+ * If `encoding` is provided, `privateKey` is expected
2911
+ * to be a string; otherwise `privateKey` is expected to be a `Buffer`,`TypedArray`, or `DataView`.
2912
+ *
2913
+ * If `privateKey` is not valid for the curve specified when the `ECDH` object was
2914
+ * created, an error is thrown. Upon setting the private key, the associated
2915
+ * public point (key) is also generated and set in the `ECDH` object.
2916
+ * @since v0.11.14
2917
+ * @param encoding The `encoding` of the `privateKey` string.
2918
+ */
601
2919
  setPrivateKey(private_key: NodeJS.ArrayBufferView): void;
602
2920
  setPrivateKey(private_key: string, encoding: BinaryToTextEncoding): void;
603
2921
  }
2922
+ /**
2923
+ * Creates an Elliptic Curve Diffie-Hellman (`ECDH`) key exchange object using a
2924
+ * predefined curve specified by the `curveName` string. Use {@link getCurves} to obtain a list of available curve names. On recent
2925
+ * OpenSSL releases, `openssl ecparam -list_curves` will also display the name
2926
+ * and description of each available elliptic curve.
2927
+ * @since v0.11.14
2928
+ */
604
2929
  function createECDH(curve_name: string): ECDH;
2930
+ /**
2931
+ * This function is based on a constant-time algorithm.
2932
+ * Returns true if `a` is equal to `b`, without leaking timing information that
2933
+ * would allow an attacker to guess one of the values. This is suitable for
2934
+ * comparing HMAC digests or secret values like authentication cookies or[capability urls](https://www.w3.org/TR/capability-urls/).
2935
+ *
2936
+ * `a` and `b` must both be `Buffer`s, `TypedArray`s, or `DataView`s, and they
2937
+ * must have the same byte length.
2938
+ *
2939
+ * If at least one of `a` and `b` is a `TypedArray` with more than one byte per
2940
+ * entry, such as `Uint16Array`, the result will be computed using the platform
2941
+ * byte order.
2942
+ *
2943
+ * Use of `crypto.timingSafeEqual` does not guarantee that the _surrounding_ code
2944
+ * is timing-safe. Care should be taken to ensure that the surrounding code does
2945
+ * not introduce timing vulnerabilities.
2946
+ * @since v6.6.0
2947
+ */
605
2948
  function timingSafeEqual(a: NodeJS.ArrayBufferView, b: NodeJS.ArrayBufferView): boolean;
606
2949
  /** @deprecated since v10.0.0 */
607
2950
  const DEFAULT_ENCODING: BufferEncoding;
608
-
609
2951
  type KeyType = 'rsa' | 'dsa' | 'ec' | 'ed25519' | 'ed448' | 'x25519' | 'x448';
610
2952
  type KeyFormat = 'pem' | 'der';
611
-
612
2953
  interface BasePrivateKeyEncodingOptions<T extends KeyFormat> {
613
2954
  format: T;
614
2955
  cipher?: string | undefined;
615
2956
  passphrase?: string | undefined;
616
2957
  }
617
-
618
2958
  interface KeyPairKeyObjectResult {
619
2959
  publicKey: KeyObject;
620
2960
  privateKey: KeyObject;
621
2961
  }
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
-
2962
+ interface ED25519KeyPairKeyObjectOptions {}
2963
+ interface ED448KeyPairKeyObjectOptions {}
2964
+ interface X25519KeyPairKeyObjectOptions {}
2965
+ interface X448KeyPairKeyObjectOptions {}
647
2966
  interface ECKeyPairKeyObjectOptions {
648
2967
  /**
649
2968
  * Name of the curve to use.
650
2969
  */
651
2970
  namedCurve: string;
652
2971
  }
653
-
654
2972
  interface RSAKeyPairKeyObjectOptions {
655
2973
  /**
656
2974
  * Key size in bits
657
2975
  */
658
2976
  modulusLength: number;
659
-
660
2977
  /**
661
2978
  * @default 0x10001
662
2979
  */
663
2980
  publicExponent?: number | undefined;
664
2981
  }
665
-
666
2982
  interface DSAKeyPairKeyObjectOptions {
667
2983
  /**
668
2984
  * Key size in bits
669
2985
  */
670
2986
  modulusLength: number;
671
-
672
2987
  /**
673
2988
  * Size of q in bits
674
2989
  */
675
2990
  divisorLength: number;
676
2991
  }
677
-
678
2992
  interface RSAKeyPairOptions<PubF extends KeyFormat, PrivF extends KeyFormat> {
679
2993
  /**
680
2994
  * Key size in bits
@@ -684,7 +2998,6 @@ declare module 'crypto' {
684
2998
  * @default 0x10001
685
2999
  */
686
3000
  publicExponent?: number | undefined;
687
-
688
3001
  publicKeyEncoding: {
689
3002
  type: 'pkcs1' | 'spki';
690
3003
  format: PubF;
@@ -693,7 +3006,6 @@ declare module 'crypto' {
693
3006
  type: 'pkcs1' | 'pkcs8';
694
3007
  };
695
3008
  }
696
-
697
3009
  interface DSAKeyPairOptions<PubF extends KeyFormat, PrivF extends KeyFormat> {
698
3010
  /**
699
3011
  * Key size in bits
@@ -703,7 +3015,6 @@ declare module 'crypto' {
703
3015
  * Size of q in bits
704
3016
  */
705
3017
  divisorLength: number;
706
-
707
3018
  publicKeyEncoding: {
708
3019
  type: 'spki';
709
3020
  format: PubF;
@@ -712,13 +3023,11 @@ declare module 'crypto' {
712
3023
  type: 'pkcs8';
713
3024
  };
714
3025
  }
715
-
716
3026
  interface ECKeyPairOptions<PubF extends KeyFormat, PrivF extends KeyFormat> {
717
3027
  /**
718
3028
  * Name of the curve to use.
719
3029
  */
720
3030
  namedCurve: string;
721
-
722
3031
  publicKeyEncoding: {
723
3032
  type: 'pkcs1' | 'spki';
724
3033
  format: PubF;
@@ -727,7 +3036,6 @@ declare module 'crypto' {
727
3036
  type: 'sec1' | 'pkcs8';
728
3037
  };
729
3038
  }
730
-
731
3039
  interface ED25519KeyPairOptions<PubF extends KeyFormat, PrivF extends KeyFormat> {
732
3040
  publicKeyEncoding: {
733
3041
  type: 'spki';
@@ -737,7 +3045,6 @@ declare module 'crypto' {
737
3045
  type: 'pkcs8';
738
3046
  };
739
3047
  }
740
-
741
3048
  interface ED448KeyPairOptions<PubF extends KeyFormat, PrivF extends KeyFormat> {
742
3049
  publicKeyEncoding: {
743
3050
  type: 'spki';
@@ -747,7 +3054,6 @@ declare module 'crypto' {
747
3054
  type: 'pkcs8';
748
3055
  };
749
3056
  }
750
-
751
3057
  interface X25519KeyPairOptions<PubF extends KeyFormat, PrivF extends KeyFormat> {
752
3058
  publicKeyEncoding: {
753
3059
  type: 'spki';
@@ -757,7 +3063,6 @@ declare module 'crypto' {
757
3063
  type: 'pkcs8';
758
3064
  };
759
3065
  }
760
-
761
3066
  interface X448KeyPairOptions<PubF extends KeyFormat, PrivF extends KeyFormat> {
762
3067
  publicKeyEncoding: {
763
3068
  type: 'spki';
@@ -767,488 +3072,446 @@ declare module 'crypto' {
767
3072
  type: 'pkcs8';
768
3073
  };
769
3074
  }
770
-
771
3075
  interface KeyPairSyncResult<T1 extends string | Buffer, T2 extends string | Buffer> {
772
3076
  publicKey: T1;
773
3077
  privateKey: T2;
774
3078
  }
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>;
3079
+ /**
3080
+ * Generates a new asymmetric key pair of the given `type`. RSA, DSA, EC, Ed25519,
3081
+ * Ed448, X25519, X448, and DH are currently supported.
3082
+ *
3083
+ * If a `publicKeyEncoding` or `privateKeyEncoding` was specified, this function
3084
+ * behaves as if `keyObject.export()` had been called on its result. Otherwise,
3085
+ * the respective part of the key is returned as a `KeyObject`.
3086
+ *
3087
+ * When encoding public keys, it is recommended to use `'spki'`. When encoding
3088
+ * private keys, it is recommended to use `'pkcs8'` with a strong passphrase,
3089
+ * and to keep the passphrase confidential.
3090
+ *
3091
+ * ```js
3092
+ * const {
3093
+ * generateKeyPairSync,
3094
+ * } = await import('crypto');
3095
+ *
3096
+ * const {
3097
+ * publicKey,
3098
+ * privateKey,
3099
+ * } = generateKeyPairSync('rsa', {
3100
+ * modulusLength: 4096,
3101
+ * publicKeyEncoding: {
3102
+ * type: 'spki',
3103
+ * format: 'pem'
3104
+ * },
3105
+ * privateKeyEncoding: {
3106
+ * type: 'pkcs8',
3107
+ * format: 'pem',
3108
+ * cipher: 'aes-256-cbc',
3109
+ * passphrase: 'top secret'
3110
+ * }
3111
+ * });
3112
+ * ```
3113
+ *
3114
+ * ```js
3115
+ * const {
3116
+ * generateKeyPairSync,
3117
+ * } = require('crypto');
3118
+ *
3119
+ * const {
3120
+ * publicKey,
3121
+ * privateKey,
3122
+ * } = generateKeyPairSync('rsa', {
3123
+ * modulusLength: 4096,
3124
+ * publicKeyEncoding: {
3125
+ * type: 'spki',
3126
+ * format: 'pem'
3127
+ * },
3128
+ * privateKeyEncoding: {
3129
+ * type: 'pkcs8',
3130
+ * format: 'pem',
3131
+ * cipher: 'aes-256-cbc',
3132
+ * passphrase: 'top secret'
3133
+ * }
3134
+ * });
3135
+ * ```
3136
+ *
3137
+ * The return value `{ publicKey, privateKey }` represents the generated key pair.
3138
+ * When PEM encoding was selected, the respective key will be a string, otherwise
3139
+ * it will be a buffer containing the data encoded as DER.
3140
+ * @since v10.12.0
3141
+ * @param type Must be `'rsa'`, `'dsa'`, `'ec'`, `'ed25519'`, `'ed448'`, `'x25519'`, `'x448'`, or `'dh'`.
3142
+ */
3143
+ function generateKeyPairSync(type: 'rsa', options: RSAKeyPairOptions<'pem', 'pem'>): KeyPairSyncResult<string, string>;
3144
+ function generateKeyPairSync(type: 'rsa', options: RSAKeyPairOptions<'pem', 'der'>): KeyPairSyncResult<string, Buffer>;
3145
+ function generateKeyPairSync(type: 'rsa', options: RSAKeyPairOptions<'der', 'pem'>): KeyPairSyncResult<Buffer, string>;
3146
+ function generateKeyPairSync(type: 'rsa', options: RSAKeyPairOptions<'der', 'der'>): KeyPairSyncResult<Buffer, Buffer>;
792
3147
  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>;
3148
+ function generateKeyPairSync(type: 'dsa', options: DSAKeyPairOptions<'pem', 'pem'>): KeyPairSyncResult<string, string>;
3149
+ function generateKeyPairSync(type: 'dsa', options: DSAKeyPairOptions<'pem', 'der'>): KeyPairSyncResult<string, Buffer>;
3150
+ function generateKeyPairSync(type: 'dsa', options: DSAKeyPairOptions<'der', 'pem'>): KeyPairSyncResult<Buffer, string>;
3151
+ function generateKeyPairSync(type: 'dsa', options: DSAKeyPairOptions<'der', 'der'>): KeyPairSyncResult<Buffer, Buffer>;
810
3152
  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>;
3153
+ function generateKeyPairSync(type: 'ec', options: ECKeyPairOptions<'pem', 'pem'>): KeyPairSyncResult<string, string>;
3154
+ function generateKeyPairSync(type: 'ec', options: ECKeyPairOptions<'pem', 'der'>): KeyPairSyncResult<string, Buffer>;
3155
+ function generateKeyPairSync(type: 'ec', options: ECKeyPairOptions<'der', 'pem'>): KeyPairSyncResult<Buffer, string>;
3156
+ function generateKeyPairSync(type: 'ec', options: ECKeyPairOptions<'der', 'der'>): KeyPairSyncResult<Buffer, Buffer>;
828
3157
  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>;
3158
+ function generateKeyPairSync(type: 'ed25519', options: ED25519KeyPairOptions<'pem', 'pem'>): KeyPairSyncResult<string, string>;
3159
+ function generateKeyPairSync(type: 'ed25519', options: ED25519KeyPairOptions<'pem', 'der'>): KeyPairSyncResult<string, Buffer>;
3160
+ function generateKeyPairSync(type: 'ed25519', options: ED25519KeyPairOptions<'der', 'pem'>): KeyPairSyncResult<Buffer, string>;
3161
+ function generateKeyPairSync(type: 'ed25519', options: ED25519KeyPairOptions<'der', 'der'>): KeyPairSyncResult<Buffer, Buffer>;
846
3162
  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>;
3163
+ function generateKeyPairSync(type: 'ed448', options: ED448KeyPairOptions<'pem', 'pem'>): KeyPairSyncResult<string, string>;
3164
+ function generateKeyPairSync(type: 'ed448', options: ED448KeyPairOptions<'pem', 'der'>): KeyPairSyncResult<string, Buffer>;
3165
+ function generateKeyPairSync(type: 'ed448', options: ED448KeyPairOptions<'der', 'pem'>): KeyPairSyncResult<Buffer, string>;
3166
+ function generateKeyPairSync(type: 'ed448', options: ED448KeyPairOptions<'der', 'der'>): KeyPairSyncResult<Buffer, Buffer>;
864
3167
  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>;
3168
+ function generateKeyPairSync(type: 'x25519', options: X25519KeyPairOptions<'pem', 'pem'>): KeyPairSyncResult<string, string>;
3169
+ function generateKeyPairSync(type: 'x25519', options: X25519KeyPairOptions<'pem', 'der'>): KeyPairSyncResult<string, Buffer>;
3170
+ function generateKeyPairSync(type: 'x25519', options: X25519KeyPairOptions<'der', 'pem'>): KeyPairSyncResult<Buffer, string>;
3171
+ function generateKeyPairSync(type: 'x25519', options: X25519KeyPairOptions<'der', 'der'>): KeyPairSyncResult<Buffer, Buffer>;
882
3172
  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>;
3173
+ function generateKeyPairSync(type: 'x448', options: X448KeyPairOptions<'pem', 'pem'>): KeyPairSyncResult<string, string>;
3174
+ function generateKeyPairSync(type: 'x448', options: X448KeyPairOptions<'pem', 'der'>): KeyPairSyncResult<string, Buffer>;
3175
+ function generateKeyPairSync(type: 'x448', options: X448KeyPairOptions<'der', 'pem'>): KeyPairSyncResult<Buffer, string>;
3176
+ function generateKeyPairSync(type: 'x448', options: X448KeyPairOptions<'der', 'der'>): KeyPairSyncResult<Buffer, Buffer>;
900
3177
  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
-
3178
+ /**
3179
+ * Generates a new asymmetric key pair of the given `type`. RSA, DSA, EC, Ed25519,
3180
+ * Ed448, X25519, X448, and DH are currently supported.
3181
+ *
3182
+ * If a `publicKeyEncoding` or `privateKeyEncoding` was specified, this function
3183
+ * behaves as if `keyObject.export()` had been called on its result. Otherwise,
3184
+ * the respective part of the key is returned as a `KeyObject`.
3185
+ *
3186
+ * It is recommended to encode public keys as `'spki'` and private keys as`'pkcs8'` with encryption for long-term storage:
3187
+ *
3188
+ * ```js
3189
+ * const {
3190
+ * generateKeyPair,
3191
+ * } = await import('crypto');
3192
+ *
3193
+ * generateKeyPair('rsa', {
3194
+ * modulusLength: 4096,
3195
+ * publicKeyEncoding: {
3196
+ * type: 'spki',
3197
+ * format: 'pem'
3198
+ * },
3199
+ * privateKeyEncoding: {
3200
+ * type: 'pkcs8',
3201
+ * format: 'pem',
3202
+ * cipher: 'aes-256-cbc',
3203
+ * passphrase: 'top secret'
3204
+ * }
3205
+ * }, (err, publicKey, privateKey) => {
3206
+ * // Handle errors and use the generated key pair.
3207
+ * });
3208
+ * ```
3209
+ *
3210
+ * ```js
3211
+ * const {
3212
+ * generateKeyPair,
3213
+ * } = require('crypto');
3214
+ *
3215
+ * generateKeyPair('rsa', {
3216
+ * modulusLength: 4096,
3217
+ * publicKeyEncoding: {
3218
+ * type: 'spki',
3219
+ * format: 'pem'
3220
+ * },
3221
+ * privateKeyEncoding: {
3222
+ * type: 'pkcs8',
3223
+ * format: 'pem',
3224
+ * cipher: 'aes-256-cbc',
3225
+ * passphrase: 'top secret'
3226
+ * }
3227
+ * }, (err, publicKey, privateKey) => {
3228
+ * // Handle errors and use the generated key pair.
3229
+ * });
3230
+ * ```
3231
+ *
3232
+ * On completion, `callback` will be called with `err` set to `undefined` and`publicKey` / `privateKey` representing the generated key pair.
3233
+ *
3234
+ * If this method is invoked as its `util.promisify()` ed version, it returns
3235
+ * a `Promise` for an `Object` with `publicKey` and `privateKey` properties.
3236
+ * @since v10.12.0
3237
+ * @param type Must be `'rsa'`, `'dsa'`, `'ec'`, `'ed25519'`, `'ed448'`, `'x25519'`, `'x448'`, or `'dh'`.
3238
+ */
3239
+ function generateKeyPair(type: 'rsa', options: RSAKeyPairOptions<'pem', 'pem'>, callback: (err: Error | null, publicKey: string, privateKey: string) => void): void;
3240
+ function generateKeyPair(type: 'rsa', options: RSAKeyPairOptions<'pem', 'der'>, callback: (err: Error | null, publicKey: string, privateKey: Buffer) => void): void;
3241
+ function generateKeyPair(type: 'rsa', options: RSAKeyPairOptions<'der', 'pem'>, callback: (err: Error | null, publicKey: Buffer, privateKey: string) => void): void;
3242
+ function generateKeyPair(type: 'rsa', options: RSAKeyPairOptions<'der', 'der'>, callback: (err: Error | null, publicKey: Buffer, privateKey: Buffer) => void): void;
3243
+ function generateKeyPair(type: 'rsa', options: RSAKeyPairKeyObjectOptions, callback: (err: Error | null, publicKey: KeyObject, privateKey: KeyObject) => void): void;
3244
+ function generateKeyPair(type: 'dsa', options: DSAKeyPairOptions<'pem', 'pem'>, callback: (err: Error | null, publicKey: string, privateKey: string) => void): void;
3245
+ function generateKeyPair(type: 'dsa', options: DSAKeyPairOptions<'pem', 'der'>, callback: (err: Error | null, publicKey: string, privateKey: Buffer) => void): void;
3246
+ function generateKeyPair(type: 'dsa', options: DSAKeyPairOptions<'der', 'pem'>, callback: (err: Error | null, publicKey: Buffer, privateKey: string) => void): void;
3247
+ function generateKeyPair(type: 'dsa', options: DSAKeyPairOptions<'der', 'der'>, callback: (err: Error | null, publicKey: Buffer, privateKey: Buffer) => void): void;
3248
+ function generateKeyPair(type: 'dsa', options: DSAKeyPairKeyObjectOptions, callback: (err: Error | null, publicKey: KeyObject, privateKey: KeyObject) => void): void;
3249
+ function generateKeyPair(type: 'ec', options: ECKeyPairOptions<'pem', 'pem'>, callback: (err: Error | null, publicKey: string, privateKey: string) => void): void;
3250
+ function generateKeyPair(type: 'ec', options: ECKeyPairOptions<'pem', 'der'>, callback: (err: Error | null, publicKey: string, privateKey: Buffer) => void): void;
3251
+ function generateKeyPair(type: 'ec', options: ECKeyPairOptions<'der', 'pem'>, callback: (err: Error | null, publicKey: Buffer, privateKey: string) => void): void;
3252
+ function generateKeyPair(type: 'ec', options: ECKeyPairOptions<'der', 'der'>, callback: (err: Error | null, publicKey: Buffer, privateKey: Buffer) => void): void;
3253
+ function generateKeyPair(type: 'ec', options: ECKeyPairKeyObjectOptions, callback: (err: Error | null, publicKey: KeyObject, privateKey: KeyObject) => void): void;
3254
+ function generateKeyPair(type: 'ed25519', options: ED25519KeyPairOptions<'pem', 'pem'>, callback: (err: Error | null, publicKey: string, privateKey: string) => void): void;
3255
+ function generateKeyPair(type: 'ed25519', options: ED25519KeyPairOptions<'pem', 'der'>, callback: (err: Error | null, publicKey: string, privateKey: Buffer) => void): void;
3256
+ function generateKeyPair(type: 'ed25519', options: ED25519KeyPairOptions<'der', 'pem'>, callback: (err: Error | null, publicKey: Buffer, privateKey: string) => void): void;
3257
+ function generateKeyPair(type: 'ed25519', options: ED25519KeyPairOptions<'der', 'der'>, callback: (err: Error | null, publicKey: Buffer, privateKey: Buffer) => void): void;
3258
+ function generateKeyPair(type: 'ed25519', options: ED25519KeyPairKeyObjectOptions | undefined, callback: (err: Error | null, publicKey: KeyObject, privateKey: KeyObject) => void): void;
3259
+ function generateKeyPair(type: 'ed448', options: ED448KeyPairOptions<'pem', 'pem'>, callback: (err: Error | null, publicKey: string, privateKey: string) => void): void;
3260
+ function generateKeyPair(type: 'ed448', options: ED448KeyPairOptions<'pem', 'der'>, callback: (err: Error | null, publicKey: string, privateKey: Buffer) => void): void;
3261
+ function generateKeyPair(type: 'ed448', options: ED448KeyPairOptions<'der', 'pem'>, callback: (err: Error | null, publicKey: Buffer, privateKey: string) => void): void;
3262
+ function generateKeyPair(type: 'ed448', options: ED448KeyPairOptions<'der', 'der'>, callback: (err: Error | null, publicKey: Buffer, privateKey: Buffer) => void): void;
3263
+ function generateKeyPair(type: 'ed448', options: ED448KeyPairKeyObjectOptions | undefined, callback: (err: Error | null, publicKey: KeyObject, privateKey: KeyObject) => void): void;
3264
+ function generateKeyPair(type: 'x25519', options: X25519KeyPairOptions<'pem', 'pem'>, callback: (err: Error | null, publicKey: string, privateKey: string) => void): void;
3265
+ function generateKeyPair(type: 'x25519', options: X25519KeyPairOptions<'pem', 'der'>, callback: (err: Error | null, publicKey: string, privateKey: Buffer) => void): void;
3266
+ function generateKeyPair(type: 'x25519', options: X25519KeyPairOptions<'der', 'pem'>, callback: (err: Error | null, publicKey: Buffer, privateKey: string) => void): void;
3267
+ function generateKeyPair(type: 'x25519', options: X25519KeyPairOptions<'der', 'der'>, callback: (err: Error | null, publicKey: Buffer, privateKey: Buffer) => void): void;
3268
+ function generateKeyPair(type: 'x25519', options: X25519KeyPairKeyObjectOptions | undefined, callback: (err: Error | null, publicKey: KeyObject, privateKey: KeyObject) => void): void;
3269
+ function generateKeyPair(type: 'x448', options: X448KeyPairOptions<'pem', 'pem'>, callback: (err: Error | null, publicKey: string, privateKey: string) => void): void;
3270
+ function generateKeyPair(type: 'x448', options: X448KeyPairOptions<'pem', 'der'>, callback: (err: Error | null, publicKey: string, privateKey: Buffer) => void): void;
3271
+ function generateKeyPair(type: 'x448', options: X448KeyPairOptions<'der', 'pem'>, callback: (err: Error | null, publicKey: Buffer, privateKey: string) => void): void;
3272
+ function generateKeyPair(type: 'x448', options: X448KeyPairOptions<'der', 'der'>, callback: (err: Error | null, publicKey: Buffer, privateKey: Buffer) => void): void;
3273
+ function generateKeyPair(type: 'x448', options: X448KeyPairKeyObjectOptions | undefined, callback: (err: Error | null, publicKey: KeyObject, privateKey: KeyObject) => void): void;
1084
3274
  namespace generateKeyPair {
1085
3275
  function __promisify__(
1086
3276
  type: 'rsa',
1087
- options: RSAKeyPairOptions<'pem', 'pem'>,
1088
- ): Promise<{ publicKey: string; privateKey: string }>;
3277
+ options: RSAKeyPairOptions<'pem', 'pem'>
3278
+ ): Promise<{
3279
+ publicKey: string;
3280
+ privateKey: string;
3281
+ }>;
1089
3282
  function __promisify__(
1090
3283
  type: 'rsa',
1091
- options: RSAKeyPairOptions<'pem', 'der'>,
1092
- ): Promise<{ publicKey: string; privateKey: Buffer }>;
3284
+ options: RSAKeyPairOptions<'pem', 'der'>
3285
+ ): Promise<{
3286
+ publicKey: string;
3287
+ privateKey: Buffer;
3288
+ }>;
1093
3289
  function __promisify__(
1094
3290
  type: 'rsa',
1095
- options: RSAKeyPairOptions<'der', 'pem'>,
1096
- ): Promise<{ publicKey: Buffer; privateKey: string }>;
3291
+ options: RSAKeyPairOptions<'der', 'pem'>
3292
+ ): Promise<{
3293
+ publicKey: Buffer;
3294
+ privateKey: string;
3295
+ }>;
1097
3296
  function __promisify__(
1098
3297
  type: 'rsa',
1099
- options: RSAKeyPairOptions<'der', 'der'>,
1100
- ): Promise<{ publicKey: Buffer; privateKey: Buffer }>;
3298
+ options: RSAKeyPairOptions<'der', 'der'>
3299
+ ): Promise<{
3300
+ publicKey: Buffer;
3301
+ privateKey: Buffer;
3302
+ }>;
1101
3303
  function __promisify__(type: 'rsa', options: RSAKeyPairKeyObjectOptions): Promise<KeyPairKeyObjectResult>;
1102
-
1103
3304
  function __promisify__(
1104
3305
  type: 'dsa',
1105
- options: DSAKeyPairOptions<'pem', 'pem'>,
1106
- ): Promise<{ publicKey: string; privateKey: string }>;
3306
+ options: DSAKeyPairOptions<'pem', 'pem'>
3307
+ ): Promise<{
3308
+ publicKey: string;
3309
+ privateKey: string;
3310
+ }>;
1107
3311
  function __promisify__(
1108
3312
  type: 'dsa',
1109
- options: DSAKeyPairOptions<'pem', 'der'>,
1110
- ): Promise<{ publicKey: string; privateKey: Buffer }>;
3313
+ options: DSAKeyPairOptions<'pem', 'der'>
3314
+ ): Promise<{
3315
+ publicKey: string;
3316
+ privateKey: Buffer;
3317
+ }>;
1111
3318
  function __promisify__(
1112
3319
  type: 'dsa',
1113
- options: DSAKeyPairOptions<'der', 'pem'>,
1114
- ): Promise<{ publicKey: Buffer; privateKey: string }>;
3320
+ options: DSAKeyPairOptions<'der', 'pem'>
3321
+ ): Promise<{
3322
+ publicKey: Buffer;
3323
+ privateKey: string;
3324
+ }>;
1115
3325
  function __promisify__(
1116
3326
  type: 'dsa',
1117
- options: DSAKeyPairOptions<'der', 'der'>,
1118
- ): Promise<{ publicKey: Buffer; privateKey: Buffer }>;
3327
+ options: DSAKeyPairOptions<'der', 'der'>
3328
+ ): Promise<{
3329
+ publicKey: Buffer;
3330
+ privateKey: Buffer;
3331
+ }>;
1119
3332
  function __promisify__(type: 'dsa', options: DSAKeyPairKeyObjectOptions): Promise<KeyPairKeyObjectResult>;
1120
-
1121
3333
  function __promisify__(
1122
3334
  type: 'ec',
1123
- options: ECKeyPairOptions<'pem', 'pem'>,
1124
- ): Promise<{ publicKey: string; privateKey: string }>;
3335
+ options: ECKeyPairOptions<'pem', 'pem'>
3336
+ ): Promise<{
3337
+ publicKey: string;
3338
+ privateKey: string;
3339
+ }>;
1125
3340
  function __promisify__(
1126
3341
  type: 'ec',
1127
- options: ECKeyPairOptions<'pem', 'der'>,
1128
- ): Promise<{ publicKey: string; privateKey: Buffer }>;
3342
+ options: ECKeyPairOptions<'pem', 'der'>
3343
+ ): Promise<{
3344
+ publicKey: string;
3345
+ privateKey: Buffer;
3346
+ }>;
1129
3347
  function __promisify__(
1130
3348
  type: 'ec',
1131
- options: ECKeyPairOptions<'der', 'pem'>,
1132
- ): Promise<{ publicKey: Buffer; privateKey: string }>;
3349
+ options: ECKeyPairOptions<'der', 'pem'>
3350
+ ): Promise<{
3351
+ publicKey: Buffer;
3352
+ privateKey: string;
3353
+ }>;
1133
3354
  function __promisify__(
1134
3355
  type: 'ec',
1135
- options: ECKeyPairOptions<'der', 'der'>,
1136
- ): Promise<{ publicKey: Buffer; privateKey: Buffer }>;
3356
+ options: ECKeyPairOptions<'der', 'der'>
3357
+ ): Promise<{
3358
+ publicKey: Buffer;
3359
+ privateKey: Buffer;
3360
+ }>;
1137
3361
  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
3362
  function __promisify__(
1144
3363
  type: 'ed25519',
1145
- options: ED25519KeyPairOptions<'pem', 'der'>,
1146
- ): Promise<{ publicKey: string; privateKey: Buffer }>;
3364
+ options: ED25519KeyPairOptions<'pem', 'pem'>
3365
+ ): Promise<{
3366
+ publicKey: string;
3367
+ privateKey: string;
3368
+ }>;
1147
3369
  function __promisify__(
1148
3370
  type: 'ed25519',
1149
- options: ED25519KeyPairOptions<'der', 'pem'>,
1150
- ): Promise<{ publicKey: Buffer; privateKey: string }>;
3371
+ options: ED25519KeyPairOptions<'pem', 'der'>
3372
+ ): Promise<{
3373
+ publicKey: string;
3374
+ privateKey: Buffer;
3375
+ }>;
1151
3376
  function __promisify__(
1152
3377
  type: 'ed25519',
1153
- options: ED25519KeyPairOptions<'der', 'der'>,
1154
- ): Promise<{ publicKey: Buffer; privateKey: Buffer }>;
3378
+ options: ED25519KeyPairOptions<'der', 'pem'>
3379
+ ): Promise<{
3380
+ publicKey: Buffer;
3381
+ privateKey: string;
3382
+ }>;
1155
3383
  function __promisify__(
1156
3384
  type: 'ed25519',
1157
- options?: ED25519KeyPairKeyObjectOptions,
1158
- ): Promise<KeyPairKeyObjectResult>;
1159
-
3385
+ options: ED25519KeyPairOptions<'der', 'der'>
3386
+ ): Promise<{
3387
+ publicKey: Buffer;
3388
+ privateKey: Buffer;
3389
+ }>;
3390
+ function __promisify__(type: 'ed25519', options?: ED25519KeyPairKeyObjectOptions): Promise<KeyPairKeyObjectResult>;
1160
3391
  function __promisify__(
1161
3392
  type: 'ed448',
1162
- options: ED448KeyPairOptions<'pem', 'pem'>,
1163
- ): Promise<{ publicKey: string; privateKey: string }>;
3393
+ options: ED448KeyPairOptions<'pem', 'pem'>
3394
+ ): Promise<{
3395
+ publicKey: string;
3396
+ privateKey: string;
3397
+ }>;
1164
3398
  function __promisify__(
1165
3399
  type: 'ed448',
1166
- options: ED448KeyPairOptions<'pem', 'der'>,
1167
- ): Promise<{ publicKey: string; privateKey: Buffer }>;
3400
+ options: ED448KeyPairOptions<'pem', 'der'>
3401
+ ): Promise<{
3402
+ publicKey: string;
3403
+ privateKey: Buffer;
3404
+ }>;
1168
3405
  function __promisify__(
1169
3406
  type: 'ed448',
1170
- options: ED448KeyPairOptions<'der', 'pem'>,
1171
- ): Promise<{ publicKey: Buffer; privateKey: string }>;
3407
+ options: ED448KeyPairOptions<'der', 'pem'>
3408
+ ): Promise<{
3409
+ publicKey: Buffer;
3410
+ privateKey: string;
3411
+ }>;
1172
3412
  function __promisify__(
1173
3413
  type: 'ed448',
1174
- options: ED448KeyPairOptions<'der', 'der'>,
1175
- ): Promise<{ publicKey: Buffer; privateKey: Buffer }>;
3414
+ options: ED448KeyPairOptions<'der', 'der'>
3415
+ ): Promise<{
3416
+ publicKey: Buffer;
3417
+ privateKey: Buffer;
3418
+ }>;
1176
3419
  function __promisify__(type: 'ed448', options?: ED448KeyPairKeyObjectOptions): Promise<KeyPairKeyObjectResult>;
1177
-
1178
3420
  function __promisify__(
1179
3421
  type: 'x25519',
1180
- options: X25519KeyPairOptions<'pem', 'pem'>,
1181
- ): Promise<{ publicKey: string; privateKey: string }>;
3422
+ options: X25519KeyPairOptions<'pem', 'pem'>
3423
+ ): Promise<{
3424
+ publicKey: string;
3425
+ privateKey: string;
3426
+ }>;
1182
3427
  function __promisify__(
1183
3428
  type: 'x25519',
1184
- options: X25519KeyPairOptions<'pem', 'der'>,
1185
- ): Promise<{ publicKey: string; privateKey: Buffer }>;
3429
+ options: X25519KeyPairOptions<'pem', 'der'>
3430
+ ): Promise<{
3431
+ publicKey: string;
3432
+ privateKey: Buffer;
3433
+ }>;
1186
3434
  function __promisify__(
1187
3435
  type: 'x25519',
1188
- options: X25519KeyPairOptions<'der', 'pem'>,
1189
- ): Promise<{ publicKey: Buffer; privateKey: string }>;
3436
+ options: X25519KeyPairOptions<'der', 'pem'>
3437
+ ): Promise<{
3438
+ publicKey: Buffer;
3439
+ privateKey: string;
3440
+ }>;
1190
3441
  function __promisify__(
1191
3442
  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
-
3443
+ options: X25519KeyPairOptions<'der', 'der'>
3444
+ ): Promise<{
3445
+ publicKey: Buffer;
3446
+ privateKey: Buffer;
3447
+ }>;
3448
+ function __promisify__(type: 'x25519', options?: X25519KeyPairKeyObjectOptions): Promise<KeyPairKeyObjectResult>;
1199
3449
  function __promisify__(
1200
3450
  type: 'x448',
1201
- options: X448KeyPairOptions<'pem', 'pem'>,
1202
- ): Promise<{ publicKey: string; privateKey: string }>;
3451
+ options: X448KeyPairOptions<'pem', 'pem'>
3452
+ ): Promise<{
3453
+ publicKey: string;
3454
+ privateKey: string;
3455
+ }>;
1203
3456
  function __promisify__(
1204
3457
  type: 'x448',
1205
- options: X448KeyPairOptions<'pem', 'der'>,
1206
- ): Promise<{ publicKey: string; privateKey: Buffer }>;
3458
+ options: X448KeyPairOptions<'pem', 'der'>
3459
+ ): Promise<{
3460
+ publicKey: string;
3461
+ privateKey: Buffer;
3462
+ }>;
1207
3463
  function __promisify__(
1208
3464
  type: 'x448',
1209
- options: X448KeyPairOptions<'der', 'pem'>,
1210
- ): Promise<{ publicKey: Buffer; privateKey: string }>;
3465
+ options: X448KeyPairOptions<'der', 'pem'>
3466
+ ): Promise<{
3467
+ publicKey: Buffer;
3468
+ privateKey: string;
3469
+ }>;
1211
3470
  function __promisify__(
1212
3471
  type: 'x448',
1213
- options: X448KeyPairOptions<'der', 'der'>,
1214
- ): Promise<{ publicKey: Buffer; privateKey: Buffer }>;
3472
+ options: X448KeyPairOptions<'der', 'der'>
3473
+ ): Promise<{
3474
+ publicKey: Buffer;
3475
+ privateKey: Buffer;
3476
+ }>;
1215
3477
  function __promisify__(type: 'x448', options?: X448KeyPairKeyObjectOptions): Promise<KeyPairKeyObjectResult>;
1216
3478
  }
1217
-
1218
3479
  /**
1219
3480
  * Calculates and returns the signature for `data` using the given private key and
1220
3481
  * algorithm. If `algorithm` is `null` or `undefined`, then the algorithm is
1221
3482
  * dependent upon the key type (especially Ed25519 and Ed448).
1222
3483
  *
1223
3484
  * If `key` is not a `KeyObject`, this function behaves as if `key` had been
1224
- * passed to `crypto.createPrivateKey().
3485
+ * passed to {@link createPrivateKey}. If it is an object, the following
3486
+ * additional properties can be passed:
3487
+ *
3488
+ * If the `callback` function is provided this function uses libuv's threadpool.
3489
+ * @since v12.0.0
1225
3490
  */
1226
- function sign(
1227
- algorithm: string | null | undefined,
1228
- data: NodeJS.ArrayBufferView,
1229
- key: KeyLike | SignKeyObjectInput | SignPrivateKeyInput,
1230
- ): Buffer;
3491
+ function sign(algorithm: string | null | undefined, data: NodeJS.ArrayBufferView, key: KeyLike | SignKeyObjectInput | SignPrivateKeyInput): Buffer;
1231
3492
  function sign(
1232
3493
  algorithm: string | null | undefined,
1233
3494
  data: NodeJS.ArrayBufferView,
1234
3495
  key: KeyLike | SignKeyObjectInput | SignPrivateKeyInput,
1235
3496
  callback: (error: Error | null, data: Buffer) => void
1236
3497
  ): void;
1237
-
1238
3498
  /**
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).
3499
+ * 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
3500
+ * key type (especially Ed25519 and Ed448).
1242
3501
  *
1243
3502
  * If `key` is not a `KeyObject`, this function behaves as if `key` had been
1244
- * passed to `crypto.createPublicKey()`.
3503
+ * passed to {@link createPublicKey}. If it is an object, the following
3504
+ * additional properties can be passed:
3505
+ *
3506
+ * The `signature` argument is the previously calculated signature for the `data`.
3507
+ *
3508
+ * Because public keys can be derived from private keys, a private key or a public
3509
+ * key may be passed for `key`.
3510
+ *
3511
+ * If the `callback` function is provided this function uses libuv's threadpool.
3512
+ * @since v12.0.0
1245
3513
  */
1246
- function verify(
1247
- algorithm: string | null | undefined,
1248
- data: NodeJS.ArrayBufferView,
1249
- key: KeyLike | VerifyKeyObjectInput | VerifyPublicKeyInput,
1250
- signature: NodeJS.ArrayBufferView,
1251
- ): boolean;
3514
+ function verify(algorithm: string | null | undefined, data: NodeJS.ArrayBufferView, key: KeyLike | VerifyKeyObjectInput | VerifyPublicKeyInput, signature: NodeJS.ArrayBufferView): boolean;
1252
3515
  function verify(
1253
3516
  algorithm: string | null | undefined,
1254
3517
  data: NodeJS.ArrayBufferView,
@@ -1256,16 +3519,13 @@ declare module 'crypto' {
1256
3519
  signature: NodeJS.ArrayBufferView,
1257
3520
  callback: (error: Error | null, result: boolean) => void
1258
3521
  ): void;
1259
-
1260
3522
  /**
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).
3523
+ * Computes the Diffie-Hellman secret based on a `privateKey` and a `publicKey`.
3524
+ * 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).
3525
+ * @since v13.9.0, v12.17.0
1264
3526
  */
1265
3527
  function diffieHellman(options: { privateKey: KeyObject; publicKey: KeyObject }): Buffer;
1266
-
1267
3528
  type CipherMode = 'cbc' | 'ccm' | 'cfb' | 'ctr' | 'ecb' | 'gcm' | 'ocb' | 'ofb' | 'stream' | 'wrap' | 'xts';
1268
-
1269
3529
  interface CipherInfoOptions {
1270
3530
  /**
1271
3531
  * A test key length.
@@ -1276,7 +3536,6 @@ declare module 'crypto' {
1276
3536
  */
1277
3537
  ivLength?: number | undefined;
1278
3538
  }
1279
-
1280
3539
  interface CipherInfo {
1281
3540
  /**
1282
3541
  * The name of the cipher.
@@ -1305,64 +3564,113 @@ declare module 'crypto' {
1305
3564
  */
1306
3565
  mode: CipherMode;
1307
3566
  }
1308
-
1309
3567
  /**
1310
3568
  * Returns information about a given cipher.
1311
3569
  *
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.
3570
+ * Some ciphers accept variable length keys and initialization vectors. By default,
3571
+ * the `crypto.getCipherInfo()` method will return the default values for these
3572
+ * ciphers. To test if a given key length or iv length is acceptable for given
3573
+ * cipher, use the `keyLength` and `ivLength` options. If the given values are
3574
+ * unacceptable, `undefined` will be returned.
3575
+ * @since v15.0.0
1317
3576
  * @param nameOrNid The name or nid of the cipher to query.
1318
3577
  */
1319
3578
  function getCipherInfo(nameOrNid: string | number, options?: CipherInfoOptions): CipherInfo | undefined;
1320
-
1321
3579
  /**
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.
3580
+ * 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.
3581
+ *
3582
+ * The supplied `callback` function is called with two arguments: `err` and`derivedKey`. If an errors occurs while deriving the key, `err` will be set;
3583
+ * otherwise `err` will be `null`. The successfully generated `derivedKey` will
3584
+ * 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
3585
+ * of the input arguments specify invalid values or types.
3586
+ *
3587
+ * ```js
3588
+ * const {
3589
+ * hkdf,
3590
+ * } = await import('crypto');
1324
3591
  *
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.
3592
+ * hkdf('sha512', 'key', 'salt', 'info', 64, (err, derivedKey) => {
3593
+ * if (err) throw err;
3594
+ * console.log(Buffer.from(derivedKey).toString('hex')); // '24156e2...5391653'
3595
+ * });
3596
+ * ```
3597
+ *
3598
+ * ```js
3599
+ * const {
3600
+ * hkdf,
3601
+ * } = require('crypto');
3602
+ *
3603
+ * hkdf('sha512', 'key', 'salt', 'info', 64, (err, derivedKey) => {
3604
+ * if (err) throw err;
3605
+ * console.log(Buffer.from(derivedKey).toString('hex')); // '24156e2...5391653'
3606
+ * });
3607
+ * ```
3608
+ * @since v15.0.0
3609
+ * @param digest The digest algorithm to use.
3610
+ * @param key The secret key. It must be at least one byte in length.
3611
+ * @param salt The salt value. Must be provided but can be zero-length.
3612
+ * @param info Additional info value. Must be provided but can be zero-length, and cannot be more than 1024 bytes.
3613
+ * @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`
3614
+ * generates 64-byte hashes, making the maximum HKDF output 16320 bytes).
1329
3615
  */
1330
3616
  function hkdf(digest: string, key: BinaryLike | KeyObject, salt: BinaryLike, info: BinaryLike, keylen: number, callback: (err: Error | null, derivedKey: ArrayBuffer) => void): void;
1331
-
1332
3617
  /**
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.
3618
+ * Provides a synchronous HKDF key derivation function as defined in RFC 5869\. The
3619
+ * given `key`, `salt` and `info` are used with the `digest` to derive a key of`keylen` bytes.
3620
+ *
3621
+ * 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).
3622
+ *
3623
+ * An error will be thrown if any of the input arguments specify invalid values or
3624
+ * types, or if the derived key cannot be generated.
1335
3625
  *
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.
3626
+ * ```js
3627
+ * const {
3628
+ * hkdfSync,
3629
+ * } = await import('crypto');
3630
+ *
3631
+ * const derivedKey = hkdfSync('sha512', 'key', 'salt', 'info', 64);
3632
+ * console.log(Buffer.from(derivedKey).toString('hex')); // '24156e2...5391653'
3633
+ * ```
3634
+ *
3635
+ * ```js
3636
+ * const {
3637
+ * hkdfSync,
3638
+ * } = require('crypto');
3639
+ *
3640
+ * const derivedKey = hkdfSync('sha512', 'key', 'salt', 'info', 64);
3641
+ * console.log(Buffer.from(derivedKey).toString('hex')); // '24156e2...5391653'
3642
+ * ```
3643
+ * @since v15.0.0
3644
+ * @param digest The digest algorithm to use.
3645
+ * @param key The secret key. It must be at least one byte in length.
3646
+ * @param salt The salt value. Must be provided but can be zero-length.
3647
+ * @param info Additional info value. Must be provided but can be zero-length, and cannot be more than 1024 bytes.
3648
+ * @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`
3649
+ * generates 64-byte hashes, making the maximum HKDF output 16320 bytes).
1339
3650
  */
1340
3651
  function hkdfSync(digest: string, key: BinaryLike | KeyObject, salt: BinaryLike, info: BinaryLike, keylen: number): ArrayBuffer;
1341
-
1342
3652
  interface SecureHeapUsage {
1343
3653
  /**
1344
3654
  * The total allocated secure heap size as specified using the `--secure-heap=n` command-line flag.
1345
3655
  */
1346
3656
  total: number;
1347
-
1348
3657
  /**
1349
3658
  * The minimum allocation from the secure heap as specified using the `--secure-heap-min` command-line flag.
1350
3659
  */
1351
3660
  min: number;
1352
-
1353
3661
  /**
1354
3662
  * The total number of bytes currently allocated from the secure heap.
1355
3663
  */
1356
3664
  used: number;
1357
-
1358
3665
  /**
1359
3666
  * The calculated ratio of `used` to `total` allocated bytes.
1360
3667
  */
1361
3668
  utilization: number;
1362
3669
  }
1363
-
3670
+ /**
3671
+ * @since v15.6.0
3672
+ */
1364
3673
  function secureHeapUsed(): SecureHeapUsage;
1365
-
1366
3674
  interface RandomUUIDOptions {
1367
3675
  /**
1368
3676
  * By default, to improve performance,
@@ -1374,166 +3682,183 @@ declare module 'crypto' {
1374
3682
  */
1375
3683
  disableEntropyCache?: boolean | undefined;
1376
3684
  }
1377
-
3685
+ /**
3686
+ * Generates a random [RFC 4122](https://www.rfc-editor.org/rfc/rfc4122.txt) Version 4 UUID. The UUID is generated using a
3687
+ * cryptographic pseudorandom number generator.
3688
+ * @since v15.6.0
3689
+ */
1378
3690
  function randomUUID(options?: RandomUUIDOptions): string;
1379
-
1380
3691
  interface X509CheckOptions {
1381
3692
  /**
1382
3693
  * @default 'always'
1383
3694
  */
1384
3695
  subject: 'always' | 'never';
1385
-
1386
3696
  /**
1387
3697
  * @default true
1388
3698
  */
1389
3699
  wildcards: boolean;
1390
-
1391
3700
  /**
1392
3701
  * @default true
1393
3702
  */
1394
3703
  partialWildcards: boolean;
1395
-
1396
3704
  /**
1397
3705
  * @default false
1398
3706
  */
1399
3707
  multiLabelWildcards: boolean;
1400
-
1401
3708
  /**
1402
3709
  * @default false
1403
3710
  */
1404
3711
  singleLabelSubdomains: boolean;
1405
3712
  }
1406
-
3713
+ /**
3714
+ * Encapsulates an X509 certificate and provides read-only access to
3715
+ * its information.
3716
+ *
3717
+ * ```js
3718
+ * const { X509Certificate } = await import('crypto');
3719
+ *
3720
+ * const x509 = new X509Certificate('{... pem encoded cert ...}');
3721
+ *
3722
+ * console.log(x509.subject);
3723
+ * ```
3724
+ *
3725
+ * ```js
3726
+ * const { X509Certificate } = require('crypto');
3727
+ *
3728
+ * const x509 = new X509Certificate('{... pem encoded cert ...}');
3729
+ *
3730
+ * console.log(x509.subject);
3731
+ * ```
3732
+ * @since v15.6.0
3733
+ */
1407
3734
  class X509Certificate {
1408
3735
  /**
1409
- * Will be `true` if this is a Certificate Authority (ca) certificate.
3736
+ * Will be \`true\` if this is a Certificate Authority (ca) certificate.
3737
+ * @since v15.6.0
1410
3738
  */
1411
3739
  readonly ca: boolean;
1412
-
1413
3740
  /**
1414
3741
  * The SHA-1 fingerprint of this certificate.
3742
+ * @since v15.6.0
1415
3743
  */
1416
3744
  readonly fingerprint: string;
1417
-
1418
3745
  /**
1419
3746
  * The SHA-256 fingerprint of this certificate.
3747
+ * @since v15.6.0
1420
3748
  */
1421
3749
  readonly fingerprint256: string;
1422
-
1423
3750
  /**
1424
3751
  * The complete subject of this certificate.
3752
+ * @since v15.6.0
1425
3753
  */
1426
3754
  readonly subject: string;
1427
-
1428
3755
  /**
1429
3756
  * The subject alternative name specified for this certificate.
3757
+ * @since v15.6.0
1430
3758
  */
1431
3759
  readonly subjectAltName: string;
1432
-
1433
3760
  /**
1434
3761
  * The information access content of this certificate.
3762
+ * @since v15.6.0
1435
3763
  */
1436
3764
  readonly infoAccess: string;
1437
-
1438
3765
  /**
1439
3766
  * An array detailing the key usages for this certificate.
3767
+ * @since v15.6.0
1440
3768
  */
1441
3769
  readonly keyUsage: string[];
1442
-
1443
3770
  /**
1444
3771
  * The issuer identification included in this certificate.
3772
+ * @since v15.6.0
1445
3773
  */
1446
3774
  readonly issuer: string;
1447
-
1448
3775
  /**
1449
- * The issuer certificate or `undefined` if the issuer certificate is not available.
3776
+ * The issuer certificate or `undefined` if the issuer certificate is not
3777
+ * available.
3778
+ * @since v15.9.0
1450
3779
  */
1451
3780
  readonly issuerCertificate?: X509Certificate | undefined;
1452
-
1453
3781
  /**
1454
- * The public key for this certificate.
3782
+ * The public key `<KeyObject>` for this certificate.
3783
+ * @since v15.6.0
1455
3784
  */
1456
3785
  readonly publicKey: KeyObject;
1457
-
1458
3786
  /**
1459
3787
  * A `Buffer` containing the DER encoding of this certificate.
3788
+ * @since v15.6.0
1460
3789
  */
1461
3790
  readonly raw: Buffer;
1462
-
1463
3791
  /**
1464
3792
  * The serial number of this certificate.
3793
+ * @since v15.6.0
1465
3794
  */
1466
3795
  readonly serialNumber: string;
1467
-
1468
3796
  /**
1469
- * Returns the PEM-encoded certificate.
3797
+ * The date/time from which this certificate is considered valid.
3798
+ * @since v15.6.0
1470
3799
  */
1471
3800
  readonly validFrom: string;
1472
-
1473
3801
  /**
1474
- * The date/time from which this certificate is considered valid.
3802
+ * The date/time until which this certificate is considered valid.
3803
+ * @since v15.6.0
1475
3804
  */
1476
3805
  readonly validTo: string;
1477
-
1478
3806
  constructor(buffer: BinaryLike);
1479
-
1480
3807
  /**
1481
3808
  * Checks whether the certificate matches the given email address.
1482
- *
1483
- * Returns `email` if the certificate matches,`undefined` if it does not.
3809
+ * @since v15.6.0
3810
+ * @return Returns `email` if the certificate matches, `undefined` if it does not.
1484
3811
  */
1485
3812
  checkEmail(email: string, options?: X509CheckOptions): string | undefined;
1486
-
1487
3813
  /**
1488
3814
  * Checks whether the certificate matches the given host name.
1489
- *
1490
- * Returns `name` if the certificate matches, `undefined` if it does not.
3815
+ * @since v15.6.0
3816
+ * @return Returns `name` if the certificate matches, `undefined` if it does not.
1491
3817
  */
1492
3818
  checkHost(name: string, options?: X509CheckOptions): string | undefined;
1493
-
1494
3819
  /**
1495
3820
  * 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.
3821
+ * @since v15.6.0
3822
+ * @return Returns `ip` if the certificate matches, `undefined` if it does not.
1498
3823
  */
1499
3824
  checkIP(ip: string, options?: X509CheckOptions): string | undefined;
1500
-
1501
3825
  /**
1502
3826
  * Checks whether this certificate was issued by the given `otherCert`.
3827
+ * @since v15.6.0
1503
3828
  */
1504
3829
  checkIssued(otherCert: X509Certificate): boolean;
1505
-
1506
3830
  /**
1507
- * Checks whether this certificate was issued by the given `otherCert`.
3831
+ * Checks whether the public key for this certificate is consistent with
3832
+ * the given private key.
3833
+ * @since v15.6.0
3834
+ * @param privateKey A private key.
1508
3835
  */
1509
3836
  checkPrivateKey(privateKey: KeyObject): boolean;
1510
-
1511
3837
  /**
1512
- * There is no standard JSON encoding for X509 certificates. The
1513
- * `toJSON()` method returns a string containing the PEM encoded
3838
+ * There is no standard JSON encoding for X509 certificates. The`toJSON()` method returns a string containing the PEM encoded
1514
3839
  * certificate.
3840
+ * @since v15.6.0
1515
3841
  */
1516
3842
  toJSON(): string;
1517
-
1518
3843
  /**
1519
- * Returns information about this certificate using the legacy certificate object encoding.
3844
+ * Returns information about this certificate using the legacy `certificate object` encoding.
3845
+ * @since v15.6.0
1520
3846
  */
1521
3847
  toLegacyObject(): PeerCertificate;
1522
-
1523
3848
  /**
1524
3849
  * Returns the PEM-encoded certificate.
3850
+ * @since v15.6.0
1525
3851
  */
1526
3852
  toString(): string;
1527
-
1528
3853
  /**
1529
3854
  * Verifies that this certificate was signed by the given public key.
1530
3855
  * Does not perform any other validation checks on the certificate.
3856
+ * @since v15.6.0
3857
+ * @param publicKey A public key.
1531
3858
  */
1532
3859
  verify(publicKey: KeyObject): boolean;
1533
3860
  }
1534
-
1535
3861
  type LargeNumberLike = NodeJS.ArrayBufferView | SharedArrayBuffer | ArrayBuffer | bigint;
1536
-
1537
3862
  interface GeneratePrimeOptions {
1538
3863
  add?: LargeNumberLike | undefined;
1539
3864
  rem?: LargeNumberLike | undefined;
@@ -1543,25 +3868,74 @@ declare module 'crypto' {
1543
3868
  safe?: boolean | undefined;
1544
3869
  bigint?: boolean | undefined;
1545
3870
  }
1546
-
1547
3871
  interface GeneratePrimeOptionsBigInt extends GeneratePrimeOptions {
1548
3872
  bigint: true;
1549
3873
  }
1550
-
1551
3874
  interface GeneratePrimeOptionsArrayBuffer extends GeneratePrimeOptions {
1552
3875
  bigint?: false | undefined;
1553
3876
  }
1554
-
3877
+ /**
3878
+ * Generates a pseudorandom prime of `size` bits.
3879
+ *
3880
+ * If `options.safe` is `true`, the prime will be a safe prime -- that is,`(prime - 1) / 2` will also be a prime.
3881
+ *
3882
+ * The `options.add` and `options.rem` parameters can be used to enforce additional
3883
+ * requirements, e.g., for Diffie-Hellman:
3884
+ *
3885
+ * * If `options.add` and `options.rem` are both set, the prime will satisfy the
3886
+ * condition that `prime % add = rem`.
3887
+ * * If only `options.add` is set and `options.safe` is not `true`, the prime will
3888
+ * satisfy the condition that `prime % add = 1`.
3889
+ * * If only `options.add` is set and `options.safe` is set to `true`, the prime
3890
+ * will instead satisfy the condition that `prime % add = 3`. This is necessary
3891
+ * because `prime % add = 1` for `options.add > 2` would contradict the condition
3892
+ * enforced by `options.safe`.
3893
+ * * `options.rem` is ignored if `options.add` is not given.
3894
+ *
3895
+ * Both `options.add` and `options.rem` must be encoded as big-endian sequences
3896
+ * if given as an `ArrayBuffer`, `SharedArrayBuffer`, `TypedArray`, `Buffer`, or`DataView`.
3897
+ *
3898
+ * By default, the prime is encoded as a big-endian sequence of octets
3899
+ * 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
3900
+ * [&lt;bigint&gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt)is provided.
3901
+ * @since v15.8.0
3902
+ * @param size The size (in bits) of the prime to generate.
3903
+ */
1555
3904
  function generatePrime(size: number, callback: (err: Error | null, prime: ArrayBuffer) => void): void;
1556
3905
  function generatePrime(size: number, options: GeneratePrimeOptionsBigInt, callback: (err: Error | null, prime: bigint) => void): void;
1557
3906
  function generatePrime(size: number, options: GeneratePrimeOptionsArrayBuffer, callback: (err: Error | null, prime: ArrayBuffer) => void): void;
1558
3907
  function generatePrime(size: number, options: GeneratePrimeOptions, callback: (err: Error | null, prime: ArrayBuffer | bigint) => void): void;
1559
-
3908
+ /**
3909
+ * Generates a pseudorandom prime of `size` bits.
3910
+ *
3911
+ * If `options.safe` is `true`, the prime will be a safe prime -- that is,`(prime - 1) / 2` will also be a prime.
3912
+ *
3913
+ * The `options.add` and `options.rem` parameters can be used to enforce additional
3914
+ * requirements, e.g., for Diffie-Hellman:
3915
+ *
3916
+ * * If `options.add` and `options.rem` are both set, the prime will satisfy the
3917
+ * condition that `prime % add = rem`.
3918
+ * * If only `options.add` is set and `options.safe` is not `true`, the prime will
3919
+ * satisfy the condition that `prime % add = 1`.
3920
+ * * If only `options.add` is set and `options.safe` is set to `true`, the prime
3921
+ * will instead satisfy the condition that `prime % add = 3`. This is necessary
3922
+ * because `prime % add = 1` for `options.add > 2` would contradict the condition
3923
+ * enforced by `options.safe`.
3924
+ * * `options.rem` is ignored if `options.add` is not given.
3925
+ *
3926
+ * Both `options.add` and `options.rem` must be encoded as big-endian sequences
3927
+ * if given as an `ArrayBuffer`, `SharedArrayBuffer`, `TypedArray`, `Buffer`, or`DataView`.
3928
+ *
3929
+ * By default, the prime is encoded as a big-endian sequence of octets
3930
+ * 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
3931
+ * [&lt;bigint&gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt)is provided.
3932
+ * @since v15.8.0
3933
+ * @param size The size (in bits) of the prime to generate.
3934
+ */
1560
3935
  function generatePrimeSync(size: number): ArrayBuffer;
1561
3936
  function generatePrimeSync(size: number, options: GeneratePrimeOptionsBigInt): bigint;
1562
3937
  function generatePrimeSync(size: number, options: GeneratePrimeOptionsArrayBuffer): ArrayBuffer;
1563
3938
  function generatePrimeSync(size: number, options: GeneratePrimeOptions): ArrayBuffer | bigint;
1564
-
1565
3939
  interface CheckPrimeOptions {
1566
3940
  /**
1567
3941
  * The number of Miller-Rabin probabilistic primality iterations to perform.
@@ -1573,23 +3947,24 @@ declare module 'crypto' {
1573
3947
  */
1574
3948
  checks?: number | undefined;
1575
3949
  }
1576
-
1577
3950
  /**
1578
- * Checks the primality of the candidate.
3951
+ * Checks the primality of the `candidate`.
3952
+ * @since v15.8.0
3953
+ * @param candidate A possible prime encoded as a sequence of big endian octets of arbitrary length.
1579
3954
  */
1580
3955
  function checkPrime(value: LargeNumberLike, callback: (err: Error | null, result: boolean) => void): void;
1581
3956
  function checkPrime(value: LargeNumberLike, options: CheckPrimeOptions, callback: (err: Error | null, result: boolean) => void): void;
1582
-
1583
3957
  /**
1584
- * Checks the primality of the candidate.
3958
+ * Checks the primality of the `candidate`.
3959
+ * @since v15.8.0
3960
+ * @param candidate A possible prime encoded as a sequence of big endian octets of arbitrary length.
3961
+ * @return `true` if the candidate is a prime with an error probability less than `0.25 ** options.checks`.
1585
3962
  */
1586
3963
  function checkPrimeSync(value: LargeNumberLike, options?: CheckPrimeOptions): boolean;
1587
-
1588
3964
  namespace webcrypto {
1589
3965
  class CryptoKey {} // placeholder
1590
3966
  }
1591
3967
  }
1592
-
1593
3968
  declare module 'node:crypto' {
1594
3969
  export * from 'crypto';
1595
3970
  }