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