• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html>
2<html lang="en">
3<head>
4  <meta charset="utf-8">
5  <meta name="viewport" content="width=device-width">
6  <meta name="nodejs.org:node-version" content="v14.20.1">
7  <title>Crypto | Node.js v14.20.1 Documentation</title>
8  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
9  <link rel="stylesheet" href="assets/style.css">
10  <link rel="stylesheet" href="assets/hljs.css">
11  <link rel="canonical" href="https://nodejs.org/api/crypto.html">
12</head>
13<body class="alt apidoc" id="api-section-crypto">
14  <div id="content" class="clearfix">
15    <div id="column2" class="interior">
16      <div id="intro" class="interior">
17        <a href="/" title="Go back to the home page">
18          Node.js
19        </a>
20      </div>
21      <ul>
22<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
23<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
24</ul>
25<hr class="line">
26<ul>
27<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
28<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
29<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
30<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
31<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
32<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
33<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
34<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
35<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
36<li><a href="console.html" class="nav-console">Console</a></li>
37<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
38<li><a href="crypto.html" class="nav-crypto active">Crypto</a></li>
39<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
40<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
41<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
42<li><a href="dns.html" class="nav-dns">DNS</a></li>
43<li><a href="domain.html" class="nav-domain">Domain</a></li>
44<li><a href="errors.html" class="nav-errors">Errors</a></li>
45<li><a href="events.html" class="nav-events">Events</a></li>
46<li><a href="fs.html" class="nav-fs">File system</a></li>
47<li><a href="globals.html" class="nav-globals">Globals</a></li>
48<li><a href="http.html" class="nav-http">HTTP</a></li>
49<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
50<li><a href="https.html" class="nav-https">HTTPS</a></li>
51<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
52<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
53<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
54<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
55<li><a href="module.html" class="nav-module">Modules: <code>module</code> API</a></li>
56<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
57<li><a href="net.html" class="nav-net">Net</a></li>
58<li><a href="os.html" class="nav-os">OS</a></li>
59<li><a href="path.html" class="nav-path">Path</a></li>
60<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
61<li><a href="policy.html" class="nav-policy">Policies</a></li>
62<li><a href="process.html" class="nav-process">Process</a></li>
63<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
64<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
65<li><a href="readline.html" class="nav-readline">Readline</a></li>
66<li><a href="repl.html" class="nav-repl">REPL</a></li>
67<li><a href="report.html" class="nav-report">Report</a></li>
68<li><a href="stream.html" class="nav-stream">Stream</a></li>
69<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
70<li><a href="timers.html" class="nav-timers">Timers</a></li>
71<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
72<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
73<li><a href="tty.html" class="nav-tty">TTY</a></li>
74<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
75<li><a href="url.html" class="nav-url">URL</a></li>
76<li><a href="util.html" class="nav-util">Utilities</a></li>
77<li><a href="v8.html" class="nav-v8">V8</a></li>
78<li><a href="vm.html" class="nav-vm">VM</a></li>
79<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
80<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
81<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
82</ul>
83<hr class="line">
84<ul>
85<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
86</ul>
87    </div>
88
89    <div id="column1" data-id="crypto" class="interior">
90      <header>
91        <div class="header-container">
92          <h1>Node.js v14.20.1 documentation</h1>
93          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
94            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
95              <path fill="none" d="M0 0h24v24H0z" />
96              <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/>
97              <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/>
98            </svg>
99            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
100              <path d="M0 0h24v24H0z" fill="none" />
101              <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/>
102            </svg>
103          </button>
104        </div>
105        <div id="gtoc">
106          <ul>
107            <li>
108              <a href="index.html">Index</a>
109            </li>
110            <li>
111              <a href="all.html">View on single page</a>
112            </li>
113            <li>
114              <a href="crypto.json">View as JSON</a>
115            </li>
116
117    <li class="version-picker">
118      <a href="#">View another version <span>&#x25bc;</span></a>
119      <ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v18.x/api/crypto.html">18.x</a></li>
120<li><a href="https://nodejs.org/docs/latest-v17.x/api/crypto.html">17.x</a></li>
121<li><a href="https://nodejs.org/docs/latest-v16.x/api/crypto.html">16.x <b>LTS</b></a></li>
122<li><a href="https://nodejs.org/docs/latest-v15.x/api/crypto.html">15.x</a></li>
123<li><a href="https://nodejs.org/docs/latest-v14.x/api/crypto.html">14.x <b>LTS</b></a></li>
124<li><a href="https://nodejs.org/docs/latest-v13.x/api/crypto.html">13.x</a></li>
125<li><a href="https://nodejs.org/docs/latest-v12.x/api/crypto.html">12.x <b>LTS</b></a></li>
126<li><a href="https://nodejs.org/docs/latest-v11.x/api/crypto.html">11.x</a></li>
127<li><a href="https://nodejs.org/docs/latest-v10.x/api/crypto.html">10.x</a></li>
128<li><a href="https://nodejs.org/docs/latest-v9.x/api/crypto.html">9.x</a></li>
129<li><a href="https://nodejs.org/docs/latest-v8.x/api/crypto.html">8.x</a></li>
130<li><a href="https://nodejs.org/docs/latest-v7.x/api/crypto.html">7.x</a></li>
131<li><a href="https://nodejs.org/docs/latest-v6.x/api/crypto.html">6.x</a></li>
132<li><a href="https://nodejs.org/docs/latest-v5.x/api/crypto.html">5.x</a></li>
133<li><a href="https://nodejs.org/docs/latest-v4.x/api/crypto.html">4.x</a></li>
134<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/crypto.html">0.12.x</a></li>
135<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/crypto.html">0.10.x</a></li></ol>
136    </li>
137
138            <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/crypto.md">Edit on GitHub</a></li>
139          </ul>
140        </div>
141        <hr>
142      </header>
143
144      <details id="toc" open><summary>Table of contents</summary><ul>
145<li><span class="stability_2"><a href="#crypto_crypto">Crypto</a></span>
146<ul>
147<li><a href="#crypto_determining_if_crypto_support_is_unavailable">Determining if crypto support is unavailable</a></li>
148<li><a href="#crypto_class_certificate">Class: <code>Certificate</code></a>
149<ul>
150<li><a href="#crypto_certificate_exportchallenge_spkac"><code>Certificate.exportChallenge(spkac)</code></a></li>
151<li><a href="#crypto_certificate_exportpublickey_spkac_encoding"><code>Certificate.exportPublicKey(spkac[, encoding])</code></a></li>
152<li><a href="#crypto_certificate_verifyspkac_spkac"><code>Certificate.verifySpkac(spkac)</code></a></li>
153<li><span class="stability_0"><a href="#crypto_legacy_api">Legacy API</a></span>
154<ul>
155<li><a href="#crypto_new_crypto_certificate"><code>new crypto.Certificate()</code></a></li>
156<li><a href="#crypto_certificate_exportchallenge_spkac_1"><code>certificate.exportChallenge(spkac)</code></a></li>
157<li><a href="#crypto_certificate_exportpublickey_spkac"><code>certificate.exportPublicKey(spkac)</code></a></li>
158<li><a href="#crypto_certificate_verifyspkac_spkac_1"><code>certificate.verifySpkac(spkac)</code></a></li>
159</ul>
160</li>
161</ul>
162</li>
163<li><a href="#crypto_class_cipher">Class: <code>Cipher</code></a>
164<ul>
165<li><a href="#crypto_cipher_final_outputencoding"><code>cipher.final([outputEncoding])</code></a></li>
166<li><a href="#crypto_cipher_getauthtag"><code>cipher.getAuthTag()</code></a></li>
167<li><a href="#crypto_cipher_setaad_buffer_options"><code>cipher.setAAD(buffer[, options])</code></a></li>
168<li><a href="#crypto_cipher_setautopadding_autopadding"><code>cipher.setAutoPadding([autoPadding])</code></a></li>
169<li><a href="#crypto_cipher_update_data_inputencoding_outputencoding"><code>cipher.update(data[, inputEncoding][, outputEncoding])</code></a></li>
170</ul>
171</li>
172<li><a href="#crypto_class_decipher">Class: <code>Decipher</code></a>
173<ul>
174<li><a href="#crypto_decipher_final_outputencoding"><code>decipher.final([outputEncoding])</code></a></li>
175<li><a href="#crypto_decipher_setaad_buffer_options"><code>decipher.setAAD(buffer[, options])</code></a></li>
176<li><a href="#crypto_decipher_setauthtag_buffer"><code>decipher.setAuthTag(buffer)</code></a></li>
177<li><a href="#crypto_decipher_setautopadding_autopadding"><code>decipher.setAutoPadding([autoPadding])</code></a></li>
178<li><a href="#crypto_decipher_update_data_inputencoding_outputencoding"><code>decipher.update(data[, inputEncoding][, outputEncoding])</code></a></li>
179</ul>
180</li>
181<li><a href="#crypto_class_diffiehellman">Class: <code>DiffieHellman</code></a>
182<ul>
183<li><a href="#crypto_diffiehellman_computesecret_otherpublickey_inputencoding_outputencoding"><code>diffieHellman.computeSecret(otherPublicKey[, inputEncoding][, outputEncoding])</code></a></li>
184<li><a href="#crypto_diffiehellman_generatekeys_encoding"><code>diffieHellman.generateKeys([encoding])</code></a></li>
185<li><a href="#crypto_diffiehellman_getgenerator_encoding"><code>diffieHellman.getGenerator([encoding])</code></a></li>
186<li><a href="#crypto_diffiehellman_getprime_encoding"><code>diffieHellman.getPrime([encoding])</code></a></li>
187<li><a href="#crypto_diffiehellman_getprivatekey_encoding"><code>diffieHellman.getPrivateKey([encoding])</code></a></li>
188<li><a href="#crypto_diffiehellman_getpublickey_encoding"><code>diffieHellman.getPublicKey([encoding])</code></a></li>
189<li><a href="#crypto_diffiehellman_setprivatekey_privatekey_encoding"><code>diffieHellman.setPrivateKey(privateKey[, encoding])</code></a></li>
190<li><a href="#crypto_diffiehellman_setpublickey_publickey_encoding"><code>diffieHellman.setPublicKey(publicKey[, encoding])</code></a></li>
191<li><a href="#crypto_diffiehellman_verifyerror"><code>diffieHellman.verifyError</code></a></li>
192</ul>
193</li>
194<li><a href="#crypto_class_diffiehellmangroup">Class: <code>DiffieHellmanGroup</code></a></li>
195<li><a href="#crypto_class_ecdh">Class: <code>ECDH</code></a>
196<ul>
197<li><a href="#crypto_static_method_ecdh_convertkey_key_curve_inputencoding_outputencoding_format">Static method: <code>ECDH.convertKey(key, curve[, inputEncoding[, outputEncoding[, format]]])</code></a></li>
198<li><a href="#crypto_ecdh_computesecret_otherpublickey_inputencoding_outputencoding"><code>ecdh.computeSecret(otherPublicKey[, inputEncoding][, outputEncoding])</code></a></li>
199<li><a href="#crypto_ecdh_generatekeys_encoding_format"><code>ecdh.generateKeys([encoding[, format]])</code></a></li>
200<li><a href="#crypto_ecdh_getprivatekey_encoding"><code>ecdh.getPrivateKey([encoding])</code></a></li>
201<li><a href="#crypto_ecdh_getpublickey_encoding_format"><code>ecdh.getPublicKey([encoding][, format])</code></a></li>
202<li><a href="#crypto_ecdh_setprivatekey_privatekey_encoding"><code>ecdh.setPrivateKey(privateKey[, encoding])</code></a></li>
203<li><span class="stability_0"><a href="#crypto_ecdh_setpublickey_publickey_encoding"><code>ecdh.setPublicKey(publicKey[, encoding])</code></a></span></li>
204</ul>
205</li>
206<li><a href="#crypto_class_hash">Class: <code>Hash</code></a>
207<ul>
208<li><a href="#crypto_hash_copy_options"><code>hash.copy([options])</code></a></li>
209<li><a href="#crypto_hash_digest_encoding"><code>hash.digest([encoding])</code></a></li>
210<li><a href="#crypto_hash_update_data_inputencoding"><code>hash.update(data[, inputEncoding])</code></a></li>
211</ul>
212</li>
213<li><a href="#crypto_class_hmac">Class: <code>Hmac</code></a>
214<ul>
215<li><a href="#crypto_hmac_digest_encoding"><code>hmac.digest([encoding])</code></a></li>
216<li><a href="#crypto_hmac_update_data_inputencoding"><code>hmac.update(data[, inputEncoding])</code></a></li>
217</ul>
218</li>
219<li><a href="#crypto_class_keyobject">Class: <code>KeyObject</code></a>
220<ul>
221<li><a href="#crypto_keyobject_asymmetrickeytype"><code>keyObject.asymmetricKeyType</code></a></li>
222<li><a href="#crypto_keyobject_export_options"><code>keyObject.export([options])</code></a></li>
223<li><a href="#crypto_keyobject_symmetrickeysize"><code>keyObject.symmetricKeySize</code></a></li>
224<li><a href="#crypto_keyobject_type"><code>keyObject.type</code></a></li>
225</ul>
226</li>
227<li><a href="#crypto_class_sign">Class: <code>Sign</code></a>
228<ul>
229<li><a href="#crypto_sign_sign_privatekey_outputencoding"><code>sign.sign(privateKey[, outputEncoding])</code></a></li>
230<li><a href="#crypto_sign_update_data_inputencoding"><code>sign.update(data[, inputEncoding])</code></a></li>
231</ul>
232</li>
233<li><a href="#crypto_class_verify">Class: <code>Verify</code></a>
234<ul>
235<li><a href="#crypto_verify_update_data_inputencoding"><code>verify.update(data[, inputEncoding])</code></a></li>
236<li><a href="#crypto_verify_verify_object_signature_signatureencoding"><code>verify.verify(object, signature[, signatureEncoding])</code></a></li>
237</ul>
238</li>
239<li><a href="#crypto_crypto_module_methods_and_properties"><code>crypto</code> module methods and properties</a>
240<ul>
241<li><a href="#crypto_crypto_constants"><code>crypto.constants</code></a></li>
242<li><span class="stability_0"><a href="#crypto_crypto_default_encoding"><code>crypto.DEFAULT_ENCODING</code></a></span></li>
243<li><span class="stability_0"><a href="#crypto_crypto_fips"><code>crypto.fips</code></a></span></li>
244<li><span class="stability_0"><a href="#crypto_crypto_createcipher_algorithm_password_options"><code>crypto.createCipher(algorithm, password[, options])</code></a></span></li>
245<li><a href="#crypto_crypto_createcipheriv_algorithm_key_iv_options"><code>crypto.createCipheriv(algorithm, key, iv[, options])</code></a></li>
246<li><span class="stability_0"><a href="#crypto_crypto_createdecipher_algorithm_password_options"><code>crypto.createDecipher(algorithm, password[, options])</code></a></span></li>
247<li><a href="#crypto_crypto_createdecipheriv_algorithm_key_iv_options"><code>crypto.createDecipheriv(algorithm, key, iv[, options])</code></a></li>
248<li><a href="#crypto_crypto_creatediffiehellman_prime_primeencoding_generator_generatorencoding"><code>crypto.createDiffieHellman(prime[, primeEncoding][, generator][, generatorEncoding])</code></a></li>
249<li><a href="#crypto_crypto_creatediffiehellman_primelength_generator"><code>crypto.createDiffieHellman(primeLength[, generator])</code></a></li>
250<li><a href="#crypto_crypto_creatediffiehellmangroup_name"><code>crypto.createDiffieHellmanGroup(name)</code></a></li>
251<li><a href="#crypto_crypto_createecdh_curvename"><code>crypto.createECDH(curveName)</code></a></li>
252<li><a href="#crypto_crypto_createhash_algorithm_options"><code>crypto.createHash(algorithm[, options])</code></a></li>
253<li><a href="#crypto_crypto_createhmac_algorithm_key_options"><code>crypto.createHmac(algorithm, key[, options])</code></a></li>
254<li><a href="#crypto_crypto_createprivatekey_key"><code>crypto.createPrivateKey(key)</code></a></li>
255<li><a href="#crypto_crypto_createpublickey_key"><code>crypto.createPublicKey(key)</code></a></li>
256<li><a href="#crypto_crypto_createsecretkey_key"><code>crypto.createSecretKey(key)</code></a></li>
257<li><a href="#crypto_crypto_createsign_algorithm_options"><code>crypto.createSign(algorithm[, options])</code></a></li>
258<li><a href="#crypto_crypto_createverify_algorithm_options"><code>crypto.createVerify(algorithm[, options])</code></a></li>
259<li><a href="#crypto_crypto_diffiehellman_options"><code>crypto.diffieHellman(options)</code></a></li>
260<li><a href="#crypto_crypto_generatekeypair_type_options_callback"><code>crypto.generateKeyPair(type, options, callback)</code></a></li>
261<li><a href="#crypto_crypto_generatekeypairsync_type_options"><code>crypto.generateKeyPairSync(type, options)</code></a></li>
262<li><a href="#crypto_crypto_getciphers"><code>crypto.getCiphers()</code></a></li>
263<li><a href="#crypto_crypto_getcurves"><code>crypto.getCurves()</code></a></li>
264<li><a href="#crypto_crypto_getdiffiehellman_groupname"><code>crypto.getDiffieHellman(groupName)</code></a></li>
265<li><a href="#crypto_crypto_getfips"><code>crypto.getFips()</code></a></li>
266<li><a href="#crypto_crypto_gethashes"><code>crypto.getHashes()</code></a></li>
267<li><a href="#crypto_crypto_pbkdf2_password_salt_iterations_keylen_digest_callback"><code>crypto.pbkdf2(password, salt, iterations, keylen, digest, callback)</code></a></li>
268<li><a href="#crypto_crypto_pbkdf2sync_password_salt_iterations_keylen_digest"><code>crypto.pbkdf2Sync(password, salt, iterations, keylen, digest)</code></a></li>
269<li><a href="#crypto_crypto_privatedecrypt_privatekey_buffer"><code>crypto.privateDecrypt(privateKey, buffer)</code></a></li>
270<li><a href="#crypto_crypto_privateencrypt_privatekey_buffer"><code>crypto.privateEncrypt(privateKey, buffer)</code></a></li>
271<li><a href="#crypto_crypto_publicdecrypt_key_buffer"><code>crypto.publicDecrypt(key, buffer)</code></a></li>
272<li><a href="#crypto_crypto_publicencrypt_key_buffer"><code>crypto.publicEncrypt(key, buffer)</code></a></li>
273<li><a href="#crypto_crypto_randombytes_size_callback"><code>crypto.randomBytes(size[, callback])</code></a></li>
274<li><a href="#crypto_crypto_randomfillsync_buffer_offset_size"><code>crypto.randomFillSync(buffer[, offset][, size])</code></a></li>
275<li><a href="#crypto_crypto_randomfill_buffer_offset_size_callback"><code>crypto.randomFill(buffer[, offset][, size], callback)</code></a></li>
276<li><a href="#crypto_crypto_randomint_min_max_callback"><code>crypto.randomInt([min, ]max[, callback])</code></a></li>
277<li><a href="#crypto_crypto_randomuuid_options"><code>crypto.randomUUID([options])</code></a></li>
278<li><a href="#crypto_crypto_scrypt_password_salt_keylen_options_callback"><code>crypto.scrypt(password, salt, keylen[, options], callback)</code></a></li>
279<li><a href="#crypto_crypto_scryptsync_password_salt_keylen_options"><code>crypto.scryptSync(password, salt, keylen[, options])</code></a></li>
280<li><a href="#crypto_crypto_setengine_engine_flags"><code>crypto.setEngine(engine[, flags])</code></a></li>
281<li><a href="#crypto_crypto_setfips_bool"><code>crypto.setFips(bool)</code></a></li>
282<li><a href="#crypto_crypto_sign_algorithm_data_key"><code>crypto.sign(algorithm, data, key)</code></a></li>
283<li><a href="#crypto_crypto_timingsafeequal_a_b"><code>crypto.timingSafeEqual(a, b)</code></a></li>
284<li><a href="#crypto_crypto_verify_algorithm_data_key_signature"><code>crypto.verify(algorithm, data, key, signature)</code></a></li>
285</ul>
286</li>
287<li><a href="#crypto_notes">Notes</a>
288<ul>
289<li><a href="#crypto_legacy_streams_api_prior_to_node_js_0_10">Legacy streams API (prior to Node.js 0.10)</a></li>
290<li><a href="#crypto_recent_ecdh_changes">Recent ECDH changes</a></li>
291<li><a href="#crypto_support_for_weak_or_compromised_algorithms">Support for weak or compromised algorithms</a></li>
292<li><a href="#crypto_ccm_mode">CCM mode</a></li>
293</ul>
294</li>
295<li><a href="#crypto_crypto_constants_1">Crypto constants</a>
296<ul>
297<li><a href="#crypto_openssl_options">OpenSSL options</a></li>
298<li><a href="#crypto_openssl_engine_constants">OpenSSL engine constants</a></li>
299<li><a href="#crypto_other_openssl_constants">Other OpenSSL constants</a></li>
300<li><a href="#crypto_node_js_crypto_constants">Node.js crypto constants</a></li>
301</ul>
302</li>
303</ul>
304</li>
305</ul></details>
306
307      <div id="apicontent">
308        <h2>Crypto<span><a class="mark" href="#crypto_crypto" id="crypto_crypto">#</a></span></h2>
309
310<p></p><div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
311<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v14.20.1/lib/crypto.js">lib/crypto.js</a></p>
312<p>The <code>crypto</code> module provides cryptographic functionality that includes a set of
313wrappers for OpenSSL's hash, HMAC, cipher, decipher, sign, and verify functions.</p>
314<p>Use <code>require('crypto')</code> to access this module.</p>
315<pre><code class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
316
317<span class="hljs-keyword">const</span> secret = <span class="hljs-string">'abcdefg'</span>;
318<span class="hljs-keyword">const</span> hash = crypto.<span class="hljs-title function_">createHmac</span>(<span class="hljs-string">'sha256'</span>, secret)
319                   .<span class="hljs-title function_">update</span>(<span class="hljs-string">'I love cupcakes'</span>)
320                   .<span class="hljs-title function_">digest</span>(<span class="hljs-string">'hex'</span>);
321<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(hash);
322<span class="hljs-comment">// Prints:</span>
323<span class="hljs-comment">//   c0fa1bc00531bd78ef38c628449c5102aeabd49b5dc3a2a516ea6ea959d6658e</span></code></pre>
324<section><h3>Determining if crypto support is unavailable<span><a class="mark" href="#crypto_determining_if_crypto_support_is_unavailable" id="crypto_determining_if_crypto_support_is_unavailable">#</a></span></h3>
325<p>It is possible for Node.js to be built without including support for the
326<code>crypto</code> module. In such cases, calling <code>require('crypto')</code> will result in an
327error being thrown.</p>
328<pre><code class="language-js"><span class="hljs-keyword">let</span> crypto;
329<span class="hljs-keyword">try</span> {
330  crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
331} <span class="hljs-keyword">catch</span> (err) {
332  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'crypto support is disabled!'</span>);
333}</code></pre>
334</section><section><h3>Class: <code>Certificate</code><span><a class="mark" href="#crypto_class_certificate" id="crypto_class_certificate">#</a></span></h3>
335<div class="api_metadata">
336<span>Added in: v0.11.8</span>
337</div>
338<p>SPKAC is a Certificate Signing Request mechanism originally implemented by
339Netscape and was specified formally as part of <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/keygen">HTML5's <code>keygen</code> element</a>.</p>
340<p><code>&#x3C;keygen></code> is deprecated since <a href="https://www.w3.org/TR/html52/changes.html#features-removed">HTML 5.2</a> and new projects
341should not use this element anymore.</p>
342<p>The <code>crypto</code> module provides the <code>Certificate</code> class for working with SPKAC
343data. The most common usage is handling output generated by the HTML5
344<code>&#x3C;keygen></code> element. Node.js uses <a href="https://www.openssl.org/docs/man1.1.0/apps/openssl-spkac.html">OpenSSL's SPKAC implementation</a> internally.</p>
345<h4><code>Certificate.exportChallenge(spkac)</code><span><a class="mark" href="#crypto_certificate_exportchallenge_spkac" id="crypto_certificate_exportchallenge_spkac">#</a></span></h4>
346<div class="api_metadata">
347<span>Added in: v9.0.0</span>
348</div>
349<ul>
350<li><code>spkac</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
351<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> The challenge component of the <code>spkac</code> data structure, which
352includes a public key and a challenge.</li>
353</ul>
354<pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Certificate</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
355<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>();
356<span class="hljs-keyword">const</span> challenge = <span class="hljs-title class_">Certificate</span>.<span class="hljs-title function_">exportChallenge</span>(spkac);
357<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(challenge.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'utf8'</span>));
358<span class="hljs-comment">// Prints: the challenge as a UTF8 string</span></code></pre>
359<h4><code>Certificate.exportPublicKey(spkac[, encoding])</code><span><a class="mark" href="#crypto_certificate_exportpublickey_spkac_encoding" id="crypto_certificate_exportpublickey_spkac_encoding">#</a></span></h4>
360<div class="api_metadata">
361<span>Added in: v9.0.0</span>
362</div>
363<ul>
364<li><code>spkac</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
365<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the <code>spkac</code> string.</li>
366<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> The public key component of the <code>spkac</code> data structure,
367which includes a public key and a challenge.</li>
368</ul>
369<pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Certificate</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
370<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>();
371<span class="hljs-keyword">const</span> publicKey = <span class="hljs-title class_">Certificate</span>.<span class="hljs-title function_">exportPublicKey</span>(spkac);
372<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(publicKey);
373<span class="hljs-comment">// Prints: the public key as &#x3C;Buffer ...></span></code></pre>
374<h4><code>Certificate.verifySpkac(spkac)</code><span><a class="mark" href="#crypto_certificate_verifyspkac_spkac" id="crypto_certificate_verifyspkac_spkac">#</a></span></h4>
375<div class="api_metadata">
376<span>Added in: v9.0.0</span>
377</div>
378<ul>
379<li><code>spkac</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
380<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <code>true</code> if the given <code>spkac</code> data structure is valid,
381<code>false</code> otherwise.</li>
382</ul>
383<pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Certificate</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
384<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>();
385<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Certificate</span>.<span class="hljs-title function_">verifySpkac</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(spkac)));
386<span class="hljs-comment">// Prints: true or false</span></code></pre>
387<h4>Legacy API<span><a class="mark" href="#crypto_legacy_api" id="crypto_legacy_api">#</a></span></h4>
388<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated</div><p></p>
389<p>As a legacy interface, it is possible to create new instances of
390the <code>crypto.Certificate</code> class as illustrated in the examples below.</p>
391<h5><code>new crypto.Certificate()</code><span><a class="mark" href="#crypto_new_crypto_certificate" id="crypto_new_crypto_certificate">#</a></span></h5>
392<p>Instances of the <code>Certificate</code> class can be created using the <code>new</code> keyword
393or by calling <code>crypto.Certificate()</code> as a function:</p>
394<pre><code class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
395
396<span class="hljs-keyword">const</span> cert1 = <span class="hljs-keyword">new</span> crypto.<span class="hljs-title class_">Certificate</span>();
397<span class="hljs-keyword">const</span> cert2 = crypto.<span class="hljs-title class_">Certificate</span>();</code></pre>
398<h5><code>certificate.exportChallenge(spkac)</code><span><a class="mark" href="#crypto_certificate_exportchallenge_spkac_1" id="crypto_certificate_exportchallenge_spkac_1">#</a></span></h5>
399<div class="api_metadata">
400<span>Added in: v0.11.8</span>
401</div>
402<ul>
403<li><code>spkac</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
404<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> The challenge component of the <code>spkac</code> data structure, which
405includes a public key and a challenge.</li>
406</ul>
407<pre><code class="language-js"><span class="hljs-keyword">const</span> cert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>).<span class="hljs-title class_">Certificate</span>();
408<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>();
409<span class="hljs-keyword">const</span> challenge = cert.<span class="hljs-title function_">exportChallenge</span>(spkac);
410<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(challenge.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'utf8'</span>));
411<span class="hljs-comment">// Prints: the challenge as a UTF8 string</span></code></pre>
412<h5><code>certificate.exportPublicKey(spkac)</code><span><a class="mark" href="#crypto_certificate_exportpublickey_spkac" id="crypto_certificate_exportpublickey_spkac">#</a></span></h5>
413<div class="api_metadata">
414<span>Added in: v0.11.8</span>
415</div>
416<ul>
417<li><code>spkac</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
418<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> The public key component of the <code>spkac</code> data structure,
419which includes a public key and a challenge.</li>
420</ul>
421<pre><code class="language-js"><span class="hljs-keyword">const</span> cert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>).<span class="hljs-title class_">Certificate</span>();
422<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>();
423<span class="hljs-keyword">const</span> publicKey = cert.<span class="hljs-title function_">exportPublicKey</span>(spkac);
424<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(publicKey);
425<span class="hljs-comment">// Prints: the public key as &#x3C;Buffer ...></span></code></pre>
426<h5><code>certificate.verifySpkac(spkac)</code><span><a class="mark" href="#crypto_certificate_verifyspkac_spkac_1" id="crypto_certificate_verifyspkac_spkac_1">#</a></span></h5>
427<div class="api_metadata">
428<span>Added in: v0.11.8</span>
429</div>
430<ul>
431<li><code>spkac</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
432<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <code>true</code> if the given <code>spkac</code> data structure is valid,
433<code>false</code> otherwise.</li>
434</ul>
435<pre><code class="language-js"><span class="hljs-keyword">const</span> cert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>).<span class="hljs-title class_">Certificate</span>();
436<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>();
437<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(cert.<span class="hljs-title function_">verifySpkac</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(spkac)));
438<span class="hljs-comment">// Prints: true or false</span></code></pre>
439</section><section><h3>Class: <code>Cipher</code><span><a class="mark" href="#crypto_class_cipher" id="crypto_class_cipher">#</a></span></h3>
440<div class="api_metadata">
441<span>Added in: v0.1.94</span>
442</div>
443<ul>
444<li>Extends: <a href="stream.html#stream_class_stream_transform" class="type">&#x3C;stream.Transform></a></li>
445</ul>
446<p>Instances of the <code>Cipher</code> class are used to encrypt data. The class can be
447used in one of two ways:</p>
448<ul>
449<li>As a <a href="stream.html">stream</a> that is both readable and writable, where plain unencrypted
450data is written to produce encrypted data on the readable side, or</li>
451<li>Using the <a href="#crypto_cipher_update_data_inputencoding_outputencoding"><code>cipher.update()</code></a> and <a href="#crypto_cipher_final_outputencoding"><code>cipher.final()</code></a> methods to produce
452the encrypted data.</li>
453</ul>
454<p>The <a href="#crypto_crypto_createcipher_algorithm_password_options"><code>crypto.createCipher()</code></a> or <a href="#crypto_crypto_createcipheriv_algorithm_key_iv_options"><code>crypto.createCipheriv()</code></a> methods are
455used to create <code>Cipher</code> instances. <code>Cipher</code> objects are not to be created
456directly using the <code>new</code> keyword.</p>
457<p>Example: Using <code>Cipher</code> objects as streams:</p>
458<pre><code class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
459
460<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>;
461<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>;
462<span class="hljs-comment">// Key length is dependent on the algorithm. In this case for aes192, it is</span>
463<span class="hljs-comment">// 24 bytes (192 bits).</span>
464<span class="hljs-comment">// Use async `crypto.scrypt()` instead.</span>
465<span class="hljs-keyword">const</span> key = crypto.<span class="hljs-title function_">scryptSync</span>(password, <span class="hljs-string">'salt'</span>, <span class="hljs-number">24</span>);
466<span class="hljs-comment">// Use `crypto.randomBytes()` to generate a random iv instead of the static iv</span>
467<span class="hljs-comment">// shown here.</span>
468<span class="hljs-keyword">const</span> iv = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">16</span>, <span class="hljs-number">0</span>); <span class="hljs-comment">// Initialization vector.</span>
469
470<span class="hljs-keyword">const</span> cipher = crypto.<span class="hljs-title function_">createCipheriv</span>(algorithm, key, iv);
471
472<span class="hljs-keyword">let</span> encrypted = <span class="hljs-string">''</span>;
473cipher.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> {
474  <span class="hljs-keyword">let</span> chunk;
475  <span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (chunk = cipher.<span class="hljs-title function_">read</span>())) {
476    encrypted += chunk.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>);
477  }
478});
479cipher.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> {
480  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(encrypted);
481  <span class="hljs-comment">// Prints: e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa</span>
482});
483
484cipher.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some clear text data'</span>);
485cipher.<span class="hljs-title function_">end</span>();</code></pre>
486<p>Example: Using <code>Cipher</code> and piped streams:</p>
487<pre><code class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
488<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>);
489
490<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>;
491<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>;
492<span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span>
493<span class="hljs-keyword">const</span> key = crypto.<span class="hljs-title function_">scryptSync</span>(password, <span class="hljs-string">'salt'</span>, <span class="hljs-number">24</span>);
494<span class="hljs-comment">// Use `crypto.randomBytes()` to generate a random iv instead of the static iv</span>
495<span class="hljs-comment">// shown here.</span>
496<span class="hljs-keyword">const</span> iv = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">16</span>, <span class="hljs-number">0</span>); <span class="hljs-comment">// Initialization vector.</span>
497
498<span class="hljs-keyword">const</span> cipher = crypto.<span class="hljs-title function_">createCipheriv</span>(algorithm, key, iv);
499
500<span class="hljs-keyword">const</span> input = fs.<span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'test.js'</span>);
501<span class="hljs-keyword">const</span> output = fs.<span class="hljs-title function_">createWriteStream</span>(<span class="hljs-string">'test.enc'</span>);
502
503input.<span class="hljs-title function_">pipe</span>(cipher).<span class="hljs-title function_">pipe</span>(output);</code></pre>
504<p>Example: Using the <a href="#crypto_cipher_update_data_inputencoding_outputencoding"><code>cipher.update()</code></a> and <a href="#crypto_cipher_final_outputencoding"><code>cipher.final()</code></a> methods:</p>
505<pre><code class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
506
507<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>;
508<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>;
509<span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span>
510<span class="hljs-keyword">const</span> key = crypto.<span class="hljs-title function_">scryptSync</span>(password, <span class="hljs-string">'salt'</span>, <span class="hljs-number">24</span>);
511<span class="hljs-comment">// Use `crypto.randomBytes` to generate a random iv instead of the static iv</span>
512<span class="hljs-comment">// shown here.</span>
513<span class="hljs-keyword">const</span> iv = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">16</span>, <span class="hljs-number">0</span>); <span class="hljs-comment">// Initialization vector.</span>
514
515<span class="hljs-keyword">const</span> cipher = crypto.<span class="hljs-title function_">createCipheriv</span>(algorithm, key, iv);
516
517<span class="hljs-keyword">let</span> encrypted = cipher.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some clear text data'</span>, <span class="hljs-string">'utf8'</span>, <span class="hljs-string">'hex'</span>);
518encrypted += cipher.<span class="hljs-title function_">final</span>(<span class="hljs-string">'hex'</span>);
519<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(encrypted);
520<span class="hljs-comment">// Prints: e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa</span></code></pre>
521<h4><code>cipher.final([outputEncoding])</code><span><a class="mark" href="#crypto_cipher_final_outputencoding" id="crypto_cipher_final_outputencoding">#</a></span></h4>
522<div class="api_metadata">
523<span>Added in: v0.1.94</span>
524</div>
525<ul>
526<li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li>
527<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Any remaining enciphered contents.
528If <code>outputEncoding</code> is specified, a string is
529returned. If an <code>outputEncoding</code> is not provided, a <a href="buffer.html"><code>Buffer</code></a> is returned.</li>
530</ul>
531<p>Once the <code>cipher.final()</code> method has been called, the <code>Cipher</code> object can no
532longer be used to encrypt data. Attempts to call <code>cipher.final()</code> more than
533once will result in an error being thrown.</p>
534<h4><code>cipher.getAuthTag()</code><span><a class="mark" href="#crypto_cipher_getauthtag" id="crypto_cipher_getauthtag">#</a></span></h4>
535<div class="api_metadata">
536<span>Added in: v1.0.0</span>
537</div>
538<ul>
539<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> When using an authenticated encryption mode (<code>GCM</code>, <code>CCM</code>
540and <code>OCB</code> are currently supported), the <code>cipher.getAuthTag()</code> method returns a
541<a href="buffer.html"><code>Buffer</code></a> containing the <em>authentication tag</em> that has been computed from
542the given data.</li>
543</ul>
544<p>The <code>cipher.getAuthTag()</code> method should only be called after encryption has
545been completed using the <a href="#crypto_cipher_final_outputencoding"><code>cipher.final()</code></a> method.</p>
546<h4><code>cipher.setAAD(buffer[, options])</code><span><a class="mark" href="#crypto_cipher_setaad_buffer_options" id="crypto_cipher_setaad_buffer_options">#</a></span></h4>
547<div class="api_metadata">
548<span>Added in: v1.0.0</span>
549</div>
550<ul>
551<li><code>buffer</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
552<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> <a href="stream.html#stream_new_stream_transform_options"><code>stream.transform</code> options</a>
553<ul>
554<li><code>plaintextLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
555</ul>
556</li>
557<li>Returns: <a href="crypto.html#crypto_class_cipher" class="type">&#x3C;Cipher></a> for method chaining.</li>
558</ul>
559<p>When using an authenticated encryption mode (<code>GCM</code>, <code>CCM</code> and <code>OCB</code> are
560currently supported), the <code>cipher.setAAD()</code> method sets the value used for the
561<em>additional authenticated data</em> (AAD) input parameter.</p>
562<p>The <code>options</code> argument is optional for <code>GCM</code> and <code>OCB</code>. When using <code>CCM</code>, the
563<code>plaintextLength</code> option must be specified and its value must match the length
564of the plaintext in bytes. See <a href="#crypto_ccm_mode">CCM mode</a>.</p>
565<p>The <code>cipher.setAAD()</code> method must be called before <a href="#crypto_cipher_update_data_inputencoding_outputencoding"><code>cipher.update()</code></a>.</p>
566<h4><code>cipher.setAutoPadding([autoPadding])</code><span><a class="mark" href="#crypto_cipher_setautopadding_autopadding" id="crypto_cipher_setautopadding_autopadding">#</a></span></h4>
567<div class="api_metadata">
568<span>Added in: v0.7.1</span>
569</div>
570<ul>
571<li><code>autoPadding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <strong>Default:</strong> <code>true</code></li>
572<li>Returns: <a href="crypto.html#crypto_class_cipher" class="type">&#x3C;Cipher></a> for method chaining.</li>
573</ul>
574<p>When using block encryption algorithms, the <code>Cipher</code> class will automatically
575add padding to the input data to the appropriate block size. To disable the
576default padding call <code>cipher.setAutoPadding(false)</code>.</p>
577<p>When <code>autoPadding</code> is <code>false</code>, the length of the entire input data must be a
578multiple of the cipher's block size or <a href="#crypto_cipher_final_outputencoding"><code>cipher.final()</code></a> will throw an error.
579Disabling automatic padding is useful for non-standard padding, for instance
580using <code>0x0</code> instead of PKCS padding.</p>
581<p>The <code>cipher.setAutoPadding()</code> method must be called before
582<a href="#crypto_cipher_final_outputencoding"><code>cipher.final()</code></a>.</p>
583<h4><code>cipher.update(data[, inputEncoding][, outputEncoding])</code><span><a class="mark" href="#crypto_cipher_update_data_inputencoding_outputencoding" id="crypto_cipher_update_data_inputencoding_outputencoding">#</a></span></h4>
584<div class="api_metadata">
585<details class="changelog"><summary>History</summary>
586<table>
587<tbody><tr><th>Version</th><th>Changes</th></tr>
588<tr><td>v6.0.0</td>
589<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p></td></tr>
590<tr><td>v0.1.94</td>
591<td><p><span>Added in: v0.1.94</span></p></td></tr>
592</tbody></table>
593</details>
594</div>
595<ul>
596<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
597<li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the data.</li>
598<li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li>
599<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
600</ul>
601<p>Updates the cipher with <code>data</code>. If the <code>inputEncoding</code> argument is given,
602the <code>data</code>
603argument is a string using the specified encoding. If the <code>inputEncoding</code>
604argument is not given, <code>data</code> must be a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or
605<code>DataView</code>. If <code>data</code> is a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or <code>DataView</code>, then
606<code>inputEncoding</code> is ignored.</p>
607<p>The <code>outputEncoding</code> specifies the output format of the enciphered
608data. If the <code>outputEncoding</code>
609is specified, a string using the specified encoding is returned. If no
610<code>outputEncoding</code> is provided, a <a href="buffer.html"><code>Buffer</code></a> is returned.</p>
611<p>The <code>cipher.update()</code> method can be called multiple times with new data until
612<a href="#crypto_cipher_final_outputencoding"><code>cipher.final()</code></a> is called. Calling <code>cipher.update()</code> after
613<a href="#crypto_cipher_final_outputencoding"><code>cipher.final()</code></a> will result in an error being thrown.</p>
614</section><section><h3>Class: <code>Decipher</code><span><a class="mark" href="#crypto_class_decipher" id="crypto_class_decipher">#</a></span></h3>
615<div class="api_metadata">
616<span>Added in: v0.1.94</span>
617</div>
618<ul>
619<li>Extends: <a href="stream.html#stream_class_stream_transform" class="type">&#x3C;stream.Transform></a></li>
620</ul>
621<p>Instances of the <code>Decipher</code> class are used to decrypt data. The class can be
622used in one of two ways:</p>
623<ul>
624<li>As a <a href="stream.html">stream</a> that is both readable and writable, where plain encrypted
625data is written to produce unencrypted data on the readable side, or</li>
626<li>Using the <a href="#crypto_decipher_update_data_inputencoding_outputencoding"><code>decipher.update()</code></a> and <a href="#crypto_decipher_final_outputencoding"><code>decipher.final()</code></a> methods to
627produce the unencrypted data.</li>
628</ul>
629<p>The <a href="#crypto_crypto_createdecipher_algorithm_password_options"><code>crypto.createDecipher()</code></a> or <a href="#crypto_crypto_createdecipheriv_algorithm_key_iv_options"><code>crypto.createDecipheriv()</code></a> methods are
630used to create <code>Decipher</code> instances. <code>Decipher</code> objects are not to be created
631directly using the <code>new</code> keyword.</p>
632<p>Example: Using <code>Decipher</code> objects as streams:</p>
633<pre><code class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
634
635<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>;
636<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>;
637<span class="hljs-comment">// Key length is dependent on the algorithm. In this case for aes192, it is</span>
638<span class="hljs-comment">// 24 bytes (192 bits).</span>
639<span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span>
640<span class="hljs-keyword">const</span> key = crypto.<span class="hljs-title function_">scryptSync</span>(password, <span class="hljs-string">'salt'</span>, <span class="hljs-number">24</span>);
641<span class="hljs-comment">// The IV is usually passed along with the ciphertext.</span>
642<span class="hljs-keyword">const</span> iv = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">16</span>, <span class="hljs-number">0</span>); <span class="hljs-comment">// Initialization vector.</span>
643
644<span class="hljs-keyword">const</span> decipher = crypto.<span class="hljs-title function_">createDecipheriv</span>(algorithm, key, iv);
645
646<span class="hljs-keyword">let</span> decrypted = <span class="hljs-string">''</span>;
647decipher.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> {
648  <span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (chunk = decipher.<span class="hljs-title function_">read</span>())) {
649    decrypted += chunk.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'utf8'</span>);
650  }
651});
652decipher.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> {
653  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(decrypted);
654  <span class="hljs-comment">// Prints: some clear text data</span>
655});
656
657<span class="hljs-comment">// Encrypted with same algorithm, key and iv.</span>
658<span class="hljs-keyword">const</span> encrypted =
659  <span class="hljs-string">'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa'</span>;
660decipher.<span class="hljs-title function_">write</span>(encrypted, <span class="hljs-string">'hex'</span>);
661decipher.<span class="hljs-title function_">end</span>();</code></pre>
662<p>Example: Using <code>Decipher</code> and piped streams:</p>
663<pre><code class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
664<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>);
665
666<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>;
667<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>;
668<span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span>
669<span class="hljs-keyword">const</span> key = crypto.<span class="hljs-title function_">scryptSync</span>(password, <span class="hljs-string">'salt'</span>, <span class="hljs-number">24</span>);
670<span class="hljs-comment">// The IV is usually passed along with the ciphertext.</span>
671<span class="hljs-keyword">const</span> iv = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">16</span>, <span class="hljs-number">0</span>); <span class="hljs-comment">// Initialization vector.</span>
672
673<span class="hljs-keyword">const</span> decipher = crypto.<span class="hljs-title function_">createDecipheriv</span>(algorithm, key, iv);
674
675<span class="hljs-keyword">const</span> input = fs.<span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'test.enc'</span>);
676<span class="hljs-keyword">const</span> output = fs.<span class="hljs-title function_">createWriteStream</span>(<span class="hljs-string">'test.js'</span>);
677
678input.<span class="hljs-title function_">pipe</span>(decipher).<span class="hljs-title function_">pipe</span>(output);</code></pre>
679<p>Example: Using the <a href="#crypto_decipher_update_data_inputencoding_outputencoding"><code>decipher.update()</code></a> and <a href="#crypto_decipher_final_outputencoding"><code>decipher.final()</code></a> methods:</p>
680<pre><code class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
681
682<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>;
683<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>;
684<span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span>
685<span class="hljs-keyword">const</span> key = crypto.<span class="hljs-title function_">scryptSync</span>(password, <span class="hljs-string">'salt'</span>, <span class="hljs-number">24</span>);
686<span class="hljs-comment">// The IV is usually passed along with the ciphertext.</span>
687<span class="hljs-keyword">const</span> iv = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">16</span>, <span class="hljs-number">0</span>); <span class="hljs-comment">// Initialization vector.</span>
688
689<span class="hljs-keyword">const</span> decipher = crypto.<span class="hljs-title function_">createDecipheriv</span>(algorithm, key, iv);
690
691<span class="hljs-comment">// Encrypted using same algorithm, key and iv.</span>
692<span class="hljs-keyword">const</span> encrypted =
693  <span class="hljs-string">'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa'</span>;
694<span class="hljs-keyword">let</span> decrypted = decipher.<span class="hljs-title function_">update</span>(encrypted, <span class="hljs-string">'hex'</span>, <span class="hljs-string">'utf8'</span>);
695decrypted += decipher.<span class="hljs-title function_">final</span>(<span class="hljs-string">'utf8'</span>);
696<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(decrypted);
697<span class="hljs-comment">// Prints: some clear text data</span></code></pre>
698<h4><code>decipher.final([outputEncoding])</code><span><a class="mark" href="#crypto_decipher_final_outputencoding" id="crypto_decipher_final_outputencoding">#</a></span></h4>
699<div class="api_metadata">
700<span>Added in: v0.1.94</span>
701</div>
702<ul>
703<li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li>
704<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Any remaining deciphered contents.
705If <code>outputEncoding</code> is specified, a string is
706returned. If an <code>outputEncoding</code> is not provided, a <a href="buffer.html"><code>Buffer</code></a> is returned.</li>
707</ul>
708<p>Once the <code>decipher.final()</code> method has been called, the <code>Decipher</code> object can
709no longer be used to decrypt data. Attempts to call <code>decipher.final()</code> more
710than once will result in an error being thrown.</p>
711<h4><code>decipher.setAAD(buffer[, options])</code><span><a class="mark" href="#crypto_decipher_setaad_buffer_options" id="crypto_decipher_setaad_buffer_options">#</a></span></h4>
712<div class="api_metadata">
713<details class="changelog"><summary>History</summary>
714<table>
715<tbody><tr><th>Version</th><th>Changes</th></tr>
716<tr><td>v7.2.0</td>
717<td><p>This method now returns a reference to <code>decipher</code>.</p></td></tr>
718<tr><td>v1.0.0</td>
719<td><p><span>Added in: v1.0.0</span></p></td></tr>
720</tbody></table>
721</details>
722</div>
723<ul>
724<li><code>buffer</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
725<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> <a href="stream.html#stream_new_stream_transform_options"><code>stream.transform</code> options</a>
726<ul>
727<li><code>plaintextLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
728</ul>
729</li>
730<li>Returns: <a href="crypto.html#crypto_class_decipher" class="type">&#x3C;Decipher></a> for method chaining.</li>
731</ul>
732<p>When using an authenticated encryption mode (<code>GCM</code>, <code>CCM</code> and <code>OCB</code> are
733currently supported), the <code>decipher.setAAD()</code> method sets the value used for the
734<em>additional authenticated data</em> (AAD) input parameter.</p>
735<p>The <code>options</code> argument is optional for <code>GCM</code>. When using <code>CCM</code>, the
736<code>plaintextLength</code> option must be specified and its value must match the length
737of the ciphertext in bytes. See <a href="#crypto_ccm_mode">CCM mode</a>.</p>
738<p>The <code>decipher.setAAD()</code> method must be called before <a href="#crypto_decipher_update_data_inputencoding_outputencoding"><code>decipher.update()</code></a>.</p>
739<h4><code>decipher.setAuthTag(buffer)</code><span><a class="mark" href="#crypto_decipher_setauthtag_buffer" id="crypto_decipher_setauthtag_buffer">#</a></span></h4>
740<div class="api_metadata">
741<details class="changelog"><summary>History</summary>
742<table>
743<tbody><tr><th>Version</th><th>Changes</th></tr>
744<tr><td>v11.0.0</td>
745<td><p>This method now throws if the GCM tag length is invalid.</p></td></tr>
746<tr><td>v7.2.0</td>
747<td><p>This method now returns a reference to <code>decipher</code>.</p></td></tr>
748<tr><td>v1.0.0</td>
749<td><p><span>Added in: v1.0.0</span></p></td></tr>
750</tbody></table>
751</details>
752</div>
753<ul>
754<li><code>buffer</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
755<li>Returns: <a href="crypto.html#crypto_class_decipher" class="type">&#x3C;Decipher></a> for method chaining.</li>
756</ul>
757<p>When using an authenticated encryption mode (<code>GCM</code>, <code>CCM</code> and <code>OCB</code> are
758currently supported), the <code>decipher.setAuthTag()</code> method is used to pass in the
759received <em>authentication tag</em>. If no tag is provided, or if the cipher text
760has been tampered with, <a href="#crypto_decipher_final_outputencoding"><code>decipher.final()</code></a> will throw, indicating that the
761cipher text should be discarded due to failed authentication. If the tag length
762is invalid according to <a href="https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf">NIST SP 800-38D</a> or does not match the value of the
763<code>authTagLength</code> option, <code>decipher.setAuthTag()</code> will throw an error.</p>
764<p>The <code>decipher.setAuthTag()</code> method must be called before <a href="#crypto_decipher_update_data_inputencoding_outputencoding"><code>decipher.update()</code></a>
765for <code>CCM</code> mode or before <a href="#crypto_decipher_final_outputencoding"><code>decipher.final()</code></a> for <code>GCM</code> and <code>OCB</code> modes.
766<code>decipher.setAuthTag()</code> can only be called once.</p>
767<h4><code>decipher.setAutoPadding([autoPadding])</code><span><a class="mark" href="#crypto_decipher_setautopadding_autopadding" id="crypto_decipher_setautopadding_autopadding">#</a></span></h4>
768<div class="api_metadata">
769<span>Added in: v0.7.1</span>
770</div>
771<ul>
772<li><code>autoPadding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <strong>Default:</strong> <code>true</code></li>
773<li>Returns: <a href="crypto.html#crypto_class_decipher" class="type">&#x3C;Decipher></a> for method chaining.</li>
774</ul>
775<p>When data has been encrypted without standard block padding, calling
776<code>decipher.setAutoPadding(false)</code> will disable automatic padding to prevent
777<a href="#crypto_decipher_final_outputencoding"><code>decipher.final()</code></a> from checking for and removing padding.</p>
778<p>Turning auto padding off will only work if the input data's length is a
779multiple of the ciphers block size.</p>
780<p>The <code>decipher.setAutoPadding()</code> method must be called before
781<a href="#crypto_decipher_final_outputencoding"><code>decipher.final()</code></a>.</p>
782<h4><code>decipher.update(data[, inputEncoding][, outputEncoding])</code><span><a class="mark" href="#crypto_decipher_update_data_inputencoding_outputencoding" id="crypto_decipher_update_data_inputencoding_outputencoding">#</a></span></h4>
783<div class="api_metadata">
784<details class="changelog"><summary>History</summary>
785<table>
786<tbody><tr><th>Version</th><th>Changes</th></tr>
787<tr><td>v6.0.0</td>
788<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p></td></tr>
789<tr><td>v0.1.94</td>
790<td><p><span>Added in: v0.1.94</span></p></td></tr>
791</tbody></table>
792</details>
793</div>
794<ul>
795<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
796<li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the <code>data</code> string.</li>
797<li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li>
798<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
799</ul>
800<p>Updates the decipher with <code>data</code>. If the <code>inputEncoding</code> argument is given,
801the <code>data</code>
802argument is a string using the specified encoding. If the <code>inputEncoding</code>
803argument is not given, <code>data</code> must be a <a href="buffer.html"><code>Buffer</code></a>. If <code>data</code> is a
804<a href="buffer.html"><code>Buffer</code></a> then <code>inputEncoding</code> is ignored.</p>
805<p>The <code>outputEncoding</code> specifies the output format of the enciphered
806data. If the <code>outputEncoding</code>
807is specified, a string using the specified encoding is returned. If no
808<code>outputEncoding</code> is provided, a <a href="buffer.html"><code>Buffer</code></a> is returned.</p>
809<p>The <code>decipher.update()</code> method can be called multiple times with new data until
810<a href="#crypto_decipher_final_outputencoding"><code>decipher.final()</code></a> is called. Calling <code>decipher.update()</code> after
811<a href="#crypto_decipher_final_outputencoding"><code>decipher.final()</code></a> will result in an error being thrown.</p>
812</section><section><h3>Class: <code>DiffieHellman</code><span><a class="mark" href="#crypto_class_diffiehellman" id="crypto_class_diffiehellman">#</a></span></h3>
813<div class="api_metadata">
814<span>Added in: v0.5.0</span>
815</div>
816<p>The <code>DiffieHellman</code> class is a utility for creating Diffie-Hellman key
817exchanges.</p>
818<p>Instances of the <code>DiffieHellman</code> class can be created using the
819<a href="#crypto_crypto_creatediffiehellman_prime_primeencoding_generator_generatorencoding"><code>crypto.createDiffieHellman()</code></a> function.</p>
820<pre><code class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
821<span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>);
822
823<span class="hljs-comment">// Generate Alice's keys...</span>
824<span class="hljs-keyword">const</span> alice = crypto.<span class="hljs-title function_">createDiffieHellman</span>(<span class="hljs-number">2048</span>);
825<span class="hljs-keyword">const</span> aliceKey = alice.<span class="hljs-title function_">generateKeys</span>();
826
827<span class="hljs-comment">// Generate Bob's keys...</span>
828<span class="hljs-keyword">const</span> bob = crypto.<span class="hljs-title function_">createDiffieHellman</span>(alice.<span class="hljs-title function_">getPrime</span>(), alice.<span class="hljs-title function_">getGenerator</span>());
829<span class="hljs-keyword">const</span> bobKey = bob.<span class="hljs-title function_">generateKeys</span>();
830
831<span class="hljs-comment">// Exchange and generate the secret...</span>
832<span class="hljs-keyword">const</span> aliceSecret = alice.<span class="hljs-title function_">computeSecret</span>(bobKey);
833<span class="hljs-keyword">const</span> bobSecret = bob.<span class="hljs-title function_">computeSecret</span>(aliceKey);
834
835<span class="hljs-comment">// OK</span>
836assert.<span class="hljs-title function_">strictEqual</span>(aliceSecret.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>), bobSecret.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));</code></pre>
837<h4><code>diffieHellman.computeSecret(otherPublicKey[, inputEncoding][, outputEncoding])</code><span><a class="mark" href="#crypto_diffiehellman_computesecret_otherpublickey_inputencoding_outputencoding" id="crypto_diffiehellman_computesecret_otherpublickey_inputencoding_outputencoding">#</a></span></h4>
838<div class="api_metadata">
839<span>Added in: v0.5.0</span>
840</div>
841<ul>
842<li><code>otherPublicKey</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
843<li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of an <code>otherPublicKey</code> string.</li>
844<li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li>
845<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
846</ul>
847<p>Computes the shared secret using <code>otherPublicKey</code> as the other
848party's public key and returns the computed shared secret. The supplied
849key is interpreted using the specified <code>inputEncoding</code>, and secret is
850encoded using specified <code>outputEncoding</code>.
851If the <code>inputEncoding</code> is not
852provided, <code>otherPublicKey</code> is expected to be a <a href="buffer.html"><code>Buffer</code></a>,
853<code>TypedArray</code>, or <code>DataView</code>.</p>
854<p>If <code>outputEncoding</code> is given a string is returned; otherwise, a
855<a href="buffer.html"><code>Buffer</code></a> is returned.</p>
856<h4><code>diffieHellman.generateKeys([encoding])</code><span><a class="mark" href="#crypto_diffiehellman_generatekeys_encoding" id="crypto_diffiehellman_generatekeys_encoding">#</a></span></h4>
857<div class="api_metadata">
858<span>Added in: v0.5.0</span>
859</div>
860<ul>
861<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li>
862<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
863</ul>
864<p>Generates private and public Diffie-Hellman key values, and returns
865the public key in the specified <code>encoding</code>. This key should be
866transferred to the other party.
867If <code>encoding</code> is provided a string is returned; otherwise a
868<a href="buffer.html"><code>Buffer</code></a> is returned.</p>
869<h4><code>diffieHellman.getGenerator([encoding])</code><span><a class="mark" href="#crypto_diffiehellman_getgenerator_encoding" id="crypto_diffiehellman_getgenerator_encoding">#</a></span></h4>
870<div class="api_metadata">
871<span>Added in: v0.5.0</span>
872</div>
873<ul>
874<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li>
875<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
876</ul>
877<p>Returns the Diffie-Hellman generator in the specified <code>encoding</code>.
878If <code>encoding</code> is provided a string is
879returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is returned.</p>
880<h4><code>diffieHellman.getPrime([encoding])</code><span><a class="mark" href="#crypto_diffiehellman_getprime_encoding" id="crypto_diffiehellman_getprime_encoding">#</a></span></h4>
881<div class="api_metadata">
882<span>Added in: v0.5.0</span>
883</div>
884<ul>
885<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li>
886<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
887</ul>
888<p>Returns the Diffie-Hellman prime in the specified <code>encoding</code>.
889If <code>encoding</code> is provided a string is
890returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is returned.</p>
891<h4><code>diffieHellman.getPrivateKey([encoding])</code><span><a class="mark" href="#crypto_diffiehellman_getprivatekey_encoding" id="crypto_diffiehellman_getprivatekey_encoding">#</a></span></h4>
892<div class="api_metadata">
893<span>Added in: v0.5.0</span>
894</div>
895<ul>
896<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li>
897<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
898</ul>
899<p>Returns the Diffie-Hellman private key in the specified <code>encoding</code>.
900If <code>encoding</code> is provided a
901string is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is returned.</p>
902<h4><code>diffieHellman.getPublicKey([encoding])</code><span><a class="mark" href="#crypto_diffiehellman_getpublickey_encoding" id="crypto_diffiehellman_getpublickey_encoding">#</a></span></h4>
903<div class="api_metadata">
904<span>Added in: v0.5.0</span>
905</div>
906<ul>
907<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li>
908<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
909</ul>
910<p>Returns the Diffie-Hellman public key in the specified <code>encoding</code>.
911If <code>encoding</code> is provided a
912string is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is returned.</p>
913<h4><code>diffieHellman.setPrivateKey(privateKey[, encoding])</code><span><a class="mark" href="#crypto_diffiehellman_setprivatekey_privatekey_encoding" id="crypto_diffiehellman_setprivatekey_privatekey_encoding">#</a></span></h4>
914<div class="api_metadata">
915<span>Added in: v0.5.0</span>
916</div>
917<ul>
918<li><code>privateKey</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
919<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the <code>privateKey</code> string.</li>
920</ul>
921<p>Sets the Diffie-Hellman private key. If the <code>encoding</code> argument is provided,
922<code>privateKey</code> is expected
923to be a string. If no <code>encoding</code> is provided, <code>privateKey</code> is expected
924to be a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or <code>DataView</code>.</p>
925<h4><code>diffieHellman.setPublicKey(publicKey[, encoding])</code><span><a class="mark" href="#crypto_diffiehellman_setpublickey_publickey_encoding" id="crypto_diffiehellman_setpublickey_publickey_encoding">#</a></span></h4>
926<div class="api_metadata">
927<span>Added in: v0.5.0</span>
928</div>
929<ul>
930<li><code>publicKey</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
931<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the <code>publicKey</code> string.</li>
932</ul>
933<p>Sets the Diffie-Hellman public key. If the <code>encoding</code> argument is provided,
934<code>publicKey</code> is expected
935to be a string. If no <code>encoding</code> is provided, <code>publicKey</code> is expected
936to be a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or <code>DataView</code>.</p>
937<h4><code>diffieHellman.verifyError</code><span><a class="mark" href="#crypto_diffiehellman_verifyerror" id="crypto_diffiehellman_verifyerror">#</a></span></h4>
938<div class="api_metadata">
939<span>Added in: v0.11.12</span>
940</div>
941<p>A bit field containing any warnings and/or errors resulting from a check
942performed during initialization of the <code>DiffieHellman</code> object.</p>
943<p>The following values are valid for this property (as defined in <code>constants</code>
944module):</p>
945<ul>
946<li><code>DH_CHECK_P_NOT_SAFE_PRIME</code></li>
947<li><code>DH_CHECK_P_NOT_PRIME</code></li>
948<li><code>DH_UNABLE_TO_CHECK_GENERATOR</code></li>
949<li><code>DH_NOT_SUITABLE_GENERATOR</code></li>
950</ul>
951</section><section><h3>Class: <code>DiffieHellmanGroup</code><span><a class="mark" href="#crypto_class_diffiehellmangroup" id="crypto_class_diffiehellmangroup">#</a></span></h3>
952<div class="api_metadata">
953<span>Added in: v0.7.5</span>
954</div>
955<p>The <code>DiffieHellmanGroup</code> class takes a well-known modp group as its argument.
956It works the same as <code>DiffieHellman</code>, except that it does not allow changing
957its keys after creation. In other words, it does not implement <code>setPublicKey()</code>
958or <code>setPrivateKey()</code> methods.</p>
959<pre><code class="language-js"><span class="hljs-keyword">const</span> name = <span class="hljs-string">'modp1'</span>;
960<span class="hljs-keyword">const</span> dh = crypto.<span class="hljs-title function_">createDiffieHellmanGroup</span>(name);</code></pre>
961<p><code>name</code> is taken from <a href="https://www.rfc-editor.org/rfc/rfc2412.txt">RFC 2412</a> (modp1 and 2) and <a href="https://www.rfc-editor.org/rfc/rfc3526.txt">RFC 3526</a>:</p>
962<pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">perl -ne <span class="hljs-string">'print "$1\n" if /"(modp\d+)"/'</span> src/node_crypto_groups.h</span>
963modp1  #  768 bits
964modp2  # 1024 bits
965modp5  # 1536 bits
966modp14 # 2048 bits
967modp15 # etc.
968modp16
969modp17
970modp18</code></pre>
971</section><section><h3>Class: <code>ECDH</code><span><a class="mark" href="#crypto_class_ecdh" id="crypto_class_ecdh">#</a></span></h3>
972<div class="api_metadata">
973<span>Added in: v0.11.14</span>
974</div>
975<p>The <code>ECDH</code> class is a utility for creating Elliptic Curve Diffie-Hellman (ECDH)
976key exchanges.</p>
977<p>Instances of the <code>ECDH</code> class can be created using the
978<a href="#crypto_crypto_createecdh_curvename"><code>crypto.createECDH()</code></a> function.</p>
979<pre><code class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
980<span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>);
981
982<span class="hljs-comment">// Generate Alice's keys...</span>
983<span class="hljs-keyword">const</span> alice = crypto.<span class="hljs-title function_">createECDH</span>(<span class="hljs-string">'secp521r1'</span>);
984<span class="hljs-keyword">const</span> aliceKey = alice.<span class="hljs-title function_">generateKeys</span>();
985
986<span class="hljs-comment">// Generate Bob's keys...</span>
987<span class="hljs-keyword">const</span> bob = crypto.<span class="hljs-title function_">createECDH</span>(<span class="hljs-string">'secp521r1'</span>);
988<span class="hljs-keyword">const</span> bobKey = bob.<span class="hljs-title function_">generateKeys</span>();
989
990<span class="hljs-comment">// Exchange and generate the secret...</span>
991<span class="hljs-keyword">const</span> aliceSecret = alice.<span class="hljs-title function_">computeSecret</span>(bobKey);
992<span class="hljs-keyword">const</span> bobSecret = bob.<span class="hljs-title function_">computeSecret</span>(aliceKey);
993
994assert.<span class="hljs-title function_">strictEqual</span>(aliceSecret.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>), bobSecret.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));
995<span class="hljs-comment">// OK</span></code></pre>
996<h4>Static method: <code>ECDH.convertKey(key, curve[, inputEncoding[, outputEncoding[, format]]])</code><span><a class="mark" href="#crypto_static_method_ecdh_convertkey_key_curve_inputencoding_outputencoding_format" id="crypto_static_method_ecdh_convertkey_key_curve_inputencoding_outputencoding_format">#</a></span></h4>
997<div class="api_metadata">
998<span>Added in: v10.0.0</span>
999</div>
1000<ul>
1001<li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
1002<li><code>curve</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1003<li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the <code>key</code> string.</li>
1004<li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li>
1005<li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <strong>Default:</strong> <code>'uncompressed'</code></li>
1006<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1007</ul>
1008<p>Converts the EC Diffie-Hellman public key specified by <code>key</code> and <code>curve</code> to the
1009format specified by <code>format</code>. The <code>format</code> argument specifies point encoding
1010and can be <code>'compressed'</code>, <code>'uncompressed'</code> or <code>'hybrid'</code>. The supplied key is
1011interpreted using the specified <code>inputEncoding</code>, and the returned key is encoded
1012using the specified <code>outputEncoding</code>.</p>
1013<p>Use <a href="#crypto_crypto_getcurves"><code>crypto.getCurves()</code></a> to obtain a list of available curve names.
1014On recent OpenSSL releases, <code>openssl ecparam -list_curves</code> will also display
1015the name and description of each available elliptic curve.</p>
1016<p>If <code>format</code> is not specified the point will be returned in <code>'uncompressed'</code>
1017format.</p>
1018<p>If the <code>inputEncoding</code> is not provided, <code>key</code> is expected to be a <a href="buffer.html"><code>Buffer</code></a>,
1019<code>TypedArray</code>, or <code>DataView</code>.</p>
1020<p>Example (uncompressing a key):</p>
1021<pre><code class="language-js"><span class="hljs-keyword">const</span> { createECDH, <span class="hljs-variable constant_">ECDH</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
1022
1023<span class="hljs-keyword">const</span> ecdh = <span class="hljs-title function_">createECDH</span>(<span class="hljs-string">'secp256k1'</span>);
1024ecdh.<span class="hljs-title function_">generateKeys</span>();
1025
1026<span class="hljs-keyword">const</span> compressedKey = ecdh.<span class="hljs-title function_">getPublicKey</span>(<span class="hljs-string">'hex'</span>, <span class="hljs-string">'compressed'</span>);
1027
1028<span class="hljs-keyword">const</span> uncompressedKey = <span class="hljs-variable constant_">ECDH</span>.<span class="hljs-title function_">convertKey</span>(compressedKey,
1029                                        <span class="hljs-string">'secp256k1'</span>,
1030                                        <span class="hljs-string">'hex'</span>,
1031                                        <span class="hljs-string">'hex'</span>,
1032                                        <span class="hljs-string">'uncompressed'</span>);
1033
1034<span class="hljs-comment">// The converted key and the uncompressed public key should be the same</span>
1035<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(uncompressedKey === ecdh.<span class="hljs-title function_">getPublicKey</span>(<span class="hljs-string">'hex'</span>));</code></pre>
1036<h4><code>ecdh.computeSecret(otherPublicKey[, inputEncoding][, outputEncoding])</code><span><a class="mark" href="#crypto_ecdh_computesecret_otherpublickey_inputencoding_outputencoding" id="crypto_ecdh_computesecret_otherpublickey_inputencoding_outputencoding">#</a></span></h4>
1037<div class="api_metadata">
1038<details class="changelog"><summary>History</summary>
1039<table>
1040<tbody><tr><th>Version</th><th>Changes</th></tr>
1041<tr><td>v10.0.0</td>
1042<td><p>Changed error format to better support invalid public key error.</p></td></tr>
1043<tr><td>v6.0.0</td>
1044<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p></td></tr>
1045<tr><td>v0.11.14</td>
1046<td><p><span>Added in: v0.11.14</span></p></td></tr>
1047</tbody></table>
1048</details>
1049</div>
1050<ul>
1051<li><code>otherPublicKey</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
1052<li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the <code>otherPublicKey</code> string.</li>
1053<li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li>
1054<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1055</ul>
1056<p>Computes the shared secret using <code>otherPublicKey</code> as the other
1057party's public key and returns the computed shared secret. The supplied
1058key is interpreted using specified <code>inputEncoding</code>, and the returned secret
1059is encoded using the specified <code>outputEncoding</code>.
1060If the <code>inputEncoding</code> is not
1061provided, <code>otherPublicKey</code> is expected to be a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or
1062<code>DataView</code>.</p>
1063<p>If <code>outputEncoding</code> is given a string will be returned; otherwise a
1064<a href="buffer.html"><code>Buffer</code></a> is returned.</p>
1065<p><code>ecdh.computeSecret</code> will throw an
1066<code>ERR_CRYPTO_ECDH_INVALID_PUBLIC_KEY</code> error when <code>otherPublicKey</code>
1067lies outside of the elliptic curve. Since <code>otherPublicKey</code> is
1068usually supplied from a remote user over an insecure network,
1069be sure to handle this exception accordingly.</p>
1070<h4><code>ecdh.generateKeys([encoding[, format]])</code><span><a class="mark" href="#crypto_ecdh_generatekeys_encoding_format" id="crypto_ecdh_generatekeys_encoding_format">#</a></span></h4>
1071<div class="api_metadata">
1072<span>Added in: v0.11.14</span>
1073</div>
1074<ul>
1075<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li>
1076<li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <strong>Default:</strong> <code>'uncompressed'</code></li>
1077<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1078</ul>
1079<p>Generates private and public EC Diffie-Hellman key values, and returns
1080the public key in the specified <code>format</code> and <code>encoding</code>. This key should be
1081transferred to the other party.</p>
1082<p>The <code>format</code> argument specifies point encoding and can be <code>'compressed'</code> or
1083<code>'uncompressed'</code>. If <code>format</code> is not specified, the point will be returned in
1084<code>'uncompressed'</code> format.</p>
1085<p>If <code>encoding</code> is provided a string is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a>
1086is returned.</p>
1087<h4><code>ecdh.getPrivateKey([encoding])</code><span><a class="mark" href="#crypto_ecdh_getprivatekey_encoding" id="crypto_ecdh_getprivatekey_encoding">#</a></span></h4>
1088<div class="api_metadata">
1089<span>Added in: v0.11.14</span>
1090</div>
1091<ul>
1092<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li>
1093<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The EC Diffie-Hellman in the specified <code>encoding</code>.</li>
1094</ul>
1095<p>If <code>encoding</code> is specified, a string is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is
1096returned.</p>
1097<h4><code>ecdh.getPublicKey([encoding][, format])</code><span><a class="mark" href="#crypto_ecdh_getpublickey_encoding_format" id="crypto_ecdh_getpublickey_encoding_format">#</a></span></h4>
1098<div class="api_metadata">
1099<span>Added in: v0.11.14</span>
1100</div>
1101<ul>
1102<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li>
1103<li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <strong>Default:</strong> <code>'uncompressed'</code></li>
1104<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The EC Diffie-Hellman public key in the specified
1105<code>encoding</code> and <code>format</code>.</li>
1106</ul>
1107<p>The <code>format</code> argument specifies point encoding and can be <code>'compressed'</code> or
1108<code>'uncompressed'</code>. If <code>format</code> is not specified the point will be returned in
1109<code>'uncompressed'</code> format.</p>
1110<p>If <code>encoding</code> is specified, a string is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is
1111returned.</p>
1112<h4><code>ecdh.setPrivateKey(privateKey[, encoding])</code><span><a class="mark" href="#crypto_ecdh_setprivatekey_privatekey_encoding" id="crypto_ecdh_setprivatekey_privatekey_encoding">#</a></span></h4>
1113<div class="api_metadata">
1114<span>Added in: v0.11.14</span>
1115</div>
1116<ul>
1117<li><code>privateKey</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
1118<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the <code>privateKey</code> string.</li>
1119</ul>
1120<p>Sets the EC Diffie-Hellman private key.
1121If <code>encoding</code> is provided, <code>privateKey</code> is expected
1122to be a string; otherwise <code>privateKey</code> is expected to be a <a href="buffer.html"><code>Buffer</code></a>,
1123<code>TypedArray</code>, or <code>DataView</code>.</p>
1124<p>If <code>privateKey</code> is not valid for the curve specified when the <code>ECDH</code> object was
1125created, an error is thrown. Upon setting the private key, the associated
1126public point (key) is also generated and set in the <code>ECDH</code> object.</p>
1127<h4><code>ecdh.setPublicKey(publicKey[, encoding])</code><span><a class="mark" href="#crypto_ecdh_setpublickey_publickey_encoding" id="crypto_ecdh_setpublickey_publickey_encoding">#</a></span></h4>
1128<div class="api_metadata">
1129<span>Added in: v0.11.14</span><span>Deprecated since: v5.2.0</span>
1130</div>
1131<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated</div><p></p>
1132<ul>
1133<li><code>publicKey</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
1134<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the <code>publicKey</code> string.</li>
1135</ul>
1136<p>Sets the EC Diffie-Hellman public key.
1137If <code>encoding</code> is provided <code>publicKey</code> is expected to
1138be a string; otherwise a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or <code>DataView</code> is expected.</p>
1139<p>There is not normally a reason to call this method because <code>ECDH</code>
1140only requires a private key and the other party's public key to compute the
1141shared secret. Typically either <a href="#crypto_ecdh_generatekeys_encoding_format"><code>ecdh.generateKeys()</code></a> or
1142<a href="#crypto_ecdh_setprivatekey_privatekey_encoding"><code>ecdh.setPrivateKey()</code></a> will be called. The <a href="#crypto_ecdh_setprivatekey_privatekey_encoding"><code>ecdh.setPrivateKey()</code></a> method
1143attempts to generate the public point/key associated with the private key being
1144set.</p>
1145<p>Example (obtaining a shared secret):</p>
1146<pre><code class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
1147<span class="hljs-keyword">const</span> alice = crypto.<span class="hljs-title function_">createECDH</span>(<span class="hljs-string">'secp256k1'</span>);
1148<span class="hljs-keyword">const</span> bob = crypto.<span class="hljs-title function_">createECDH</span>(<span class="hljs-string">'secp256k1'</span>);
1149
1150<span class="hljs-comment">// This is a shortcut way of specifying one of Alice's previous private</span>
1151<span class="hljs-comment">// keys. It would be unwise to use such a predictable private key in a real</span>
1152<span class="hljs-comment">// application.</span>
1153alice.<span class="hljs-title function_">setPrivateKey</span>(
1154  crypto.<span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>).<span class="hljs-title function_">update</span>(<span class="hljs-string">'alice'</span>, <span class="hljs-string">'utf8'</span>).<span class="hljs-title function_">digest</span>()
1155);
1156
1157<span class="hljs-comment">// Bob uses a newly generated cryptographically strong</span>
1158<span class="hljs-comment">// pseudorandom key pair</span>
1159bob.<span class="hljs-title function_">generateKeys</span>();
1160
1161<span class="hljs-keyword">const</span> aliceSecret = alice.<span class="hljs-title function_">computeSecret</span>(bob.<span class="hljs-title function_">getPublicKey</span>(), <span class="hljs-literal">null</span>, <span class="hljs-string">'hex'</span>);
1162<span class="hljs-keyword">const</span> bobSecret = bob.<span class="hljs-title function_">computeSecret</span>(alice.<span class="hljs-title function_">getPublicKey</span>(), <span class="hljs-literal">null</span>, <span class="hljs-string">'hex'</span>);
1163
1164<span class="hljs-comment">// aliceSecret and bobSecret should be the same shared secret value</span>
1165<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(aliceSecret === bobSecret);</code></pre>
1166</section><section><h3>Class: <code>Hash</code><span><a class="mark" href="#crypto_class_hash" id="crypto_class_hash">#</a></span></h3>
1167<div class="api_metadata">
1168<span>Added in: v0.1.92</span>
1169</div>
1170<ul>
1171<li>Extends: <a href="stream.html#stream_class_stream_transform" class="type">&#x3C;stream.Transform></a></li>
1172</ul>
1173<p>The <code>Hash</code> class is a utility for creating hash digests of data. It can be
1174used in one of two ways:</p>
1175<ul>
1176<li>As a <a href="stream.html">stream</a> that is both readable and writable, where data is written
1177to produce a computed hash digest on the readable side, or</li>
1178<li>Using the <a href="#crypto_hash_update_data_inputencoding"><code>hash.update()</code></a> and <a href="#crypto_hash_digest_encoding"><code>hash.digest()</code></a> methods to produce the
1179computed hash.</li>
1180</ul>
1181<p>The <a href="#crypto_crypto_createhash_algorithm_options"><code>crypto.createHash()</code></a> method is used to create <code>Hash</code> instances. <code>Hash</code>
1182objects are not to be created directly using the <code>new</code> keyword.</p>
1183<p>Example: Using <code>Hash</code> objects as streams:</p>
1184<pre><code class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
1185<span class="hljs-keyword">const</span> hash = crypto.<span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>);
1186
1187hash.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> {
1188  <span class="hljs-comment">// Only one element is going to be produced by the</span>
1189  <span class="hljs-comment">// hash stream.</span>
1190  <span class="hljs-keyword">const</span> data = hash.<span class="hljs-title function_">read</span>();
1191  <span class="hljs-keyword">if</span> (data) {
1192    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(data.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));
1193    <span class="hljs-comment">// Prints:</span>
1194    <span class="hljs-comment">//   6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50</span>
1195  }
1196});
1197
1198hash.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to hash'</span>);
1199hash.<span class="hljs-title function_">end</span>();</code></pre>
1200<p>Example: Using <code>Hash</code> and piped streams:</p>
1201<pre><code class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
1202<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>);
1203<span class="hljs-keyword">const</span> hash = crypto.<span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>);
1204
1205<span class="hljs-keyword">const</span> input = fs.<span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'test.js'</span>);
1206input.<span class="hljs-title function_">pipe</span>(hash).<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'hex'</span>).<span class="hljs-title function_">pipe</span>(process.<span class="hljs-property">stdout</span>);</code></pre>
1207<p>Example: Using the <a href="#crypto_hash_update_data_inputencoding"><code>hash.update()</code></a> and <a href="#crypto_hash_digest_encoding"><code>hash.digest()</code></a> methods:</p>
1208<pre><code class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
1209<span class="hljs-keyword">const</span> hash = crypto.<span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>);
1210
1211hash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to hash'</span>);
1212<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(hash.<span class="hljs-title function_">digest</span>(<span class="hljs-string">'hex'</span>));
1213<span class="hljs-comment">// Prints:</span>
1214<span class="hljs-comment">//   6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50</span></code></pre>
1215<h4><code>hash.copy([options])</code><span><a class="mark" href="#crypto_hash_copy_options" id="crypto_hash_copy_options">#</a></span></h4>
1216<div class="api_metadata">
1217<span>Added in: v13.1.0</span>
1218</div>
1219<ul>
1220<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> <a href="stream.html#stream_new_stream_transform_options"><code>stream.transform</code> options</a></li>
1221<li>Returns: <a href="crypto.html#crypto_class_hash" class="type">&#x3C;Hash></a></li>
1222</ul>
1223<p>Creates a new <code>Hash</code> object that contains a deep copy of the internal state
1224of the current <code>Hash</code> object.</p>
1225<p>The optional <code>options</code> argument controls stream behavior. For XOF hash
1226functions such as <code>'shake256'</code>, the <code>outputLength</code> option can be used to
1227specify the desired output length in bytes.</p>
1228<p>An error is thrown when an attempt is made to copy the <code>Hash</code> object after
1229its <a href="#crypto_hash_digest_encoding"><code>hash.digest()</code></a> method has been called.</p>
1230<pre><code class="language-js"><span class="hljs-comment">// Calculate a rolling hash.</span>
1231<span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
1232<span class="hljs-keyword">const</span> hash = crypto.<span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>);
1233
1234hash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'one'</span>);
1235<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(hash.<span class="hljs-title function_">copy</span>().<span class="hljs-title function_">digest</span>(<span class="hljs-string">'hex'</span>));
1236
1237hash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'two'</span>);
1238<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(hash.<span class="hljs-title function_">copy</span>().<span class="hljs-title function_">digest</span>(<span class="hljs-string">'hex'</span>));
1239
1240hash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'three'</span>);
1241<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(hash.<span class="hljs-title function_">copy</span>().<span class="hljs-title function_">digest</span>(<span class="hljs-string">'hex'</span>));
1242
1243<span class="hljs-comment">// Etc.</span></code></pre>
1244<h4><code>hash.digest([encoding])</code><span><a class="mark" href="#crypto_hash_digest_encoding" id="crypto_hash_digest_encoding">#</a></span></h4>
1245<div class="api_metadata">
1246<span>Added in: v0.1.92</span>
1247</div>
1248<ul>
1249<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li>
1250<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1251</ul>
1252<p>Calculates the digest of all of the data passed to be hashed (using the
1253<a href="#crypto_hash_update_data_inputencoding"><code>hash.update()</code></a> method).
1254If <code>encoding</code> is provided a string will be returned; otherwise
1255a <a href="buffer.html"><code>Buffer</code></a> is returned.</p>
1256<p>The <code>Hash</code> object can not be used again after <code>hash.digest()</code> method has been
1257called. Multiple calls will cause an error to be thrown.</p>
1258<h4><code>hash.update(data[, inputEncoding])</code><span><a class="mark" href="#crypto_hash_update_data_inputencoding" id="crypto_hash_update_data_inputencoding">#</a></span></h4>
1259<div class="api_metadata">
1260<details class="changelog"><summary>History</summary>
1261<table>
1262<tbody><tr><th>Version</th><th>Changes</th></tr>
1263<tr><td>v6.0.0</td>
1264<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p></td></tr>
1265<tr><td>v0.1.92</td>
1266<td><p><span>Added in: v0.1.92</span></p></td></tr>
1267</tbody></table>
1268</details>
1269</div>
1270<ul>
1271<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
1272<li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the <code>data</code> string.</li>
1273</ul>
1274<p>Updates the hash content with the given <code>data</code>, the encoding of which
1275is given in <code>inputEncoding</code>.
1276If <code>encoding</code> is not provided, and the <code>data</code> is a string, an
1277encoding of <code>'utf8'</code> is enforced. If <code>data</code> is a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or
1278<code>DataView</code>, then <code>inputEncoding</code> is ignored.</p>
1279<p>This can be called many times with new data as it is streamed.</p>
1280</section><section><h3>Class: <code>Hmac</code><span><a class="mark" href="#crypto_class_hmac" id="crypto_class_hmac">#</a></span></h3>
1281<div class="api_metadata">
1282<span>Added in: v0.1.94</span>
1283</div>
1284<ul>
1285<li>Extends: <a href="stream.html#stream_class_stream_transform" class="type">&#x3C;stream.Transform></a></li>
1286</ul>
1287<p>The <code>Hmac</code> class is a utility for creating cryptographic HMAC digests. It can
1288be used in one of two ways:</p>
1289<ul>
1290<li>As a <a href="stream.html">stream</a> that is both readable and writable, where data is written
1291to produce a computed HMAC digest on the readable side, or</li>
1292<li>Using the <a href="#crypto_hmac_update_data_inputencoding"><code>hmac.update()</code></a> and <a href="#crypto_hmac_digest_encoding"><code>hmac.digest()</code></a> methods to produce the
1293computed HMAC digest.</li>
1294</ul>
1295<p>The <a href="#crypto_crypto_createhmac_algorithm_key_options"><code>crypto.createHmac()</code></a> method is used to create <code>Hmac</code> instances. <code>Hmac</code>
1296objects are not to be created directly using the <code>new</code> keyword.</p>
1297<p>Example: Using <code>Hmac</code> objects as streams:</p>
1298<pre><code class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
1299<span class="hljs-keyword">const</span> hmac = crypto.<span class="hljs-title function_">createHmac</span>(<span class="hljs-string">'sha256'</span>, <span class="hljs-string">'a secret'</span>);
1300
1301hmac.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> {
1302  <span class="hljs-comment">// Only one element is going to be produced by the</span>
1303  <span class="hljs-comment">// hash stream.</span>
1304  <span class="hljs-keyword">const</span> data = hmac.<span class="hljs-title function_">read</span>();
1305  <span class="hljs-keyword">if</span> (data) {
1306    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(data.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));
1307    <span class="hljs-comment">// Prints:</span>
1308    <span class="hljs-comment">//   7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e</span>
1309  }
1310});
1311
1312hmac.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to hash'</span>);
1313hmac.<span class="hljs-title function_">end</span>();</code></pre>
1314<p>Example: Using <code>Hmac</code> and piped streams:</p>
1315<pre><code class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
1316<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>);
1317<span class="hljs-keyword">const</span> hmac = crypto.<span class="hljs-title function_">createHmac</span>(<span class="hljs-string">'sha256'</span>, <span class="hljs-string">'a secret'</span>);
1318
1319<span class="hljs-keyword">const</span> input = fs.<span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'test.js'</span>);
1320input.<span class="hljs-title function_">pipe</span>(hmac).<span class="hljs-title function_">pipe</span>(process.<span class="hljs-property">stdout</span>);</code></pre>
1321<p>Example: Using the <a href="#crypto_hmac_update_data_inputencoding"><code>hmac.update()</code></a> and <a href="#crypto_hmac_digest_encoding"><code>hmac.digest()</code></a> methods:</p>
1322<pre><code class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
1323<span class="hljs-keyword">const</span> hmac = crypto.<span class="hljs-title function_">createHmac</span>(<span class="hljs-string">'sha256'</span>, <span class="hljs-string">'a secret'</span>);
1324
1325hmac.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to hash'</span>);
1326<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(hmac.<span class="hljs-title function_">digest</span>(<span class="hljs-string">'hex'</span>));
1327<span class="hljs-comment">// Prints:</span>
1328<span class="hljs-comment">//   7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e</span></code></pre>
1329<h4><code>hmac.digest([encoding])</code><span><a class="mark" href="#crypto_hmac_digest_encoding" id="crypto_hmac_digest_encoding">#</a></span></h4>
1330<div class="api_metadata">
1331<span>Added in: v0.1.94</span>
1332</div>
1333<ul>
1334<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li>
1335<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1336</ul>
1337<p>Calculates the HMAC digest of all of the data passed using <a href="#crypto_hmac_update_data_inputencoding"><code>hmac.update()</code></a>.
1338If <code>encoding</code> is
1339provided a string is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is returned;</p>
1340<p>The <code>Hmac</code> object can not be used again after <code>hmac.digest()</code> has been
1341called. Multiple calls to <code>hmac.digest()</code> will result in an error being thrown.</p>
1342<h4><code>hmac.update(data[, inputEncoding])</code><span><a class="mark" href="#crypto_hmac_update_data_inputencoding" id="crypto_hmac_update_data_inputencoding">#</a></span></h4>
1343<div class="api_metadata">
1344<details class="changelog"><summary>History</summary>
1345<table>
1346<tbody><tr><th>Version</th><th>Changes</th></tr>
1347<tr><td>v6.0.0</td>
1348<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p></td></tr>
1349<tr><td>v0.1.94</td>
1350<td><p><span>Added in: v0.1.94</span></p></td></tr>
1351</tbody></table>
1352</details>
1353</div>
1354<ul>
1355<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
1356<li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the <code>data</code> string.</li>
1357</ul>
1358<p>Updates the <code>Hmac</code> content with the given <code>data</code>, the encoding of which
1359is given in <code>inputEncoding</code>.
1360If <code>encoding</code> is not provided, and the <code>data</code> is a string, an
1361encoding of <code>'utf8'</code> is enforced. If <code>data</code> is a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or
1362<code>DataView</code>, then <code>inputEncoding</code> is ignored.</p>
1363<p>This can be called many times with new data as it is streamed.</p>
1364</section><section><h3>Class: <code>KeyObject</code><span><a class="mark" href="#crypto_class_keyobject" id="crypto_class_keyobject">#</a></span></h3>
1365<div class="api_metadata">
1366<details class="changelog"><summary>History</summary>
1367<table>
1368<tbody><tr><th>Version</th><th>Changes</th></tr>
1369<tr><td>v14.5.0</td>
1370<td><p>Instances of this class can now be passed to worker threads using <code>postMessage</code>.</p></td></tr>
1371<tr><td>v11.13.0</td>
1372<td><p>This class is now exported.</p></td></tr>
1373<tr><td>v11.6.0</td>
1374<td><p><span>Added in: v11.6.0</span></p></td></tr>
1375</tbody></table>
1376</details>
1377</div>
1378<p>Node.js uses a <code>KeyObject</code> class to represent a symmetric or asymmetric key,
1379and each kind of key exposes different functions. The
1380<a href="#crypto_crypto_createsecretkey_key"><code>crypto.createSecretKey()</code></a>, <a href="#crypto_crypto_createpublickey_key"><code>crypto.createPublicKey()</code></a> and
1381<a href="#crypto_crypto_createprivatekey_key"><code>crypto.createPrivateKey()</code></a> methods are used to create <code>KeyObject</code>
1382instances. <code>KeyObject</code> objects are not to be created directly using the <code>new</code>
1383keyword.</p>
1384<p>Most applications should consider using the new <code>KeyObject</code> API instead of
1385passing keys as strings or <code>Buffer</code>s due to improved security features.</p>
1386<p><code>KeyObject</code> instances can be passed to other threads via <a href="worker_threads.html#worker_threads_port_postmessage_value_transferlist"><code>postMessage()</code></a>.
1387The receiver obtains a cloned <code>KeyObject</code>, and the <code>KeyObject</code> does not need to
1388be listed in the <code>transferList</code> argument.</p>
1389<h4><code>keyObject.asymmetricKeyType</code><span><a class="mark" href="#crypto_keyobject_asymmetrickeytype" id="crypto_keyobject_asymmetrickeytype">#</a></span></h4>
1390<div class="api_metadata">
1391<details class="changelog"><summary>History</summary>
1392<table>
1393<tbody><tr><th>Version</th><th>Changes</th></tr>
1394<tr><td>v13.9.0</td>
1395<td><p>Added support for <code>'dh'</code>.</p></td></tr>
1396<tr><td>v12.0.0</td>
1397<td><p>Added support for <code>'rsa-pss'</code>.</p></td></tr>
1398<tr><td>v12.0.0</td>
1399<td><p>This property now returns <code>undefined</code> for KeyObject instances of unrecognized type instead of aborting.</p></td></tr>
1400<tr><td>v12.0.0</td>
1401<td><p>Added support for <code>'x25519'</code> and <code>'x448'</code>.</p></td></tr>
1402<tr><td>v12.0.0</td>
1403<td><p>Added support for <code>'ed25519'</code> and <code>'ed448'</code>.</p></td></tr>
1404<tr><td>v11.6.0</td>
1405<td><p><span>Added in: v11.6.0</span></p></td></tr>
1406</tbody></table>
1407</details>
1408</div>
1409<ul>
1410<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1411</ul>
1412<p>For asymmetric keys, this property represents the type of the key. Supported key
1413types are:</p>
1414<ul>
1415<li><code>'rsa'</code> (OID 1.2.840.113549.1.1.1)</li>
1416<li><code>'rsa-pss'</code> (OID 1.2.840.113549.1.1.10)</li>
1417<li><code>'dsa'</code> (OID 1.2.840.10040.4.1)</li>
1418<li><code>'ec'</code> (OID 1.2.840.10045.2.1)</li>
1419<li><code>'x25519'</code> (OID 1.3.101.110)</li>
1420<li><code>'x448'</code> (OID 1.3.101.111)</li>
1421<li><code>'ed25519'</code> (OID 1.3.101.112)</li>
1422<li><code>'ed448'</code> (OID 1.3.101.113)</li>
1423<li><code>'dh'</code> (OID 1.2.840.113549.1.3.1)</li>
1424</ul>
1425<p>This property is <code>undefined</code> for unrecognized <code>KeyObject</code> types and symmetric
1426keys.</p>
1427<h4><code>keyObject.export([options])</code><span><a class="mark" href="#crypto_keyobject_export_options" id="crypto_keyobject_export_options">#</a></span></h4>
1428<div class="api_metadata">
1429<span>Added in: v11.6.0</span>
1430</div>
1431<ul>
1432<li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
1433<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a></li>
1434</ul>
1435<p>For symmetric keys, this function allocates a <code>Buffer</code> containing the key
1436material and ignores any options.</p>
1437<p>For asymmetric keys, the <code>options</code> parameter is used to determine the export
1438format.</p>
1439<p>For public keys, the following encoding options can be used:</p>
1440<ul>
1441<li><code>type</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Must be one of <code>'pkcs1'</code> (RSA only) or <code>'spki'</code>.</li>
1442<li><code>format</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Must be <code>'pem'</code> or <code>'der'</code>.</li>
1443</ul>
1444<p>For private keys, the following encoding options can be used:</p>
1445<ul>
1446<li><code>type</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Must be one of <code>'pkcs1'</code> (RSA only), <code>'pkcs8'</code> or
1447<code>'sec1'</code> (EC only).</li>
1448<li><code>format</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Must be <code>'pem'</code> or <code>'der'</code>.</li>
1449<li><code>cipher</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> If specified, the private key will be encrypted with
1450the given <code>cipher</code> and <code>passphrase</code> using PKCS#5 v2.0 password based
1451encryption.</li>
1452<li><code>passphrase</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> The passphrase to use for encryption, see
1453<code>cipher</code>.</li>
1454</ul>
1455<p>When PEM encoding was selected, the result will be a string, otherwise it will
1456be a buffer containing the data encoded as DER.</p>
1457<p>PKCS#1, SEC1, and PKCS#8 type keys can be encrypted by using a combination of
1458the <code>cipher</code> and <code>format</code> options. The PKCS#8 <code>type</code> can be used with any
1459<code>format</code> to encrypt any key algorithm (RSA, EC, or DH) by specifying a
1460<code>cipher</code>. PKCS#1 and SEC1 can only be encrypted by specifying a <code>cipher</code>
1461when the PEM <code>format</code> is used. For maximum compatibility, use PKCS#8 for
1462encrypted private keys. Since PKCS#8 defines its own
1463encryption mechanism, PEM-level encryption is not supported when encrypting
1464a PKCS#8 key. See <a href="https://www.rfc-editor.org/rfc/rfc5208.txt">RFC 5208</a> for PKCS#8 encryption and <a href="https://www.rfc-editor.org/rfc/rfc1421.txt">RFC 1421</a> for
1465PKCS#1 and SEC1 encryption.</p>
1466<h4><code>keyObject.symmetricKeySize</code><span><a class="mark" href="#crypto_keyobject_symmetrickeysize" id="crypto_keyobject_symmetrickeysize">#</a></span></h4>
1467<div class="api_metadata">
1468<span>Added in: v11.6.0</span>
1469</div>
1470<ul>
1471<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1472</ul>
1473<p>For secret keys, this property represents the size of the key in bytes. This
1474property is <code>undefined</code> for asymmetric keys.</p>
1475<h4><code>keyObject.type</code><span><a class="mark" href="#crypto_keyobject_type" id="crypto_keyobject_type">#</a></span></h4>
1476<div class="api_metadata">
1477<span>Added in: v11.6.0</span>
1478</div>
1479<ul>
1480<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1481</ul>
1482<p>Depending on the type of this <code>KeyObject</code>, this property is either
1483<code>'secret'</code> for secret (symmetric) keys, <code>'public'</code> for public (asymmetric) keys
1484or <code>'private'</code> for private (asymmetric) keys.</p>
1485</section><section><h3>Class: <code>Sign</code><span><a class="mark" href="#crypto_class_sign" id="crypto_class_sign">#</a></span></h3>
1486<div class="api_metadata">
1487<span>Added in: v0.1.92</span>
1488</div>
1489<ul>
1490<li>Extends: <a href="stream.html#stream_class_stream_writable" class="type">&#x3C;stream.Writable></a></li>
1491</ul>
1492<p>The <code>Sign</code> class is a utility for generating signatures. It can be used in one
1493of two ways:</p>
1494<ul>
1495<li>As a writable <a href="stream.html">stream</a>, where data to be signed is written and the
1496<a href="#crypto_sign_sign_privatekey_outputencoding"><code>sign.sign()</code></a> method is used to generate and return the signature, or</li>
1497<li>Using the <a href="#crypto_sign_update_data_inputencoding"><code>sign.update()</code></a> and <a href="#crypto_sign_sign_privatekey_outputencoding"><code>sign.sign()</code></a> methods to produce the
1498signature.</li>
1499</ul>
1500<p>The <a href="#crypto_crypto_createsign_algorithm_options"><code>crypto.createSign()</code></a> method is used to create <code>Sign</code> instances. The
1501argument is the string name of the hash function to use. <code>Sign</code> objects are not
1502to be created directly using the <code>new</code> keyword.</p>
1503<p>Example: Using <code>Sign</code> and <a href="#crypto_class_verify"><code>Verify</code></a> objects as streams:</p>
1504<pre><code class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
1505
1506<span class="hljs-keyword">const</span> { privateKey, publicKey } = crypto.<span class="hljs-title function_">generateKeyPairSync</span>(<span class="hljs-string">'ec'</span>, {
1507  <span class="hljs-attr">namedCurve</span>: <span class="hljs-string">'sect239k1'</span>
1508});
1509
1510<span class="hljs-keyword">const</span> sign = crypto.<span class="hljs-title function_">createSign</span>(<span class="hljs-string">'SHA256'</span>);
1511sign.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to sign'</span>);
1512sign.<span class="hljs-title function_">end</span>();
1513<span class="hljs-keyword">const</span> signature = sign.<span class="hljs-title function_">sign</span>(privateKey, <span class="hljs-string">'hex'</span>);
1514
1515<span class="hljs-keyword">const</span> verify = crypto.<span class="hljs-title function_">createVerify</span>(<span class="hljs-string">'SHA256'</span>);
1516verify.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to sign'</span>);
1517verify.<span class="hljs-title function_">end</span>();
1518<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(verify.<span class="hljs-title function_">verify</span>(publicKey, signature, <span class="hljs-string">'hex'</span>));
1519<span class="hljs-comment">// Prints: true</span></code></pre>
1520<p>Example: Using the <a href="#crypto_sign_update_data_inputencoding"><code>sign.update()</code></a> and <a href="#crypto_verify_update_data_inputencoding"><code>verify.update()</code></a> methods:</p>
1521<pre><code class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
1522
1523<span class="hljs-keyword">const</span> { privateKey, publicKey } = crypto.<span class="hljs-title function_">generateKeyPairSync</span>(<span class="hljs-string">'rsa'</span>, {
1524  <span class="hljs-attr">modulusLength</span>: <span class="hljs-number">2048</span>,
1525});
1526
1527<span class="hljs-keyword">const</span> sign = crypto.<span class="hljs-title function_">createSign</span>(<span class="hljs-string">'SHA256'</span>);
1528sign.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to sign'</span>);
1529sign.<span class="hljs-title function_">end</span>();
1530<span class="hljs-keyword">const</span> signature = sign.<span class="hljs-title function_">sign</span>(privateKey);
1531
1532<span class="hljs-keyword">const</span> verify = crypto.<span class="hljs-title function_">createVerify</span>(<span class="hljs-string">'SHA256'</span>);
1533verify.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to sign'</span>);
1534verify.<span class="hljs-title function_">end</span>();
1535<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(verify.<span class="hljs-title function_">verify</span>(publicKey, signature));
1536<span class="hljs-comment">// Prints: true</span></code></pre>
1537<h4><code>sign.sign(privateKey[, outputEncoding])</code><span><a class="mark" href="#crypto_sign_sign_privatekey_outputencoding" id="crypto_sign_sign_privatekey_outputencoding">#</a></span></h4>
1538<div class="api_metadata">
1539<details class="changelog"><summary>History</summary>
1540<table>
1541<tbody><tr><th>Version</th><th>Changes</th></tr>
1542<tr><td>v13.2.0, v12.16.0</td>
1543<td><p>This function now supports IEEE-P1363 DSA and ECDSA signatures.</p></td></tr>
1544<tr><td>v12.0.0</td>
1545<td><p>This function now supports RSA-PSS keys.</p></td></tr>
1546<tr><td>v11.6.0</td>
1547<td><p>This function now supports key objects.</p></td></tr>
1548<tr><td>v8.0.0</td>
1549<td><p>Support for RSASSA-PSS and additional options was added.</p></td></tr>
1550<tr><td>v0.1.92</td>
1551<td><p><span>Added in: v0.1.92</span></p></td></tr>
1552</tbody></table>
1553</details>
1554</div>
1555<ul>
1556<li><code>privateKey</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="crypto.html#crypto_class_keyobject" class="type">&#x3C;KeyObject></a>
1557<ul>
1558<li><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1559<li><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
1560<li><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
1561</ul>
1562</li>
1563<li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li>
1564<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1565</ul>
1566<p>Calculates the signature on all the data passed through using either
1567<a href="#crypto_sign_update_data_inputencoding"><code>sign.update()</code></a> or <a href="stream.html#stream_writable_write_chunk_encoding_callback"><code>sign.write()</code></a>.</p>
1568<p>If <code>privateKey</code> is not a <a href="#crypto_class_keyobject"><code>KeyObject</code></a>, this function behaves as if
1569<code>privateKey</code> had been passed to <a href="#crypto_crypto_createprivatekey_key"><code>crypto.createPrivateKey()</code></a>. If it is an
1570object, the following additional properties can be passed:</p>
1571<ul>
1572<li>
1573<p><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> For DSA and ECDSA, this option specifies the
1574format of the generated signature. It can be one of the following:</p>
1575<ul>
1576<li><code>'der'</code> (default): DER-encoded ASN.1 signature structure encoding <code>(r, s)</code>.</li>
1577<li><code>'ieee-p1363'</code>: Signature format <code>r || s</code> as proposed in IEEE-P1363.</li>
1578</ul>
1579</li>
1580<li>
1581<p><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Optional padding value for RSA, one of the following:</p>
1582<ul>
1583<li><code>crypto.constants.RSA_PKCS1_PADDING</code> (default)</li>
1584<li><code>crypto.constants.RSA_PKCS1_PSS_PADDING</code></li>
1585</ul>
1586<p><code>RSA_PKCS1_PSS_PADDING</code> will use MGF1 with the same hash function
1587used to sign the message as specified in section 3.1 of <a href="https://www.rfc-editor.org/rfc/rfc4055.txt">RFC 4055</a>, unless
1588an MGF1 hash function has been specified as part of the key in compliance with
1589section 3.3 of <a href="https://www.rfc-editor.org/rfc/rfc4055.txt">RFC 4055</a>.</p>
1590</li>
1591<li>
1592<p><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Salt length for when padding is
1593<code>RSA_PKCS1_PSS_PADDING</code>. The special value
1594<code>crypto.constants.RSA_PSS_SALTLEN_DIGEST</code> sets the salt length to the digest
1595size, <code>crypto.constants.RSA_PSS_SALTLEN_MAX_SIGN</code> (default) sets it to the
1596maximum permissible value.</p>
1597</li>
1598</ul>
1599<p>If <code>outputEncoding</code> is provided a string is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a>
1600is returned.</p>
1601<p>The <code>Sign</code> object can not be again used after <code>sign.sign()</code> method has been
1602called. Multiple calls to <code>sign.sign()</code> will result in an error being thrown.</p>
1603<h4><code>sign.update(data[, inputEncoding])</code><span><a class="mark" href="#crypto_sign_update_data_inputencoding" id="crypto_sign_update_data_inputencoding">#</a></span></h4>
1604<div class="api_metadata">
1605<details class="changelog"><summary>History</summary>
1606<table>
1607<tbody><tr><th>Version</th><th>Changes</th></tr>
1608<tr><td>v6.0.0</td>
1609<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p></td></tr>
1610<tr><td>v0.1.92</td>
1611<td><p><span>Added in: v0.1.92</span></p></td></tr>
1612</tbody></table>
1613</details>
1614</div>
1615<ul>
1616<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
1617<li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the <code>data</code> string.</li>
1618</ul>
1619<p>Updates the <code>Sign</code> content with the given <code>data</code>, the encoding of which
1620is given in <code>inputEncoding</code>.
1621If <code>encoding</code> is not provided, and the <code>data</code> is a string, an
1622encoding of <code>'utf8'</code> is enforced. If <code>data</code> is a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or
1623<code>DataView</code>, then <code>inputEncoding</code> is ignored.</p>
1624<p>This can be called many times with new data as it is streamed.</p>
1625</section><section><h3>Class: <code>Verify</code><span><a class="mark" href="#crypto_class_verify" id="crypto_class_verify">#</a></span></h3>
1626<div class="api_metadata">
1627<span>Added in: v0.1.92</span>
1628</div>
1629<ul>
1630<li>Extends: <a href="stream.html#stream_class_stream_writable" class="type">&#x3C;stream.Writable></a></li>
1631</ul>
1632<p>The <code>Verify</code> class is a utility for verifying signatures. It can be used in one
1633of two ways:</p>
1634<ul>
1635<li>As a writable <a href="stream.html">stream</a> where written data is used to validate against the
1636supplied signature, or</li>
1637<li>Using the <a href="#crypto_verify_update_data_inputencoding"><code>verify.update()</code></a> and <a href="#crypto_verify_verify_object_signature_signatureencoding"><code>verify.verify()</code></a> methods to verify
1638the signature.</li>
1639</ul>
1640<p>The <a href="#crypto_crypto_createverify_algorithm_options"><code>crypto.createVerify()</code></a> method is used to create <code>Verify</code> instances.
1641<code>Verify</code> objects are not to be created directly using the <code>new</code> keyword.</p>
1642<p>See <a href="#crypto_class_sign"><code>Sign</code></a> for examples.</p>
1643<h4><code>verify.update(data[, inputEncoding])</code><span><a class="mark" href="#crypto_verify_update_data_inputencoding" id="crypto_verify_update_data_inputencoding">#</a></span></h4>
1644<div class="api_metadata">
1645<details class="changelog"><summary>History</summary>
1646<table>
1647<tbody><tr><th>Version</th><th>Changes</th></tr>
1648<tr><td>v6.0.0</td>
1649<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p></td></tr>
1650<tr><td>v0.1.92</td>
1651<td><p><span>Added in: v0.1.92</span></p></td></tr>
1652</tbody></table>
1653</details>
1654</div>
1655<ul>
1656<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
1657<li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the <code>data</code> string.</li>
1658</ul>
1659<p>Updates the <code>Verify</code> content with the given <code>data</code>, the encoding of which
1660is given in <code>inputEncoding</code>.
1661If <code>inputEncoding</code> is not provided, and the <code>data</code> is a string, an
1662encoding of <code>'utf8'</code> is enforced. If <code>data</code> is a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or
1663<code>DataView</code>, then <code>inputEncoding</code> is ignored.</p>
1664<p>This can be called many times with new data as it is streamed.</p>
1665<h4><code>verify.verify(object, signature[, signatureEncoding])</code><span><a class="mark" href="#crypto_verify_verify_object_signature_signatureencoding" id="crypto_verify_verify_object_signature_signatureencoding">#</a></span></h4>
1666<div class="api_metadata">
1667<details class="changelog"><summary>History</summary>
1668<table>
1669<tbody><tr><th>Version</th><th>Changes</th></tr>
1670<tr><td>v13.2.0, v12.16.0</td>
1671<td><p>This function now supports IEEE-P1363 DSA and ECDSA signatures.</p></td></tr>
1672<tr><td>v12.0.0</td>
1673<td><p>This function now supports RSA-PSS keys.</p></td></tr>
1674<tr><td>v11.7.0</td>
1675<td><p>The key can now be a private key.</p></td></tr>
1676<tr><td>v8.0.0</td>
1677<td><p>Support for RSASSA-PSS and additional options was added.</p></td></tr>
1678<tr><td>v0.1.92</td>
1679<td><p><span>Added in: v0.1.92</span></p></td></tr>
1680</tbody></table>
1681</details>
1682</div>
1683<ul>
1684<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="crypto.html#crypto_class_keyobject" class="type">&#x3C;KeyObject></a>
1685<ul>
1686<li><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1687<li><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
1688<li><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
1689</ul>
1690</li>
1691<li><code>signature</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
1692<li><code>signatureEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the <code>signature</code> string.</li>
1693<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <code>true</code> or <code>false</code> depending on the validity of the
1694signature for the data and public key.</li>
1695</ul>
1696<p>Verifies the provided data using the given <code>object</code> and <code>signature</code>.</p>
1697<p>If <code>object</code> is not a <a href="#crypto_class_keyobject"><code>KeyObject</code></a>, this function behaves as if
1698<code>object</code> had been passed to <a href="#crypto_crypto_createpublickey_key"><code>crypto.createPublicKey()</code></a>. If it is an
1699object, the following additional properties can be passed:</p>
1700<ul>
1701<li>
1702<p><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> For DSA and ECDSA, this option specifies the
1703format of the signature. It can be one of the following:</p>
1704<ul>
1705<li><code>'der'</code> (default): DER-encoded ASN.1 signature structure encoding <code>(r, s)</code>.</li>
1706<li><code>'ieee-p1363'</code>: Signature format <code>r || s</code> as proposed in IEEE-P1363.</li>
1707</ul>
1708</li>
1709<li>
1710<p><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Optional padding value for RSA, one of the following:</p>
1711<ul>
1712<li><code>crypto.constants.RSA_PKCS1_PADDING</code> (default)</li>
1713<li><code>crypto.constants.RSA_PKCS1_PSS_PADDING</code></li>
1714</ul>
1715<p><code>RSA_PKCS1_PSS_PADDING</code> will use MGF1 with the same hash function
1716used to verify the message as specified in section 3.1 of <a href="https://www.rfc-editor.org/rfc/rfc4055.txt">RFC 4055</a>, unless
1717an MGF1 hash function has been specified as part of the key in compliance with
1718section 3.3 of <a href="https://www.rfc-editor.org/rfc/rfc4055.txt">RFC 4055</a>.</p>
1719</li>
1720<li>
1721<p><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Salt length for when padding is
1722<code>RSA_PKCS1_PSS_PADDING</code>. The special value
1723<code>crypto.constants.RSA_PSS_SALTLEN_DIGEST</code> sets the salt length to the digest
1724size, <code>crypto.constants.RSA_PSS_SALTLEN_AUTO</code> (default) causes it to be
1725determined automatically.</p>
1726</li>
1727</ul>
1728<p>The <code>signature</code> argument is the previously calculated signature for the data, in
1729the <code>signatureEncoding</code>.
1730If a <code>signatureEncoding</code> is specified, the <code>signature</code> is expected to be a
1731string; otherwise <code>signature</code> is expected to be a <a href="buffer.html"><code>Buffer</code></a>,
1732<code>TypedArray</code>, or <code>DataView</code>.</p>
1733<p>The <code>verify</code> object can not be used again after <code>verify.verify()</code> has been
1734called. Multiple calls to <code>verify.verify()</code> will result in an error being
1735thrown.</p>
1736<p>Because public keys can be derived from private keys, a private key may
1737be passed instead of a public key.</p>
1738</section><section><h3><code>crypto</code> module methods and properties<span><a class="mark" href="#crypto_crypto_module_methods_and_properties" id="crypto_crypto_module_methods_and_properties">#</a></span></h3>
1739<h4><code>crypto.constants</code><span><a class="mark" href="#crypto_crypto_constants" id="crypto_crypto_constants">#</a></span></h4>
1740<div class="api_metadata">
1741<span>Added in: v6.3.0</span>
1742</div>
1743<ul>
1744<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> An object containing commonly used constants for crypto and
1745security related operations. The specific constants currently defined are
1746described in <a href="#crypto_crypto_constants_1">Crypto constants</a>.</li>
1747</ul>
1748<h4><code>crypto.DEFAULT_ENCODING</code><span><a class="mark" href="#crypto_crypto_default_encoding" id="crypto_crypto_default_encoding">#</a></span></h4>
1749<div class="api_metadata">
1750<span>Added in: v0.9.3</span><span>Deprecated since: v10.0.0</span>
1751</div>
1752<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated</div><p></p>
1753<p>The default encoding to use for functions that can take either strings
1754or <a href="buffer.html">buffers</a>. The default value is <code>'buffer'</code>, which makes methods
1755default to <a href="buffer.html"><code>Buffer</code></a> objects.</p>
1756<p>The <code>crypto.DEFAULT_ENCODING</code> mechanism is provided for backward compatibility
1757with legacy programs that expect <code>'latin1'</code> to be the default encoding.</p>
1758<p>New applications should expect the default to be <code>'buffer'</code>.</p>
1759<p>This property is deprecated.</p>
1760<h4><code>crypto.fips</code><span><a class="mark" href="#crypto_crypto_fips" id="crypto_crypto_fips">#</a></span></h4>
1761<div class="api_metadata">
1762<span>Added in: v6.0.0</span><span>Deprecated since: v10.0.0</span>
1763</div>
1764<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated</div><p></p>
1765<p>Property for checking and controlling whether a FIPS compliant crypto provider
1766is currently in use. Setting to true requires a FIPS build of Node.js.</p>
1767<p>This property is deprecated. Please use <code>crypto.setFips()</code> and
1768<code>crypto.getFips()</code> instead.</p>
1769<h4><code>crypto.createCipher(algorithm, password[, options])</code><span><a class="mark" href="#crypto_crypto_createcipher_algorithm_password_options" id="crypto_crypto_createcipher_algorithm_password_options">#</a></span></h4>
1770<div class="api_metadata">
1771<details class="changelog"><summary>History</summary>
1772<table>
1773<tbody><tr><th>Version</th><th>Changes</th></tr>
1774<tr><td>v10.10.0</td>
1775<td><p>Ciphers in OCB mode are now supported.</p></td></tr>
1776<tr><td>v10.2.0</td>
1777<td><p>The <code>authTagLength</code> option can now be used to produce shorter authentication tags in GCM mode and defaults to 16 bytes.</p></td></tr>
1778<tr><td>v10.0.0</td>
1779<td><p><span>Deprecated since: v10.0.0</span></p></td></tr>
1780<tr><td>v0.1.94</td>
1781<td><p><span>Added in: v0.1.94</span></p></td></tr>
1782</tbody></table>
1783</details>
1784</div>
1785<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="#crypto_crypto_createcipheriv_algorithm_key_iv_options"><code>crypto.createCipheriv()</code></a> instead.</div><p></p>
1786<ul>
1787<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1788<li><code>password</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
1789<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> <a href="stream.html#stream_new_stream_transform_options"><code>stream.transform</code> options</a></li>
1790<li>Returns: <a href="crypto.html#crypto_class_cipher" class="type">&#x3C;Cipher></a></li>
1791</ul>
1792<p>Creates and returns a <code>Cipher</code> object that uses the given <code>algorithm</code> and
1793<code>password</code>.</p>
1794<p>The <code>options</code> argument controls stream behavior and is optional except when a
1795cipher in CCM or OCB mode is used (e.g. <code>'aes-128-ccm'</code>). In that case, the
1796<code>authTagLength</code> option is required and specifies the length of the
1797authentication tag in bytes, see <a href="#crypto_ccm_mode">CCM mode</a>. In GCM mode, the <code>authTagLength</code>
1798option is not required but can be used to set the length of the authentication
1799tag that will be returned by <code>getAuthTag()</code> and defaults to 16 bytes.</p>
1800<p>The <code>algorithm</code> is dependent on OpenSSL, examples are <code>'aes192'</code>, etc. On
1801recent OpenSSL releases, <code>openssl list -cipher-algorithms</code>
1802(<code>openssl list-cipher-algorithms</code> for older versions of OpenSSL) will
1803display the available cipher algorithms.</p>
1804<p>The <code>password</code> is used to derive the cipher key and initialization vector (IV).
1805The value must be either a <code>'latin1'</code> encoded string, a <a href="buffer.html"><code>Buffer</code></a>, a
1806<code>TypedArray</code>, or a <code>DataView</code>.</p>
1807<p>The implementation of <code>crypto.createCipher()</code> derives keys using the OpenSSL
1808function <a href="https://www.openssl.org/docs/man1.1.0/crypto/EVP_BytesToKey.html"><code>EVP_BytesToKey</code></a> with the digest algorithm set to MD5, one
1809iteration, and no salt. The lack of salt allows dictionary attacks as the same
1810password always creates the same key. The low iteration count and
1811non-cryptographically secure hash algorithm allow passwords to be tested very
1812rapidly.</p>
1813<p>In line with OpenSSL's recommendation to use a more modern algorithm instead of
1814<a href="https://www.openssl.org/docs/man1.1.0/crypto/EVP_BytesToKey.html"><code>EVP_BytesToKey</code></a> it is recommended that developers derive a key and IV on
1815their own using <a href="#crypto_crypto_scrypt_password_salt_keylen_options_callback"><code>crypto.scrypt()</code></a> and to use <a href="#crypto_crypto_createcipheriv_algorithm_key_iv_options"><code>crypto.createCipheriv()</code></a>
1816to create the <code>Cipher</code> object. Users should not use ciphers with counter mode
1817(e.g. CTR, GCM, or CCM) in <code>crypto.createCipher()</code>. A warning is emitted when
1818they are used in order to avoid the risk of IV reuse that causes
1819vulnerabilities. For the case when IV is reused in GCM, see <a href="https://github.com/nonce-disrespect/nonce-disrespect">Nonce-Disrespecting
1820Adversaries</a> for details.</p>
1821<h4><code>crypto.createCipheriv(algorithm, key, iv[, options])</code><span><a class="mark" href="#crypto_crypto_createcipheriv_algorithm_key_iv_options" id="crypto_crypto_createcipheriv_algorithm_key_iv_options">#</a></span></h4>
1822<div class="api_metadata">
1823<details class="changelog"><summary>History</summary>
1824<table>
1825<tbody><tr><th>Version</th><th>Changes</th></tr>
1826<tr><td>v11.6.0</td>
1827<td><p>The <code>key</code> argument can now be a <code>KeyObject</code>.</p></td></tr>
1828<tr><td>v11.2.0, v10.17.0</td>
1829<td><p>The cipher <code>chacha20-poly1305</code> is now supported.</p></td></tr>
1830<tr><td>v10.10.0</td>
1831<td><p>Ciphers in OCB mode are now supported.</p></td></tr>
1832<tr><td>v10.2.0</td>
1833<td><p>The <code>authTagLength</code> option can now be used to produce shorter authentication tags in GCM mode and defaults to 16 bytes.</p></td></tr>
1834<tr><td>v9.9.0</td>
1835<td><p>The <code>iv</code> parameter may now be <code>null</code> for ciphers which do not need an initialization vector.</p></td></tr>
1836<tr><td>v0.1.94</td>
1837<td><p><span>Added in: v0.1.94</span></p></td></tr>
1838</tbody></table>
1839</details>
1840</div>
1841<ul>
1842<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1843<li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> | <a href="crypto.html#crypto_class_keyobject" class="type">&#x3C;KeyObject></a></li>
1844<li><code>iv</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a></li>
1845<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> <a href="stream.html#stream_new_stream_transform_options"><code>stream.transform</code> options</a></li>
1846<li>Returns: <a href="crypto.html#crypto_class_cipher" class="type">&#x3C;Cipher></a></li>
1847</ul>
1848<p>Creates and returns a <code>Cipher</code> object, with the given <code>algorithm</code>, <code>key</code> and
1849initialization vector (<code>iv</code>).</p>
1850<p>The <code>options</code> argument controls stream behavior and is optional except when a
1851cipher in CCM or OCB mode is used (e.g. <code>'aes-128-ccm'</code>). In that case, the
1852<code>authTagLength</code> option is required and specifies the length of the
1853authentication tag in bytes, see <a href="#crypto_ccm_mode">CCM mode</a>. In GCM mode, the <code>authTagLength</code>
1854option is not required but can be used to set the length of the authentication
1855tag that will be returned by <code>getAuthTag()</code> and defaults to 16 bytes.</p>
1856<p>The <code>algorithm</code> is dependent on OpenSSL, examples are <code>'aes192'</code>, etc. On
1857recent OpenSSL releases, <code>openssl list -cipher-algorithms</code>
1858(<code>openssl list-cipher-algorithms</code> for older versions of OpenSSL) will
1859display the available cipher algorithms.</p>
1860<p>The <code>key</code> is the raw key used by the <code>algorithm</code> and <code>iv</code> is an
1861<a href="https://en.wikipedia.org/wiki/Initialization_vector">initialization vector</a>. Both arguments must be <code>'utf8'</code> encoded strings,
1862<a href="buffer.html">Buffers</a>, <code>TypedArray</code>, or <code>DataView</code>s. The <code>key</code> may optionally be
1863a <a href="#crypto_class_keyobject"><code>KeyObject</code></a> of type <code>secret</code>. If the cipher does not need
1864an initialization vector, <code>iv</code> may be <code>null</code>.</p>
1865<p>Initialization vectors should be unpredictable and unique; ideally, they will be
1866cryptographically random. They do not have to be secret: IVs are typically just
1867added to ciphertext messages unencrypted. It may sound contradictory that
1868something has to be unpredictable and unique, but does not have to be secret;
1869remember that an attacker must not be able to predict ahead of time what a
1870given IV will be.</p>
1871<h4><code>crypto.createDecipher(algorithm, password[, options])</code><span><a class="mark" href="#crypto_crypto_createdecipher_algorithm_password_options" id="crypto_crypto_createdecipher_algorithm_password_options">#</a></span></h4>
1872<div class="api_metadata">
1873<details class="changelog"><summary>History</summary>
1874<table>
1875<tbody><tr><th>Version</th><th>Changes</th></tr>
1876<tr><td>v10.10.0</td>
1877<td><p>Ciphers in OCB mode are now supported.</p></td></tr>
1878<tr><td>v10.0.0</td>
1879<td><p><span>Deprecated since: v10.0.0</span></p></td></tr>
1880<tr><td>v0.1.94</td>
1881<td><p><span>Added in: v0.1.94</span></p></td></tr>
1882</tbody></table>
1883</details>
1884</div>
1885<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="#crypto_crypto_createdecipheriv_algorithm_key_iv_options"><code>crypto.createDecipheriv()</code></a> instead.</div><p></p>
1886<ul>
1887<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1888<li><code>password</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
1889<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> <a href="stream.html#stream_new_stream_transform_options"><code>stream.transform</code> options</a></li>
1890<li>Returns: <a href="crypto.html#crypto_class_decipher" class="type">&#x3C;Decipher></a></li>
1891</ul>
1892<p>Creates and returns a <code>Decipher</code> object that uses the given <code>algorithm</code> and
1893<code>password</code> (key).</p>
1894<p>The <code>options</code> argument controls stream behavior and is optional except when a
1895cipher in CCM or OCB mode is used (e.g. <code>'aes-128-ccm'</code>). In that case, the
1896<code>authTagLength</code> option is required and specifies the length of the
1897authentication tag in bytes, see <a href="#crypto_ccm_mode">CCM mode</a>.</p>
1898<p>The implementation of <code>crypto.createDecipher()</code> derives keys using the OpenSSL
1899function <a href="https://www.openssl.org/docs/man1.1.0/crypto/EVP_BytesToKey.html"><code>EVP_BytesToKey</code></a> with the digest algorithm set to MD5, one
1900iteration, and no salt. The lack of salt allows dictionary attacks as the same
1901password always creates the same key. The low iteration count and
1902non-cryptographically secure hash algorithm allow passwords to be tested very
1903rapidly.</p>
1904<p>In line with OpenSSL's recommendation to use a more modern algorithm instead of
1905<a href="https://www.openssl.org/docs/man1.1.0/crypto/EVP_BytesToKey.html"><code>EVP_BytesToKey</code></a> it is recommended that developers derive a key and IV on
1906their own using <a href="#crypto_crypto_scrypt_password_salt_keylen_options_callback"><code>crypto.scrypt()</code></a> and to use <a href="#crypto_crypto_createdecipheriv_algorithm_key_iv_options"><code>crypto.createDecipheriv()</code></a>
1907to create the <code>Decipher</code> object.</p>
1908<h4><code>crypto.createDecipheriv(algorithm, key, iv[, options])</code><span><a class="mark" href="#crypto_crypto_createdecipheriv_algorithm_key_iv_options" id="crypto_crypto_createdecipheriv_algorithm_key_iv_options">#</a></span></h4>
1909<div class="api_metadata">
1910<details class="changelog"><summary>History</summary>
1911<table>
1912<tbody><tr><th>Version</th><th>Changes</th></tr>
1913<tr><td>v11.6.0</td>
1914<td><p>The <code>key</code> argument can now be a <code>KeyObject</code>.</p></td></tr>
1915<tr><td>v11.2.0, v10.17.0</td>
1916<td><p>The cipher <code>chacha20-poly1305</code> is now supported.</p></td></tr>
1917<tr><td>v10.10.0</td>
1918<td><p>Ciphers in OCB mode are now supported.</p></td></tr>
1919<tr><td>v10.2.0</td>
1920<td><p>The <code>authTagLength</code> option can now be used to restrict accepted GCM authentication tag lengths.</p></td></tr>
1921<tr><td>v9.9.0</td>
1922<td><p>The <code>iv</code> parameter may now be <code>null</code> for ciphers which do not need an initialization vector.</p></td></tr>
1923<tr><td>v0.1.94</td>
1924<td><p><span>Added in: v0.1.94</span></p></td></tr>
1925</tbody></table>
1926</details>
1927</div>
1928<ul>
1929<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1930<li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> | <a href="crypto.html#crypto_class_keyobject" class="type">&#x3C;KeyObject></a></li>
1931<li><code>iv</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a></li>
1932<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> <a href="stream.html#stream_new_stream_transform_options"><code>stream.transform</code> options</a></li>
1933<li>Returns: <a href="crypto.html#crypto_class_decipher" class="type">&#x3C;Decipher></a></li>
1934</ul>
1935<p>Creates and returns a <code>Decipher</code> object that uses the given <code>algorithm</code>, <code>key</code>
1936and initialization vector (<code>iv</code>).</p>
1937<p>The <code>options</code> argument controls stream behavior and is optional except when a
1938cipher in CCM or OCB mode is used (e.g. <code>'aes-128-ccm'</code>). In that case, the
1939<code>authTagLength</code> option is required and specifies the length of the
1940authentication tag in bytes, see <a href="#crypto_ccm_mode">CCM mode</a>. In GCM mode, the <code>authTagLength</code>
1941option is not required but can be used to restrict accepted authentication tags
1942to those with the specified length.</p>
1943<p>The <code>algorithm</code> is dependent on OpenSSL, examples are <code>'aes192'</code>, etc. On
1944recent OpenSSL releases, <code>openssl list -cipher-algorithms</code>
1945(<code>openssl list-cipher-algorithms</code> for older versions of OpenSSL) will
1946display the available cipher algorithms.</p>
1947<p>The <code>key</code> is the raw key used by the <code>algorithm</code> and <code>iv</code> is an
1948<a href="https://en.wikipedia.org/wiki/Initialization_vector">initialization vector</a>. Both arguments must be <code>'utf8'</code> encoded strings,
1949<a href="buffer.html">Buffers</a>, <code>TypedArray</code>, or <code>DataView</code>s. The <code>key</code> may optionally be
1950a <a href="#crypto_class_keyobject"><code>KeyObject</code></a> of type <code>secret</code>. If the cipher does not need
1951an initialization vector, <code>iv</code> may be <code>null</code>.</p>
1952<p>Initialization vectors should be unpredictable and unique; ideally, they will be
1953cryptographically random. They do not have to be secret: IVs are typically just
1954added to ciphertext messages unencrypted. It may sound contradictory that
1955something has to be unpredictable and unique, but does not have to be secret;
1956remember that an attacker must not be able to predict ahead of time what a given
1957IV will be.</p>
1958<h4><code>crypto.createDiffieHellman(prime[, primeEncoding][, generator][, generatorEncoding])</code><span><a class="mark" href="#crypto_crypto_creatediffiehellman_prime_primeencoding_generator_generatorencoding" id="crypto_crypto_creatediffiehellman_prime_primeencoding_generator_generatorencoding">#</a></span></h4>
1959<div class="api_metadata">
1960<details class="changelog"><summary>History</summary>
1961<table>
1962<tbody><tr><th>Version</th><th>Changes</th></tr>
1963<tr><td>v8.0.0</td>
1964<td><p>The <code>prime</code> argument can be any <code>TypedArray</code> or <code>DataView</code> now.</p></td></tr>
1965<tr><td>v8.0.0</td>
1966<td><p>The <code>prime</code> argument can be a <code>Uint8Array</code> now.</p></td></tr>
1967<tr><td>v6.0.0</td>
1968<td><p>The default for the encoding parameters changed from <code>binary</code> to <code>utf8</code>.</p></td></tr>
1969<tr><td>v0.11.12</td>
1970<td><p><span>Added in: v0.11.12</span></p></td></tr>
1971</tbody></table>
1972</details>
1973</div>
1974<ul>
1975<li><code>prime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
1976<li><code>primeEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the <code>prime</code> string.</li>
1977<li><code>generator</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> <strong>Default:</strong>
1978<code>2</code></li>
1979<li><code>generatorEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the <code>generator</code> string.</li>
1980<li>Returns: <a href="crypto.html#crypto_class_diffiehellman" class="type">&#x3C;DiffieHellman></a></li>
1981</ul>
1982<p>Creates a <code>DiffieHellman</code> key exchange object using the supplied <code>prime</code> and an
1983optional specific <code>generator</code>.</p>
1984<p>The <code>generator</code> argument can be a number, string, or <a href="buffer.html"><code>Buffer</code></a>. If
1985<code>generator</code> is not specified, the value <code>2</code> is used.</p>
1986<p>If <code>primeEncoding</code> is specified, <code>prime</code> is expected to be a string; otherwise
1987a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or <code>DataView</code> is expected.</p>
1988<p>If <code>generatorEncoding</code> is specified, <code>generator</code> is expected to be a string;
1989otherwise a number, <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or <code>DataView</code> is expected.</p>
1990<h4><code>crypto.createDiffieHellman(primeLength[, generator])</code><span><a class="mark" href="#crypto_crypto_creatediffiehellman_primelength_generator" id="crypto_crypto_creatediffiehellman_primelength_generator">#</a></span></h4>
1991<div class="api_metadata">
1992<span>Added in: v0.5.0</span>
1993</div>
1994<ul>
1995<li><code>primeLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1996<li><code>generator</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> <strong>Default:</strong> <code>2</code></li>
1997<li>Returns: <a href="crypto.html#crypto_class_diffiehellman" class="type">&#x3C;DiffieHellman></a></li>
1998</ul>
1999<p>Creates a <code>DiffieHellman</code> key exchange object and generates a prime of
2000<code>primeLength</code> bits using an optional specific numeric <code>generator</code>.
2001If <code>generator</code> is not specified, the value <code>2</code> is used.</p>
2002<h4><code>crypto.createDiffieHellmanGroup(name)</code><span><a class="mark" href="#crypto_crypto_creatediffiehellmangroup_name" id="crypto_crypto_creatediffiehellmangroup_name">#</a></span></h4>
2003<div class="api_metadata">
2004<span>Added in: v0.9.3</span>
2005</div>
2006<ul>
2007<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
2008<li>Returns: <a href="crypto.html#crypto_class_diffiehellmangroup" class="type">&#x3C;DiffieHellmanGroup></a></li>
2009</ul>
2010<p>An alias for <a href="#crypto_crypto_getdiffiehellman_groupname"><code>crypto.getDiffieHellman()</code></a></p>
2011<h4><code>crypto.createECDH(curveName)</code><span><a class="mark" href="#crypto_crypto_createecdh_curvename" id="crypto_crypto_createecdh_curvename">#</a></span></h4>
2012<div class="api_metadata">
2013<span>Added in: v0.11.14</span>
2014</div>
2015<ul>
2016<li><code>curveName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
2017<li>Returns: <a href="crypto.html#crypto_class_ecdh" class="type">&#x3C;ECDH></a></li>
2018</ul>
2019<p>Creates an Elliptic Curve Diffie-Hellman (<code>ECDH</code>) key exchange object using a
2020predefined curve specified by the <code>curveName</code> string. Use
2021<a href="#crypto_crypto_getcurves"><code>crypto.getCurves()</code></a> to obtain a list of available curve names. On recent
2022OpenSSL releases, <code>openssl ecparam -list_curves</code> will also display the name
2023and description of each available elliptic curve.</p>
2024<h4><code>crypto.createHash(algorithm[, options])</code><span><a class="mark" href="#crypto_crypto_createhash_algorithm_options" id="crypto_crypto_createhash_algorithm_options">#</a></span></h4>
2025<div class="api_metadata">
2026<details class="changelog"><summary>History</summary>
2027<table>
2028<tbody><tr><th>Version</th><th>Changes</th></tr>
2029<tr><td>v12.8.0</td>
2030<td><p>The <code>outputLength</code> option was added for XOF hash functions.</p></td></tr>
2031<tr><td>v0.1.92</td>
2032<td><p><span>Added in: v0.1.92</span></p></td></tr>
2033</tbody></table>
2034</details>
2035</div>
2036<ul>
2037<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
2038<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> <a href="stream.html#stream_new_stream_transform_options"><code>stream.transform</code> options</a></li>
2039<li>Returns: <a href="crypto.html#crypto_class_hash" class="type">&#x3C;Hash></a></li>
2040</ul>
2041<p>Creates and returns a <code>Hash</code> object that can be used to generate hash digests
2042using the given <code>algorithm</code>. Optional <code>options</code> argument controls stream
2043behavior. For XOF hash functions such as <code>'shake256'</code>, the <code>outputLength</code> option
2044can be used to specify the desired output length in bytes.</p>
2045<p>The <code>algorithm</code> is dependent on the available algorithms supported by the
2046version of OpenSSL on the platform. Examples are <code>'sha256'</code>, <code>'sha512'</code>, etc.
2047On recent releases of OpenSSL, <code>openssl list -digest-algorithms</code>
2048(<code>openssl list-message-digest-algorithms</code> for older versions of OpenSSL) will
2049display the available digest algorithms.</p>
2050<p>Example: generating the sha256 sum of a file</p>
2051<pre><code class="language-js"><span class="hljs-keyword">const</span> filename = process.<span class="hljs-property">argv</span>[<span class="hljs-number">2</span>];
2052<span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
2053<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>);
2054
2055<span class="hljs-keyword">const</span> hash = crypto.<span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>);
2056
2057<span class="hljs-keyword">const</span> input = fs.<span class="hljs-title function_">createReadStream</span>(filename);
2058input.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> {
2059  <span class="hljs-comment">// Only one element is going to be produced by the</span>
2060  <span class="hljs-comment">// hash stream.</span>
2061  <span class="hljs-keyword">const</span> data = input.<span class="hljs-title function_">read</span>();
2062  <span class="hljs-keyword">if</span> (data)
2063    hash.<span class="hljs-title function_">update</span>(data);
2064  <span class="hljs-keyword">else</span> {
2065    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${hash.digest(<span class="hljs-string">'hex'</span>)}</span> <span class="hljs-subst">${filename}</span>`</span>);
2066  }
2067});</code></pre>
2068<h4><code>crypto.createHmac(algorithm, key[, options])</code><span><a class="mark" href="#crypto_crypto_createhmac_algorithm_key_options" id="crypto_crypto_createhmac_algorithm_key_options">#</a></span></h4>
2069<div class="api_metadata">
2070<details class="changelog"><summary>History</summary>
2071<table>
2072<tbody><tr><th>Version</th><th>Changes</th></tr>
2073<tr><td>v11.6.0</td>
2074<td><p>The <code>key</code> argument can now be a <code>KeyObject</code>.</p></td></tr>
2075<tr><td>v0.1.94</td>
2076<td><p><span>Added in: v0.1.94</span></p></td></tr>
2077</tbody></table>
2078</details>
2079</div>
2080<ul>
2081<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
2082<li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> | <a href="crypto.html#crypto_class_keyobject" class="type">&#x3C;KeyObject></a></li>
2083<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> <a href="stream.html#stream_new_stream_transform_options"><code>stream.transform</code> options</a></li>
2084<li>Returns: <a href="crypto.html#crypto_class_hmac" class="type">&#x3C;Hmac></a></li>
2085</ul>
2086<p>Creates and returns an <code>Hmac</code> object that uses the given <code>algorithm</code> and <code>key</code>.
2087Optional <code>options</code> argument controls stream behavior.</p>
2088<p>The <code>algorithm</code> is dependent on the available algorithms supported by the
2089version of OpenSSL on the platform. Examples are <code>'sha256'</code>, <code>'sha512'</code>, etc.
2090On recent releases of OpenSSL, <code>openssl list -digest-algorithms</code>
2091(<code>openssl list-message-digest-algorithms</code> for older versions of OpenSSL) will
2092display the available digest algorithms.</p>
2093<p>The <code>key</code> is the HMAC key used to generate the cryptographic HMAC hash. If it is
2094a <a href="#crypto_class_keyobject"><code>KeyObject</code></a>, its type must be <code>secret</code>.</p>
2095<p>Example: generating the sha256 HMAC of a file</p>
2096<pre><code class="language-js"><span class="hljs-keyword">const</span> filename = process.<span class="hljs-property">argv</span>[<span class="hljs-number">2</span>];
2097<span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
2098<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>);
2099
2100<span class="hljs-keyword">const</span> hmac = crypto.<span class="hljs-title function_">createHmac</span>(<span class="hljs-string">'sha256'</span>, <span class="hljs-string">'a secret'</span>);
2101
2102<span class="hljs-keyword">const</span> input = fs.<span class="hljs-title function_">createReadStream</span>(filename);
2103input.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> {
2104  <span class="hljs-comment">// Only one element is going to be produced by the</span>
2105  <span class="hljs-comment">// hash stream.</span>
2106  <span class="hljs-keyword">const</span> data = input.<span class="hljs-title function_">read</span>();
2107  <span class="hljs-keyword">if</span> (data)
2108    hmac.<span class="hljs-title function_">update</span>(data);
2109  <span class="hljs-keyword">else</span> {
2110    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${hmac.digest(<span class="hljs-string">'hex'</span>)}</span> <span class="hljs-subst">${filename}</span>`</span>);
2111  }
2112});</code></pre>
2113<h4><code>crypto.createPrivateKey(key)</code><span><a class="mark" href="#crypto_crypto_createprivatekey_key" id="crypto_crypto_createprivatekey_key">#</a></span></h4>
2114<div class="api_metadata">
2115<span>Added in: v11.6.0</span>
2116</div>
2117<ul>
2118<li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a>
2119<ul>
2120<li><code>key</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> The key material, either in PEM or DER format.</li>
2121<li><code>format</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Must be <code>'pem'</code> or <code>'der'</code>. <strong>Default:</strong> <code>'pem'</code>.</li>
2122<li><code>type</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Must be <code>'pkcs1'</code>, <code>'pkcs8'</code> or <code>'sec1'</code>. This option is
2123required only if the <code>format</code> is <code>'der'</code> and ignored if it is <code>'pem'</code>.</li>
2124<li><code>passphrase</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> The passphrase to use for decryption.</li>
2125</ul>
2126</li>
2127<li>Returns: <a href="crypto.html#crypto_class_keyobject" class="type">&#x3C;KeyObject></a></li>
2128</ul>
2129<p>Creates and returns a new key object containing a private key. If <code>key</code> is a
2130string or <code>Buffer</code>, <code>format</code> is assumed to be <code>'pem'</code>; otherwise, <code>key</code>
2131must be an object with the properties described above.</p>
2132<p>If the private key is encrypted, a <code>passphrase</code> must be specified. The length
2133of the passphrase is limited to 1024 bytes.</p>
2134<h4><code>crypto.createPublicKey(key)</code><span><a class="mark" href="#crypto_crypto_createpublickey_key" id="crypto_crypto_createpublickey_key">#</a></span></h4>
2135<div class="api_metadata">
2136<details class="changelog"><summary>History</summary>
2137<table>
2138<tbody><tr><th>Version</th><th>Changes</th></tr>
2139<tr><td>v11.13.0</td>
2140<td><p>The <code>key</code> argument can now be a <code>KeyObject</code> with type <code>private</code>.</p></td></tr>
2141<tr><td>v11.7.0</td>
2142<td><p>The <code>key</code> argument can now be a private key.</p></td></tr>
2143<tr><td>v11.6.0</td>
2144<td><p><span>Added in: v11.6.0</span></p></td></tr>
2145</tbody></table>
2146</details>
2147</div>
2148<ul>
2149<li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="crypto.html#crypto_class_keyobject" class="type">&#x3C;KeyObject></a>
2150<ul>
2151<li><code>key</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a></li>
2152<li><code>format</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Must be <code>'pem'</code> or <code>'der'</code>. <strong>Default:</strong> <code>'pem'</code>.</li>
2153<li><code>type</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Must be <code>'pkcs1'</code> or <code>'spki'</code>. This option is required
2154only if the <code>format</code> is <code>'der'</code>.</li>
2155</ul>
2156</li>
2157<li>Returns: <a href="crypto.html#crypto_class_keyobject" class="type">&#x3C;KeyObject></a></li>
2158</ul>
2159<p>Creates and returns a new key object containing a public key. If <code>key</code> is a
2160string or <code>Buffer</code>, <code>format</code> is assumed to be <code>'pem'</code>; if <code>key</code> is a <code>KeyObject</code>
2161with type <code>'private'</code>, the public key is derived from the given private key;
2162otherwise, <code>key</code> must be an object with the properties described above.</p>
2163<p>If the format is <code>'pem'</code>, the <code>'key'</code> may also be an X.509 certificate.</p>
2164<p>Because public keys can be derived from private keys, a private key may be
2165passed instead of a public key. In that case, this function behaves as if
2166<a href="#crypto_crypto_createprivatekey_key"><code>crypto.createPrivateKey()</code></a> had been called, except that the type of the
2167returned <code>KeyObject</code> will be <code>'public'</code> and that the private key cannot be
2168extracted from the returned <code>KeyObject</code>. Similarly, if a <code>KeyObject</code> with type
2169<code>'private'</code> is given, a new <code>KeyObject</code> with type <code>'public'</code> will be returned
2170and it will be impossible to extract the private key from the returned object.</p>
2171<h4><code>crypto.createSecretKey(key)</code><span><a class="mark" href="#crypto_crypto_createsecretkey_key" id="crypto_crypto_createsecretkey_key">#</a></span></h4>
2172<div class="api_metadata">
2173<span>Added in: v11.6.0</span>
2174</div>
2175<ul>
2176<li><code>key</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
2177<li>Returns: <a href="crypto.html#crypto_class_keyobject" class="type">&#x3C;KeyObject></a></li>
2178</ul>
2179<p>Creates and returns a new key object containing a secret key for symmetric
2180encryption or <code>Hmac</code>.</p>
2181<h4><code>crypto.createSign(algorithm[, options])</code><span><a class="mark" href="#crypto_crypto_createsign_algorithm_options" id="crypto_crypto_createsign_algorithm_options">#</a></span></h4>
2182<div class="api_metadata">
2183<span>Added in: v0.1.92</span>
2184</div>
2185<ul>
2186<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
2187<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> <a href="stream.html#stream_new_stream_writable_options"><code>stream.Writable</code> options</a></li>
2188<li>Returns: <a href="crypto.html#crypto_class_sign" class="type">&#x3C;Sign></a></li>
2189</ul>
2190<p>Creates and returns a <code>Sign</code> object that uses the given <code>algorithm</code>. Use
2191<a href="#crypto_crypto_gethashes"><code>crypto.getHashes()</code></a> to obtain the names of the available digest algorithms.
2192Optional <code>options</code> argument controls the <code>stream.Writable</code> behavior.</p>
2193<p>In some cases, a <code>Sign</code> instance can be created using the name of a signature
2194algorithm, such as <code>'RSA-SHA256'</code>, instead of a digest algorithm. This will use
2195the corresponding digest algorithm. This does not work for all signature
2196algorithms, such as <code>'ecdsa-with-SHA256'</code>, so it is best to always use digest
2197algorithm names.</p>
2198<h4><code>crypto.createVerify(algorithm[, options])</code><span><a class="mark" href="#crypto_crypto_createverify_algorithm_options" id="crypto_crypto_createverify_algorithm_options">#</a></span></h4>
2199<div class="api_metadata">
2200<span>Added in: v0.1.92</span>
2201</div>
2202<ul>
2203<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
2204<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> <a href="stream.html#stream_new_stream_writable_options"><code>stream.Writable</code> options</a></li>
2205<li>Returns: <a href="crypto.html#crypto_class_verify" class="type">&#x3C;Verify></a></li>
2206</ul>
2207<p>Creates and returns a <code>Verify</code> object that uses the given algorithm.
2208Use <a href="#crypto_crypto_gethashes"><code>crypto.getHashes()</code></a> to obtain an array of names of the available
2209signing algorithms. Optional <code>options</code> argument controls the
2210<code>stream.Writable</code> behavior.</p>
2211<p>In some cases, a <code>Verify</code> instance can be created using the name of a signature
2212algorithm, such as <code>'RSA-SHA256'</code>, instead of a digest algorithm. This will use
2213the corresponding digest algorithm. This does not work for all signature
2214algorithms, such as <code>'ecdsa-with-SHA256'</code>, so it is best to always use digest
2215algorithm names.</p>
2216<h4><code>crypto.diffieHellman(options)</code><span><a class="mark" href="#crypto_crypto_diffiehellman_options" id="crypto_crypto_diffiehellman_options">#</a></span></h4>
2217<div class="api_metadata">
2218<span>Added in: v13.9.0</span>
2219</div>
2220<ul>
2221<li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
2222<ul>
2223<li><code>privateKey</code>: <a href="crypto.html#crypto_class_keyobject" class="type">&#x3C;KeyObject></a></li>
2224<li><code>publicKey</code>: <a href="crypto.html#crypto_class_keyobject" class="type">&#x3C;KeyObject></a></li>
2225</ul>
2226</li>
2227<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a></li>
2228</ul>
2229<p>Computes the Diffie-Hellman secret based on a <code>privateKey</code> and a <code>publicKey</code>.
2230Both keys must have the same <code>asymmetricKeyType</code>, which must be one of <code>'dh'</code>
2231(for Diffie-Hellman), <code>'ec'</code> (for ECDH), <code>'x448'</code>, or <code>'x25519'</code> (for ECDH-ES).</p>
2232<h4><code>crypto.generateKeyPair(type, options, callback)</code><span><a class="mark" href="#crypto_crypto_generatekeypair_type_options_callback" id="crypto_crypto_generatekeypair_type_options_callback">#</a></span></h4>
2233<div class="api_metadata">
2234<details class="changelog"><summary>History</summary>
2235<table>
2236<tbody><tr><th>Version</th><th>Changes</th></tr>
2237<tr><td>v13.9.0</td>
2238<td><p>Add support for Diffie-Hellman.</p></td></tr>
2239<tr><td>v12.0.0</td>
2240<td><p>Add ability to generate X25519 and X448 key pairs.</p></td></tr>
2241<tr><td>v12.0.0</td>
2242<td><p>Add ability to generate Ed25519 and Ed448 key pairs.</p></td></tr>
2243<tr><td>v11.6.0</td>
2244<td><p>The <code>generateKeyPair</code> and <code>generateKeyPairSync</code> functions now produce key objects if no encoding was specified.</p></td></tr>
2245<tr><td>v10.12.0</td>
2246<td><p><span>Added in: v10.12.0</span></p></td></tr>
2247</tbody></table>
2248</details>
2249</div>
2250<ul>
2251<li><code>type</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Must be <code>'rsa'</code>, <code>'dsa'</code>, <code>'ec'</code>, <code>'ed25519'</code>, <code>'ed448'</code>,
2252<code>'x25519'</code>, <code>'x448'</code>, or <code>'dh'</code>.</li>
2253<li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
2254<ul>
2255<li><code>modulusLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Key size in bits (RSA, DSA).</li>
2256<li><code>publicExponent</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Public exponent (RSA). <strong>Default:</strong> <code>0x10001</code>.</li>
2257<li><code>divisorLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Size of <code>q</code> in bits (DSA).</li>
2258<li><code>namedCurve</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Name of the curve to use (EC).</li>
2259<li><code>prime</code>: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> The prime parameter (DH).</li>
2260<li><code>primeLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Prime length in bits (DH).</li>
2261<li><code>generator</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Custom generator (DH). <strong>Default:</strong> <code>2</code>.</li>
2262<li><code>groupName</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Diffie-Hellman group name (DH). See
2263<a href="#crypto_crypto_getdiffiehellman_groupname"><code>crypto.getDiffieHellman()</code></a>.</li>
2264<li><code>publicKeyEncoding</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> See <a href="#crypto_keyobject_export_options"><code>keyObject.export()</code></a>.</li>
2265<li><code>privateKeyEncoding</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> See <a href="#crypto_keyobject_export_options"><code>keyObject.export()</code></a>.</li>
2266</ul>
2267</li>
2268<li><code>callback</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
2269<ul>
2270<li><code>err</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
2271<li><code>publicKey</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="crypto.html#crypto_class_keyobject" class="type">&#x3C;KeyObject></a></li>
2272<li><code>privateKey</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="crypto.html#crypto_class_keyobject" class="type">&#x3C;KeyObject></a></li>
2273</ul>
2274</li>
2275</ul>
2276<p>Generates a new asymmetric key pair of the given <code>type</code>. RSA, DSA, EC, Ed25519,
2277Ed448, X25519, X448, and DH are currently supported.</p>
2278<p>If a <code>publicKeyEncoding</code> or <code>privateKeyEncoding</code> was specified, this function
2279behaves as if <a href="#crypto_keyobject_export_options"><code>keyObject.export()</code></a> had been called on its result. Otherwise,
2280the respective part of the key is returned as a <a href="#crypto_class_keyobject"><code>KeyObject</code></a>.</p>
2281<p>It is recommended to encode public keys as <code>'spki'</code> and private keys as
2282<code>'pkcs8'</code> with encryption for long-term storage:</p>
2283<pre><code class="language-js"><span class="hljs-keyword">const</span> { generateKeyPair } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
2284<span class="hljs-title function_">generateKeyPair</span>(<span class="hljs-string">'rsa'</span>, {
2285  <span class="hljs-attr">modulusLength</span>: <span class="hljs-number">4096</span>,
2286  <span class="hljs-attr">publicKeyEncoding</span>: {
2287    <span class="hljs-attr">type</span>: <span class="hljs-string">'spki'</span>,
2288    <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>
2289  },
2290  <span class="hljs-attr">privateKeyEncoding</span>: {
2291    <span class="hljs-attr">type</span>: <span class="hljs-string">'pkcs8'</span>,
2292    <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>,
2293    <span class="hljs-attr">cipher</span>: <span class="hljs-string">'aes-256-cbc'</span>,
2294    <span class="hljs-attr">passphrase</span>: <span class="hljs-string">'top secret'</span>
2295  }
2296}, <span class="hljs-function">(<span class="hljs-params">err, publicKey, privateKey</span>) =></span> {
2297  <span class="hljs-comment">// Handle errors and use the generated key pair.</span>
2298});</code></pre>
2299<p>On completion, <code>callback</code> will be called with <code>err</code> set to <code>undefined</code> and
2300<code>publicKey</code> / <code>privateKey</code> representing the generated key pair.</p>
2301<p>If this method is invoked as its <a href="util.html#util_util_promisify_original"><code>util.promisify()</code></a>ed version, it returns
2302a <code>Promise</code> for an <code>Object</code> with <code>publicKey</code> and <code>privateKey</code> properties.</p>
2303<h4><code>crypto.generateKeyPairSync(type, options)</code><span><a class="mark" href="#crypto_crypto_generatekeypairsync_type_options" id="crypto_crypto_generatekeypairsync_type_options">#</a></span></h4>
2304<div class="api_metadata">
2305<details class="changelog"><summary>History</summary>
2306<table>
2307<tbody><tr><th>Version</th><th>Changes</th></tr>
2308<tr><td>v13.9.0</td>
2309<td><p>Add support for Diffie-Hellman.</p></td></tr>
2310<tr><td>v12.0.0</td>
2311<td><p>Add ability to generate Ed25519 and Ed448 key pairs.</p></td></tr>
2312<tr><td>v11.6.0</td>
2313<td><p>The <code>generateKeyPair</code> and <code>generateKeyPairSync</code> functions now produce key objects if no encoding was specified.</p></td></tr>
2314<tr><td>v10.12.0</td>
2315<td><p><span>Added in: v10.12.0</span></p></td></tr>
2316</tbody></table>
2317</details>
2318</div>
2319<ul>
2320<li><code>type</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Must be <code>'rsa'</code>, <code>'dsa'</code>, <code>'ec'</code>, <code>'ed25519'</code>, <code>'ed448'</code>,
2321<code>'x25519'</code>, <code>'x448'</code>, or <code>'dh'</code>.</li>
2322<li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
2323<ul>
2324<li><code>modulusLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Key size in bits (RSA, DSA).</li>
2325<li><code>publicExponent</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Public exponent (RSA). <strong>Default:</strong> <code>0x10001</code>.</li>
2326<li><code>divisorLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Size of <code>q</code> in bits (DSA).</li>
2327<li><code>namedCurve</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Name of the curve to use (EC).</li>
2328<li><code>prime</code>: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> The prime parameter (DH).</li>
2329<li><code>primeLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Prime length in bits (DH).</li>
2330<li><code>generator</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Custom generator (DH). <strong>Default:</strong> <code>2</code>.</li>
2331<li><code>groupName</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Diffie-Hellman group name (DH). See
2332<a href="#crypto_crypto_getdiffiehellman_groupname"><code>crypto.getDiffieHellman()</code></a>.</li>
2333<li><code>publicKeyEncoding</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> See <a href="#crypto_keyobject_export_options"><code>keyObject.export()</code></a>.</li>
2334<li><code>privateKeyEncoding</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> See <a href="#crypto_keyobject_export_options"><code>keyObject.export()</code></a>.</li>
2335</ul>
2336</li>
2337<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
2338<ul>
2339<li><code>publicKey</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="crypto.html#crypto_class_keyobject" class="type">&#x3C;KeyObject></a></li>
2340<li><code>privateKey</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="crypto.html#crypto_class_keyobject" class="type">&#x3C;KeyObject></a></li>
2341</ul>
2342</li>
2343</ul>
2344<p>Generates a new asymmetric key pair of the given <code>type</code>. RSA, DSA, EC, Ed25519,
2345Ed448, X25519, X448, and DH are currently supported.</p>
2346<p>If a <code>publicKeyEncoding</code> or <code>privateKeyEncoding</code> was specified, this function
2347behaves as if <a href="#crypto_keyobject_export_options"><code>keyObject.export()</code></a> had been called on its result. Otherwise,
2348the respective part of the key is returned as a <a href="#crypto_class_keyobject"><code>KeyObject</code></a>.</p>
2349<p>When encoding public keys, it is recommended to use <code>'spki'</code>. When encoding
2350private keys, it is recommended to use <code>'pkcs8'</code> with a strong passphrase,
2351and to keep the passphrase confidential.</p>
2352<pre><code class="language-js"><span class="hljs-keyword">const</span> { generateKeyPairSync } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
2353<span class="hljs-keyword">const</span> { publicKey, privateKey } = <span class="hljs-title function_">generateKeyPairSync</span>(<span class="hljs-string">'rsa'</span>, {
2354  <span class="hljs-attr">modulusLength</span>: <span class="hljs-number">4096</span>,
2355  <span class="hljs-attr">publicKeyEncoding</span>: {
2356    <span class="hljs-attr">type</span>: <span class="hljs-string">'spki'</span>,
2357    <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>
2358  },
2359  <span class="hljs-attr">privateKeyEncoding</span>: {
2360    <span class="hljs-attr">type</span>: <span class="hljs-string">'pkcs8'</span>,
2361    <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>,
2362    <span class="hljs-attr">cipher</span>: <span class="hljs-string">'aes-256-cbc'</span>,
2363    <span class="hljs-attr">passphrase</span>: <span class="hljs-string">'top secret'</span>
2364  }
2365});</code></pre>
2366<p>The return value <code>{ publicKey, privateKey }</code> represents the generated key pair.
2367When PEM encoding was selected, the respective key will be a string, otherwise
2368it will be a buffer containing the data encoded as DER.</p>
2369<h4><code>crypto.getCiphers()</code><span><a class="mark" href="#crypto_crypto_getciphers" id="crypto_crypto_getciphers">#</a></span></h4>
2370<div class="api_metadata">
2371<span>Added in: v0.9.3</span>
2372</div>
2373<ul>
2374<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> An array with the names of the supported cipher
2375algorithms.</li>
2376</ul>
2377<pre><code class="language-js"><span class="hljs-keyword">const</span> ciphers = crypto.<span class="hljs-title function_">getCiphers</span>();
2378<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(ciphers); <span class="hljs-comment">// ['aes-128-cbc', 'aes-128-ccm', ...]</span></code></pre>
2379<h4><code>crypto.getCurves()</code><span><a class="mark" href="#crypto_crypto_getcurves" id="crypto_crypto_getcurves">#</a></span></h4>
2380<div class="api_metadata">
2381<span>Added in: v2.3.0</span>
2382</div>
2383<ul>
2384<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> An array with the names of the supported elliptic curves.</li>
2385</ul>
2386<pre><code class="language-js"><span class="hljs-keyword">const</span> curves = crypto.<span class="hljs-title function_">getCurves</span>();
2387<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(curves); <span class="hljs-comment">// ['Oakley-EC2N-3', 'Oakley-EC2N-4', ...]</span></code></pre>
2388<h4><code>crypto.getDiffieHellman(groupName)</code><span><a class="mark" href="#crypto_crypto_getdiffiehellman_groupname" id="crypto_crypto_getdiffiehellman_groupname">#</a></span></h4>
2389<div class="api_metadata">
2390<span>Added in: v0.7.5</span>
2391</div>
2392<ul>
2393<li><code>groupName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
2394<li>Returns: <a href="crypto.html#crypto_class_diffiehellmangroup" class="type">&#x3C;DiffieHellmanGroup></a></li>
2395</ul>
2396<p>Creates a predefined <code>DiffieHellmanGroup</code> key exchange object. The
2397supported groups are: <code>'modp1'</code>, <code>'modp2'</code>, <code>'modp5'</code> (defined in
2398<a href="https://www.rfc-editor.org/rfc/rfc2412.txt">RFC 2412</a>, but see <a href="#crypto_support_for_weak_or_compromised_algorithms">Caveats</a>) and <code>'modp14'</code>, <code>'modp15'</code>,
2399<code>'modp16'</code>, <code>'modp17'</code>, <code>'modp18'</code> (defined in <a href="https://www.rfc-editor.org/rfc/rfc3526.txt">RFC 3526</a>). The
2400returned object mimics the interface of objects created by
2401<a href="#crypto_crypto_creatediffiehellman_prime_primeencoding_generator_generatorencoding"><code>crypto.createDiffieHellman()</code></a>, but will not allow changing
2402the keys (with <a href="#crypto_diffiehellman_setpublickey_publickey_encoding"><code>diffieHellman.setPublicKey()</code></a>, for example). The
2403advantage of using this method is that the parties do not have to
2404generate nor exchange a group modulus beforehand, saving both processor
2405and communication time.</p>
2406<p>Example (obtaining a shared secret):</p>
2407<pre><code class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
2408<span class="hljs-keyword">const</span> alice = crypto.<span class="hljs-title function_">getDiffieHellman</span>(<span class="hljs-string">'modp14'</span>);
2409<span class="hljs-keyword">const</span> bob = crypto.<span class="hljs-title function_">getDiffieHellman</span>(<span class="hljs-string">'modp14'</span>);
2410
2411alice.<span class="hljs-title function_">generateKeys</span>();
2412bob.<span class="hljs-title function_">generateKeys</span>();
2413
2414<span class="hljs-keyword">const</span> aliceSecret = alice.<span class="hljs-title function_">computeSecret</span>(bob.<span class="hljs-title function_">getPublicKey</span>(), <span class="hljs-literal">null</span>, <span class="hljs-string">'hex'</span>);
2415<span class="hljs-keyword">const</span> bobSecret = bob.<span class="hljs-title function_">computeSecret</span>(alice.<span class="hljs-title function_">getPublicKey</span>(), <span class="hljs-literal">null</span>, <span class="hljs-string">'hex'</span>);
2416
2417<span class="hljs-comment">/* aliceSecret and bobSecret should be the same */</span>
2418<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(aliceSecret === bobSecret);</code></pre>
2419<h4><code>crypto.getFips()</code><span><a class="mark" href="#crypto_crypto_getfips" id="crypto_crypto_getfips">#</a></span></h4>
2420<div class="api_metadata">
2421<span>Added in: v10.0.0</span>
2422</div>
2423<ul>
2424<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> <code>1</code> if and only if a FIPS compliant crypto provider is
2425currently in use, <code>0</code> otherwise. A future semver-major release may change
2426the return type of this API to a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a>.</li>
2427</ul>
2428<h4><code>crypto.getHashes()</code><span><a class="mark" href="#crypto_crypto_gethashes" id="crypto_crypto_gethashes">#</a></span></h4>
2429<div class="api_metadata">
2430<span>Added in: v0.9.3</span>
2431</div>
2432<ul>
2433<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> An array of the names of the supported hash algorithms,
2434such as <code>'RSA-SHA256'</code>. Hash algorithms are also called "digest" algorithms.</li>
2435</ul>
2436<pre><code class="language-js"><span class="hljs-keyword">const</span> hashes = crypto.<span class="hljs-title function_">getHashes</span>();
2437<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(hashes); <span class="hljs-comment">// ['DSA', 'DSA-SHA', 'DSA-SHA1', ...]</span></code></pre>
2438<h4><code>crypto.pbkdf2(password, salt, iterations, keylen, digest, callback)</code><span><a class="mark" href="#crypto_crypto_pbkdf2_password_salt_iterations_keylen_digest_callback" id="crypto_crypto_pbkdf2_password_salt_iterations_keylen_digest_callback">#</a></span></h4>
2439<div class="api_metadata">
2440<details class="changelog"><summary>History</summary>
2441<table>
2442<tbody><tr><th>Version</th><th>Changes</th></tr>
2443<tr><td>v14.0.0</td>
2444<td><p>The <code>iterations</code> parameter is now restricted to positive values. Earlier releases treated other values as one.</p></td></tr>
2445<tr><td>v8.0.0</td>
2446<td><p>The <code>digest</code> parameter is always required now.</p></td></tr>
2447<tr><td>v6.0.0</td>
2448<td><p>Calling this function without passing the <code>digest</code> parameter is deprecated now and will emit a warning.</p></td></tr>
2449<tr><td>v6.0.0</td>
2450<td><p>The default encoding for <code>password</code> if it is a string changed from <code>binary</code> to <code>utf8</code>.</p></td></tr>
2451<tr><td>v0.5.5</td>
2452<td><p><span>Added in: v0.5.5</span></p></td></tr>
2453</tbody></table>
2454</details>
2455</div>
2456<ul>
2457<li><code>password</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
2458<li><code>salt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
2459<li><code>iterations</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
2460<li><code>keylen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
2461<li><code>digest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
2462<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
2463<ul>
2464<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
2465<li><code>derivedKey</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a></li>
2466</ul>
2467</li>
2468</ul>
2469<p>Provides an asynchronous Password-Based Key Derivation Function 2 (PBKDF2)
2470implementation. A selected HMAC digest algorithm specified by <code>digest</code> is
2471applied to derive a key of the requested byte length (<code>keylen</code>) from the
2472<code>password</code>, <code>salt</code> and <code>iterations</code>.</p>
2473<p>The supplied <code>callback</code> function is called with two arguments: <code>err</code> and
2474<code>derivedKey</code>. If an error occurs while deriving the key, <code>err</code> will be set;
2475otherwise <code>err</code> will be <code>null</code>. By default, the successfully generated
2476<code>derivedKey</code> will be passed to the callback as a <a href="buffer.html"><code>Buffer</code></a>. An error will be
2477thrown if any of the input arguments specify invalid values or types.</p>
2478<p>If <code>digest</code> is <code>null</code>, <code>'sha1'</code> will be used. This behavior is deprecated,
2479please specify a <code>digest</code> explicitly.</p>
2480<p>The <code>iterations</code> argument must be a number set as high as possible. The
2481higher the number of iterations, the more secure the derived key will be,
2482but will take a longer amount of time to complete.</p>
2483<p>The <code>salt</code> should be as unique as possible. It is recommended that a salt is
2484random and at least 16 bytes long. See <a href="https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf">NIST SP 800-132</a> for details.</p>
2485<pre><code class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
2486crypto.<span class="hljs-title function_">pbkdf2</span>(<span class="hljs-string">'secret'</span>, <span class="hljs-string">'salt'</span>, <span class="hljs-number">100000</span>, <span class="hljs-number">64</span>, <span class="hljs-string">'sha512'</span>, <span class="hljs-function">(<span class="hljs-params">err, derivedKey</span>) =></span> {
2487  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
2488  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(derivedKey.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));  <span class="hljs-comment">// '3745e48...08d59ae'</span>
2489});</code></pre>
2490<p>The <code>crypto.DEFAULT_ENCODING</code> property can be used to change the way the
2491<code>derivedKey</code> is passed to the callback. This property, however, has been
2492deprecated and use should be avoided.</p>
2493<pre><code class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
2494crypto.<span class="hljs-property">DEFAULT_ENCODING</span> = <span class="hljs-string">'hex'</span>;
2495crypto.<span class="hljs-title function_">pbkdf2</span>(<span class="hljs-string">'secret'</span>, <span class="hljs-string">'salt'</span>, <span class="hljs-number">100000</span>, <span class="hljs-number">512</span>, <span class="hljs-string">'sha512'</span>, <span class="hljs-function">(<span class="hljs-params">err, derivedKey</span>) =></span> {
2496  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
2497  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(derivedKey);  <span class="hljs-comment">// '3745e48...aa39b34'</span>
2498});</code></pre>
2499<p>An array of supported digest functions can be retrieved using
2500<a href="#crypto_crypto_gethashes"><code>crypto.getHashes()</code></a>.</p>
2501<p>This API uses libuv's threadpool, which can have surprising and
2502negative performance implications for some applications; see the
2503<a href="cli.html#cli_uv_threadpool_size_size"><code>UV_THREADPOOL_SIZE</code></a> documentation for more information.</p>
2504<h4><code>crypto.pbkdf2Sync(password, salt, iterations, keylen, digest)</code><span><a class="mark" href="#crypto_crypto_pbkdf2sync_password_salt_iterations_keylen_digest" id="crypto_crypto_pbkdf2sync_password_salt_iterations_keylen_digest">#</a></span></h4>
2505<div class="api_metadata">
2506<details class="changelog"><summary>History</summary>
2507<table>
2508<tbody><tr><th>Version</th><th>Changes</th></tr>
2509<tr><td>v14.0.0</td>
2510<td><p>The <code>iterations</code> parameter is now restricted to positive values. Earlier releases treated other values as one.</p></td></tr>
2511<tr><td>v6.0.0</td>
2512<td><p>Calling this function without passing the <code>digest</code> parameter is deprecated now and will emit a warning.</p></td></tr>
2513<tr><td>v6.0.0</td>
2514<td><p>The default encoding for <code>password</code> if it is a string changed from <code>binary</code> to <code>utf8</code>.</p></td></tr>
2515<tr><td>v0.9.3</td>
2516<td><p><span>Added in: v0.9.3</span></p></td></tr>
2517</tbody></table>
2518</details>
2519</div>
2520<ul>
2521<li><code>password</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
2522<li><code>salt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
2523<li><code>iterations</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
2524<li><code>keylen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
2525<li><code>digest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
2526<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a></li>
2527</ul>
2528<p>Provides a synchronous Password-Based Key Derivation Function 2 (PBKDF2)
2529implementation. A selected HMAC digest algorithm specified by <code>digest</code> is
2530applied to derive a key of the requested byte length (<code>keylen</code>) from the
2531<code>password</code>, <code>salt</code> and <code>iterations</code>.</p>
2532<p>If an error occurs an <code>Error</code> will be thrown, otherwise the derived key will be
2533returned as a <a href="buffer.html"><code>Buffer</code></a>.</p>
2534<p>If <code>digest</code> is <code>null</code>, <code>'sha1'</code> will be used. This behavior is deprecated,
2535please specify a <code>digest</code> explicitly.</p>
2536<p>The <code>iterations</code> argument must be a number set as high as possible. The
2537higher the number of iterations, the more secure the derived key will be,
2538but will take a longer amount of time to complete.</p>
2539<p>The <code>salt</code> should be as unique as possible. It is recommended that a salt is
2540random and at least 16 bytes long. See <a href="https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf">NIST SP 800-132</a> for details.</p>
2541<pre><code class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
2542<span class="hljs-keyword">const</span> key = crypto.<span class="hljs-title function_">pbkdf2Sync</span>(<span class="hljs-string">'secret'</span>, <span class="hljs-string">'salt'</span>, <span class="hljs-number">100000</span>, <span class="hljs-number">64</span>, <span class="hljs-string">'sha512'</span>);
2543<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(key.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));  <span class="hljs-comment">// '3745e48...08d59ae'</span></code></pre>
2544<p>The <code>crypto.DEFAULT_ENCODING</code> property may be used to change the way the
2545<code>derivedKey</code> is returned. This property, however, is deprecated and use
2546should be avoided.</p>
2547<pre><code class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
2548crypto.<span class="hljs-property">DEFAULT_ENCODING</span> = <span class="hljs-string">'hex'</span>;
2549<span class="hljs-keyword">const</span> key = crypto.<span class="hljs-title function_">pbkdf2Sync</span>(<span class="hljs-string">'secret'</span>, <span class="hljs-string">'salt'</span>, <span class="hljs-number">100000</span>, <span class="hljs-number">512</span>, <span class="hljs-string">'sha512'</span>);
2550<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(key);  <span class="hljs-comment">// '3745e48...aa39b34'</span></code></pre>
2551<p>An array of supported digest functions can be retrieved using
2552<a href="#crypto_crypto_gethashes"><code>crypto.getHashes()</code></a>.</p>
2553<h4><code>crypto.privateDecrypt(privateKey, buffer)</code><span><a class="mark" href="#crypto_crypto_privatedecrypt_privatekey_buffer" id="crypto_crypto_privatedecrypt_privatekey_buffer">#</a></span></h4>
2554<div class="api_metadata">
2555<details class="changelog"><summary>History</summary>
2556<table>
2557<tbody><tr><th>Version</th><th>Changes</th></tr>
2558<tr><td>v12.11.0</td>
2559<td><p>The <code>oaepLabel</code> option was added.</p></td></tr>
2560<tr><td>v12.9.0</td>
2561<td><p>The <code>oaepHash</code> option was added.</p></td></tr>
2562<tr><td>v11.6.0</td>
2563<td><p>This function now supports key objects.</p></td></tr>
2564<tr><td>v0.11.14</td>
2565<td><p><span>Added in: v0.11.14</span></p></td></tr>
2566</tbody></table>
2567</details>
2568</div>
2569<ul>
2570<li><code>privateKey</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="crypto.html#crypto_class_keyobject" class="type">&#x3C;KeyObject></a>
2571<ul>
2572<li><code>oaepHash</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The hash function to use for OAEP padding and MGF1.
2573<strong>Default:</strong> <code>'sha1'</code></li>
2574<li><code>oaepLabel</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> The label to use for OAEP
2575padding. If not specified, no label is used.</li>
2576<li><code>padding</code> <a href="crypto.html#crypto_crypto_constants_1" class="type">&#x3C;crypto.constants></a> An optional padding value defined in
2577<code>crypto.constants</code>, which may be: <code>crypto.constants.RSA_NO_PADDING</code>,
2578<code>crypto.constants.RSA_PKCS1_PADDING</code>, or
2579<code>crypto.constants.RSA_PKCS1_OAEP_PADDING</code>.</li>
2580</ul>
2581</li>
2582<li><code>buffer</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
2583<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> A new <code>Buffer</code> with the decrypted content.</li>
2584</ul>
2585<p>Decrypts <code>buffer</code> with <code>privateKey</code>. <code>buffer</code> was previously encrypted using
2586the corresponding public key, for example using <a href="#crypto_crypto_publicencrypt_key_buffer"><code>crypto.publicEncrypt()</code></a>.</p>
2587<p>If <code>privateKey</code> is not a <a href="#crypto_class_keyobject"><code>KeyObject</code></a>, this function behaves as if
2588<code>privateKey</code> had been passed to <a href="#crypto_crypto_createprivatekey_key"><code>crypto.createPrivateKey()</code></a>. If it is an
2589object, the <code>padding</code> property can be passed. Otherwise, this function uses
2590<code>RSA_PKCS1_OAEP_PADDING</code>.</p>
2591<h4><code>crypto.privateEncrypt(privateKey, buffer)</code><span><a class="mark" href="#crypto_crypto_privateencrypt_privatekey_buffer" id="crypto_crypto_privateencrypt_privatekey_buffer">#</a></span></h4>
2592<div class="api_metadata">
2593<details class="changelog"><summary>History</summary>
2594<table>
2595<tbody><tr><th>Version</th><th>Changes</th></tr>
2596<tr><td>v11.6.0</td>
2597<td><p>This function now supports key objects.</p></td></tr>
2598<tr><td>v1.1.0</td>
2599<td><p><span>Added in: v1.1.0</span></p></td></tr>
2600</tbody></table>
2601</details>
2602</div>
2603<ul>
2604<li><code>privateKey</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="crypto.html#crypto_class_keyobject" class="type">&#x3C;KeyObject></a>
2605<ul>
2606<li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="crypto.html#crypto_class_keyobject" class="type">&#x3C;KeyObject></a> A PEM encoded private key.</li>
2607<li><code>passphrase</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> An optional passphrase for the private key.</li>
2608<li><code>padding</code> <a href="crypto.html#crypto_crypto_constants_1" class="type">&#x3C;crypto.constants></a> An optional padding value defined in
2609<code>crypto.constants</code>, which may be: <code>crypto.constants.RSA_NO_PADDING</code> or
2610<code>crypto.constants.RSA_PKCS1_PADDING</code>.</li>
2611</ul>
2612</li>
2613<li><code>buffer</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
2614<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> A new <code>Buffer</code> with the encrypted content.</li>
2615</ul>
2616<p>Encrypts <code>buffer</code> with <code>privateKey</code>. The returned data can be decrypted using
2617the corresponding public key, for example using <a href="#crypto_crypto_publicdecrypt_key_buffer"><code>crypto.publicDecrypt()</code></a>.</p>
2618<p>If <code>privateKey</code> is not a <a href="#crypto_class_keyobject"><code>KeyObject</code></a>, this function behaves as if
2619<code>privateKey</code> had been passed to <a href="#crypto_crypto_createprivatekey_key"><code>crypto.createPrivateKey()</code></a>. If it is an
2620object, the <code>padding</code> property can be passed. Otherwise, this function uses
2621<code>RSA_PKCS1_PADDING</code>.</p>
2622<h4><code>crypto.publicDecrypt(key, buffer)</code><span><a class="mark" href="#crypto_crypto_publicdecrypt_key_buffer" id="crypto_crypto_publicdecrypt_key_buffer">#</a></span></h4>
2623<div class="api_metadata">
2624<details class="changelog"><summary>History</summary>
2625<table>
2626<tbody><tr><th>Version</th><th>Changes</th></tr>
2627<tr><td>v11.6.0</td>
2628<td><p>This function now supports key objects.</p></td></tr>
2629<tr><td>v1.1.0</td>
2630<td><p><span>Added in: v1.1.0</span></p></td></tr>
2631</tbody></table>
2632</details>
2633</div>
2634<ul>
2635<li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="crypto.html#crypto_class_keyobject" class="type">&#x3C;KeyObject></a>
2636<ul>
2637<li><code>passphrase</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> An optional passphrase for the private key.</li>
2638<li><code>padding</code> <a href="crypto.html#crypto_crypto_constants_1" class="type">&#x3C;crypto.constants></a> An optional padding value defined in
2639<code>crypto.constants</code>, which may be: <code>crypto.constants.RSA_NO_PADDING</code> or
2640<code>crypto.constants.RSA_PKCS1_PADDING</code>.</li>
2641</ul>
2642</li>
2643<li><code>buffer</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
2644<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> A new <code>Buffer</code> with the decrypted content.</li>
2645</ul>
2646<p>Decrypts <code>buffer</code> with <code>key</code>.<code>buffer</code> was previously encrypted using
2647the corresponding private key, for example using <a href="#crypto_crypto_privateencrypt_privatekey_buffer"><code>crypto.privateEncrypt()</code></a>.</p>
2648<p>If <code>key</code> is not a <a href="#crypto_class_keyobject"><code>KeyObject</code></a>, this function behaves as if
2649<code>key</code> had been passed to <a href="#crypto_crypto_createpublickey_key"><code>crypto.createPublicKey()</code></a>. If it is an
2650object, the <code>padding</code> property can be passed. Otherwise, this function uses
2651<code>RSA_PKCS1_PADDING</code>.</p>
2652<p>Because RSA public keys can be derived from private keys, a private key may
2653be passed instead of a public key.</p>
2654<h4><code>crypto.publicEncrypt(key, buffer)</code><span><a class="mark" href="#crypto_crypto_publicencrypt_key_buffer" id="crypto_crypto_publicencrypt_key_buffer">#</a></span></h4>
2655<div class="api_metadata">
2656<details class="changelog"><summary>History</summary>
2657<table>
2658<tbody><tr><th>Version</th><th>Changes</th></tr>
2659<tr><td>v12.11.0</td>
2660<td><p>The <code>oaepLabel</code> option was added.</p></td></tr>
2661<tr><td>v12.9.0</td>
2662<td><p>The <code>oaepHash</code> option was added.</p></td></tr>
2663<tr><td>v11.6.0</td>
2664<td><p>This function now supports key objects.</p></td></tr>
2665<tr><td>v0.11.14</td>
2666<td><p><span>Added in: v0.11.14</span></p></td></tr>
2667</tbody></table>
2668</details>
2669</div>
2670<ul>
2671<li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="crypto.html#crypto_class_keyobject" class="type">&#x3C;KeyObject></a>
2672<ul>
2673<li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="crypto.html#crypto_class_keyobject" class="type">&#x3C;KeyObject></a> A PEM encoded public or private key.</li>
2674<li><code>oaepHash</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The hash function to use for OAEP padding and MGF1.
2675<strong>Default:</strong> <code>'sha1'</code></li>
2676<li><code>oaepLabel</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> The label to use for OAEP
2677padding. If not specified, no label is used.</li>
2678<li><code>passphrase</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> An optional passphrase for the private key.</li>
2679<li><code>padding</code> <a href="crypto.html#crypto_crypto_constants_1" class="type">&#x3C;crypto.constants></a> An optional padding value defined in
2680<code>crypto.constants</code>, which may be: <code>crypto.constants.RSA_NO_PADDING</code>,
2681<code>crypto.constants.RSA_PKCS1_PADDING</code>, or
2682<code>crypto.constants.RSA_PKCS1_OAEP_PADDING</code>.</li>
2683</ul>
2684</li>
2685<li><code>buffer</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
2686<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> A new <code>Buffer</code> with the encrypted content.</li>
2687</ul>
2688<p>Encrypts the content of <code>buffer</code> with <code>key</code> and returns a new
2689<a href="buffer.html"><code>Buffer</code></a> with encrypted content. The returned data can be decrypted using
2690the corresponding private key, for example using <a href="#crypto_crypto_privatedecrypt_privatekey_buffer"><code>crypto.privateDecrypt()</code></a>.</p>
2691<p>If <code>key</code> is not a <a href="#crypto_class_keyobject"><code>KeyObject</code></a>, this function behaves as if
2692<code>key</code> had been passed to <a href="#crypto_crypto_createpublickey_key"><code>crypto.createPublicKey()</code></a>. If it is an
2693object, the <code>padding</code> property can be passed. Otherwise, this function uses
2694<code>RSA_PKCS1_OAEP_PADDING</code>.</p>
2695<p>Because RSA public keys can be derived from private keys, a private key may
2696be passed instead of a public key.</p>
2697<h4><code>crypto.randomBytes(size[, callback])</code><span><a class="mark" href="#crypto_crypto_randombytes_size_callback" id="crypto_crypto_randombytes_size_callback">#</a></span></h4>
2698<div class="api_metadata">
2699<details class="changelog"><summary>History</summary>
2700<table>
2701<tbody><tr><th>Version</th><th>Changes</th></tr>
2702<tr><td>v9.0.0</td>
2703<td><p>Passing <code>null</code> as the <code>callback</code> argument now throws <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
2704<tr><td>v0.5.8</td>
2705<td><p><span>Added in: v0.5.8</span></p></td></tr>
2706</tbody></table>
2707</details>
2708</div>
2709<ul>
2710<li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
2711<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
2712<ul>
2713<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
2714<li><code>buf</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a></li>
2715</ul>
2716</li>
2717<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> if the <code>callback</code> function is not provided.</li>
2718</ul>
2719<p>Generates cryptographically strong pseudo-random data. The <code>size</code> argument
2720is a number indicating the number of bytes to generate.</p>
2721<p>If a <code>callback</code> function is provided, the bytes are generated asynchronously
2722and the <code>callback</code> function is invoked with two arguments: <code>err</code> and <code>buf</code>.
2723If an error occurs, <code>err</code> will be an <code>Error</code> object; otherwise it is <code>null</code>. The
2724<code>buf</code> argument is a <a href="buffer.html"><code>Buffer</code></a> containing the generated bytes.</p>
2725<pre><code class="language-js"><span class="hljs-comment">// Asynchronous</span>
2726<span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
2727crypto.<span class="hljs-title function_">randomBytes</span>(<span class="hljs-number">256</span>, <span class="hljs-function">(<span class="hljs-params">err, buf</span>) =></span> {
2728  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
2729  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${buf.length}</span> bytes of random data: <span class="hljs-subst">${buf.toString(<span class="hljs-string">'hex'</span>)}</span>`</span>);
2730});</code></pre>
2731<p>If the <code>callback</code> function is not provided, the random bytes are generated
2732synchronously and returned as a <a href="buffer.html"><code>Buffer</code></a>. An error will be thrown if
2733there is a problem generating the bytes.</p>
2734<pre><code class="language-js"><span class="hljs-comment">// Synchronous</span>
2735<span class="hljs-keyword">const</span> buf = crypto.<span class="hljs-title function_">randomBytes</span>(<span class="hljs-number">256</span>);
2736<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(
2737  <span class="hljs-string">`<span class="hljs-subst">${buf.length}</span> bytes of random data: <span class="hljs-subst">${buf.toString(<span class="hljs-string">'hex'</span>)}</span>`</span>);</code></pre>
2738<p>The <code>crypto.randomBytes()</code> method will not complete until there is
2739sufficient entropy available.
2740This should normally never take longer than a few milliseconds. The only time
2741when generating the random bytes may conceivably block for a longer period of
2742time is right after boot, when the whole system is still low on entropy.</p>
2743<p>This API uses libuv's threadpool, which can have surprising and
2744negative performance implications for some applications; see the
2745<a href="cli.html#cli_uv_threadpool_size_size"><code>UV_THREADPOOL_SIZE</code></a> documentation for more information.</p>
2746<p>The asynchronous version of <code>crypto.randomBytes()</code> is carried out in a single
2747threadpool request. To minimize threadpool task length variation, partition
2748large <code>randomBytes</code> requests when doing so as part of fulfilling a client
2749request.</p>
2750<h4><code>crypto.randomFillSync(buffer[, offset][, size])</code><span><a class="mark" href="#crypto_crypto_randomfillsync_buffer_offset_size" id="crypto_crypto_randomfillsync_buffer_offset_size">#</a></span></h4>
2751<div class="api_metadata">
2752<details class="changelog"><summary>History</summary>
2753<table>
2754<tbody><tr><th>Version</th><th>Changes</th></tr>
2755<tr><td>v9.0.0</td>
2756<td><p>The <code>buffer</code> argument may be any <code>TypedArray</code> or <code>DataView</code>.</p></td></tr>
2757<tr><td>v7.10.0, v6.13.0</td>
2758<td><p><span>Added in: v7.10.0, v6.13.0</span></p></td></tr>
2759</tbody></table>
2760</details>
2761</div>
2762<ul>
2763<li><code>buffer</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> Must be supplied.</li>
2764<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> <strong>Default:</strong> <code>0</code></li>
2765<li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> <strong>Default:</strong> <code>buffer.length - offset</code></li>
2766<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> The object passed as <code>buffer</code> argument.</li>
2767</ul>
2768<p>Synchronous version of <a href="#crypto_crypto_randomfill_buffer_offset_size_callback"><code>crypto.randomFill()</code></a>.</p>
2769<pre><code class="language-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">10</span>);
2770<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(crypto.<span class="hljs-title function_">randomFillSync</span>(buf).<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));
2771
2772crypto.<span class="hljs-title function_">randomFillSync</span>(buf, <span class="hljs-number">5</span>);
2773<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));
2774
2775<span class="hljs-comment">// The above is equivalent to the following:</span>
2776crypto.<span class="hljs-title function_">randomFillSync</span>(buf, <span class="hljs-number">5</span>, <span class="hljs-number">5</span>);
2777<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));</code></pre>
2778<p>Any <code>TypedArray</code> or <code>DataView</code> instance may be passed as <code>buffer</code>.</p>
2779<pre><code class="language-js"><span class="hljs-keyword">const</span> a = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint</span>32<span class="hljs-built_in">Array</span>(<span class="hljs-number">10</span>);
2780<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(crypto.<span class="hljs-title function_">randomFillSync</span>(a).<span class="hljs-property">buffer</span>,
2781                        a.<span class="hljs-property">byteOffset</span>, a.<span class="hljs-property">byteLength</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));
2782
2783<span class="hljs-keyword">const</span> b = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Float</span>64<span class="hljs-built_in">Array</span>(<span class="hljs-number">10</span>);
2784<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(crypto.<span class="hljs-title function_">randomFillSync</span>(b).<span class="hljs-property">buffer</span>,
2785                        b.<span class="hljs-property">byteOffset</span>, b.<span class="hljs-property">byteLength</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));
2786
2787<span class="hljs-keyword">const</span> c = <span class="hljs-keyword">new</span> <span class="hljs-title class_">DataView</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayBuffer</span>(<span class="hljs-number">10</span>));
2788<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(crypto.<span class="hljs-title function_">randomFillSync</span>(c).<span class="hljs-property">buffer</span>,
2789                        c.<span class="hljs-property">byteOffset</span>, c.<span class="hljs-property">byteLength</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));</code></pre>
2790<h4><code>crypto.randomFill(buffer[, offset][, size], callback)</code><span><a class="mark" href="#crypto_crypto_randomfill_buffer_offset_size_callback" id="crypto_crypto_randomfill_buffer_offset_size_callback">#</a></span></h4>
2791<div class="api_metadata">
2792<details class="changelog"><summary>History</summary>
2793<table>
2794<tbody><tr><th>Version</th><th>Changes</th></tr>
2795<tr><td>v9.0.0</td>
2796<td><p>The <code>buffer</code> argument may be any <code>TypedArray</code> or <code>DataView</code>.</p></td></tr>
2797<tr><td>v7.10.0, v6.13.0</td>
2798<td><p><span>Added in: v7.10.0, v6.13.0</span></p></td></tr>
2799</tbody></table>
2800</details>
2801</div>
2802<ul>
2803<li><code>buffer</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> Must be supplied.</li>
2804<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> <strong>Default:</strong> <code>0</code></li>
2805<li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> <strong>Default:</strong> <code>buffer.length - offset</code></li>
2806<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> <code>function(err, buf) {}</code>.</li>
2807</ul>
2808<p>This function is similar to <a href="#crypto_crypto_randombytes_size_callback"><code>crypto.randomBytes()</code></a> but requires the first
2809argument to be a <a href="buffer.html"><code>Buffer</code></a> that will be filled. It also
2810requires that a callback is passed in.</p>
2811<p>If the <code>callback</code> function is not provided, an error will be thrown.</p>
2812<pre><code class="language-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">10</span>);
2813crypto.<span class="hljs-title function_">randomFill</span>(buf, <span class="hljs-function">(<span class="hljs-params">err, buf</span>) =></span> {
2814  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
2815  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));
2816});
2817
2818crypto.<span class="hljs-title function_">randomFill</span>(buf, <span class="hljs-number">5</span>, <span class="hljs-function">(<span class="hljs-params">err, buf</span>) =></span> {
2819  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
2820  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));
2821});
2822
2823<span class="hljs-comment">// The above is equivalent to the following:</span>
2824crypto.<span class="hljs-title function_">randomFill</span>(buf, <span class="hljs-number">5</span>, <span class="hljs-number">5</span>, <span class="hljs-function">(<span class="hljs-params">err, buf</span>) =></span> {
2825  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
2826  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));
2827});</code></pre>
2828<p>Any <code>TypedArray</code> or <code>DataView</code> instance may be passed as <code>buffer</code>.</p>
2829<pre><code class="language-js"><span class="hljs-keyword">const</span> a = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint</span>32<span class="hljs-built_in">Array</span>(<span class="hljs-number">10</span>);
2830crypto.<span class="hljs-title function_">randomFill</span>(a, <span class="hljs-function">(<span class="hljs-params">err, buf</span>) =></span> {
2831  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
2832  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(buf.<span class="hljs-property">buffer</span>, buf.<span class="hljs-property">byteOffset</span>, buf.<span class="hljs-property">byteLength</span>)
2833    .<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));
2834});
2835
2836<span class="hljs-keyword">const</span> b = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Float</span>64<span class="hljs-built_in">Array</span>(<span class="hljs-number">10</span>);
2837crypto.<span class="hljs-title function_">randomFill</span>(b, <span class="hljs-function">(<span class="hljs-params">err, buf</span>) =></span> {
2838  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
2839  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(buf.<span class="hljs-property">buffer</span>, buf.<span class="hljs-property">byteOffset</span>, buf.<span class="hljs-property">byteLength</span>)
2840    .<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));
2841});
2842
2843<span class="hljs-keyword">const</span> c = <span class="hljs-keyword">new</span> <span class="hljs-title class_">DataView</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayBuffer</span>(<span class="hljs-number">10</span>));
2844crypto.<span class="hljs-title function_">randomFill</span>(c, <span class="hljs-function">(<span class="hljs-params">err, buf</span>) =></span> {
2845  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
2846  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(buf.<span class="hljs-property">buffer</span>, buf.<span class="hljs-property">byteOffset</span>, buf.<span class="hljs-property">byteLength</span>)
2847    .<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));
2848});</code></pre>
2849<p>This API uses libuv's threadpool, which can have surprising and
2850negative performance implications for some applications; see the
2851<a href="cli.html#cli_uv_threadpool_size_size"><code>UV_THREADPOOL_SIZE</code></a> documentation for more information.</p>
2852<p>The asynchronous version of <code>crypto.randomFill()</code> is carried out in a single
2853threadpool request. To minimize threadpool task length variation, partition
2854large <code>randomFill</code> requests when doing so as part of fulfilling a client
2855request.</p>
2856<h4><code>crypto.randomInt([min, ]max[, callback])</code><span><a class="mark" href="#crypto_crypto_randomint_min_max_callback" id="crypto_crypto_randomint_min_max_callback">#</a></span></h4>
2857<div class="api_metadata">
2858<span>Added in: v14.10.0</span>
2859</div>
2860<ul>
2861<li><code>min</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Start of random range (inclusive). <strong>Default</strong>: <code>0</code>.</li>
2862<li><code>max</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> End of random range (exclusive).</li>
2863<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> <code>function(err, n) {}</code>.</li>
2864</ul>
2865<p>Return a random integer <code>n</code> such that <code>min &#x3C;= n &#x3C; max</code>.  This
2866implementation avoids <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#Modulo_bias">modulo bias</a>.</p>
2867<p>The range (<code>max - min</code>) must be less than 2<sup>48</sup>. <code>min</code> and <code>max</code> must
2868be <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isSafeInteger">safe integers</a>.</p>
2869<p>If the <code>callback</code> function is not provided, the random integer is
2870generated synchronously.</p>
2871<pre><code class="language-js"><span class="hljs-comment">// Asynchronous</span>
2872crypto.<span class="hljs-title function_">randomInt</span>(<span class="hljs-number">3</span>, <span class="hljs-function">(<span class="hljs-params">err, n</span>) =></span> {
2873  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
2874  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Random number chosen from (0, 1, 2): <span class="hljs-subst">${n}</span>`</span>);
2875});</code></pre>
2876<pre><code class="language-js"><span class="hljs-comment">// Synchronous</span>
2877<span class="hljs-keyword">const</span> n = crypto.<span class="hljs-title function_">randomInt</span>(<span class="hljs-number">3</span>);
2878<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Random number chosen from (0, 1, 2): <span class="hljs-subst">${n}</span>`</span>);</code></pre>
2879<pre><code class="language-js"><span class="hljs-comment">// With `min` argument</span>
2880<span class="hljs-keyword">const</span> n = crypto.<span class="hljs-title function_">randomInt</span>(<span class="hljs-number">1</span>, <span class="hljs-number">7</span>);
2881<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`The dice rolled: <span class="hljs-subst">${n}</span>`</span>);</code></pre>
2882<h4><code>crypto.randomUUID([options])</code><span><a class="mark" href="#crypto_crypto_randomuuid_options" id="crypto_crypto_randomuuid_options">#</a></span></h4>
2883<div class="api_metadata">
2884<span>Added in: v14.17.0</span>
2885</div>
2886<ul>
2887<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
2888<ul>
2889<li><code>disableEntropyCache</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> By default, to improve performance,
2890Node.js generates and caches enough random data to generate up to
2891128 random UUIDs. To generate a UUID without using the cache, set
2892<code>disableEntropyCache</code> to <code>true</code>. <strong>Defaults</strong>: <code>false</code>.</li>
2893</ul>
2894</li>
2895<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
2896</ul>
2897<p>Generates a random <a href="https://www.rfc-editor.org/rfc/rfc4122.txt">RFC 4122</a> version 4 UUID. The UUID is generated using a
2898cryptographic pseudorandom number generator.</p>
2899<h4><code>crypto.scrypt(password, salt, keylen[, options], callback)</code><span><a class="mark" href="#crypto_crypto_scrypt_password_salt_keylen_options_callback" id="crypto_crypto_scrypt_password_salt_keylen_options_callback">#</a></span></h4>
2900<div class="api_metadata">
2901<details class="changelog"><summary>History</summary>
2902<table>
2903<tbody><tr><th>Version</th><th>Changes</th></tr>
2904<tr><td>v12.8.0, v10.17.0</td>
2905<td><p>The <code>maxmem</code> value can now be any safe integer.</p></td></tr>
2906<tr><td>v10.9.0</td>
2907<td><p>The <code>cost</code>, <code>blockSize</code> and <code>parallelization</code> option names have been added.</p></td></tr>
2908<tr><td>v10.5.0</td>
2909<td><p><span>Added in: v10.5.0</span></p></td></tr>
2910</tbody></table>
2911</details>
2912</div>
2913<ul>
2914<li><code>password</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
2915<li><code>salt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
2916<li><code>keylen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
2917<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
2918<ul>
2919<li><code>cost</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> CPU/memory cost parameter. Must be a power of two greater
2920than one. <strong>Default:</strong> <code>16384</code>.</li>
2921<li><code>blockSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Block size parameter. <strong>Default:</strong> <code>8</code>.</li>
2922<li><code>parallelization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Parallelization parameter. <strong>Default:</strong> <code>1</code>.</li>
2923<li><code>N</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Alias for <code>cost</code>. Only one of both may be specified.</li>
2924<li><code>r</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Alias for <code>blockSize</code>. Only one of both may be specified.</li>
2925<li><code>p</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Alias for <code>parallelization</code>. Only one of both may be specified.</li>
2926<li><code>maxmem</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Memory upper bound. It is an error when (approximately)
2927<code>128 * N * r > maxmem</code>. <strong>Default:</strong> <code>32 * 1024 * 1024</code>.</li>
2928</ul>
2929</li>
2930<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
2931<ul>
2932<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
2933<li><code>derivedKey</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a></li>
2934</ul>
2935</li>
2936</ul>
2937<p>Provides an asynchronous <a href="https://en.wikipedia.org/wiki/Scrypt">scrypt</a> implementation. Scrypt is a password-based
2938key derivation function that is designed to be expensive computationally and
2939memory-wise in order to make brute-force attacks unrewarding.</p>
2940<p>The <code>salt</code> should be as unique as possible. It is recommended that a salt is
2941random and at least 16 bytes long. See <a href="https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf">NIST SP 800-132</a> for details.</p>
2942<p>The <code>callback</code> function is called with two arguments: <code>err</code> and <code>derivedKey</code>.
2943<code>err</code> is an exception object when key derivation fails, otherwise <code>err</code> is
2944<code>null</code>. <code>derivedKey</code> is passed to the callback as a <a href="buffer.html"><code>Buffer</code></a>.</p>
2945<p>An exception is thrown when any of the input arguments specify invalid values
2946or types.</p>
2947<pre><code class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
2948<span class="hljs-comment">// Using the factory defaults.</span>
2949crypto.<span class="hljs-title function_">scrypt</span>(<span class="hljs-string">'password'</span>, <span class="hljs-string">'salt'</span>, <span class="hljs-number">64</span>, <span class="hljs-function">(<span class="hljs-params">err, derivedKey</span>) =></span> {
2950  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
2951  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(derivedKey.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));  <span class="hljs-comment">// '3745e48...08d59ae'</span>
2952});
2953<span class="hljs-comment">// Using a custom N parameter. Must be a power of two.</span>
2954crypto.<span class="hljs-title function_">scrypt</span>(<span class="hljs-string">'password'</span>, <span class="hljs-string">'salt'</span>, <span class="hljs-number">64</span>, { <span class="hljs-attr">N</span>: <span class="hljs-number">1024</span> }, <span class="hljs-function">(<span class="hljs-params">err, derivedKey</span>) =></span> {
2955  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
2956  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(derivedKey.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));  <span class="hljs-comment">// '3745e48...aa39b34'</span>
2957});</code></pre>
2958<h4><code>crypto.scryptSync(password, salt, keylen[, options])</code><span><a class="mark" href="#crypto_crypto_scryptsync_password_salt_keylen_options" id="crypto_crypto_scryptsync_password_salt_keylen_options">#</a></span></h4>
2959<div class="api_metadata">
2960<details class="changelog"><summary>History</summary>
2961<table>
2962<tbody><tr><th>Version</th><th>Changes</th></tr>
2963<tr><td>v12.8.0, v10.17.0</td>
2964<td><p>The <code>maxmem</code> value can now be any safe integer.</p></td></tr>
2965<tr><td>v10.9.0</td>
2966<td><p>The <code>cost</code>, <code>blockSize</code> and <code>parallelization</code> option names have been added.</p></td></tr>
2967<tr><td>v10.5.0</td>
2968<td><p><span>Added in: v10.5.0</span></p></td></tr>
2969</tbody></table>
2970</details>
2971</div>
2972<ul>
2973<li><code>password</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
2974<li><code>salt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
2975<li><code>keylen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
2976<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
2977<ul>
2978<li><code>cost</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> CPU/memory cost parameter. Must be a power of two greater
2979than one. <strong>Default:</strong> <code>16384</code>.</li>
2980<li><code>blockSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Block size parameter. <strong>Default:</strong> <code>8</code>.</li>
2981<li><code>parallelization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Parallelization parameter. <strong>Default:</strong> <code>1</code>.</li>
2982<li><code>N</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Alias for <code>cost</code>. Only one of both may be specified.</li>
2983<li><code>r</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Alias for <code>blockSize</code>. Only one of both may be specified.</li>
2984<li><code>p</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Alias for <code>parallelization</code>. Only one of both may be specified.</li>
2985<li><code>maxmem</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Memory upper bound. It is an error when (approximately)
2986<code>128 * N * r > maxmem</code>. <strong>Default:</strong> <code>32 * 1024 * 1024</code>.</li>
2987</ul>
2988</li>
2989<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a></li>
2990</ul>
2991<p>Provides a synchronous <a href="https://en.wikipedia.org/wiki/Scrypt">scrypt</a> implementation. Scrypt is a password-based
2992key derivation function that is designed to be expensive computationally and
2993memory-wise in order to make brute-force attacks unrewarding.</p>
2994<p>The <code>salt</code> should be as unique as possible. It is recommended that a salt is
2995random and at least 16 bytes long. See <a href="https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf">NIST SP 800-132</a> for details.</p>
2996<p>An exception is thrown when key derivation fails, otherwise the derived key is
2997returned as a <a href="buffer.html"><code>Buffer</code></a>.</p>
2998<p>An exception is thrown when any of the input arguments specify invalid values
2999or types.</p>
3000<pre><code class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
3001<span class="hljs-comment">// Using the factory defaults.</span>
3002<span class="hljs-keyword">const</span> key1 = crypto.<span class="hljs-title function_">scryptSync</span>(<span class="hljs-string">'password'</span>, <span class="hljs-string">'salt'</span>, <span class="hljs-number">64</span>);
3003<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(key1.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));  <span class="hljs-comment">// '3745e48...08d59ae'</span>
3004<span class="hljs-comment">// Using a custom N parameter. Must be a power of two.</span>
3005<span class="hljs-keyword">const</span> key2 = crypto.<span class="hljs-title function_">scryptSync</span>(<span class="hljs-string">'password'</span>, <span class="hljs-string">'salt'</span>, <span class="hljs-number">64</span>, { <span class="hljs-attr">N</span>: <span class="hljs-number">1024</span> });
3006<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(key2.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));  <span class="hljs-comment">// '3745e48...aa39b34'</span></code></pre>
3007<h4><code>crypto.setEngine(engine[, flags])</code><span><a class="mark" href="#crypto_crypto_setengine_engine_flags" id="crypto_crypto_setengine_engine_flags">#</a></span></h4>
3008<div class="api_metadata">
3009<span>Added in: v0.11.11</span>
3010</div>
3011<ul>
3012<li><code>engine</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
3013<li><code>flags</code> <a href="crypto.html#crypto_crypto_constants_1" class="type">&#x3C;crypto.constants></a> <strong>Default:</strong> <code>crypto.constants.ENGINE_METHOD_ALL</code></li>
3014</ul>
3015<p>Load and set the <code>engine</code> for some or all OpenSSL functions (selected by flags).</p>
3016<p><code>engine</code> could be either an id or a path to the engine's shared library.</p>
3017<p>The optional <code>flags</code> argument uses <code>ENGINE_METHOD_ALL</code> by default. The <code>flags</code>
3018is a bit field taking one of or a mix of the following flags (defined in
3019<code>crypto.constants</code>):</p>
3020<ul>
3021<li><code>crypto.constants.ENGINE_METHOD_RSA</code></li>
3022<li><code>crypto.constants.ENGINE_METHOD_DSA</code></li>
3023<li><code>crypto.constants.ENGINE_METHOD_DH</code></li>
3024<li><code>crypto.constants.ENGINE_METHOD_RAND</code></li>
3025<li><code>crypto.constants.ENGINE_METHOD_EC</code></li>
3026<li><code>crypto.constants.ENGINE_METHOD_CIPHERS</code></li>
3027<li><code>crypto.constants.ENGINE_METHOD_DIGESTS</code></li>
3028<li><code>crypto.constants.ENGINE_METHOD_PKEY_METHS</code></li>
3029<li><code>crypto.constants.ENGINE_METHOD_PKEY_ASN1_METHS</code></li>
3030<li><code>crypto.constants.ENGINE_METHOD_ALL</code></li>
3031<li><code>crypto.constants.ENGINE_METHOD_NONE</code></li>
3032</ul>
3033<p>The flags below are deprecated in OpenSSL-1.1.0.</p>
3034<ul>
3035<li><code>crypto.constants.ENGINE_METHOD_ECDH</code></li>
3036<li><code>crypto.constants.ENGINE_METHOD_ECDSA</code></li>
3037<li><code>crypto.constants.ENGINE_METHOD_STORE</code></li>
3038</ul>
3039<h4><code>crypto.setFips(bool)</code><span><a class="mark" href="#crypto_crypto_setfips_bool" id="crypto_crypto_setfips_bool">#</a></span></h4>
3040<div class="api_metadata">
3041<span>Added in: v10.0.0</span>
3042</div>
3043<ul>
3044<li><code>bool</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <code>true</code> to enable FIPS mode.</li>
3045</ul>
3046<p>Enables the FIPS compliant crypto provider in a FIPS-enabled Node.js build.
3047Throws an error if FIPS mode is not available.</p>
3048<h4><code>crypto.sign(algorithm, data, key)</code><span><a class="mark" href="#crypto_crypto_sign_algorithm_data_key" id="crypto_crypto_sign_algorithm_data_key">#</a></span></h4>
3049<div class="api_metadata">
3050<details class="changelog"><summary>History</summary>
3051<table>
3052<tbody><tr><th>Version</th><th>Changes</th></tr>
3053<tr><td>v13.2.0, v12.16.0</td>
3054<td><p>This function now supports IEEE-P1363 DSA and ECDSA signatures.</p></td></tr>
3055<tr><td>v12.0.0</td>
3056<td><p><span>Added in: v12.0.0</span></p></td></tr>
3057</tbody></table>
3058</details>
3059</div>
3060<ul>
3061<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a></li>
3062<li><code>data</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
3063<li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="crypto.html#crypto_class_keyobject" class="type">&#x3C;KeyObject></a></li>
3064<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a></li>
3065</ul>
3066<p>Calculates and returns the signature for <code>data</code> using the given private key and
3067algorithm. If <code>algorithm</code> is <code>null</code> or <code>undefined</code>, then the algorithm is
3068dependent upon the key type (especially Ed25519 and Ed448).</p>
3069<p>If <code>key</code> is not a <a href="#crypto_class_keyobject"><code>KeyObject</code></a>, this function behaves as if <code>key</code> had been
3070passed to <a href="#crypto_crypto_createprivatekey_key"><code>crypto.createPrivateKey()</code></a>. If it is an object, the following
3071additional properties can be passed:</p>
3072<ul>
3073<li>
3074<p><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> For DSA and ECDSA, this option specifies the
3075format of the generated signature. It can be one of the following:</p>
3076<ul>
3077<li><code>'der'</code> (default): DER-encoded ASN.1 signature structure encoding <code>(r, s)</code>.</li>
3078<li><code>'ieee-p1363'</code>: Signature format <code>r || s</code> as proposed in IEEE-P1363.</li>
3079</ul>
3080</li>
3081<li>
3082<p><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Optional padding value for RSA, one of the following:</p>
3083<ul>
3084<li><code>crypto.constants.RSA_PKCS1_PADDING</code> (default)</li>
3085<li><code>crypto.constants.RSA_PKCS1_PSS_PADDING</code></li>
3086</ul>
3087<p><code>RSA_PKCS1_PSS_PADDING</code> will use MGF1 with the same hash function
3088used to sign the message as specified in section 3.1 of <a href="https://www.rfc-editor.org/rfc/rfc4055.txt">RFC 4055</a>.</p>
3089</li>
3090<li>
3091<p><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Salt length for when padding is
3092<code>RSA_PKCS1_PSS_PADDING</code>. The special value
3093<code>crypto.constants.RSA_PSS_SALTLEN_DIGEST</code> sets the salt length to the digest
3094size, <code>crypto.constants.RSA_PSS_SALTLEN_MAX_SIGN</code> (default) sets it to the
3095maximum permissible value.</p>
3096</li>
3097</ul>
3098<h4><code>crypto.timingSafeEqual(a, b)</code><span><a class="mark" href="#crypto_crypto_timingsafeequal_a_b" id="crypto_crypto_timingsafeequal_a_b">#</a></span></h4>
3099<div class="api_metadata">
3100<span>Added in: v6.6.0</span>
3101</div>
3102<ul>
3103<li><code>a</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
3104<li><code>b</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
3105<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
3106</ul>
3107<p>This function is based on a constant-time algorithm.
3108Returns true if <code>a</code> is equal to <code>b</code>, without leaking timing information that
3109would allow an attacker to guess one of the values. This is suitable for
3110comparing HMAC digests or secret values like authentication cookies or
3111<a href="https://www.w3.org/TR/capability-urls/">capability urls</a>.</p>
3112<p><code>a</code> and <code>b</code> must both be <code>Buffer</code>s, <code>TypedArray</code>s, or <code>DataView</code>s, and they
3113must have the same byte length.</p>
3114<p>If at least one of <code>a</code> and <code>b</code> is a <code>TypedArray</code> with more than one byte per
3115entry, such as <code>Uint16Array</code>, the result will be computed using the platform
3116byte order.</p>
3117<p>Use of <code>crypto.timingSafeEqual</code> does not guarantee that the <em>surrounding</em> code
3118is timing-safe. Care should be taken to ensure that the surrounding code does
3119not introduce timing vulnerabilities.</p>
3120<h4><code>crypto.verify(algorithm, data, key, signature)</code><span><a class="mark" href="#crypto_crypto_verify_algorithm_data_key_signature" id="crypto_crypto_verify_algorithm_data_key_signature">#</a></span></h4>
3121<div class="api_metadata">
3122<details class="changelog"><summary>History</summary>
3123<table>
3124<tbody><tr><th>Version</th><th>Changes</th></tr>
3125<tr><td>v13.2.0, v12.16.0</td>
3126<td><p>This function now supports IEEE-P1363 DSA and ECDSA signatures.</p></td></tr>
3127<tr><td>v12.0.0</td>
3128<td><p><span>Added in: v12.0.0</span></p></td></tr>
3129</tbody></table>
3130</details>
3131</div>
3132<ul>
3133<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a></li>
3134<li><code>data</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
3135<li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="crypto.html#crypto_class_keyobject" class="type">&#x3C;KeyObject></a></li>
3136<li><code>signature</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a></li>
3137<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
3138</ul>
3139<p>Verifies the given signature for <code>data</code> using the given key and algorithm. If
3140<code>algorithm</code> is <code>null</code> or <code>undefined</code>, then the algorithm is dependent upon the
3141key type (especially Ed25519 and Ed448).</p>
3142<p>If <code>key</code> is not a <a href="#crypto_class_keyobject"><code>KeyObject</code></a>, this function behaves as if <code>key</code> had been
3143passed to <a href="#crypto_crypto_createpublickey_key"><code>crypto.createPublicKey()</code></a>. If it is an object, the following
3144additional properties can be passed:</p>
3145<ul>
3146<li>
3147<p><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> For DSA and ECDSA, this option specifies the
3148format of the signature. It can be one of the following:</p>
3149<ul>
3150<li><code>'der'</code> (default): DER-encoded ASN.1 signature structure encoding <code>(r, s)</code>.</li>
3151<li><code>'ieee-p1363'</code>: Signature format <code>r || s</code> as proposed in IEEE-P1363.</li>
3152</ul>
3153</li>
3154<li>
3155<p><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Optional padding value for RSA, one of the following:</p>
3156<ul>
3157<li><code>crypto.constants.RSA_PKCS1_PADDING</code> (default)</li>
3158<li><code>crypto.constants.RSA_PKCS1_PSS_PADDING</code></li>
3159</ul>
3160<p><code>RSA_PKCS1_PSS_PADDING</code> will use MGF1 with the same hash function
3161used to sign the message as specified in section 3.1 of <a href="https://www.rfc-editor.org/rfc/rfc4055.txt">RFC 4055</a>.</p>
3162</li>
3163<li>
3164<p><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Salt length for when padding is
3165<code>RSA_PKCS1_PSS_PADDING</code>. The special value
3166<code>crypto.constants.RSA_PSS_SALTLEN_DIGEST</code> sets the salt length to the digest
3167size, <code>crypto.constants.RSA_PSS_SALTLEN_MAX_SIGN</code> (default) sets it to the
3168maximum permissible value.</p>
3169</li>
3170</ul>
3171<p>The <code>signature</code> argument is the previously calculated signature for the <code>data</code>.</p>
3172<p>Because public keys can be derived from private keys, a private key or a public
3173key may be passed for <code>key</code>.</p>
3174</section><section><h3>Notes<span><a class="mark" href="#crypto_notes" id="crypto_notes">#</a></span></h3>
3175<h4>Legacy streams API (prior to Node.js 0.10)<span><a class="mark" href="#crypto_legacy_streams_api_prior_to_node_js_0_10" id="crypto_legacy_streams_api_prior_to_node_js_0_10">#</a></span></h4>
3176<p>The Crypto module was added to Node.js before there was the concept of a
3177unified Stream API, and before there were <a href="buffer.html"><code>Buffer</code></a> objects for handling
3178binary data. As such, the many of the <code>crypto</code> defined classes have methods not
3179typically found on other Node.js classes that implement the <a href="stream.html">streams</a>
3180API (e.g. <code>update()</code>, <code>final()</code>, or <code>digest()</code>). Also, many methods accepted
3181and returned <code>'latin1'</code> encoded strings by default rather than <code>Buffer</code>s. This
3182default was changed after Node.js v0.8 to use <a href="buffer.html"><code>Buffer</code></a> objects by default
3183instead.</p>
3184<h4>Recent ECDH changes<span><a class="mark" href="#crypto_recent_ecdh_changes" id="crypto_recent_ecdh_changes">#</a></span></h4>
3185<p>Usage of <code>ECDH</code> with non-dynamically generated key pairs has been simplified.
3186Now, <a href="#crypto_ecdh_setprivatekey_privatekey_encoding"><code>ecdh.setPrivateKey()</code></a> can be called with a preselected private key
3187and the associated public point (key) will be computed and stored in the object.
3188This allows code to only store and provide the private part of the EC key pair.
3189<a href="#crypto_ecdh_setprivatekey_privatekey_encoding"><code>ecdh.setPrivateKey()</code></a> now also validates that the private key is valid for
3190the selected curve.</p>
3191<p>The <a href="#crypto_ecdh_setpublickey_publickey_encoding"><code>ecdh.setPublicKey()</code></a> method is now deprecated as its inclusion in the
3192API is not useful. Either a previously stored private key should be set, which
3193automatically generates the associated public key, or <a href="#crypto_ecdh_generatekeys_encoding_format"><code>ecdh.generateKeys()</code></a>
3194should be called. The main drawback of using <a href="#crypto_ecdh_setpublickey_publickey_encoding"><code>ecdh.setPublicKey()</code></a> is that
3195it can be used to put the ECDH key pair into an inconsistent state.</p>
3196<h4>Support for weak or compromised algorithms<span><a class="mark" href="#crypto_support_for_weak_or_compromised_algorithms" id="crypto_support_for_weak_or_compromised_algorithms">#</a></span></h4>
3197<p>The <code>crypto</code> module still supports some algorithms which are already
3198compromised and are not currently recommended for use. The API also allows
3199the use of ciphers and hashes with a small key size that are too weak for safe
3200use.</p>
3201<p>Users should take full responsibility for selecting the crypto
3202algorithm and key size according to their security requirements.</p>
3203<p>Based on the recommendations of <a href="https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar1.pdf">NIST SP 800-131A</a>:</p>
3204<ul>
3205<li>MD5 and SHA-1 are no longer acceptable where collision resistance is
3206required such as digital signatures.</li>
3207<li>The key used with RSA, DSA, and DH algorithms is recommended to have
3208at least 2048 bits and that of the curve of ECDSA and ECDH at least
3209224 bits, to be safe to use for several years.</li>
3210<li>The DH groups of <code>modp1</code>, <code>modp2</code> and <code>modp5</code> have a key size
3211smaller than 2048 bits and are not recommended.</li>
3212</ul>
3213<p>See the reference for other recommendations and details.</p>
3214<h4>CCM mode<span><a class="mark" href="#crypto_ccm_mode" id="crypto_ccm_mode">#</a></span></h4>
3215<p>CCM is one of the supported <a href="https://en.wikipedia.org/wiki/Authenticated_encryption">AEAD algorithms</a>. Applications which use this
3216mode must adhere to certain restrictions when using the cipher API:</p>
3217<ul>
3218<li>The authentication tag length must be specified during cipher creation by
3219setting the <code>authTagLength</code> option and must be one of 4, 6, 8, 10, 12, 14 or
322016 bytes.</li>
3221<li>The length of the initialization vector (nonce) <code>N</code> must be between 7 and 13
3222bytes (<code>7 ≤ N ≤ 13</code>).</li>
3223<li>The length of the plaintext is limited to <code>2 ** (8 * (15 - N))</code> bytes.</li>
3224<li>When decrypting, the authentication tag must be set via <code>setAuthTag()</code> before
3225calling <code>update()</code>.
3226Otherwise, decryption will fail and <code>final()</code> will throw an error in
3227compliance with section 2.6 of <a href="https://www.rfc-editor.org/rfc/rfc3610.txt">RFC 3610</a>.</li>
3228<li>Using stream methods such as <code>write(data)</code>, <code>end(data)</code> or <code>pipe()</code> in CCM
3229mode might fail as CCM cannot handle more than one chunk of data per instance.</li>
3230<li>When passing additional authenticated data (AAD), the length of the actual
3231message in bytes must be passed to <code>setAAD()</code> via the <code>plaintextLength</code>
3232option.
3233Many crypto libraries include the authentication tag in the ciphertext,
3234which means that they produce ciphertexts of the length
3235<code>plaintextLength + authTagLength</code>. Node.js does not include the authentication
3236tag, so the ciphertext length is always <code>plaintextLength</code>.
3237This is not necessary if no AAD is used.</li>
3238<li>As CCM processes the whole message at once, <code>update()</code> can only be called
3239once.</li>
3240<li>Even though calling <code>update()</code> is sufficient to encrypt/decrypt the message,
3241applications <em>must</em> call <code>final()</code> to compute or verify the
3242authentication tag.</li>
3243</ul>
3244<pre><code class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
3245
3246<span class="hljs-keyword">const</span> key = <span class="hljs-string">'keykeykeykeykeykeykeykey'</span>;
3247<span class="hljs-keyword">const</span> nonce = crypto.<span class="hljs-title function_">randomBytes</span>(<span class="hljs-number">12</span>);
3248
3249<span class="hljs-keyword">const</span> aad = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'0123456789'</span>, <span class="hljs-string">'hex'</span>);
3250
3251<span class="hljs-keyword">const</span> cipher = crypto.<span class="hljs-title function_">createCipheriv</span>(<span class="hljs-string">'aes-192-ccm'</span>, key, nonce, {
3252  <span class="hljs-attr">authTagLength</span>: <span class="hljs-number">16</span>
3253});
3254<span class="hljs-keyword">const</span> plaintext = <span class="hljs-string">'Hello world'</span>;
3255cipher.<span class="hljs-title function_">setAAD</span>(aad, {
3256  <span class="hljs-attr">plaintextLength</span>: <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">byteLength</span>(plaintext)
3257});
3258<span class="hljs-keyword">const</span> ciphertext = cipher.<span class="hljs-title function_">update</span>(plaintext, <span class="hljs-string">'utf8'</span>);
3259cipher.<span class="hljs-title function_">final</span>();
3260<span class="hljs-keyword">const</span> tag = cipher.<span class="hljs-title function_">getAuthTag</span>();
3261
3262<span class="hljs-comment">// Now transmit { ciphertext, nonce, tag }.</span>
3263
3264<span class="hljs-keyword">const</span> decipher = crypto.<span class="hljs-title function_">createDecipheriv</span>(<span class="hljs-string">'aes-192-ccm'</span>, key, nonce, {
3265  <span class="hljs-attr">authTagLength</span>: <span class="hljs-number">16</span>
3266});
3267decipher.<span class="hljs-title function_">setAuthTag</span>(tag);
3268decipher.<span class="hljs-title function_">setAAD</span>(aad, {
3269  <span class="hljs-attr">plaintextLength</span>: ciphertext.<span class="hljs-property">length</span>
3270});
3271<span class="hljs-keyword">const</span> receivedPlaintext = decipher.<span class="hljs-title function_">update</span>(ciphertext, <span class="hljs-literal">null</span>, <span class="hljs-string">'utf8'</span>);
3272
3273<span class="hljs-keyword">try</span> {
3274  decipher.<span class="hljs-title function_">final</span>();
3275} <span class="hljs-keyword">catch</span> (err) {
3276  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'Authentication failed!'</span>);
3277  <span class="hljs-keyword">return</span>;
3278}
3279
3280<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(receivedPlaintext);</code></pre>
3281</section><section><h3>Crypto constants<span><a class="mark" href="#crypto_crypto_constants_1" id="crypto_crypto_constants_1">#</a></span></h3>
3282<p>The following constants exported by <code>crypto.constants</code> apply to various uses of
3283the <code>crypto</code>, <code>tls</code>, and <code>https</code> modules and are generally specific to OpenSSL.</p>
3284<h4>OpenSSL options<span><a class="mark" href="#crypto_openssl_options" id="crypto_openssl_options">#</a></span></h4>
3285<table>
3286  <tbody><tr>
3287    <th>Constant</th>
3288    <th>Description</th>
3289  </tr>
3290  <tr>
3291    <td><code>SSL_OP_ALL</code></td>
3292    <td>Applies multiple bug workarounds within OpenSSL. See
3293    <a href="https://www.openssl.org/docs/man1.0.2/ssl/SSL_CTX_set_options.html">https://www.openssl.org/docs/man1.0.2/ssl/SSL_CTX_set_options.html</a>
3294    for detail.</td>
3295  </tr>
3296  <tr>
3297    <td><code>SSL_OP_ALLOW_NO_DHE_KEX</code></td>
3298    <td>Instructs OpenSSL to allow a non-[EC]DHE-based key exchange mode
3299    for TLS v1.3</td>
3300  </tr>
3301  <tr>
3302    <td><code>SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION</code></td>
3303    <td>Allows legacy insecure renegotiation between OpenSSL and unpatched
3304    clients or servers. See
3305    <a href="https://www.openssl.org/docs/man1.0.2/ssl/SSL_CTX_set_options.html">https://www.openssl.org/docs/man1.0.2/ssl/SSL_CTX_set_options.html</a>.</td>
3306  </tr>
3307  <tr>
3308    <td><code>SSL_OP_CIPHER_SERVER_PREFERENCE</code></td>
3309    <td>Attempts to use the server's preferences instead of the client's when
3310    selecting a cipher. Behavior depends on protocol version. See
3311    <a href="https://www.openssl.org/docs/man1.0.2/ssl/SSL_CTX_set_options.html">https://www.openssl.org/docs/man1.0.2/ssl/SSL_CTX_set_options.html</a>.</td>
3312  </tr>
3313  <tr>
3314    <td><code>SSL_OP_CISCO_ANYCONNECT</code></td>
3315    <td>Instructs OpenSSL to use Cisco's "speshul" version of DTLS_BAD_VER.</td>
3316  </tr>
3317  <tr>
3318    <td><code>SSL_OP_COOKIE_EXCHANGE</code></td>
3319    <td>Instructs OpenSSL to turn on cookie exchange.</td>
3320  </tr>
3321  <tr>
3322    <td><code>SSL_OP_CRYPTOPRO_TLSEXT_BUG</code></td>
3323    <td>Instructs OpenSSL to add server-hello extension from an early version
3324    of the cryptopro draft.</td>
3325  </tr>
3326  <tr>
3327    <td><code>SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS</code></td>
3328    <td>Instructs OpenSSL to disable a SSL 3.0/TLS 1.0 vulnerability
3329    workaround added in OpenSSL 0.9.6d.</td>
3330  </tr>
3331  <tr>
3332    <td><code>SSL_OP_EPHEMERAL_RSA</code></td>
3333    <td>Instructs OpenSSL to always use the tmp_rsa key when performing RSA
3334    operations.</td>
3335  </tr>
3336  <tr>
3337    <td><code>SSL_OP_LEGACY_SERVER_CONNECT</code></td>
3338    <td>Allows initial connection to servers that do not support RI.</td>
3339  </tr>
3340  <tr>
3341    <td><code>SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER</code></td>
3342    <td></td>
3343  </tr>
3344  <tr>
3345    <td><code>SSL_OP_MICROSOFT_SESS_ID_BUG</code></td>
3346    <td></td>
3347  </tr>
3348  <tr>
3349    <td><code>SSL_OP_MSIE_SSLV2_RSA_PADDING</code></td>
3350    <td>Instructs OpenSSL to disable the workaround for a man-in-the-middle
3351    protocol-version vulnerability in the SSL 2.0 server implementation.</td>
3352  </tr>
3353  <tr>
3354    <td><code>SSL_OP_NETSCAPE_CA_DN_BUG</code></td>
3355    <td></td>
3356  </tr>
3357  <tr>
3358    <td><code>SSL_OP_NETSCAPE_CHALLENGE_BUG</code></td>
3359    <td></td>
3360  </tr>
3361  <tr>
3362    <td><code>SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG</code></td>
3363    <td></td>
3364  </tr>
3365  <tr>
3366    <td><code>SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG</code></td>
3367    <td></td>
3368  </tr>
3369  <tr>
3370    <td><code>SSL_OP_NO_COMPRESSION</code></td>
3371    <td>Instructs OpenSSL to disable support for SSL/TLS compression.</td>
3372  </tr>
3373  <tr>
3374    <td><code>SSL_OP_NO_ENCRYPT_THEN_MAC</code></td>
3375    <td>Instructs OpenSSL to disable encrypt-then-MAC.</td>
3376  </tr>
3377  <tr>
3378    <td><code>SSL_OP_NO_QUERY_MTU</code></td>
3379    <td></td>
3380  </tr>
3381  <tr>
3382    <td><code>SSL_OP_NO_RENEGOTIATION</code></td>
3383    <td>Instructs OpenSSL to disable renegotiation.</td>
3384  </tr>
3385  <tr>
3386    <td><code>SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION</code></td>
3387    <td>Instructs OpenSSL to always start a new session when performing
3388    renegotiation.</td>
3389  </tr>
3390  <tr>
3391    <td><code>SSL_OP_NO_SSLv2</code></td>
3392    <td>Instructs OpenSSL to turn off SSL v2</td>
3393  </tr>
3394  <tr>
3395    <td><code>SSL_OP_NO_SSLv3</code></td>
3396    <td>Instructs OpenSSL to turn off SSL v3</td>
3397  </tr>
3398  <tr>
3399    <td><code>SSL_OP_NO_TICKET</code></td>
3400    <td>Instructs OpenSSL to disable use of RFC4507bis tickets.</td>
3401  </tr>
3402  <tr>
3403    <td><code>SSL_OP_NO_TLSv1</code></td>
3404    <td>Instructs OpenSSL to turn off TLS v1</td>
3405  </tr>
3406  <tr>
3407    <td><code>SSL_OP_NO_TLSv1_1</code></td>
3408    <td>Instructs OpenSSL to turn off TLS v1.1</td>
3409  </tr>
3410  <tr>
3411    <td><code>SSL_OP_NO_TLSv1_2</code></td>
3412    <td>Instructs OpenSSL to turn off TLS v1.2</td>
3413  </tr>
3414  <tr>
3415    <td><code>SSL_OP_NO_TLSv1_3</code></td>
3416    <td>Instructs OpenSSL to turn off TLS v1.3</td>
3417  </tr>
3418    <tr><td><code>SSL_OP_PKCS1_CHECK_1</code></td>
3419    <td></td>
3420  </tr>
3421  <tr>
3422    <td><code>SSL_OP_PKCS1_CHECK_2</code></td>
3423    <td></td>
3424  </tr>
3425  <tr>
3426    <td><code>SSL_OP_PRIORITIZE_CHACHA</code></td>
3427    <td>Instructs OpenSSL server to prioritize ChaCha20Poly1305
3428    when client does.
3429    This option has no effect if
3430    <code>SSL_OP_CIPHER_SERVER_PREFERENCE</code>
3431    is not enabled.</td>
3432  </tr>
3433  <tr>
3434    <td><code>SSL_OP_SINGLE_DH_USE</code></td>
3435    <td>Instructs OpenSSL to always create a new key when using
3436    temporary/ephemeral DH parameters.</td>
3437  </tr>
3438  <tr>
3439    <td><code>SSL_OP_SINGLE_ECDH_USE</code></td>
3440    <td>Instructs OpenSSL to always create a new key when using
3441    temporary/ephemeral ECDH parameters.</td>
3442  </tr>
3443    <tr><td><code>SSL_OP_SSLEAY_080_CLIENT_DH_BUG</code></td>
3444    <td></td>
3445  </tr>
3446  <tr>
3447    <td><code>SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG</code></td>
3448    <td></td>
3449  </tr>
3450  <tr>
3451    <td><code>SSL_OP_TLS_BLOCK_PADDING_BUG</code></td>
3452    <td></td>
3453  </tr>
3454  <tr>
3455    <td><code>SSL_OP_TLS_D5_BUG</code></td>
3456    <td></td>
3457  </tr>
3458  <tr>
3459    <td><code>SSL_OP_TLS_ROLLBACK_BUG</code></td>
3460    <td>Instructs OpenSSL to disable version rollback attack detection.</td>
3461  </tr>
3462</tbody></table>
3463<h4>OpenSSL engine constants<span><a class="mark" href="#crypto_openssl_engine_constants" id="crypto_openssl_engine_constants">#</a></span></h4>
3464<table>
3465  <tbody><tr>
3466    <th>Constant</th>
3467    <th>Description</th>
3468  </tr>
3469  <tr>
3470    <td><code>ENGINE_METHOD_RSA</code></td>
3471    <td>Limit engine usage to RSA</td>
3472  </tr>
3473  <tr>
3474    <td><code>ENGINE_METHOD_DSA</code></td>
3475    <td>Limit engine usage to DSA</td>
3476  </tr>
3477  <tr>
3478    <td><code>ENGINE_METHOD_DH</code></td>
3479    <td>Limit engine usage to DH</td>
3480  </tr>
3481  <tr>
3482    <td><code>ENGINE_METHOD_RAND</code></td>
3483    <td>Limit engine usage to RAND</td>
3484  </tr>
3485  <tr>
3486    <td><code>ENGINE_METHOD_EC</code></td>
3487    <td>Limit engine usage to EC</td>
3488  </tr>
3489  <tr>
3490    <td><code>ENGINE_METHOD_CIPHERS</code></td>
3491    <td>Limit engine usage to CIPHERS</td>
3492  </tr>
3493  <tr>
3494    <td><code>ENGINE_METHOD_DIGESTS</code></td>
3495    <td>Limit engine usage to DIGESTS</td>
3496  </tr>
3497  <tr>
3498    <td><code>ENGINE_METHOD_PKEY_METHS</code></td>
3499    <td>Limit engine usage to PKEY_METHDS</td>
3500  </tr>
3501  <tr>
3502    <td><code>ENGINE_METHOD_PKEY_ASN1_METHS</code></td>
3503    <td>Limit engine usage to PKEY_ASN1_METHS</td>
3504  </tr>
3505  <tr>
3506    <td><code>ENGINE_METHOD_ALL</code></td>
3507    <td></td>
3508  </tr>
3509  <tr>
3510    <td><code>ENGINE_METHOD_NONE</code></td>
3511    <td></td>
3512  </tr>
3513</tbody></table>
3514<h4>Other OpenSSL constants<span><a class="mark" href="#crypto_other_openssl_constants" id="crypto_other_openssl_constants">#</a></span></h4>
3515<p>See the <a href="https://wiki.openssl.org/index.php/List_of_SSL_OP_Flags#Table_of_Options">list of SSL OP Flags</a> for details.</p>
3516<table>
3517  <tbody><tr>
3518    <th>Constant</th>
3519    <th>Description</th>
3520  </tr>
3521  <tr>
3522    <td><code>DH_CHECK_P_NOT_SAFE_PRIME</code></td>
3523    <td></td>
3524  </tr>
3525  <tr>
3526    <td><code>DH_CHECK_P_NOT_PRIME</code></td>
3527    <td></td>
3528  </tr>
3529  <tr>
3530    <td><code>DH_UNABLE_TO_CHECK_GENERATOR</code></td>
3531    <td></td>
3532  </tr>
3533  <tr>
3534    <td><code>DH_NOT_SUITABLE_GENERATOR</code></td>
3535    <td></td>
3536  </tr>
3537  <tr>
3538    <td><code>ALPN_ENABLED</code></td>
3539    <td></td>
3540  </tr>
3541  <tr>
3542    <td><code>RSA_PKCS1_PADDING</code></td>
3543    <td></td>
3544  </tr>
3545  <tr>
3546    <td><code>RSA_SSLV23_PADDING</code></td>
3547    <td></td>
3548  </tr>
3549  <tr>
3550    <td><code>RSA_NO_PADDING</code></td>
3551    <td></td>
3552  </tr>
3553  <tr>
3554    <td><code>RSA_PKCS1_OAEP_PADDING</code></td>
3555    <td></td>
3556  </tr>
3557  <tr>
3558    <td><code>RSA_X931_PADDING</code></td>
3559    <td></td>
3560  </tr>
3561  <tr>
3562    <td><code>RSA_PKCS1_PSS_PADDING</code></td>
3563    <td></td>
3564  </tr>
3565  <tr>
3566    <td><code>RSA_PSS_SALTLEN_DIGEST</code></td>
3567    <td>Sets the salt length for <code>RSA_PKCS1_PSS_PADDING</code> to the
3568        digest size when signing or verifying.</td>
3569  </tr>
3570  <tr>
3571    <td><code>RSA_PSS_SALTLEN_MAX_SIGN</code></td>
3572    <td>Sets the salt length for <code>RSA_PKCS1_PSS_PADDING</code> to the
3573        maximum permissible value when signing data.</td>
3574  </tr>
3575  <tr>
3576    <td><code>RSA_PSS_SALTLEN_AUTO</code></td>
3577    <td>Causes the salt length for <code>RSA_PKCS1_PSS_PADDING</code> to be
3578        determined automatically when verifying a signature.</td>
3579  </tr>
3580  <tr>
3581    <td><code>POINT_CONVERSION_COMPRESSED</code></td>
3582    <td></td>
3583  </tr>
3584  <tr>
3585    <td><code>POINT_CONVERSION_UNCOMPRESSED</code></td>
3586    <td></td>
3587  </tr>
3588  <tr>
3589    <td><code>POINT_CONVERSION_HYBRID</code></td>
3590    <td></td>
3591  </tr>
3592</tbody></table>
3593<h4>Node.js crypto constants<span><a class="mark" href="#crypto_node_js_crypto_constants" id="crypto_node_js_crypto_constants">#</a></span></h4>
3594<table>
3595  <tbody><tr>
3596    <th>Constant</th>
3597    <th>Description</th>
3598  </tr>
3599  <tr>
3600    <td><code>defaultCoreCipherList</code></td>
3601    <td>Specifies the built-in default cipher list used by Node.js.</td>
3602  </tr>
3603  <tr>
3604    <td><code>defaultCipherList</code></td>
3605    <td>Specifies the active default cipher list used by the current Node.js
3606    process.</td>
3607  </tr>
3608</tbody></table></section>
3609        <!-- API END -->
3610      </div>
3611    </div>
3612  </div>
3613  <script>
3614    'use strict';
3615    {
3616      const kCustomPreference = 'customDarkTheme';
3617      const userSettings = sessionStorage.getItem(kCustomPreference);
3618      const themeToggleButton = document.getElementById('theme-toggle-btn');
3619      if (userSettings === null && window.matchMedia) {
3620        const mq = window.matchMedia('(prefers-color-scheme: dark)');
3621        if ('onchange' in mq) {
3622          function mqChangeListener(e) {
3623            document.body.classList.toggle('dark-mode', e.matches);
3624          }
3625          mq.addEventListener('change', mqChangeListener);
3626          if (themeToggleButton) {
3627            themeToggleButton.addEventListener('click', function() {
3628              mq.removeEventListener('change', mqChangeListener);
3629            }, { once: true });
3630          }
3631        }
3632        if (mq.matches) {
3633          document.body.classList.add('dark-mode');
3634        }
3635      } else if (userSettings === 'true') {
3636        document.body.classList.add('dark-mode');
3637      }
3638      if (themeToggleButton) {
3639        themeToggleButton.hidden = false;
3640        themeToggleButton.addEventListener('click', function() {
3641          sessionStorage.setItem(
3642            kCustomPreference,
3643            document.body.classList.toggle('dark-mode')
3644          );
3645        });
3646      }
3647    }
3648  </script>
3649</body>
3650</html>
3651