b6834d0a94597927b1d78f0173f2fd0fde3da883
[platform/upstream/nodejs.git] / doc / api / crypto.markdown
1 # Crypto
2
3     Stability: 2 - Unstable; API changes are being discussed for
4     future versions.  Breaking changes will be minimized.  See below.
5
6 Use `require('crypto')` to access this module.
7
8 The crypto module offers a way of encapsulating secure credentials to be
9 used as part of a secure HTTPS net or http connection.
10
11 It also offers a set of wrappers for OpenSSL's hash, hmac, cipher,
12 decipher, sign and verify methods.
13
14
15 ## crypto.setEngine(engine[, flags])
16
17 Load and set engine for some/all OpenSSL functions (selected by flags).
18
19 `engine` could be either an id or a path to the to the engine's shared library.
20
21 `flags` is optional and has `ENGINE_METHOD_ALL` value by default. It could take
22 one of or mix of following flags (defined in `constants` module):
23
24 * `ENGINE_METHOD_RSA`
25 * `ENGINE_METHOD_DSA`
26 * `ENGINE_METHOD_DH`
27 * `ENGINE_METHOD_RAND`
28 * `ENGINE_METHOD_ECDH`
29 * `ENGINE_METHOD_ECDSA`
30 * `ENGINE_METHOD_CIPHERS`
31 * `ENGINE_METHOD_DIGESTS`
32 * `ENGINE_METHOD_STORE`
33 * `ENGINE_METHOD_PKEY_METH`
34 * `ENGINE_METHOD_PKEY_ASN1_METH`
35 * `ENGINE_METHOD_ALL`
36 * `ENGINE_METHOD_NONE`
37
38
39 ## crypto.getCiphers()
40
41 Returns an array with the names of the supported ciphers.
42
43 Example:
44
45     var ciphers = crypto.getCiphers();
46     console.log(ciphers); // ['AES-128-CBC', 'AES-128-CBC-HMAC-SHA1', ...]
47
48
49 ## crypto.getHashes()
50
51 Returns an array with the names of the supported hash algorithms.
52
53 Example:
54
55     var hashes = crypto.getHashes();
56     console.log(hashes); // ['sha', 'sha1', 'sha1WithRSAEncryption', ...]
57
58
59 ## crypto.createCredentials(details)
60
61     Stability: 0 - Deprecated. Use [tls.createSecureContext][] instead.
62
63 Creates a credentials object, with the optional details being a
64 dictionary with keys:
65
66 * `pfx` : A string or buffer holding the PFX or PKCS12 encoded private
67   key, certificate and CA certificates
68 * `key` : A string holding the PEM encoded private key
69 * `passphrase` : A string of passphrase for the private key or pfx
70 * `cert` : A string holding the PEM encoded certificate
71 * `ca` : Either a string or list of strings of PEM encoded CA
72   certificates to trust.
73 * `crl` : Either a string or list of strings of PEM encoded CRLs
74   (Certificate Revocation List)
75 * `ciphers`: A string describing the ciphers to use or exclude.
76   Consult
77   <http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT>
78   for details on the format.
79
80 If no 'ca' details are given, then node.js will use the default
81 publicly trusted list of CAs as given in
82 <http://mxr.mozilla.org/mozilla/source/security/nss/lib/ckfw/builtins/certdata.txt>.
83
84
85 ## crypto.createHash(algorithm)
86
87 Creates and returns a hash object, a cryptographic hash with the given
88 algorithm which can be used to generate hash digests.
89
90 `algorithm` is dependent on the available algorithms supported by the
91 version of OpenSSL on the platform. Examples are `'sha1'`, `'md5'`,
92 `'sha256'`, `'sha512'`, etc.  On recent releases, `openssl
93 list-message-digest-algorithms` will display the available digest
94 algorithms.
95
96 Example: this program that takes the sha1 sum of a file
97
98     var filename = process.argv[2];
99     var crypto = require('crypto');
100     var fs = require('fs');
101
102     var shasum = crypto.createHash('sha1');
103
104     var s = fs.ReadStream(filename);
105     s.on('data', function(d) {
106       shasum.update(d);
107     });
108
109     s.on('end', function() {
110       var d = shasum.digest('hex');
111       console.log(d + '  ' + filename);
112     });
113
114 ## Class: Hash
115
116 The class for creating hash digests of data.
117
118 It is a [stream](stream.html) that is both readable and writable.  The
119 written data is used to compute the hash.  Once the writable side of
120 the stream is ended, use the `read()` method to get the computed hash
121 digest.  The legacy `update` and `digest` methods are also supported.
122
123 Returned by `crypto.createHash`.
124
125 ### hash.update(data[, input_encoding])
126
127 Updates the hash content with the given `data`, the encoding of which
128 is given in `input_encoding` and can be `'utf8'`, `'ascii'` or
129 `'binary'`.  If no encoding is provided and the input is a string an
130 encoding of `'binary'` is enforced. If `data` is a `Buffer` then
131 `input_encoding` is ignored.
132
133 This can be called many times with new data as it is streamed.
134
135 ### hash.digest([encoding])
136
137 Calculates the digest of all of the passed data to be hashed.  The
138 `encoding` can be `'hex'`, `'binary'` or `'base64'`.  If no encoding
139 is provided, then a buffer is returned.
140
141 Note: `hash` object can not be used after `digest()` method has been
142 called.
143
144
145 ## crypto.createHmac(algorithm, key)
146
147 Creates and returns a hmac object, a cryptographic hmac with the given
148 algorithm and key.
149
150 It is a [stream](stream.html) that is both readable and writable.  The
151 written data is used to compute the hmac.  Once the writable side of
152 the stream is ended, use the `read()` method to get the computed
153 digest.  The legacy `update` and `digest` methods are also supported.
154
155 `algorithm` is dependent on the available algorithms supported by
156 OpenSSL - see createHash above.  `key` is the hmac key to be used.
157
158 ## Class: Hmac
159
160 Class for creating cryptographic hmac content.
161
162 Returned by `crypto.createHmac`.
163
164 ### hmac.update(data)
165
166 Update the hmac content with the given `data`.  This can be called
167 many times with new data as it is streamed.
168
169 ### hmac.digest([encoding])
170
171 Calculates the digest of all of the passed data to the hmac.  The
172 `encoding` can be `'hex'`, `'binary'` or `'base64'`.  If no encoding
173 is provided, then a buffer is returned.
174
175 Note: `hmac` object can not be used after `digest()` method has been
176 called.
177
178
179 ## crypto.createCipher(algorithm, password)
180
181 Creates and returns a cipher object, with the given algorithm and
182 password.
183
184 `algorithm` is dependent on OpenSSL, examples are `'aes192'`, etc.  On
185 recent releases, `openssl list-cipher-algorithms` will display the
186 available cipher algorithms.  `password` is used to derive key and IV,
187 which must be a `'binary'` encoded string or a [buffer](buffer.html).
188
189 It is a [stream](stream.html) that is both readable and writable.  The
190 written data is used to compute the hash.  Once the writable side of
191 the stream is ended, use the `read()` method to get the enciphered
192 contents.  The legacy `update` and `final` methods are also supported.
193
194 Note: `createCipher` derives keys with the OpenSSL function [EVP_BytesToKey][]
195 with the digest algorithm set to MD5, one iteration, and no salt. The lack of
196 salt allows dictionary attacks as the same password always creates the same key.
197 The low iteration count and non-cryptographically secure hash algorithm allow
198 passwords to be tested very rapidly.
199
200 In line with OpenSSL's recommendation to use pbkdf2 instead of EVP_BytesToKey it
201 is recommended you derive a key and iv yourself with [crypto.pbkdf2][] and to
202 then use [createCipheriv()][] to create the cipher stream.
203
204 ## crypto.createCipheriv(algorithm, key, iv)
205
206 Creates and returns a cipher object, with the given algorithm, key and
207 iv.
208
209 `algorithm` is the same as the argument to `createCipher()`.  `key` is
210 the raw key used by the algorithm.  `iv` is an [initialization
211 vector](http://en.wikipedia.org/wiki/Initialization_vector).
212
213 `key` and `iv` must be `'binary'` encoded strings or
214 [buffers](buffer.html).
215
216 ## Class: Cipher
217
218 Class for encrypting data.
219
220 Returned by `crypto.createCipher` and `crypto.createCipheriv`.
221
222 Cipher objects are [streams](stream.html) that are both readable and
223 writable.  The written plain text data is used to produce the
224 encrypted data on the readable side.  The legacy `update` and `final`
225 methods are also supported.
226
227 ### cipher.update(data[, input_encoding][, output_encoding])
228
229 Updates the cipher with `data`, the encoding of which is given in
230 `input_encoding` and can be `'utf8'`, `'ascii'` or `'binary'`.  If no
231 encoding is provided, then a buffer is expected.
232 If `data` is a `Buffer` then `input_encoding` is ignored.
233
234 The `output_encoding` specifies the output format of the enciphered
235 data, and can be `'binary'`, `'base64'` or `'hex'`.  If no encoding is
236 provided, then a buffer is returned.
237
238 Returns the enciphered contents, and can be called many times with new
239 data as it is streamed.
240
241 ### cipher.final([output_encoding])
242
243 Returns any remaining enciphered contents, with `output_encoding`
244 being one of: `'binary'`, `'base64'` or `'hex'`.  If no encoding is
245 provided, then a buffer is returned.
246
247 Note: `cipher` object can not be used after `final()` method has been
248 called.
249
250 ### cipher.setAutoPadding(auto_padding=true)
251
252 You can disable automatic padding of the input data to block size. If
253 `auto_padding` is false, the length of the entire input data must be a
254 multiple of the cipher's block size or `final` will fail.  Useful for
255 non-standard padding, e.g. using `0x0` instead of PKCS padding. You
256 must call this before `cipher.final`.
257
258 ### cipher.getAuthTag()
259
260 For authenticated encryption modes (currently supported: GCM), this
261 method returns a `Buffer` that represents the _authentication tag_ that
262 has been computed from the given data. Should be called after
263 encryption has been completed using the `final` method!
264
265 ### cipher.setAAD(buffer)
266
267 For authenticated encryption modes (currently supported: GCM), this
268 method sets the value used for the additional authenticated data (AAD) input
269 parameter.
270
271
272 ## crypto.createDecipher(algorithm, password)
273
274 Creates and returns a decipher object, with the given algorithm and
275 key.  This is the mirror of the [createCipher()][] above.
276
277 ## crypto.createDecipheriv(algorithm, key, iv)
278
279 Creates and returns a decipher object, with the given algorithm, key
280 and iv.  This is the mirror of the [createCipheriv()][] above.
281
282 ## Class: Decipher
283
284 Class for decrypting data.
285
286 Returned by `crypto.createDecipher` and `crypto.createDecipheriv`.
287
288 Decipher objects are [streams](stream.html) that are both readable and
289 writable.  The written enciphered data is used to produce the
290 plain-text data on the the readable side.  The legacy `update` and
291 `final` methods are also supported.
292
293 ### decipher.update(data[, input_encoding][, output_encoding])
294
295 Updates the decipher with `data`, which is encoded in `'binary'`,
296 `'base64'` or `'hex'`.  If no encoding is provided, then a buffer is
297 expected.
298 If `data` is a `Buffer` then `input_encoding` is ignored.
299
300 The `output_decoding` specifies in what format to return the
301 deciphered plaintext: `'binary'`, `'ascii'` or `'utf8'`.  If no
302 encoding is provided, then a buffer is returned.
303
304 ### decipher.final([output_encoding])
305
306 Returns any remaining plaintext which is deciphered, with
307 `output_encoding` being one of: `'binary'`, `'ascii'` or `'utf8'`.  If
308 no encoding is provided, then a buffer is returned.
309
310 Note: `decipher` object can not be used after `final()` method has been
311 called.
312
313 ### decipher.setAutoPadding(auto_padding=true)
314
315 You can disable auto padding if the data has been encrypted without
316 standard block padding to prevent `decipher.final` from checking and
317 removing it. Can only work if the input data's length is a multiple of
318 the ciphers block size. You must call this before streaming data to
319 `decipher.update`.
320
321 ### decipher.setAuthTag(buffer)
322
323 For authenticated encryption modes (currently supported: GCM), this
324 method must be used to pass in the received _authentication tag_.
325 If no tag is provided or if the ciphertext has been tampered with,
326 `final` will throw, thus indicating that the ciphertext should
327 be discarded due to failed authentication.
328
329 ### decipher.setAAD(buffer)
330
331 For authenticated encryption modes (currently supported: GCM), this
332 method sets the value used for the additional authenticated data (AAD) input
333 parameter.
334
335
336 ## crypto.createSign(algorithm)
337
338 Creates and returns a signing object, with the given algorithm.  On
339 recent OpenSSL releases, `openssl list-public-key-algorithms` will
340 display the available signing algorithms. Examples are `'RSA-SHA256'`.
341
342 ## Class: Sign
343
344 Class for generating signatures.
345
346 Returned by `crypto.createSign`.
347
348 Sign objects are writable [streams](stream.html).  The written data is
349 used to generate the signature.  Once all of the data has been
350 written, the `sign` method will return the signature.  The legacy
351 `update` method is also supported.
352
353 ### sign.update(data)
354
355 Updates the sign object with data.  This can be called many times
356 with new data as it is streamed.
357
358 ### sign.sign(private_key[, output_format])
359
360 Calculates the signature on all the updated data passed through the
361 sign.
362
363 `private_key` can be an object or a string. If `private_key` is a string, it is
364 treated as the key with no passphrase.
365
366 `private_key`:
367
368 * `key` : A string holding the PEM encoded private key
369 * `passphrase` : A string of passphrase for the private key
370
371 Returns the signature in `output_format` which can be `'binary'`,
372 `'hex'` or `'base64'`. If no encoding is provided, then a buffer is
373 returned.
374
375 Note: `sign` object can not be used after `sign()` method has been
376 called.
377
378 ## crypto.createVerify(algorithm)
379
380 Creates and returns a verification object, with the given algorithm.
381 This is the mirror of the signing object above.
382
383 ## Class: Verify
384
385 Class for verifying signatures.
386
387 Returned by `crypto.createVerify`.
388
389 Verify objects are writable [streams](stream.html).  The written data
390 is used to validate against the supplied signature.  Once all of the
391 data has been written, the `verify` method will return true if the
392 supplied signature is valid.  The legacy `update` method is also
393 supported.
394
395 ### verifier.update(data)
396
397 Updates the verifier object with data.  This can be called many times
398 with new data as it is streamed.
399
400 ### verifier.verify(object, signature[, signature_format])
401
402 Verifies the signed data by using the `object` and `signature`.
403 `object` is  a string containing a PEM encoded object, which can be
404 one of RSA public key, DSA public key, or X.509 certificate.
405 `signature` is the previously calculated signature for the data, in
406 the `signature_format` which can be `'binary'`, `'hex'` or `'base64'`.
407 If no encoding is specified, then a buffer is expected.
408
409 Returns true or false depending on the validity of the signature for
410 the data and public key.
411
412 Note: `verifier` object can not be used after `verify()` method has been
413 called.
414
415 ## crypto.createDiffieHellman(prime_length[, generator])
416
417 Creates a Diffie-Hellman key exchange object and generates a prime of
418 `prime_length` bits and using an optional specific numeric `generator`.
419 If no `generator` is specified, then `2` is used.
420
421 ## crypto.createDiffieHellman(prime[, prime_encoding][, generator][, generator_encoding])
422
423 Creates a Diffie-Hellman key exchange object using the supplied `prime` and an
424 optional specific `generator`.
425 `generator` can be a number, string, or Buffer.
426 If no `generator` is specified, then `2` is used.
427 `prime_encoding` and `generator_encoding` can be `'binary'`, `'hex'`, or `'base64'`.
428 If no `prime_encoding` is specified, then a Buffer is expected for `prime`.
429 If no `generator_encoding` is specified, then a Buffer is expected for `generator`.
430
431 ## Class: DiffieHellman
432
433 The class for creating Diffie-Hellman key exchanges.
434
435 Returned by `crypto.createDiffieHellman`.
436
437 ### diffieHellman.verifyError
438
439 A bit field containing any warnings and/or errors as a result of a check performed
440 during initialization. The following values are valid for this property
441 (defined in `constants` module):
442
443 * `DH_CHECK_P_NOT_SAFE_PRIME`
444 * `DH_CHECK_P_NOT_PRIME`
445 * `DH_UNABLE_TO_CHECK_GENERATOR`
446 * `DH_NOT_SUITABLE_GENERATOR`
447
448 ### diffieHellman.generateKeys([encoding])
449
450 Generates private and public Diffie-Hellman key values, and returns
451 the public key in the specified encoding. This key should be
452 transferred to the other party. Encoding can be `'binary'`, `'hex'`,
453 or `'base64'`.  If no encoding is provided, then a buffer is returned.
454
455 ### diffieHellman.computeSecret(other_public_key[, input_encoding][, output_encoding])
456
457 Computes the shared secret using `other_public_key` as the other
458 party's public key and returns the computed shared secret. Supplied
459 key is interpreted using specified `input_encoding`, and secret is
460 encoded using specified `output_encoding`. Encodings can be
461 `'binary'`, `'hex'`, or `'base64'`. If the input encoding is not
462 provided, then a buffer is expected.
463
464 If no output encoding is given, then a buffer is returned.
465
466 ### diffieHellman.getPrime([encoding])
467
468 Returns the Diffie-Hellman prime in the specified encoding, which can
469 be `'binary'`, `'hex'`, or `'base64'`. If no encoding is provided,
470 then a buffer is returned.
471
472 ### diffieHellman.getGenerator([encoding])
473
474 Returns the Diffie-Hellman generator in the specified encoding, which can
475 be `'binary'`, `'hex'`, or `'base64'`. If no encoding is provided,
476 then a buffer is returned.
477
478 ### diffieHellman.getPublicKey([encoding])
479
480 Returns the Diffie-Hellman public key in the specified encoding, which
481 can be `'binary'`, `'hex'`, or `'base64'`. If no encoding is provided,
482 then a buffer is returned.
483
484 ### diffieHellman.getPrivateKey([encoding])
485
486 Returns the Diffie-Hellman private key in the specified encoding,
487 which can be `'binary'`, `'hex'`, or `'base64'`. If no encoding is
488 provided, then a buffer is returned.
489
490 ### diffieHellman.setPublicKey(public_key[, encoding])
491
492 Sets the Diffie-Hellman public key. Key encoding can be `'binary'`,
493 `'hex'` or `'base64'`. If no encoding is provided, then a buffer is
494 expected.
495
496 ### diffieHellman.setPrivateKey(private_key[, encoding])
497
498 Sets the Diffie-Hellman private key. Key encoding can be `'binary'`,
499 `'hex'` or `'base64'`. If no encoding is provided, then a buffer is
500 expected.
501
502 ## crypto.getDiffieHellman(group_name)
503
504 Creates a predefined Diffie-Hellman key exchange object.  The
505 supported groups are: `'modp1'`, `'modp2'`, `'modp5'` (defined in [RFC
506 2412][]) and `'modp14'`, `'modp15'`, `'modp16'`, `'modp17'`,
507 `'modp18'` (defined in [RFC 3526][]).  The returned object mimics the
508 interface of objects created by [crypto.createDiffieHellman()][]
509 above, but will not allow to change the keys (with
510 [diffieHellman.setPublicKey()][] for example).  The advantage of using
511 this routine is that the parties don't have to generate nor exchange
512 group modulus beforehand, saving both processor and communication
513 time.
514
515 Example (obtaining a shared secret):
516
517     var crypto = require('crypto');
518     var alice = crypto.getDiffieHellman('modp5');
519     var bob = crypto.getDiffieHellman('modp5');
520
521     alice.generateKeys();
522     bob.generateKeys();
523
524     var alice_secret = alice.computeSecret(bob.getPublicKey(), null, 'hex');
525     var bob_secret = bob.computeSecret(alice.getPublicKey(), null, 'hex');
526
527     /* alice_secret and bob_secret should be the same */
528     console.log(alice_secret == bob_secret);
529
530 ## crypto.createECDH(curve_name)
531
532 Creates a Elliptic Curve (EC) Diffie-Hellman key exchange object using a
533 predefined curve specified by `curve_name` string.
534
535 ## Class: ECDH
536
537 The class for creating EC Diffie-Hellman key exchanges.
538
539 Returned by `crypto.createECDH`.
540
541 ### ECDH.generateKeys([encoding[, format]])
542
543 Generates private and public EC Diffie-Hellman key values, and returns
544 the public key in the specified format and encoding. This key should be
545 transferred to the other party.
546
547 Format specifies point encoding and can be `'compressed'`, `'uncompressed'`, or
548 `'hybrid'`. If no format is provided - the point will be returned in
549 `'uncompressed'` format.
550
551 Encoding can be `'binary'`, `'hex'`, or `'base64'`. If no encoding is provided,
552 then a buffer is returned.
553
554 ### ECDH.computeSecret(other_public_key[, input_encoding][, output_encoding])
555
556 Computes the shared secret using `other_public_key` as the other
557 party's public key and returns the computed shared secret. Supplied
558 key is interpreted using specified `input_encoding`, and secret is
559 encoded using specified `output_encoding`. Encodings can be
560 `'binary'`, `'hex'`, or `'base64'`. If the input encoding is not
561 provided, then a buffer is expected.
562
563 If no output encoding is given, then a buffer is returned.
564
565 ### ECDH.getPublicKey([encoding[, format]])
566
567 Returns the EC Diffie-Hellman public key in the specified encoding and format.
568
569 Format specifies point encoding and can be `'compressed'`, `'uncompressed'`, or
570 `'hybrid'`. If no format is provided - the point will be returned in
571 `'uncompressed'` format.
572
573 Encoding can be `'binary'`, `'hex'`, or `'base64'`. If no encoding is provided,
574 then a buffer is returned.
575
576 ### ECDH.getPrivateKey([encoding])
577
578 Returns the EC Diffie-Hellman private key in the specified encoding,
579 which can be `'binary'`, `'hex'`, or `'base64'`. If no encoding is
580 provided, then a buffer is returned.
581
582 ### ECDH.setPublicKey(public_key[, encoding])
583
584 Sets the EC Diffie-Hellman public key. Key encoding can be `'binary'`,
585 `'hex'` or `'base64'`. If no encoding is provided, then a buffer is
586 expected.
587
588 ### ECDH.setPrivateKey(private_key[, encoding])
589
590 Sets the EC Diffie-Hellman private key. Key encoding can be `'binary'`,
591 `'hex'` or `'base64'`. If no encoding is provided, then a buffer is
592 expected.
593
594 Example (obtaining a shared secret):
595
596     var crypto = require('crypto');
597     var alice = crypto.createECDH('secp256k1');
598     var bob = crypto.createECDH('secp256k1');
599
600     alice.generateKeys();
601     bob.generateKeys();
602
603     var alice_secret = alice.computeSecret(bob.getPublicKey(), null, 'hex');
604     var bob_secret = bob.computeSecret(alice.getPublicKey(), null, 'hex');
605
606     /* alice_secret and bob_secret should be the same */
607     console.log(alice_secret == bob_secret);
608
609 ## crypto.pbkdf2(password, salt, iterations, keylen[, digest], callback)
610
611 Asynchronous PBKDF2 function.  Applies the selected HMAC digest function
612 (default: SHA1) to derive a key of the requested length from the password,
613 salt and number of iterations.  The callback gets two arguments:
614 `(err, derivedKey)`.
615
616 Example:
617
618     crypto.pbkdf2('secret', 'salt', 4096, 512, 'sha256', function(err, key) {
619       if (err)
620         throw err;
621       console.log(key.toString('hex'));  // 'c5e478d...1469e50'
622     });
623
624 You can get a list of supported digest functions with
625 [crypto.getHashes()](#crypto_crypto_gethashes).
626
627 ## crypto.pbkdf2Sync(password, salt, iterations, keylen[, digest])
628
629 Synchronous PBKDF2 function.  Returns derivedKey or throws error.
630
631 ## crypto.randomBytes(size[, callback])
632
633 Generates cryptographically strong pseudo-random data. Usage:
634
635     // async
636     crypto.randomBytes(256, function(ex, buf) {
637       if (ex) throw ex;
638       console.log('Have %d bytes of random data: %s', buf.length, buf);
639     });
640
641     // sync
642     try {
643       var buf = crypto.randomBytes(256);
644       console.log('Have %d bytes of random data: %s', buf.length, buf);
645     } catch (ex) {
646       // handle error
647       // most likely, entropy sources are drained
648     }
649
650 NOTE: This will block if there is insufficient entropy, although it should
651 normally never take longer than a few milliseconds. The only time when this
652 may conceivably block is right after boot, when the whole system is still
653 low on entropy.
654
655 ## Class: Certificate
656
657 The class used for working with signed public key & challenges. The most
658 common usage for this series of functions is when dealing with the `<keygen>`
659 element. http://www.openssl.org/docs/apps/spkac.html
660
661 Returned by `crypto.Certificate`.
662
663 ### Certificate.verifySpkac(spkac)
664
665 Returns true of false based on the validity of the SPKAC.
666
667 ### Certificate.exportChallenge(spkac)
668
669 Exports the encoded public key from the supplied SPKAC.
670
671 ### Certificate.exportPublicKey(spkac)
672
673 Exports the encoded challenge associated with the SPKAC.
674
675 ## crypto.publicEncrypt(public_key, buffer)
676
677 Encrypts `buffer` with `public_key`. Only RSA is currently supported.
678
679 `public_key` can be an object or a string. If `public_key` is a string, it is
680 treated as the key with no passphrase and will use `RSA_PKCS1_OAEP_PADDING`.
681
682 `public_key`:
683
684 * `key` : A string holding the PEM encoded private key
685 * `padding` : An optional padding value, one of the following:
686   * `constants.RSA_NO_PADDING`
687   * `constants.RSA_PKCS1_PADDING`
688   * `constants.RSA_PKCS1_OAEP_PADDING`
689
690 NOTE: All paddings are defined in `constants` module.
691
692 ## crypto.privateDecrypt(private_key, buffer)
693
694 Decrypts `buffer` with `private_key`.
695
696 `private_key` can be an object or a string. If `private_key` is a string, it is
697 treated as the key with no passphrase and will use `RSA_PKCS1_OAEP_PADDING`.
698
699 `private_key`:
700
701 * `key` : A string holding the PEM encoded private key
702 * `passphrase` : An optional string of passphrase for the private key
703 * `padding` : An optional padding value, one of the following:
704   * `constants.RSA_NO_PADDING`
705   * `constants.RSA_PKCS1_PADDING`
706   * `constants.RSA_PKCS1_OAEP_PADDING`
707
708 NOTE: All paddings are defined in `constants` module.
709
710 ## crypto.privateEncrypt(private_key, buffer)
711
712 See above for details. Has the same API as `crypto.privateDecrypt`.
713
714 ## crypto.publicDecrypt(public_key, buffer)
715
716 See above for details. Has the same API as `crypto.publicEncrypt`.
717
718 ## crypto.DEFAULT_ENCODING
719
720 The default encoding to use for functions that can take either strings
721 or buffers.  The default value is `'buffer'`, which makes it default
722 to using Buffer objects.  This is here to make the crypto module more
723 easily compatible with legacy programs that expected `'binary'` to be
724 the default encoding.
725
726 Note that new programs will probably expect buffers, so only use this
727 as a temporary measure.
728
729 ## Recent API Changes
730
731 The Crypto module was added to Node before there was the concept of a
732 unified Stream API, and before there were Buffer objects for handling
733 binary data.
734
735 As such, the streaming classes don't have the typical methods found on
736 other Node classes, and many methods accepted and returned
737 Binary-encoded strings by default rather than Buffers.  This was
738 changed to use Buffers by default instead.
739
740 This is a breaking change for some use cases, but not all.
741
742 For example, if you currently use the default arguments to the Sign
743 class, and then pass the results to the Verify class, without ever
744 inspecting the data, then it will continue to work as before.  Where
745 you once got a binary string and then presented the binary string to
746 the Verify object, you'll now get a Buffer, and present the Buffer to
747 the Verify object.
748
749 However, if you were doing things with the string data that will not
750 work properly on Buffers (such as, concatenating them, storing in
751 databases, etc.), or you are passing binary strings to the crypto
752 functions without an encoding argument, then you will need to start
753 providing encoding arguments to specify which encoding you'd like to
754 use.  To switch to the previous style of using binary strings by
755 default, set the `crypto.DEFAULT_ENCODING` field to 'binary'.  Note
756 that new programs will probably expect buffers, so only use this as a
757 temporary measure.
758
759
760 [createCipher()]: #crypto_crypto_createcipher_algorithm_password
761 [createCipheriv()]: #crypto_crypto_createcipheriv_algorithm_key_iv
762 [crypto.createDiffieHellman()]: #crypto_crypto_creatediffiehellman_prime_encoding
763 [tls.createSecureContext]: tls.html#tls_tls_createsecurecontext_details
764 [diffieHellman.setPublicKey()]: #crypto_diffiehellman_setpublickey_public_key_encoding
765 [RFC 2412]: http://www.rfc-editor.org/rfc/rfc2412.txt
766 [RFC 3526]: http://www.rfc-editor.org/rfc/rfc3526.txt
767 [crypto.pbkdf2]: #crypto_crypto_pbkdf2_password_salt_iterations_keylen_callback
768 [EVP_BytesToKey]: https://www.openssl.org/docs/crypto/EVP_BytesToKey.html