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