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.21.2"> 7 <title>Crypto | Node.js v14.21.2 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.21.2 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>▼</span></a> 119 <ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v19.x/api/crypto.html">19.x</a></li> 120<li><a href="https://nodejs.org/docs/latest-v18.x/api/crypto.html">18.x <b>LTS</b></a></li> 121<li><a href="https://nodejs.org/docs/latest-v17.x/api/crypto.html">17.x</a></li> 122<li><a href="https://nodejs.org/docs/latest-v16.x/api/crypto.html">16.x <b>LTS</b></a></li> 123<li><a href="https://nodejs.org/docs/latest-v15.x/api/crypto.html">15.x</a></li> 124<li><a href="https://nodejs.org/docs/latest-v14.x/api/crypto.html">14.x <b>LTS</b></a></li> 125<li><a href="https://nodejs.org/docs/latest-v13.x/api/crypto.html">13.x</a></li> 126<li><a href="https://nodejs.org/docs/latest-v12.x/api/crypto.html">12.x</a></li> 127<li><a href="https://nodejs.org/docs/latest-v11.x/api/crypto.html">11.x</a></li> 128<li><a href="https://nodejs.org/docs/latest-v10.x/api/crypto.html">10.x</a></li> 129<li><a href="https://nodejs.org/docs/latest-v9.x/api/crypto.html">9.x</a></li> 130<li><a href="https://nodejs.org/docs/latest-v8.x/api/crypto.html">8.x</a></li> 131<li><a href="https://nodejs.org/docs/latest-v7.x/api/crypto.html">7.x</a></li> 132<li><a href="https://nodejs.org/docs/latest-v6.x/api/crypto.html">6.x</a></li> 133<li><a href="https://nodejs.org/docs/latest-v5.x/api/crypto.html">5.x</a></li> 134<li><a href="https://nodejs.org/docs/latest-v4.x/api/crypto.html">4.x</a></li> 135<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/crypto.html">0.12.x</a></li> 136<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/crypto.html">0.10.x</a></li></ol> 137 </li> 138 139 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/crypto.md">Edit on GitHub</a></li> 140 </ul> 141 </div> 142 <hr> 143 </header> 144 145 <details id="toc" open><summary>Table of contents</summary><ul> 146<li><span class="stability_2"><a href="#crypto_crypto">Crypto</a></span> 147<ul> 148<li><a href="#crypto_determining_if_crypto_support_is_unavailable">Determining if crypto support is unavailable</a></li> 149<li><a href="#crypto_class_certificate">Class: <code>Certificate</code></a> 150<ul> 151<li><a href="#crypto_certificate_exportchallenge_spkac"><code>Certificate.exportChallenge(spkac)</code></a></li> 152<li><a href="#crypto_certificate_exportpublickey_spkac_encoding"><code>Certificate.exportPublicKey(spkac[, encoding])</code></a></li> 153<li><a href="#crypto_certificate_verifyspkac_spkac"><code>Certificate.verifySpkac(spkac)</code></a></li> 154<li><span class="stability_0"><a href="#crypto_legacy_api">Legacy API</a></span> 155<ul> 156<li><a href="#crypto_new_crypto_certificate"><code>new crypto.Certificate()</code></a></li> 157<li><a href="#crypto_certificate_exportchallenge_spkac_1"><code>certificate.exportChallenge(spkac)</code></a></li> 158<li><a href="#crypto_certificate_exportpublickey_spkac"><code>certificate.exportPublicKey(spkac)</code></a></li> 159<li><a href="#crypto_certificate_verifyspkac_spkac_1"><code>certificate.verifySpkac(spkac)</code></a></li> 160</ul> 161</li> 162</ul> 163</li> 164<li><a href="#crypto_class_cipher">Class: <code>Cipher</code></a> 165<ul> 166<li><a href="#crypto_cipher_final_outputencoding"><code>cipher.final([outputEncoding])</code></a></li> 167<li><a href="#crypto_cipher_getauthtag"><code>cipher.getAuthTag()</code></a></li> 168<li><a href="#crypto_cipher_setaad_buffer_options"><code>cipher.setAAD(buffer[, options])</code></a></li> 169<li><a href="#crypto_cipher_setautopadding_autopadding"><code>cipher.setAutoPadding([autoPadding])</code></a></li> 170<li><a href="#crypto_cipher_update_data_inputencoding_outputencoding"><code>cipher.update(data[, inputEncoding][, outputEncoding])</code></a></li> 171</ul> 172</li> 173<li><a href="#crypto_class_decipher">Class: <code>Decipher</code></a> 174<ul> 175<li><a href="#crypto_decipher_final_outputencoding"><code>decipher.final([outputEncoding])</code></a></li> 176<li><a href="#crypto_decipher_setaad_buffer_options"><code>decipher.setAAD(buffer[, options])</code></a></li> 177<li><a href="#crypto_decipher_setauthtag_buffer"><code>decipher.setAuthTag(buffer)</code></a></li> 178<li><a href="#crypto_decipher_setautopadding_autopadding"><code>decipher.setAutoPadding([autoPadding])</code></a></li> 179<li><a href="#crypto_decipher_update_data_inputencoding_outputencoding"><code>decipher.update(data[, inputEncoding][, outputEncoding])</code></a></li> 180</ul> 181</li> 182<li><a href="#crypto_class_diffiehellman">Class: <code>DiffieHellman</code></a> 183<ul> 184<li><a href="#crypto_diffiehellman_computesecret_otherpublickey_inputencoding_outputencoding"><code>diffieHellman.computeSecret(otherPublicKey[, inputEncoding][, outputEncoding])</code></a></li> 185<li><a href="#crypto_diffiehellman_generatekeys_encoding"><code>diffieHellman.generateKeys([encoding])</code></a></li> 186<li><a href="#crypto_diffiehellman_getgenerator_encoding"><code>diffieHellman.getGenerator([encoding])</code></a></li> 187<li><a href="#crypto_diffiehellman_getprime_encoding"><code>diffieHellman.getPrime([encoding])</code></a></li> 188<li><a href="#crypto_diffiehellman_getprivatekey_encoding"><code>diffieHellman.getPrivateKey([encoding])</code></a></li> 189<li><a href="#crypto_diffiehellman_getpublickey_encoding"><code>diffieHellman.getPublicKey([encoding])</code></a></li> 190<li><a href="#crypto_diffiehellman_setprivatekey_privatekey_encoding"><code>diffieHellman.setPrivateKey(privateKey[, encoding])</code></a></li> 191<li><a href="#crypto_diffiehellman_setpublickey_publickey_encoding"><code>diffieHellman.setPublicKey(publicKey[, encoding])</code></a></li> 192<li><a href="#crypto_diffiehellman_verifyerror"><code>diffieHellman.verifyError</code></a></li> 193</ul> 194</li> 195<li><a href="#crypto_class_diffiehellmangroup">Class: <code>DiffieHellmanGroup</code></a></li> 196<li><a href="#crypto_class_ecdh">Class: <code>ECDH</code></a> 197<ul> 198<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> 199<li><a href="#crypto_ecdh_computesecret_otherpublickey_inputencoding_outputencoding"><code>ecdh.computeSecret(otherPublicKey[, inputEncoding][, outputEncoding])</code></a></li> 200<li><a href="#crypto_ecdh_generatekeys_encoding_format"><code>ecdh.generateKeys([encoding[, format]])</code></a></li> 201<li><a href="#crypto_ecdh_getprivatekey_encoding"><code>ecdh.getPrivateKey([encoding])</code></a></li> 202<li><a href="#crypto_ecdh_getpublickey_encoding_format"><code>ecdh.getPublicKey([encoding][, format])</code></a></li> 203<li><a href="#crypto_ecdh_setprivatekey_privatekey_encoding"><code>ecdh.setPrivateKey(privateKey[, encoding])</code></a></li> 204<li><span class="stability_0"><a href="#crypto_ecdh_setpublickey_publickey_encoding"><code>ecdh.setPublicKey(publicKey[, encoding])</code></a></span></li> 205</ul> 206</li> 207<li><a href="#crypto_class_hash">Class: <code>Hash</code></a> 208<ul> 209<li><a href="#crypto_hash_copy_options"><code>hash.copy([options])</code></a></li> 210<li><a href="#crypto_hash_digest_encoding"><code>hash.digest([encoding])</code></a></li> 211<li><a href="#crypto_hash_update_data_inputencoding"><code>hash.update(data[, inputEncoding])</code></a></li> 212</ul> 213</li> 214<li><a href="#crypto_class_hmac">Class: <code>Hmac</code></a> 215<ul> 216<li><a href="#crypto_hmac_digest_encoding"><code>hmac.digest([encoding])</code></a></li> 217<li><a href="#crypto_hmac_update_data_inputencoding"><code>hmac.update(data[, inputEncoding])</code></a></li> 218</ul> 219</li> 220<li><a href="#crypto_class_keyobject">Class: <code>KeyObject</code></a> 221<ul> 222<li><a href="#crypto_keyobject_asymmetrickeytype"><code>keyObject.asymmetricKeyType</code></a></li> 223<li><a href="#crypto_keyobject_export_options"><code>keyObject.export([options])</code></a></li> 224<li><a href="#crypto_keyobject_symmetrickeysize"><code>keyObject.symmetricKeySize</code></a></li> 225<li><a href="#crypto_keyobject_type"><code>keyObject.type</code></a></li> 226</ul> 227</li> 228<li><a href="#crypto_class_sign">Class: <code>Sign</code></a> 229<ul> 230<li><a href="#crypto_sign_sign_privatekey_outputencoding"><code>sign.sign(privateKey[, outputEncoding])</code></a></li> 231<li><a href="#crypto_sign_update_data_inputencoding"><code>sign.update(data[, inputEncoding])</code></a></li> 232</ul> 233</li> 234<li><a href="#crypto_class_verify">Class: <code>Verify</code></a> 235<ul> 236<li><a href="#crypto_verify_update_data_inputencoding"><code>verify.update(data[, inputEncoding])</code></a></li> 237<li><a href="#crypto_verify_verify_object_signature_signatureencoding"><code>verify.verify(object, signature[, signatureEncoding])</code></a></li> 238</ul> 239</li> 240<li><a href="#crypto_crypto_module_methods_and_properties"><code>crypto</code> module methods and properties</a> 241<ul> 242<li><a href="#crypto_crypto_constants"><code>crypto.constants</code></a></li> 243<li><span class="stability_0"><a href="#crypto_crypto_default_encoding"><code>crypto.DEFAULT_ENCODING</code></a></span></li> 244<li><span class="stability_0"><a href="#crypto_crypto_fips"><code>crypto.fips</code></a></span></li> 245<li><span class="stability_0"><a href="#crypto_crypto_createcipher_algorithm_password_options"><code>crypto.createCipher(algorithm, password[, options])</code></a></span></li> 246<li><a href="#crypto_crypto_createcipheriv_algorithm_key_iv_options"><code>crypto.createCipheriv(algorithm, key, iv[, options])</code></a></li> 247<li><span class="stability_0"><a href="#crypto_crypto_createdecipher_algorithm_password_options"><code>crypto.createDecipher(algorithm, password[, options])</code></a></span></li> 248<li><a href="#crypto_crypto_createdecipheriv_algorithm_key_iv_options"><code>crypto.createDecipheriv(algorithm, key, iv[, options])</code></a></li> 249<li><a href="#crypto_crypto_creatediffiehellman_prime_primeencoding_generator_generatorencoding"><code>crypto.createDiffieHellman(prime[, primeEncoding][, generator][, generatorEncoding])</code></a></li> 250<li><a href="#crypto_crypto_creatediffiehellman_primelength_generator"><code>crypto.createDiffieHellman(primeLength[, generator])</code></a></li> 251<li><a href="#crypto_crypto_creatediffiehellmangroup_name"><code>crypto.createDiffieHellmanGroup(name)</code></a></li> 252<li><a href="#crypto_crypto_createecdh_curvename"><code>crypto.createECDH(curveName)</code></a></li> 253<li><a href="#crypto_crypto_createhash_algorithm_options"><code>crypto.createHash(algorithm[, options])</code></a></li> 254<li><a href="#crypto_crypto_createhmac_algorithm_key_options"><code>crypto.createHmac(algorithm, key[, options])</code></a></li> 255<li><a href="#crypto_crypto_createprivatekey_key"><code>crypto.createPrivateKey(key)</code></a></li> 256<li><a href="#crypto_crypto_createpublickey_key"><code>crypto.createPublicKey(key)</code></a></li> 257<li><a href="#crypto_crypto_createsecretkey_key"><code>crypto.createSecretKey(key)</code></a></li> 258<li><a href="#crypto_crypto_createsign_algorithm_options"><code>crypto.createSign(algorithm[, options])</code></a></li> 259<li><a href="#crypto_crypto_createverify_algorithm_options"><code>crypto.createVerify(algorithm[, options])</code></a></li> 260<li><a href="#crypto_crypto_diffiehellman_options"><code>crypto.diffieHellman(options)</code></a></li> 261<li><a href="#crypto_crypto_generatekeypair_type_options_callback"><code>crypto.generateKeyPair(type, options, callback)</code></a></li> 262<li><a href="#crypto_crypto_generatekeypairsync_type_options"><code>crypto.generateKeyPairSync(type, options)</code></a></li> 263<li><a href="#crypto_crypto_getciphers"><code>crypto.getCiphers()</code></a></li> 264<li><a href="#crypto_crypto_getcurves"><code>crypto.getCurves()</code></a></li> 265<li><a href="#crypto_crypto_getdiffiehellman_groupname"><code>crypto.getDiffieHellman(groupName)</code></a></li> 266<li><a href="#crypto_crypto_getfips"><code>crypto.getFips()</code></a></li> 267<li><a href="#crypto_crypto_gethashes"><code>crypto.getHashes()</code></a></li> 268<li><a href="#crypto_crypto_pbkdf2_password_salt_iterations_keylen_digest_callback"><code>crypto.pbkdf2(password, salt, iterations, keylen, digest, callback)</code></a></li> 269<li><a href="#crypto_crypto_pbkdf2sync_password_salt_iterations_keylen_digest"><code>crypto.pbkdf2Sync(password, salt, iterations, keylen, digest)</code></a></li> 270<li><a href="#crypto_crypto_privatedecrypt_privatekey_buffer"><code>crypto.privateDecrypt(privateKey, buffer)</code></a></li> 271<li><a href="#crypto_crypto_privateencrypt_privatekey_buffer"><code>crypto.privateEncrypt(privateKey, buffer)</code></a></li> 272<li><a href="#crypto_crypto_publicdecrypt_key_buffer"><code>crypto.publicDecrypt(key, buffer)</code></a></li> 273<li><a href="#crypto_crypto_publicencrypt_key_buffer"><code>crypto.publicEncrypt(key, buffer)</code></a></li> 274<li><a href="#crypto_crypto_randombytes_size_callback"><code>crypto.randomBytes(size[, callback])</code></a></li> 275<li><a href="#crypto_crypto_randomfillsync_buffer_offset_size"><code>crypto.randomFillSync(buffer[, offset][, size])</code></a></li> 276<li><a href="#crypto_crypto_randomfill_buffer_offset_size_callback"><code>crypto.randomFill(buffer[, offset][, size], callback)</code></a></li> 277<li><a href="#crypto_crypto_randomint_min_max_callback"><code>crypto.randomInt([min, ]max[, callback])</code></a></li> 278<li><a href="#crypto_crypto_randomuuid_options"><code>crypto.randomUUID([options])</code></a></li> 279<li><a href="#crypto_crypto_scrypt_password_salt_keylen_options_callback"><code>crypto.scrypt(password, salt, keylen[, options], callback)</code></a></li> 280<li><a href="#crypto_crypto_scryptsync_password_salt_keylen_options"><code>crypto.scryptSync(password, salt, keylen[, options])</code></a></li> 281<li><a href="#crypto_crypto_setengine_engine_flags"><code>crypto.setEngine(engine[, flags])</code></a></li> 282<li><a href="#crypto_crypto_setfips_bool"><code>crypto.setFips(bool)</code></a></li> 283<li><a href="#crypto_crypto_sign_algorithm_data_key"><code>crypto.sign(algorithm, data, key)</code></a></li> 284<li><a href="#crypto_crypto_timingsafeequal_a_b"><code>crypto.timingSafeEqual(a, b)</code></a></li> 285<li><a href="#crypto_crypto_verify_algorithm_data_key_signature"><code>crypto.verify(algorithm, data, key, signature)</code></a></li> 286</ul> 287</li> 288<li><a href="#crypto_notes">Notes</a> 289<ul> 290<li><a href="#crypto_legacy_streams_api_prior_to_node_js_0_10">Legacy streams API (prior to Node.js 0.10)</a></li> 291<li><a href="#crypto_recent_ecdh_changes">Recent ECDH changes</a></li> 292<li><a href="#crypto_support_for_weak_or_compromised_algorithms">Support for weak or compromised algorithms</a></li> 293<li><a href="#crypto_ccm_mode">CCM mode</a></li> 294</ul> 295</li> 296<li><a href="#crypto_crypto_constants_1">Crypto constants</a> 297<ul> 298<li><a href="#crypto_openssl_options">OpenSSL options</a></li> 299<li><a href="#crypto_openssl_engine_constants">OpenSSL engine constants</a></li> 300<li><a href="#crypto_other_openssl_constants">Other OpenSSL constants</a></li> 301<li><a href="#crypto_node_js_crypto_constants">Node.js crypto constants</a></li> 302</ul> 303</li> 304</ul> 305</li> 306</ul></details> 307 308 <div id="apicontent"> 309 <h2>Crypto<span><a class="mark" href="#crypto_crypto" id="crypto_crypto">#</a></span></h2> 310 311<p></p><div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p></p> 312<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v14.21.2/lib/crypto.js">lib/crypto.js</a></p> 313<p>The <code>crypto</code> module provides cryptographic functionality that includes a set of 314wrappers for OpenSSL's hash, HMAC, cipher, decipher, sign, and verify functions.</p> 315<p>Use <code>require('crypto')</code> to access this module.</p> 316<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>); 317 318<span class="hljs-keyword">const</span> secret = <span class="hljs-string">'abcdefg'</span>; 319<span class="hljs-keyword">const</span> hash = crypto.<span class="hljs-title function_">createHmac</span>(<span class="hljs-string">'sha256'</span>, secret) 320 .<span class="hljs-title function_">update</span>(<span class="hljs-string">'I love cupcakes'</span>) 321 .<span class="hljs-title function_">digest</span>(<span class="hljs-string">'hex'</span>); 322<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(hash); 323<span class="hljs-comment">// Prints:</span> 324<span class="hljs-comment">// c0fa1bc00531bd78ef38c628449c5102aeabd49b5dc3a2a516ea6ea959d6658e</span></code></pre> 325<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> 326<p>It is possible for Node.js to be built without including support for the 327<code>crypto</code> module. In such cases, calling <code>require('crypto')</code> will result in an 328error being thrown.</p> 329<pre><code class="language-js"><span class="hljs-keyword">let</span> crypto; 330<span class="hljs-keyword">try</span> { 331 crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>); 332} <span class="hljs-keyword">catch</span> (err) { 333 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'crypto support is disabled!'</span>); 334}</code></pre> 335</section><section><h3>Class: <code>Certificate</code><span><a class="mark" href="#crypto_class_certificate" id="crypto_class_certificate">#</a></span></h3> 336<div class="api_metadata"> 337<span>Added in: v0.11.8</span> 338</div> 339<p>SPKAC is a Certificate Signing Request mechanism originally implemented by 340Netscape 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> 341<p><code><keygen></code> is deprecated since <a href="https://www.w3.org/TR/html52/changes.html#features-removed">HTML 5.2</a> and new projects 342should not use this element anymore.</p> 343<p>The <code>crypto</code> module provides the <code>Certificate</code> class for working with SPKAC 344data. The most common usage is handling output generated by the HTML5 345<code><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> 346<h4><code>Certificate.exportChallenge(spkac)</code><span><a class="mark" href="#crypto_certificate_exportchallenge_spkac" id="crypto_certificate_exportchallenge_spkac">#</a></span></h4> 347<div class="api_metadata"> 348<span>Added in: v9.0.0</span> 349</div> 350<ul> 351<li><code>spkac</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 352<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> The challenge component of the <code>spkac</code> data structure, which 353includes a public key and a challenge.</li> 354</ul> 355<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>); 356<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>(); 357<span class="hljs-keyword">const</span> challenge = <span class="hljs-title class_">Certificate</span>.<span class="hljs-title function_">exportChallenge</span>(spkac); 358<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>)); 359<span class="hljs-comment">// Prints: the challenge as a UTF8 string</span></code></pre> 360<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> 361<div class="api_metadata"> 362<span>Added in: v9.0.0</span> 363</div> 364<ul> 365<li><code>spkac</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 366<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the <code>spkac</code> string.</li> 367<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> The public key component of the <code>spkac</code> data structure, 368which includes a public key and a challenge.</li> 369</ul> 370<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>); 371<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>(); 372<span class="hljs-keyword">const</span> publicKey = <span class="hljs-title class_">Certificate</span>.<span class="hljs-title function_">exportPublicKey</span>(spkac); 373<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(publicKey); 374<span class="hljs-comment">// Prints: the public key as <Buffer ...></span></code></pre> 375<h4><code>Certificate.verifySpkac(spkac)</code><span><a class="mark" href="#crypto_certificate_verifyspkac_spkac" id="crypto_certificate_verifyspkac_spkac">#</a></span></h4> 376<div class="api_metadata"> 377<span>Added in: v9.0.0</span> 378</div> 379<ul> 380<li><code>spkac</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 381<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> if the given <code>spkac</code> data structure is valid, 382<code>false</code> otherwise.</li> 383</ul> 384<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>); 385<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>(); 386<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))); 387<span class="hljs-comment">// Prints: true or false</span></code></pre> 388<h4>Legacy API<span><a class="mark" href="#crypto_legacy_api" id="crypto_legacy_api">#</a></span></h4> 389<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated</div><p></p> 390<p>As a legacy interface, it is possible to create new instances of 391the <code>crypto.Certificate</code> class as illustrated in the examples below.</p> 392<h5><code>new crypto.Certificate()</code><span><a class="mark" href="#crypto_new_crypto_certificate" id="crypto_new_crypto_certificate">#</a></span></h5> 393<p>Instances of the <code>Certificate</code> class can be created using the <code>new</code> keyword 394or by calling <code>crypto.Certificate()</code> as a function:</p> 395<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>); 396 397<span class="hljs-keyword">const</span> cert1 = <span class="hljs-keyword">new</span> crypto.<span class="hljs-title class_">Certificate</span>(); 398<span class="hljs-keyword">const</span> cert2 = crypto.<span class="hljs-title class_">Certificate</span>();</code></pre> 399<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> 400<div class="api_metadata"> 401<span>Added in: v0.11.8</span> 402</div> 403<ul> 404<li><code>spkac</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 405<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> The challenge component of the <code>spkac</code> data structure, which 406includes a public key and a challenge.</li> 407</ul> 408<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>(); 409<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>(); 410<span class="hljs-keyword">const</span> challenge = cert.<span class="hljs-title function_">exportChallenge</span>(spkac); 411<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>)); 412<span class="hljs-comment">// Prints: the challenge as a UTF8 string</span></code></pre> 413<h5><code>certificate.exportPublicKey(spkac)</code><span><a class="mark" href="#crypto_certificate_exportpublickey_spkac" id="crypto_certificate_exportpublickey_spkac">#</a></span></h5> 414<div class="api_metadata"> 415<span>Added in: v0.11.8</span> 416</div> 417<ul> 418<li><code>spkac</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 419<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> The public key component of the <code>spkac</code> data structure, 420which includes a public key and a challenge.</li> 421</ul> 422<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>(); 423<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>(); 424<span class="hljs-keyword">const</span> publicKey = cert.<span class="hljs-title function_">exportPublicKey</span>(spkac); 425<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(publicKey); 426<span class="hljs-comment">// Prints: the public key as <Buffer ...></span></code></pre> 427<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> 428<div class="api_metadata"> 429<span>Added in: v0.11.8</span> 430</div> 431<ul> 432<li><code>spkac</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 433<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> if the given <code>spkac</code> data structure is valid, 434<code>false</code> otherwise.</li> 435</ul> 436<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>(); 437<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>(); 438<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))); 439<span class="hljs-comment">// Prints: true or false</span></code></pre> 440</section><section><h3>Class: <code>Cipher</code><span><a class="mark" href="#crypto_class_cipher" id="crypto_class_cipher">#</a></span></h3> 441<div class="api_metadata"> 442<span>Added in: v0.1.94</span> 443</div> 444<ul> 445<li>Extends: <a href="stream.html#stream_class_stream_transform" class="type"><stream.Transform></a></li> 446</ul> 447<p>Instances of the <code>Cipher</code> class are used to encrypt data. The class can be 448used in one of two ways:</p> 449<ul> 450<li>As a <a href="stream.html">stream</a> that is both readable and writable, where plain unencrypted 451data is written to produce encrypted data on the readable side, or</li> 452<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 453the encrypted data.</li> 454</ul> 455<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 456used to create <code>Cipher</code> instances. <code>Cipher</code> objects are not to be created 457directly using the <code>new</code> keyword.</p> 458<p>Example: Using <code>Cipher</code> objects as streams:</p> 459<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>); 460 461<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>; 462<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>; 463<span class="hljs-comment">// Key length is dependent on the algorithm. In this case for aes192, it is</span> 464<span class="hljs-comment">// 24 bytes (192 bits).</span> 465<span class="hljs-comment">// Use async `crypto.scrypt()` instead.</span> 466<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>); 467<span class="hljs-comment">// Use `crypto.randomBytes()` to generate a random iv instead of the static iv</span> 468<span class="hljs-comment">// shown here.</span> 469<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> 470 471<span class="hljs-keyword">const</span> cipher = crypto.<span class="hljs-title function_">createCipheriv</span>(algorithm, key, iv); 472 473<span class="hljs-keyword">let</span> encrypted = <span class="hljs-string">''</span>; 474cipher.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> { 475 <span class="hljs-keyword">let</span> chunk; 476 <span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (chunk = cipher.<span class="hljs-title function_">read</span>())) { 477 encrypted += chunk.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>); 478 } 479}); 480cipher.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> { 481 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(encrypted); 482 <span class="hljs-comment">// Prints: e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa</span> 483}); 484 485cipher.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some clear text data'</span>); 486cipher.<span class="hljs-title function_">end</span>();</code></pre> 487<p>Example: Using <code>Cipher</code> and piped streams:</p> 488<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>); 489<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>); 490 491<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>; 492<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>; 493<span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span> 494<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>); 495<span class="hljs-comment">// Use `crypto.randomBytes()` to generate a random iv instead of the static iv</span> 496<span class="hljs-comment">// shown here.</span> 497<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> 498 499<span class="hljs-keyword">const</span> cipher = crypto.<span class="hljs-title function_">createCipheriv</span>(algorithm, key, iv); 500 501<span class="hljs-keyword">const</span> input = fs.<span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'test.js'</span>); 502<span class="hljs-keyword">const</span> output = fs.<span class="hljs-title function_">createWriteStream</span>(<span class="hljs-string">'test.enc'</span>); 503 504input.<span class="hljs-title function_">pipe</span>(cipher).<span class="hljs-title function_">pipe</span>(output);</code></pre> 505<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> 506<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>); 507 508<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>; 509<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>; 510<span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span> 511<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>); 512<span class="hljs-comment">// Use `crypto.randomBytes` to generate a random iv instead of the static iv</span> 513<span class="hljs-comment">// shown here.</span> 514<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> 515 516<span class="hljs-keyword">const</span> cipher = crypto.<span class="hljs-title function_">createCipheriv</span>(algorithm, key, iv); 517 518<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>); 519encrypted += cipher.<span class="hljs-title function_">final</span>(<span class="hljs-string">'hex'</span>); 520<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(encrypted); 521<span class="hljs-comment">// Prints: e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa</span></code></pre> 522<h4><code>cipher.final([outputEncoding])</code><span><a class="mark" href="#crypto_cipher_final_outputencoding" id="crypto_cipher_final_outputencoding">#</a></span></h4> 523<div class="api_metadata"> 524<span>Added in: v0.1.94</span> 525</div> 526<ul> 527<li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li> 528<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Any remaining enciphered contents. 529If <code>outputEncoding</code> is specified, a string is 530returned. If an <code>outputEncoding</code> is not provided, a <a href="buffer.html"><code>Buffer</code></a> is returned.</li> 531</ul> 532<p>Once the <code>cipher.final()</code> method has been called, the <code>Cipher</code> object can no 533longer be used to encrypt data. Attempts to call <code>cipher.final()</code> more than 534once will result in an error being thrown.</p> 535<h4><code>cipher.getAuthTag()</code><span><a class="mark" href="#crypto_cipher_getauthtag" id="crypto_cipher_getauthtag">#</a></span></h4> 536<div class="api_metadata"> 537<span>Added in: v1.0.0</span> 538</div> 539<ul> 540<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> When using an authenticated encryption mode (<code>GCM</code>, <code>CCM</code> 541and <code>OCB</code> are currently supported), the <code>cipher.getAuthTag()</code> method returns a 542<a href="buffer.html"><code>Buffer</code></a> containing the <em>authentication tag</em> that has been computed from 543the given data.</li> 544</ul> 545<p>The <code>cipher.getAuthTag()</code> method should only be called after encryption has 546been completed using the <a href="#crypto_cipher_final_outputencoding"><code>cipher.final()</code></a> method.</p> 547<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> 548<div class="api_metadata"> 549<span>Added in: v1.0.0</span> 550</div> 551<ul> 552<li><code>buffer</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 553<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <a href="stream.html#stream_new_stream_transform_options"><code>stream.transform</code> options</a> 554<ul> 555<li><code>plaintextLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 556</ul> 557</li> 558<li>Returns: <a href="crypto.html#crypto_class_cipher" class="type"><Cipher></a> for method chaining.</li> 559</ul> 560<p>When using an authenticated encryption mode (<code>GCM</code>, <code>CCM</code> and <code>OCB</code> are 561currently supported), the <code>cipher.setAAD()</code> method sets the value used for the 562<em>additional authenticated data</em> (AAD) input parameter.</p> 563<p>The <code>options</code> argument is optional for <code>GCM</code> and <code>OCB</code>. When using <code>CCM</code>, the 564<code>plaintextLength</code> option must be specified and its value must match the length 565of the plaintext in bytes. See <a href="#crypto_ccm_mode">CCM mode</a>.</p> 566<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> 567<h4><code>cipher.setAutoPadding([autoPadding])</code><span><a class="mark" href="#crypto_cipher_setautopadding_autopadding" id="crypto_cipher_setautopadding_autopadding">#</a></span></h4> 568<div class="api_metadata"> 569<span>Added in: v0.7.1</span> 570</div> 571<ul> 572<li><code>autoPadding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>true</code></li> 573<li>Returns: <a href="crypto.html#crypto_class_cipher" class="type"><Cipher></a> for method chaining.</li> 574</ul> 575<p>When using block encryption algorithms, the <code>Cipher</code> class will automatically 576add padding to the input data to the appropriate block size. To disable the 577default padding call <code>cipher.setAutoPadding(false)</code>.</p> 578<p>When <code>autoPadding</code> is <code>false</code>, the length of the entire input data must be a 579multiple of the cipher's block size or <a href="#crypto_cipher_final_outputencoding"><code>cipher.final()</code></a> will throw an error. 580Disabling automatic padding is useful for non-standard padding, for instance 581using <code>0x0</code> instead of PKCS padding.</p> 582<p>The <code>cipher.setAutoPadding()</code> method must be called before 583<a href="#crypto_cipher_final_outputencoding"><code>cipher.final()</code></a>.</p> 584<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> 585<div class="api_metadata"> 586<details class="changelog"><summary>History</summary> 587<table> 588<tbody><tr><th>Version</th><th>Changes</th></tr> 589<tr><td>v6.0.0</td> 590<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p></td></tr> 591<tr><td>v0.1.94</td> 592<td><p><span>Added in: v0.1.94</span></p></td></tr> 593</tbody></table> 594</details> 595</div> 596<ul> 597<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 598<li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the data.</li> 599<li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li> 600<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 601</ul> 602<p>Updates the cipher with <code>data</code>. If the <code>inputEncoding</code> argument is given, 603the <code>data</code> 604argument is a string using the specified encoding. If the <code>inputEncoding</code> 605argument is not given, <code>data</code> must be a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or 606<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 607<code>inputEncoding</code> is ignored.</p> 608<p>The <code>outputEncoding</code> specifies the output format of the enciphered 609data. If the <code>outputEncoding</code> 610is specified, a string using the specified encoding is returned. If no 611<code>outputEncoding</code> is provided, a <a href="buffer.html"><code>Buffer</code></a> is returned.</p> 612<p>The <code>cipher.update()</code> method can be called multiple times with new data until 613<a href="#crypto_cipher_final_outputencoding"><code>cipher.final()</code></a> is called. Calling <code>cipher.update()</code> after 614<a href="#crypto_cipher_final_outputencoding"><code>cipher.final()</code></a> will result in an error being thrown.</p> 615</section><section><h3>Class: <code>Decipher</code><span><a class="mark" href="#crypto_class_decipher" id="crypto_class_decipher">#</a></span></h3> 616<div class="api_metadata"> 617<span>Added in: v0.1.94</span> 618</div> 619<ul> 620<li>Extends: <a href="stream.html#stream_class_stream_transform" class="type"><stream.Transform></a></li> 621</ul> 622<p>Instances of the <code>Decipher</code> class are used to decrypt data. The class can be 623used in one of two ways:</p> 624<ul> 625<li>As a <a href="stream.html">stream</a> that is both readable and writable, where plain encrypted 626data is written to produce unencrypted data on the readable side, or</li> 627<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 628produce the unencrypted data.</li> 629</ul> 630<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 631used to create <code>Decipher</code> instances. <code>Decipher</code> objects are not to be created 632directly using the <code>new</code> keyword.</p> 633<p>Example: Using <code>Decipher</code> objects as streams:</p> 634<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>); 635 636<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>; 637<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>; 638<span class="hljs-comment">// Key length is dependent on the algorithm. In this case for aes192, it is</span> 639<span class="hljs-comment">// 24 bytes (192 bits).</span> 640<span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span> 641<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>); 642<span class="hljs-comment">// The IV is usually passed along with the ciphertext.</span> 643<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> 644 645<span class="hljs-keyword">const</span> decipher = crypto.<span class="hljs-title function_">createDecipheriv</span>(algorithm, key, iv); 646 647<span class="hljs-keyword">let</span> decrypted = <span class="hljs-string">''</span>; 648decipher.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> { 649 <span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (chunk = decipher.<span class="hljs-title function_">read</span>())) { 650 decrypted += chunk.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'utf8'</span>); 651 } 652}); 653decipher.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> { 654 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(decrypted); 655 <span class="hljs-comment">// Prints: some clear text data</span> 656}); 657 658<span class="hljs-comment">// Encrypted with same algorithm, key and iv.</span> 659<span class="hljs-keyword">const</span> encrypted = 660 <span class="hljs-string">'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa'</span>; 661decipher.<span class="hljs-title function_">write</span>(encrypted, <span class="hljs-string">'hex'</span>); 662decipher.<span class="hljs-title function_">end</span>();</code></pre> 663<p>Example: Using <code>Decipher</code> and piped streams:</p> 664<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>); 665<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>); 666 667<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>; 668<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>; 669<span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span> 670<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>); 671<span class="hljs-comment">// The IV is usually passed along with the ciphertext.</span> 672<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> 673 674<span class="hljs-keyword">const</span> decipher = crypto.<span class="hljs-title function_">createDecipheriv</span>(algorithm, key, iv); 675 676<span class="hljs-keyword">const</span> input = fs.<span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'test.enc'</span>); 677<span class="hljs-keyword">const</span> output = fs.<span class="hljs-title function_">createWriteStream</span>(<span class="hljs-string">'test.js'</span>); 678 679input.<span class="hljs-title function_">pipe</span>(decipher).<span class="hljs-title function_">pipe</span>(output);</code></pre> 680<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> 681<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>); 682 683<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>; 684<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>; 685<span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span> 686<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>); 687<span class="hljs-comment">// The IV is usually passed along with the ciphertext.</span> 688<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> 689 690<span class="hljs-keyword">const</span> decipher = crypto.<span class="hljs-title function_">createDecipheriv</span>(algorithm, key, iv); 691 692<span class="hljs-comment">// Encrypted using same algorithm, key and iv.</span> 693<span class="hljs-keyword">const</span> encrypted = 694 <span class="hljs-string">'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa'</span>; 695<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>); 696decrypted += decipher.<span class="hljs-title function_">final</span>(<span class="hljs-string">'utf8'</span>); 697<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(decrypted); 698<span class="hljs-comment">// Prints: some clear text data</span></code></pre> 699<h4><code>decipher.final([outputEncoding])</code><span><a class="mark" href="#crypto_decipher_final_outputencoding" id="crypto_decipher_final_outputencoding">#</a></span></h4> 700<div class="api_metadata"> 701<span>Added in: v0.1.94</span> 702</div> 703<ul> 704<li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li> 705<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Any remaining deciphered contents. 706If <code>outputEncoding</code> is specified, a string is 707returned. If an <code>outputEncoding</code> is not provided, a <a href="buffer.html"><code>Buffer</code></a> is returned.</li> 708</ul> 709<p>Once the <code>decipher.final()</code> method has been called, the <code>Decipher</code> object can 710no longer be used to decrypt data. Attempts to call <code>decipher.final()</code> more 711than once will result in an error being thrown.</p> 712<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> 713<div class="api_metadata"> 714<details class="changelog"><summary>History</summary> 715<table> 716<tbody><tr><th>Version</th><th>Changes</th></tr> 717<tr><td>v7.2.0</td> 718<td><p>This method now returns a reference to <code>decipher</code>.</p></td></tr> 719<tr><td>v1.0.0</td> 720<td><p><span>Added in: v1.0.0</span></p></td></tr> 721</tbody></table> 722</details> 723</div> 724<ul> 725<li><code>buffer</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 726<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <a href="stream.html#stream_new_stream_transform_options"><code>stream.transform</code> options</a> 727<ul> 728<li><code>plaintextLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 729</ul> 730</li> 731<li>Returns: <a href="crypto.html#crypto_class_decipher" class="type"><Decipher></a> for method chaining.</li> 732</ul> 733<p>When using an authenticated encryption mode (<code>GCM</code>, <code>CCM</code> and <code>OCB</code> are 734currently supported), the <code>decipher.setAAD()</code> method sets the value used for the 735<em>additional authenticated data</em> (AAD) input parameter.</p> 736<p>The <code>options</code> argument is optional for <code>GCM</code>. When using <code>CCM</code>, the 737<code>plaintextLength</code> option must be specified and its value must match the length 738of the ciphertext in bytes. See <a href="#crypto_ccm_mode">CCM mode</a>.</p> 739<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> 740<h4><code>decipher.setAuthTag(buffer)</code><span><a class="mark" href="#crypto_decipher_setauthtag_buffer" id="crypto_decipher_setauthtag_buffer">#</a></span></h4> 741<div class="api_metadata"> 742<details class="changelog"><summary>History</summary> 743<table> 744<tbody><tr><th>Version</th><th>Changes</th></tr> 745<tr><td>v11.0.0</td> 746<td><p>This method now throws if the GCM tag length is invalid.</p></td></tr> 747<tr><td>v7.2.0</td> 748<td><p>This method now returns a reference to <code>decipher</code>.</p></td></tr> 749<tr><td>v1.0.0</td> 750<td><p><span>Added in: v1.0.0</span></p></td></tr> 751</tbody></table> 752</details> 753</div> 754<ul> 755<li><code>buffer</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 756<li>Returns: <a href="crypto.html#crypto_class_decipher" class="type"><Decipher></a> for method chaining.</li> 757</ul> 758<p>When using an authenticated encryption mode (<code>GCM</code>, <code>CCM</code> and <code>OCB</code> are 759currently supported), the <code>decipher.setAuthTag()</code> method is used to pass in the 760received <em>authentication tag</em>. If no tag is provided, or if the cipher text 761has been tampered with, <a href="#crypto_decipher_final_outputencoding"><code>decipher.final()</code></a> will throw, indicating that the 762cipher text should be discarded due to failed authentication. If the tag length 763is 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 764<code>authTagLength</code> option, <code>decipher.setAuthTag()</code> will throw an error.</p> 765<p>The <code>decipher.setAuthTag()</code> method must be called before <a href="#crypto_decipher_update_data_inputencoding_outputencoding"><code>decipher.update()</code></a> 766for <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. 767<code>decipher.setAuthTag()</code> can only be called once.</p> 768<h4><code>decipher.setAutoPadding([autoPadding])</code><span><a class="mark" href="#crypto_decipher_setautopadding_autopadding" id="crypto_decipher_setautopadding_autopadding">#</a></span></h4> 769<div class="api_metadata"> 770<span>Added in: v0.7.1</span> 771</div> 772<ul> 773<li><code>autoPadding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>true</code></li> 774<li>Returns: <a href="crypto.html#crypto_class_decipher" class="type"><Decipher></a> for method chaining.</li> 775</ul> 776<p>When data has been encrypted without standard block padding, calling 777<code>decipher.setAutoPadding(false)</code> will disable automatic padding to prevent 778<a href="#crypto_decipher_final_outputencoding"><code>decipher.final()</code></a> from checking for and removing padding.</p> 779<p>Turning auto padding off will only work if the input data's length is a 780multiple of the ciphers block size.</p> 781<p>The <code>decipher.setAutoPadding()</code> method must be called before 782<a href="#crypto_decipher_final_outputencoding"><code>decipher.final()</code></a>.</p> 783<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> 784<div class="api_metadata"> 785<details class="changelog"><summary>History</summary> 786<table> 787<tbody><tr><th>Version</th><th>Changes</th></tr> 788<tr><td>v6.0.0</td> 789<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p></td></tr> 790<tr><td>v0.1.94</td> 791<td><p><span>Added in: v0.1.94</span></p></td></tr> 792</tbody></table> 793</details> 794</div> 795<ul> 796<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 797<li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the <code>data</code> string.</li> 798<li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li> 799<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 800</ul> 801<p>Updates the decipher with <code>data</code>. If the <code>inputEncoding</code> argument is given, 802the <code>data</code> 803argument is a string using the specified encoding. If the <code>inputEncoding</code> 804argument is not given, <code>data</code> must be a <a href="buffer.html"><code>Buffer</code></a>. If <code>data</code> is a 805<a href="buffer.html"><code>Buffer</code></a> then <code>inputEncoding</code> is ignored.</p> 806<p>The <code>outputEncoding</code> specifies the output format of the enciphered 807data. If the <code>outputEncoding</code> 808is specified, a string using the specified encoding is returned. If no 809<code>outputEncoding</code> is provided, a <a href="buffer.html"><code>Buffer</code></a> is returned.</p> 810<p>The <code>decipher.update()</code> method can be called multiple times with new data until 811<a href="#crypto_decipher_final_outputencoding"><code>decipher.final()</code></a> is called. Calling <code>decipher.update()</code> after 812<a href="#crypto_decipher_final_outputencoding"><code>decipher.final()</code></a> will result in an error being thrown.</p> 813</section><section><h3>Class: <code>DiffieHellman</code><span><a class="mark" href="#crypto_class_diffiehellman" id="crypto_class_diffiehellman">#</a></span></h3> 814<div class="api_metadata"> 815<span>Added in: v0.5.0</span> 816</div> 817<p>The <code>DiffieHellman</code> class is a utility for creating Diffie-Hellman key 818exchanges.</p> 819<p>Instances of the <code>DiffieHellman</code> class can be created using the 820<a href="#crypto_crypto_creatediffiehellman_prime_primeencoding_generator_generatorencoding"><code>crypto.createDiffieHellman()</code></a> function.</p> 821<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>); 822<span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>); 823 824<span class="hljs-comment">// Generate Alice's keys...</span> 825<span class="hljs-keyword">const</span> alice = crypto.<span class="hljs-title function_">createDiffieHellman</span>(<span class="hljs-number">2048</span>); 826<span class="hljs-keyword">const</span> aliceKey = alice.<span class="hljs-title function_">generateKeys</span>(); 827 828<span class="hljs-comment">// Generate Bob's keys...</span> 829<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>()); 830<span class="hljs-keyword">const</span> bobKey = bob.<span class="hljs-title function_">generateKeys</span>(); 831 832<span class="hljs-comment">// Exchange and generate the secret...</span> 833<span class="hljs-keyword">const</span> aliceSecret = alice.<span class="hljs-title function_">computeSecret</span>(bobKey); 834<span class="hljs-keyword">const</span> bobSecret = bob.<span class="hljs-title function_">computeSecret</span>(aliceKey); 835 836<span class="hljs-comment">// OK</span> 837assert.<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> 838<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> 839<div class="api_metadata"> 840<span>Added in: v0.5.0</span> 841</div> 842<ul> 843<li><code>otherPublicKey</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 844<li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of an <code>otherPublicKey</code> string.</li> 845<li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li> 846<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 847</ul> 848<p>Computes the shared secret using <code>otherPublicKey</code> as the other 849party's public key and returns the computed shared secret. The supplied 850key is interpreted using the specified <code>inputEncoding</code>, and secret is 851encoded using specified <code>outputEncoding</code>. 852If the <code>inputEncoding</code> is not 853provided, <code>otherPublicKey</code> is expected to be a <a href="buffer.html"><code>Buffer</code></a>, 854<code>TypedArray</code>, or <code>DataView</code>.</p> 855<p>If <code>outputEncoding</code> is given a string is returned; otherwise, a 856<a href="buffer.html"><code>Buffer</code></a> is returned.</p> 857<h4><code>diffieHellman.generateKeys([encoding])</code><span><a class="mark" href="#crypto_diffiehellman_generatekeys_encoding" id="crypto_diffiehellman_generatekeys_encoding">#</a></span></h4> 858<div class="api_metadata"> 859<span>Added in: v0.5.0</span> 860</div> 861<ul> 862<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li> 863<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 864</ul> 865<p>Generates private and public Diffie-Hellman key values, and returns 866the public key in the specified <code>encoding</code>. This key should be 867transferred to the other party. 868If <code>encoding</code> is provided a string is returned; otherwise a 869<a href="buffer.html"><code>Buffer</code></a> is returned.</p> 870<h4><code>diffieHellman.getGenerator([encoding])</code><span><a class="mark" href="#crypto_diffiehellman_getgenerator_encoding" id="crypto_diffiehellman_getgenerator_encoding">#</a></span></h4> 871<div class="api_metadata"> 872<span>Added in: v0.5.0</span> 873</div> 874<ul> 875<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li> 876<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 877</ul> 878<p>Returns the Diffie-Hellman generator in the specified <code>encoding</code>. 879If <code>encoding</code> is provided a string is 880returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is returned.</p> 881<h4><code>diffieHellman.getPrime([encoding])</code><span><a class="mark" href="#crypto_diffiehellman_getprime_encoding" id="crypto_diffiehellman_getprime_encoding">#</a></span></h4> 882<div class="api_metadata"> 883<span>Added in: v0.5.0</span> 884</div> 885<ul> 886<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li> 887<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 888</ul> 889<p>Returns the Diffie-Hellman prime in the specified <code>encoding</code>. 890If <code>encoding</code> is provided a string is 891returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is returned.</p> 892<h4><code>diffieHellman.getPrivateKey([encoding])</code><span><a class="mark" href="#crypto_diffiehellman_getprivatekey_encoding" id="crypto_diffiehellman_getprivatekey_encoding">#</a></span></h4> 893<div class="api_metadata"> 894<span>Added in: v0.5.0</span> 895</div> 896<ul> 897<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li> 898<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 899</ul> 900<p>Returns the Diffie-Hellman private key in the specified <code>encoding</code>. 901If <code>encoding</code> is provided a 902string is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is returned.</p> 903<h4><code>diffieHellman.getPublicKey([encoding])</code><span><a class="mark" href="#crypto_diffiehellman_getpublickey_encoding" id="crypto_diffiehellman_getpublickey_encoding">#</a></span></h4> 904<div class="api_metadata"> 905<span>Added in: v0.5.0</span> 906</div> 907<ul> 908<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li> 909<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 910</ul> 911<p>Returns the Diffie-Hellman public key in the specified <code>encoding</code>. 912If <code>encoding</code> is provided a 913string is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is returned.</p> 914<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> 915<div class="api_metadata"> 916<span>Added in: v0.5.0</span> 917</div> 918<ul> 919<li><code>privateKey</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 920<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the <code>privateKey</code> string.</li> 921</ul> 922<p>Sets the Diffie-Hellman private key. If the <code>encoding</code> argument is provided, 923<code>privateKey</code> is expected 924to be a string. If no <code>encoding</code> is provided, <code>privateKey</code> is expected 925to be a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or <code>DataView</code>.</p> 926<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> 927<div class="api_metadata"> 928<span>Added in: v0.5.0</span> 929</div> 930<ul> 931<li><code>publicKey</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 932<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the <code>publicKey</code> string.</li> 933</ul> 934<p>Sets the Diffie-Hellman public key. If the <code>encoding</code> argument is provided, 935<code>publicKey</code> is expected 936to be a string. If no <code>encoding</code> is provided, <code>publicKey</code> is expected 937to be a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or <code>DataView</code>.</p> 938<h4><code>diffieHellman.verifyError</code><span><a class="mark" href="#crypto_diffiehellman_verifyerror" id="crypto_diffiehellman_verifyerror">#</a></span></h4> 939<div class="api_metadata"> 940<span>Added in: v0.11.12</span> 941</div> 942<p>A bit field containing any warnings and/or errors resulting from a check 943performed during initialization of the <code>DiffieHellman</code> object.</p> 944<p>The following values are valid for this property (as defined in <code>constants</code> 945module):</p> 946<ul> 947<li><code>DH_CHECK_P_NOT_SAFE_PRIME</code></li> 948<li><code>DH_CHECK_P_NOT_PRIME</code></li> 949<li><code>DH_UNABLE_TO_CHECK_GENERATOR</code></li> 950<li><code>DH_NOT_SUITABLE_GENERATOR</code></li> 951</ul> 952</section><section><h3>Class: <code>DiffieHellmanGroup</code><span><a class="mark" href="#crypto_class_diffiehellmangroup" id="crypto_class_diffiehellmangroup">#</a></span></h3> 953<div class="api_metadata"> 954<span>Added in: v0.7.5</span> 955</div> 956<p>The <code>DiffieHellmanGroup</code> class takes a well-known modp group as its argument. 957It works the same as <code>DiffieHellman</code>, except that it does not allow changing 958its keys after creation. In other words, it does not implement <code>setPublicKey()</code> 959or <code>setPrivateKey()</code> methods.</p> 960<pre><code class="language-js"><span class="hljs-keyword">const</span> name = <span class="hljs-string">'modp1'</span>; 961<span class="hljs-keyword">const</span> dh = crypto.<span class="hljs-title function_">createDiffieHellmanGroup</span>(name);</code></pre> 962<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> 963<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> 964modp1 # 768 bits 965modp2 # 1024 bits 966modp5 # 1536 bits 967modp14 # 2048 bits 968modp15 # etc. 969modp16 970modp17 971modp18</code></pre> 972</section><section><h3>Class: <code>ECDH</code><span><a class="mark" href="#crypto_class_ecdh" id="crypto_class_ecdh">#</a></span></h3> 973<div class="api_metadata"> 974<span>Added in: v0.11.14</span> 975</div> 976<p>The <code>ECDH</code> class is a utility for creating Elliptic Curve Diffie-Hellman (ECDH) 977key exchanges.</p> 978<p>Instances of the <code>ECDH</code> class can be created using the 979<a href="#crypto_crypto_createecdh_curvename"><code>crypto.createECDH()</code></a> function.</p> 980<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>); 981<span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>); 982 983<span class="hljs-comment">// Generate Alice's keys...</span> 984<span class="hljs-keyword">const</span> alice = crypto.<span class="hljs-title function_">createECDH</span>(<span class="hljs-string">'secp521r1'</span>); 985<span class="hljs-keyword">const</span> aliceKey = alice.<span class="hljs-title function_">generateKeys</span>(); 986 987<span class="hljs-comment">// Generate Bob's keys...</span> 988<span class="hljs-keyword">const</span> bob = crypto.<span class="hljs-title function_">createECDH</span>(<span class="hljs-string">'secp521r1'</span>); 989<span class="hljs-keyword">const</span> bobKey = bob.<span class="hljs-title function_">generateKeys</span>(); 990 991<span class="hljs-comment">// Exchange and generate the secret...</span> 992<span class="hljs-keyword">const</span> aliceSecret = alice.<span class="hljs-title function_">computeSecret</span>(bobKey); 993<span class="hljs-keyword">const</span> bobSecret = bob.<span class="hljs-title function_">computeSecret</span>(aliceKey); 994 995assert.<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>)); 996<span class="hljs-comment">// OK</span></code></pre> 997<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> 998<div class="api_metadata"> 999<span>Added in: v10.0.0</span> 1000</div> 1001<ul> 1002<li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 1003<li><code>curve</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1004<li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the <code>key</code> string.</li> 1005<li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li> 1006<li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'uncompressed'</code></li> 1007<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1008</ul> 1009<p>Converts the EC Diffie-Hellman public key specified by <code>key</code> and <code>curve</code> to the 1010format specified by <code>format</code>. The <code>format</code> argument specifies point encoding 1011and can be <code>'compressed'</code>, <code>'uncompressed'</code> or <code>'hybrid'</code>. The supplied key is 1012interpreted using the specified <code>inputEncoding</code>, and the returned key is encoded 1013using the specified <code>outputEncoding</code>.</p> 1014<p>Use <a href="#crypto_crypto_getcurves"><code>crypto.getCurves()</code></a> to obtain a list of available curve names. 1015On recent OpenSSL releases, <code>openssl ecparam -list_curves</code> will also display 1016the name and description of each available elliptic curve.</p> 1017<p>If <code>format</code> is not specified the point will be returned in <code>'uncompressed'</code> 1018format.</p> 1019<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>, 1020<code>TypedArray</code>, or <code>DataView</code>.</p> 1021<p>Example (uncompressing a key):</p> 1022<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>); 1023 1024<span class="hljs-keyword">const</span> ecdh = <span class="hljs-title function_">createECDH</span>(<span class="hljs-string">'secp256k1'</span>); 1025ecdh.<span class="hljs-title function_">generateKeys</span>(); 1026 1027<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>); 1028 1029<span class="hljs-keyword">const</span> uncompressedKey = <span class="hljs-variable constant_">ECDH</span>.<span class="hljs-title function_">convertKey</span>(compressedKey, 1030 <span class="hljs-string">'secp256k1'</span>, 1031 <span class="hljs-string">'hex'</span>, 1032 <span class="hljs-string">'hex'</span>, 1033 <span class="hljs-string">'uncompressed'</span>); 1034 1035<span class="hljs-comment">// The converted key and the uncompressed public key should be the same</span> 1036<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> 1037<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> 1038<div class="api_metadata"> 1039<details class="changelog"><summary>History</summary> 1040<table> 1041<tbody><tr><th>Version</th><th>Changes</th></tr> 1042<tr><td>v10.0.0</td> 1043<td><p>Changed error format to better support invalid public key error.</p></td></tr> 1044<tr><td>v6.0.0</td> 1045<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p></td></tr> 1046<tr><td>v0.11.14</td> 1047<td><p><span>Added in: v0.11.14</span></p></td></tr> 1048</tbody></table> 1049</details> 1050</div> 1051<ul> 1052<li><code>otherPublicKey</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 1053<li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the <code>otherPublicKey</code> string.</li> 1054<li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li> 1055<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1056</ul> 1057<p>Computes the shared secret using <code>otherPublicKey</code> as the other 1058party's public key and returns the computed shared secret. The supplied 1059key is interpreted using specified <code>inputEncoding</code>, and the returned secret 1060is encoded using the specified <code>outputEncoding</code>. 1061If the <code>inputEncoding</code> is not 1062provided, <code>otherPublicKey</code> is expected to be a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or 1063<code>DataView</code>.</p> 1064<p>If <code>outputEncoding</code> is given a string will be returned; otherwise a 1065<a href="buffer.html"><code>Buffer</code></a> is returned.</p> 1066<p><code>ecdh.computeSecret</code> will throw an 1067<code>ERR_CRYPTO_ECDH_INVALID_PUBLIC_KEY</code> error when <code>otherPublicKey</code> 1068lies outside of the elliptic curve. Since <code>otherPublicKey</code> is 1069usually supplied from a remote user over an insecure network, 1070be sure to handle this exception accordingly.</p> 1071<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> 1072<div class="api_metadata"> 1073<span>Added in: v0.11.14</span> 1074</div> 1075<ul> 1076<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li> 1077<li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'uncompressed'</code></li> 1078<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1079</ul> 1080<p>Generates private and public EC Diffie-Hellman key values, and returns 1081the public key in the specified <code>format</code> and <code>encoding</code>. This key should be 1082transferred to the other party.</p> 1083<p>The <code>format</code> argument specifies point encoding and can be <code>'compressed'</code> or 1084<code>'uncompressed'</code>. If <code>format</code> is not specified, the point will be returned in 1085<code>'uncompressed'</code> format.</p> 1086<p>If <code>encoding</code> is provided a string is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> 1087is returned.</p> 1088<h4><code>ecdh.getPrivateKey([encoding])</code><span><a class="mark" href="#crypto_ecdh_getprivatekey_encoding" id="crypto_ecdh_getprivatekey_encoding">#</a></span></h4> 1089<div class="api_metadata"> 1090<span>Added in: v0.11.14</span> 1091</div> 1092<ul> 1093<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li> 1094<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The EC Diffie-Hellman in the specified <code>encoding</code>.</li> 1095</ul> 1096<p>If <code>encoding</code> is specified, a string is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is 1097returned.</p> 1098<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> 1099<div class="api_metadata"> 1100<span>Added in: v0.11.14</span> 1101</div> 1102<ul> 1103<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li> 1104<li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'uncompressed'</code></li> 1105<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The EC Diffie-Hellman public key in the specified 1106<code>encoding</code> and <code>format</code>.</li> 1107</ul> 1108<p>The <code>format</code> argument specifies point encoding and can be <code>'compressed'</code> or 1109<code>'uncompressed'</code>. If <code>format</code> is not specified the point will be returned in 1110<code>'uncompressed'</code> format.</p> 1111<p>If <code>encoding</code> is specified, a string is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is 1112returned.</p> 1113<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> 1114<div class="api_metadata"> 1115<span>Added in: v0.11.14</span> 1116</div> 1117<ul> 1118<li><code>privateKey</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 1119<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the <code>privateKey</code> string.</li> 1120</ul> 1121<p>Sets the EC Diffie-Hellman private key. 1122If <code>encoding</code> is provided, <code>privateKey</code> is expected 1123to be a string; otherwise <code>privateKey</code> is expected to be a <a href="buffer.html"><code>Buffer</code></a>, 1124<code>TypedArray</code>, or <code>DataView</code>.</p> 1125<p>If <code>privateKey</code> is not valid for the curve specified when the <code>ECDH</code> object was 1126created, an error is thrown. Upon setting the private key, the associated 1127public point (key) is also generated and set in the <code>ECDH</code> object.</p> 1128<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> 1129<div class="api_metadata"> 1130<span>Added in: v0.11.14</span><span>Deprecated since: v5.2.0</span> 1131</div> 1132<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated</div><p></p> 1133<ul> 1134<li><code>publicKey</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 1135<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the <code>publicKey</code> string.</li> 1136</ul> 1137<p>Sets the EC Diffie-Hellman public key. 1138If <code>encoding</code> is provided <code>publicKey</code> is expected to 1139be a string; otherwise a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or <code>DataView</code> is expected.</p> 1140<p>There is not normally a reason to call this method because <code>ECDH</code> 1141only requires a private key and the other party's public key to compute the 1142shared secret. Typically either <a href="#crypto_ecdh_generatekeys_encoding_format"><code>ecdh.generateKeys()</code></a> or 1143<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 1144attempts to generate the public point/key associated with the private key being 1145set.</p> 1146<p>Example (obtaining a shared secret):</p> 1147<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>); 1148<span class="hljs-keyword">const</span> alice = crypto.<span class="hljs-title function_">createECDH</span>(<span class="hljs-string">'secp256k1'</span>); 1149<span class="hljs-keyword">const</span> bob = crypto.<span class="hljs-title function_">createECDH</span>(<span class="hljs-string">'secp256k1'</span>); 1150 1151<span class="hljs-comment">// This is a shortcut way of specifying one of Alice's previous private</span> 1152<span class="hljs-comment">// keys. It would be unwise to use such a predictable private key in a real</span> 1153<span class="hljs-comment">// application.</span> 1154alice.<span class="hljs-title function_">setPrivateKey</span>( 1155 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>() 1156); 1157 1158<span class="hljs-comment">// Bob uses a newly generated cryptographically strong</span> 1159<span class="hljs-comment">// pseudorandom key pair</span> 1160bob.<span class="hljs-title function_">generateKeys</span>(); 1161 1162<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>); 1163<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>); 1164 1165<span class="hljs-comment">// aliceSecret and bobSecret should be the same shared secret value</span> 1166<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(aliceSecret === bobSecret);</code></pre> 1167</section><section><h3>Class: <code>Hash</code><span><a class="mark" href="#crypto_class_hash" id="crypto_class_hash">#</a></span></h3> 1168<div class="api_metadata"> 1169<span>Added in: v0.1.92</span> 1170</div> 1171<ul> 1172<li>Extends: <a href="stream.html#stream_class_stream_transform" class="type"><stream.Transform></a></li> 1173</ul> 1174<p>The <code>Hash</code> class is a utility for creating hash digests of data. It can be 1175used in one of two ways:</p> 1176<ul> 1177<li>As a <a href="stream.html">stream</a> that is both readable and writable, where data is written 1178to produce a computed hash digest on the readable side, or</li> 1179<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 1180computed hash.</li> 1181</ul> 1182<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> 1183objects are not to be created directly using the <code>new</code> keyword.</p> 1184<p>Example: Using <code>Hash</code> objects as streams:</p> 1185<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>); 1186<span class="hljs-keyword">const</span> hash = crypto.<span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>); 1187 1188hash.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> { 1189 <span class="hljs-comment">// Only one element is going to be produced by the</span> 1190 <span class="hljs-comment">// hash stream.</span> 1191 <span class="hljs-keyword">const</span> data = hash.<span class="hljs-title function_">read</span>(); 1192 <span class="hljs-keyword">if</span> (data) { 1193 <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>)); 1194 <span class="hljs-comment">// Prints:</span> 1195 <span class="hljs-comment">// 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50</span> 1196 } 1197}); 1198 1199hash.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to hash'</span>); 1200hash.<span class="hljs-title function_">end</span>();</code></pre> 1201<p>Example: Using <code>Hash</code> and piped streams:</p> 1202<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>); 1203<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>); 1204<span class="hljs-keyword">const</span> hash = crypto.<span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>); 1205 1206<span class="hljs-keyword">const</span> input = fs.<span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'test.js'</span>); 1207input.<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> 1208<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> 1209<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>); 1210<span class="hljs-keyword">const</span> hash = crypto.<span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>); 1211 1212hash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to hash'</span>); 1213<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>)); 1214<span class="hljs-comment">// Prints:</span> 1215<span class="hljs-comment">// 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50</span></code></pre> 1216<h4><code>hash.copy([options])</code><span><a class="mark" href="#crypto_hash_copy_options" id="crypto_hash_copy_options">#</a></span></h4> 1217<div class="api_metadata"> 1218<span>Added in: v13.1.0</span> 1219</div> 1220<ul> 1221<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <a href="stream.html#stream_new_stream_transform_options"><code>stream.transform</code> options</a></li> 1222<li>Returns: <a href="crypto.html#crypto_class_hash" class="type"><Hash></a></li> 1223</ul> 1224<p>Creates a new <code>Hash</code> object that contains a deep copy of the internal state 1225of the current <code>Hash</code> object.</p> 1226<p>The optional <code>options</code> argument controls stream behavior. For XOF hash 1227functions such as <code>'shake256'</code>, the <code>outputLength</code> option can be used to 1228specify the desired output length in bytes.</p> 1229<p>An error is thrown when an attempt is made to copy the <code>Hash</code> object after 1230its <a href="#crypto_hash_digest_encoding"><code>hash.digest()</code></a> method has been called.</p> 1231<pre><code class="language-js"><span class="hljs-comment">// Calculate a rolling hash.</span> 1232<span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>); 1233<span class="hljs-keyword">const</span> hash = crypto.<span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>); 1234 1235hash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'one'</span>); 1236<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>)); 1237 1238hash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'two'</span>); 1239<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>)); 1240 1241hash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'three'</span>); 1242<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>)); 1243 1244<span class="hljs-comment">// Etc.</span></code></pre> 1245<h4><code>hash.digest([encoding])</code><span><a class="mark" href="#crypto_hash_digest_encoding" id="crypto_hash_digest_encoding">#</a></span></h4> 1246<div class="api_metadata"> 1247<span>Added in: v0.1.92</span> 1248</div> 1249<ul> 1250<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li> 1251<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1252</ul> 1253<p>Calculates the digest of all of the data passed to be hashed (using the 1254<a href="#crypto_hash_update_data_inputencoding"><code>hash.update()</code></a> method). 1255If <code>encoding</code> is provided a string will be returned; otherwise 1256a <a href="buffer.html"><code>Buffer</code></a> is returned.</p> 1257<p>The <code>Hash</code> object can not be used again after <code>hash.digest()</code> method has been 1258called. Multiple calls will cause an error to be thrown.</p> 1259<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> 1260<div class="api_metadata"> 1261<details class="changelog"><summary>History</summary> 1262<table> 1263<tbody><tr><th>Version</th><th>Changes</th></tr> 1264<tr><td>v6.0.0</td> 1265<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p></td></tr> 1266<tr><td>v0.1.92</td> 1267<td><p><span>Added in: v0.1.92</span></p></td></tr> 1268</tbody></table> 1269</details> 1270</div> 1271<ul> 1272<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 1273<li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the <code>data</code> string.</li> 1274</ul> 1275<p>Updates the hash content with the given <code>data</code>, the encoding of which 1276is given in <code>inputEncoding</code>. 1277If <code>encoding</code> is not provided, and the <code>data</code> is a string, an 1278encoding of <code>'utf8'</code> is enforced. If <code>data</code> is a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or 1279<code>DataView</code>, then <code>inputEncoding</code> is ignored.</p> 1280<p>This can be called many times with new data as it is streamed.</p> 1281</section><section><h3>Class: <code>Hmac</code><span><a class="mark" href="#crypto_class_hmac" id="crypto_class_hmac">#</a></span></h3> 1282<div class="api_metadata"> 1283<span>Added in: v0.1.94</span> 1284</div> 1285<ul> 1286<li>Extends: <a href="stream.html#stream_class_stream_transform" class="type"><stream.Transform></a></li> 1287</ul> 1288<p>The <code>Hmac</code> class is a utility for creating cryptographic HMAC digests. It can 1289be used in one of two ways:</p> 1290<ul> 1291<li>As a <a href="stream.html">stream</a> that is both readable and writable, where data is written 1292to produce a computed HMAC digest on the readable side, or</li> 1293<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 1294computed HMAC digest.</li> 1295</ul> 1296<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> 1297objects are not to be created directly using the <code>new</code> keyword.</p> 1298<p>Example: Using <code>Hmac</code> objects as streams:</p> 1299<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>); 1300<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>); 1301 1302hmac.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> { 1303 <span class="hljs-comment">// Only one element is going to be produced by the</span> 1304 <span class="hljs-comment">// hash stream.</span> 1305 <span class="hljs-keyword">const</span> data = hmac.<span class="hljs-title function_">read</span>(); 1306 <span class="hljs-keyword">if</span> (data) { 1307 <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>)); 1308 <span class="hljs-comment">// Prints:</span> 1309 <span class="hljs-comment">// 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e</span> 1310 } 1311}); 1312 1313hmac.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to hash'</span>); 1314hmac.<span class="hljs-title function_">end</span>();</code></pre> 1315<p>Example: Using <code>Hmac</code> and piped streams:</p> 1316<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>); 1317<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>); 1318<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>); 1319 1320<span class="hljs-keyword">const</span> input = fs.<span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'test.js'</span>); 1321input.<span class="hljs-title function_">pipe</span>(hmac).<span class="hljs-title function_">pipe</span>(process.<span class="hljs-property">stdout</span>);</code></pre> 1322<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> 1323<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>); 1324<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>); 1325 1326hmac.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to hash'</span>); 1327<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>)); 1328<span class="hljs-comment">// Prints:</span> 1329<span class="hljs-comment">// 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e</span></code></pre> 1330<h4><code>hmac.digest([encoding])</code><span><a class="mark" href="#crypto_hmac_digest_encoding" id="crypto_hmac_digest_encoding">#</a></span></h4> 1331<div class="api_metadata"> 1332<span>Added in: v0.1.94</span> 1333</div> 1334<ul> 1335<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li> 1336<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1337</ul> 1338<p>Calculates the HMAC digest of all of the data passed using <a href="#crypto_hmac_update_data_inputencoding"><code>hmac.update()</code></a>. 1339If <code>encoding</code> is 1340provided a string is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is returned;</p> 1341<p>The <code>Hmac</code> object can not be used again after <code>hmac.digest()</code> has been 1342called. Multiple calls to <code>hmac.digest()</code> will result in an error being thrown.</p> 1343<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> 1344<div class="api_metadata"> 1345<details class="changelog"><summary>History</summary> 1346<table> 1347<tbody><tr><th>Version</th><th>Changes</th></tr> 1348<tr><td>v6.0.0</td> 1349<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p></td></tr> 1350<tr><td>v0.1.94</td> 1351<td><p><span>Added in: v0.1.94</span></p></td></tr> 1352</tbody></table> 1353</details> 1354</div> 1355<ul> 1356<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 1357<li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the <code>data</code> string.</li> 1358</ul> 1359<p>Updates the <code>Hmac</code> content with the given <code>data</code>, the encoding of which 1360is given in <code>inputEncoding</code>. 1361If <code>encoding</code> is not provided, and the <code>data</code> is a string, an 1362encoding of <code>'utf8'</code> is enforced. If <code>data</code> is a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or 1363<code>DataView</code>, then <code>inputEncoding</code> is ignored.</p> 1364<p>This can be called many times with new data as it is streamed.</p> 1365</section><section><h3>Class: <code>KeyObject</code><span><a class="mark" href="#crypto_class_keyobject" id="crypto_class_keyobject">#</a></span></h3> 1366<div class="api_metadata"> 1367<details class="changelog"><summary>History</summary> 1368<table> 1369<tbody><tr><th>Version</th><th>Changes</th></tr> 1370<tr><td>v14.5.0</td> 1371<td><p>Instances of this class can now be passed to worker threads using <code>postMessage</code>.</p></td></tr> 1372<tr><td>v11.13.0</td> 1373<td><p>This class is now exported.</p></td></tr> 1374<tr><td>v11.6.0</td> 1375<td><p><span>Added in: v11.6.0</span></p></td></tr> 1376</tbody></table> 1377</details> 1378</div> 1379<p>Node.js uses a <code>KeyObject</code> class to represent a symmetric or asymmetric key, 1380and each kind of key exposes different functions. The 1381<a href="#crypto_crypto_createsecretkey_key"><code>crypto.createSecretKey()</code></a>, <a href="#crypto_crypto_createpublickey_key"><code>crypto.createPublicKey()</code></a> and 1382<a href="#crypto_crypto_createprivatekey_key"><code>crypto.createPrivateKey()</code></a> methods are used to create <code>KeyObject</code> 1383instances. <code>KeyObject</code> objects are not to be created directly using the <code>new</code> 1384keyword.</p> 1385<p>Most applications should consider using the new <code>KeyObject</code> API instead of 1386passing keys as strings or <code>Buffer</code>s due to improved security features.</p> 1387<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>. 1388The receiver obtains a cloned <code>KeyObject</code>, and the <code>KeyObject</code> does not need to 1389be listed in the <code>transferList</code> argument.</p> 1390<h4><code>keyObject.asymmetricKeyType</code><span><a class="mark" href="#crypto_keyobject_asymmetrickeytype" id="crypto_keyobject_asymmetrickeytype">#</a></span></h4> 1391<div class="api_metadata"> 1392<details class="changelog"><summary>History</summary> 1393<table> 1394<tbody><tr><th>Version</th><th>Changes</th></tr> 1395<tr><td>v13.9.0</td> 1396<td><p>Added support for <code>'dh'</code>.</p></td></tr> 1397<tr><td>v12.0.0</td> 1398<td><p>Added support for <code>'rsa-pss'</code>.</p></td></tr> 1399<tr><td>v12.0.0</td> 1400<td><p>This property now returns <code>undefined</code> for KeyObject instances of unrecognized type instead of aborting.</p></td></tr> 1401<tr><td>v12.0.0</td> 1402<td><p>Added support for <code>'x25519'</code> and <code>'x448'</code>.</p></td></tr> 1403<tr><td>v12.0.0</td> 1404<td><p>Added support for <code>'ed25519'</code> and <code>'ed448'</code>.</p></td></tr> 1405<tr><td>v11.6.0</td> 1406<td><p><span>Added in: v11.6.0</span></p></td></tr> 1407</tbody></table> 1408</details> 1409</div> 1410<ul> 1411<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1412</ul> 1413<p>For asymmetric keys, this property represents the type of the key. Supported key 1414types are:</p> 1415<ul> 1416<li><code>'rsa'</code> (OID 1.2.840.113549.1.1.1)</li> 1417<li><code>'rsa-pss'</code> (OID 1.2.840.113549.1.1.10)</li> 1418<li><code>'dsa'</code> (OID 1.2.840.10040.4.1)</li> 1419<li><code>'ec'</code> (OID 1.2.840.10045.2.1)</li> 1420<li><code>'x25519'</code> (OID 1.3.101.110)</li> 1421<li><code>'x448'</code> (OID 1.3.101.111)</li> 1422<li><code>'ed25519'</code> (OID 1.3.101.112)</li> 1423<li><code>'ed448'</code> (OID 1.3.101.113)</li> 1424<li><code>'dh'</code> (OID 1.2.840.113549.1.3.1)</li> 1425</ul> 1426<p>This property is <code>undefined</code> for unrecognized <code>KeyObject</code> types and symmetric 1427keys.</p> 1428<h4><code>keyObject.export([options])</code><span><a class="mark" href="#crypto_keyobject_export_options" id="crypto_keyobject_export_options">#</a></span></h4> 1429<div class="api_metadata"> 1430<span>Added in: v11.6.0</span> 1431</div> 1432<ul> 1433<li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 1434<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li> 1435</ul> 1436<p>For symmetric keys, this function allocates a <code>Buffer</code> containing the key 1437material and ignores any options.</p> 1438<p>For asymmetric keys, the <code>options</code> parameter is used to determine the export 1439format.</p> 1440<p>For public keys, the following encoding options can be used:</p> 1441<ul> 1442<li><code>type</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Must be one of <code>'pkcs1'</code> (RSA only) or <code>'spki'</code>.</li> 1443<li><code>format</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Must be <code>'pem'</code> or <code>'der'</code>.</li> 1444</ul> 1445<p>For private keys, the following encoding options can be used:</p> 1446<ul> 1447<li><code>type</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Must be one of <code>'pkcs1'</code> (RSA only), <code>'pkcs8'</code> or 1448<code>'sec1'</code> (EC only).</li> 1449<li><code>format</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Must be <code>'pem'</code> or <code>'der'</code>.</li> 1450<li><code>cipher</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> If specified, the private key will be encrypted with 1451the given <code>cipher</code> and <code>passphrase</code> using PKCS#5 v2.0 password based 1452encryption.</li> 1453<li><code>passphrase</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> The passphrase to use for encryption, see 1454<code>cipher</code>.</li> 1455</ul> 1456<p>When PEM encoding was selected, the result will be a string, otherwise it will 1457be a buffer containing the data encoded as DER.</p> 1458<p>PKCS#1, SEC1, and PKCS#8 type keys can be encrypted by using a combination of 1459the <code>cipher</code> and <code>format</code> options. The PKCS#8 <code>type</code> can be used with any 1460<code>format</code> to encrypt any key algorithm (RSA, EC, or DH) by specifying a 1461<code>cipher</code>. PKCS#1 and SEC1 can only be encrypted by specifying a <code>cipher</code> 1462when the PEM <code>format</code> is used. For maximum compatibility, use PKCS#8 for 1463encrypted private keys. Since PKCS#8 defines its own 1464encryption mechanism, PEM-level encryption is not supported when encrypting 1465a 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 1466PKCS#1 and SEC1 encryption.</p> 1467<h4><code>keyObject.symmetricKeySize</code><span><a class="mark" href="#crypto_keyobject_symmetrickeysize" id="crypto_keyobject_symmetrickeysize">#</a></span></h4> 1468<div class="api_metadata"> 1469<span>Added in: v11.6.0</span> 1470</div> 1471<ul> 1472<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1473</ul> 1474<p>For secret keys, this property represents the size of the key in bytes. This 1475property is <code>undefined</code> for asymmetric keys.</p> 1476<h4><code>keyObject.type</code><span><a class="mark" href="#crypto_keyobject_type" id="crypto_keyobject_type">#</a></span></h4> 1477<div class="api_metadata"> 1478<span>Added in: v11.6.0</span> 1479</div> 1480<ul> 1481<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1482</ul> 1483<p>Depending on the type of this <code>KeyObject</code>, this property is either 1484<code>'secret'</code> for secret (symmetric) keys, <code>'public'</code> for public (asymmetric) keys 1485or <code>'private'</code> for private (asymmetric) keys.</p> 1486</section><section><h3>Class: <code>Sign</code><span><a class="mark" href="#crypto_class_sign" id="crypto_class_sign">#</a></span></h3> 1487<div class="api_metadata"> 1488<span>Added in: v0.1.92</span> 1489</div> 1490<ul> 1491<li>Extends: <a href="stream.html#stream_class_stream_writable" class="type"><stream.Writable></a></li> 1492</ul> 1493<p>The <code>Sign</code> class is a utility for generating signatures. It can be used in one 1494of two ways:</p> 1495<ul> 1496<li>As a writable <a href="stream.html">stream</a>, where data to be signed is written and the 1497<a href="#crypto_sign_sign_privatekey_outputencoding"><code>sign.sign()</code></a> method is used to generate and return the signature, or</li> 1498<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 1499signature.</li> 1500</ul> 1501<p>The <a href="#crypto_crypto_createsign_algorithm_options"><code>crypto.createSign()</code></a> method is used to create <code>Sign</code> instances. The 1502argument is the string name of the hash function to use. <code>Sign</code> objects are not 1503to be created directly using the <code>new</code> keyword.</p> 1504<p>Example: Using <code>Sign</code> and <a href="#crypto_class_verify"><code>Verify</code></a> objects as streams:</p> 1505<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>); 1506 1507<span class="hljs-keyword">const</span> { privateKey, publicKey } = crypto.<span class="hljs-title function_">generateKeyPairSync</span>(<span class="hljs-string">'ec'</span>, { 1508 <span class="hljs-attr">namedCurve</span>: <span class="hljs-string">'sect239k1'</span> 1509}); 1510 1511<span class="hljs-keyword">const</span> sign = crypto.<span class="hljs-title function_">createSign</span>(<span class="hljs-string">'SHA256'</span>); 1512sign.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to sign'</span>); 1513sign.<span class="hljs-title function_">end</span>(); 1514<span class="hljs-keyword">const</span> signature = sign.<span class="hljs-title function_">sign</span>(privateKey, <span class="hljs-string">'hex'</span>); 1515 1516<span class="hljs-keyword">const</span> verify = crypto.<span class="hljs-title function_">createVerify</span>(<span class="hljs-string">'SHA256'</span>); 1517verify.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to sign'</span>); 1518verify.<span class="hljs-title function_">end</span>(); 1519<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>)); 1520<span class="hljs-comment">// Prints: true</span></code></pre> 1521<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> 1522<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>); 1523 1524<span class="hljs-keyword">const</span> { privateKey, publicKey } = crypto.<span class="hljs-title function_">generateKeyPairSync</span>(<span class="hljs-string">'rsa'</span>, { 1525 <span class="hljs-attr">modulusLength</span>: <span class="hljs-number">2048</span>, 1526}); 1527 1528<span class="hljs-keyword">const</span> sign = crypto.<span class="hljs-title function_">createSign</span>(<span class="hljs-string">'SHA256'</span>); 1529sign.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to sign'</span>); 1530sign.<span class="hljs-title function_">end</span>(); 1531<span class="hljs-keyword">const</span> signature = sign.<span class="hljs-title function_">sign</span>(privateKey); 1532 1533<span class="hljs-keyword">const</span> verify = crypto.<span class="hljs-title function_">createVerify</span>(<span class="hljs-string">'SHA256'</span>); 1534verify.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to sign'</span>); 1535verify.<span class="hljs-title function_">end</span>(); 1536<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(verify.<span class="hljs-title function_">verify</span>(publicKey, signature)); 1537<span class="hljs-comment">// Prints: true</span></code></pre> 1538<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> 1539<div class="api_metadata"> 1540<details class="changelog"><summary>History</summary> 1541<table> 1542<tbody><tr><th>Version</th><th>Changes</th></tr> 1543<tr><td>v13.2.0, v12.16.0</td> 1544<td><p>This function now supports IEEE-P1363 DSA and ECDSA signatures.</p></td></tr> 1545<tr><td>v12.0.0</td> 1546<td><p>This function now supports RSA-PSS keys.</p></td></tr> 1547<tr><td>v11.6.0</td> 1548<td><p>This function now supports key objects.</p></td></tr> 1549<tr><td>v8.0.0</td> 1550<td><p>Support for RSASSA-PSS and additional options was added.</p></td></tr> 1551<tr><td>v0.1.92</td> 1552<td><p><span>Added in: v0.1.92</span></p></td></tr> 1553</tbody></table> 1554</details> 1555</div> 1556<ul> 1557<li><code>privateKey</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="crypto.html#crypto_class_keyobject" class="type"><KeyObject></a> 1558<ul> 1559<li><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1560<li><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1561<li><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1562</ul> 1563</li> 1564<li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the return value.</li> 1565<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1566</ul> 1567<p>Calculates the signature on all the data passed through using either 1568<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> 1569<p>If <code>privateKey</code> is not a <a href="#crypto_class_keyobject"><code>KeyObject</code></a>, this function behaves as if 1570<code>privateKey</code> had been passed to <a href="#crypto_crypto_createprivatekey_key"><code>crypto.createPrivateKey()</code></a>. If it is an 1571object, the following additional properties can be passed:</p> 1572<ul> 1573<li> 1574<p><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> For DSA and ECDSA, this option specifies the 1575format of the generated signature. It can be one of the following:</p> 1576<ul> 1577<li><code>'der'</code> (default): DER-encoded ASN.1 signature structure encoding <code>(r, s)</code>.</li> 1578<li><code>'ieee-p1363'</code>: Signature format <code>r || s</code> as proposed in IEEE-P1363.</li> 1579</ul> 1580</li> 1581<li> 1582<p><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Optional padding value for RSA, one of the following:</p> 1583<ul> 1584<li><code>crypto.constants.RSA_PKCS1_PADDING</code> (default)</li> 1585<li><code>crypto.constants.RSA_PKCS1_PSS_PADDING</code></li> 1586</ul> 1587<p><code>RSA_PKCS1_PSS_PADDING</code> will use MGF1 with the same hash function 1588used 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 1589an MGF1 hash function has been specified as part of the key in compliance with 1590section 3.3 of <a href="https://www.rfc-editor.org/rfc/rfc4055.txt">RFC 4055</a>.</p> 1591</li> 1592<li> 1593<p><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Salt length for when padding is 1594<code>RSA_PKCS1_PSS_PADDING</code>. The special value 1595<code>crypto.constants.RSA_PSS_SALTLEN_DIGEST</code> sets the salt length to the digest 1596size, <code>crypto.constants.RSA_PSS_SALTLEN_MAX_SIGN</code> (default) sets it to the 1597maximum permissible value.</p> 1598</li> 1599</ul> 1600<p>If <code>outputEncoding</code> is provided a string is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> 1601is returned.</p> 1602<p>The <code>Sign</code> object can not be again used after <code>sign.sign()</code> method has been 1603called. Multiple calls to <code>sign.sign()</code> will result in an error being thrown.</p> 1604<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> 1605<div class="api_metadata"> 1606<details class="changelog"><summary>History</summary> 1607<table> 1608<tbody><tr><th>Version</th><th>Changes</th></tr> 1609<tr><td>v6.0.0</td> 1610<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p></td></tr> 1611<tr><td>v0.1.92</td> 1612<td><p><span>Added in: v0.1.92</span></p></td></tr> 1613</tbody></table> 1614</details> 1615</div> 1616<ul> 1617<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 1618<li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the <code>data</code> string.</li> 1619</ul> 1620<p>Updates the <code>Sign</code> content with the given <code>data</code>, the encoding of which 1621is given in <code>inputEncoding</code>. 1622If <code>encoding</code> is not provided, and the <code>data</code> is a string, an 1623encoding of <code>'utf8'</code> is enforced. If <code>data</code> is a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or 1624<code>DataView</code>, then <code>inputEncoding</code> is ignored.</p> 1625<p>This can be called many times with new data as it is streamed.</p> 1626</section><section><h3>Class: <code>Verify</code><span><a class="mark" href="#crypto_class_verify" id="crypto_class_verify">#</a></span></h3> 1627<div class="api_metadata"> 1628<span>Added in: v0.1.92</span> 1629</div> 1630<ul> 1631<li>Extends: <a href="stream.html#stream_class_stream_writable" class="type"><stream.Writable></a></li> 1632</ul> 1633<p>The <code>Verify</code> class is a utility for verifying signatures. It can be used in one 1634of two ways:</p> 1635<ul> 1636<li>As a writable <a href="stream.html">stream</a> where written data is used to validate against the 1637supplied signature, or</li> 1638<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 1639the signature.</li> 1640</ul> 1641<p>The <a href="#crypto_crypto_createverify_algorithm_options"><code>crypto.createVerify()</code></a> method is used to create <code>Verify</code> instances. 1642<code>Verify</code> objects are not to be created directly using the <code>new</code> keyword.</p> 1643<p>See <a href="#crypto_class_sign"><code>Sign</code></a> for examples.</p> 1644<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> 1645<div class="api_metadata"> 1646<details class="changelog"><summary>History</summary> 1647<table> 1648<tbody><tr><th>Version</th><th>Changes</th></tr> 1649<tr><td>v6.0.0</td> 1650<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p></td></tr> 1651<tr><td>v0.1.92</td> 1652<td><p><span>Added in: v0.1.92</span></p></td></tr> 1653</tbody></table> 1654</details> 1655</div> 1656<ul> 1657<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 1658<li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the <code>data</code> string.</li> 1659</ul> 1660<p>Updates the <code>Verify</code> content with the given <code>data</code>, the encoding of which 1661is given in <code>inputEncoding</code>. 1662If <code>inputEncoding</code> is not provided, and the <code>data</code> is a string, an 1663encoding of <code>'utf8'</code> is enforced. If <code>data</code> is a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or 1664<code>DataView</code>, then <code>inputEncoding</code> is ignored.</p> 1665<p>This can be called many times with new data as it is streamed.</p> 1666<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> 1667<div class="api_metadata"> 1668<details class="changelog"><summary>History</summary> 1669<table> 1670<tbody><tr><th>Version</th><th>Changes</th></tr> 1671<tr><td>v13.2.0, v12.16.0</td> 1672<td><p>This function now supports IEEE-P1363 DSA and ECDSA signatures.</p></td></tr> 1673<tr><td>v12.0.0</td> 1674<td><p>This function now supports RSA-PSS keys.</p></td></tr> 1675<tr><td>v11.7.0</td> 1676<td><p>The key can now be a private key.</p></td></tr> 1677<tr><td>v8.0.0</td> 1678<td><p>Support for RSASSA-PSS and additional options was added.</p></td></tr> 1679<tr><td>v0.1.92</td> 1680<td><p><span>Added in: v0.1.92</span></p></td></tr> 1681</tbody></table> 1682</details> 1683</div> 1684<ul> 1685<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="crypto.html#crypto_class_keyobject" class="type"><KeyObject></a> 1686<ul> 1687<li><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1688<li><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1689<li><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1690</ul> 1691</li> 1692<li><code>signature</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 1693<li><code>signatureEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the <code>signature</code> string.</li> 1694<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> or <code>false</code> depending on the validity of the 1695signature for the data and public key.</li> 1696</ul> 1697<p>Verifies the provided data using the given <code>object</code> and <code>signature</code>.</p> 1698<p>If <code>object</code> is not a <a href="#crypto_class_keyobject"><code>KeyObject</code></a>, this function behaves as if 1699<code>object</code> had been passed to <a href="#crypto_crypto_createpublickey_key"><code>crypto.createPublicKey()</code></a>. If it is an 1700object, the following additional properties can be passed:</p> 1701<ul> 1702<li> 1703<p><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> For DSA and ECDSA, this option specifies the 1704format of the signature. It can be one of the following:</p> 1705<ul> 1706<li><code>'der'</code> (default): DER-encoded ASN.1 signature structure encoding <code>(r, s)</code>.</li> 1707<li><code>'ieee-p1363'</code>: Signature format <code>r || s</code> as proposed in IEEE-P1363.</li> 1708</ul> 1709</li> 1710<li> 1711<p><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Optional padding value for RSA, one of the following:</p> 1712<ul> 1713<li><code>crypto.constants.RSA_PKCS1_PADDING</code> (default)</li> 1714<li><code>crypto.constants.RSA_PKCS1_PSS_PADDING</code></li> 1715</ul> 1716<p><code>RSA_PKCS1_PSS_PADDING</code> will use MGF1 with the same hash function 1717used 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 1718an MGF1 hash function has been specified as part of the key in compliance with 1719section 3.3 of <a href="https://www.rfc-editor.org/rfc/rfc4055.txt">RFC 4055</a>.</p> 1720</li> 1721<li> 1722<p><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Salt length for when padding is 1723<code>RSA_PKCS1_PSS_PADDING</code>. The special value 1724<code>crypto.constants.RSA_PSS_SALTLEN_DIGEST</code> sets the salt length to the digest 1725size, <code>crypto.constants.RSA_PSS_SALTLEN_AUTO</code> (default) causes it to be 1726determined automatically.</p> 1727</li> 1728</ul> 1729<p>The <code>signature</code> argument is the previously calculated signature for the data, in 1730the <code>signatureEncoding</code>. 1731If a <code>signatureEncoding</code> is specified, the <code>signature</code> is expected to be a 1732string; otherwise <code>signature</code> is expected to be a <a href="buffer.html"><code>Buffer</code></a>, 1733<code>TypedArray</code>, or <code>DataView</code>.</p> 1734<p>The <code>verify</code> object can not be used again after <code>verify.verify()</code> has been 1735called. Multiple calls to <code>verify.verify()</code> will result in an error being 1736thrown.</p> 1737<p>Because public keys can be derived from private keys, a private key may 1738be passed instead of a public key.</p> 1739</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> 1740<h4><code>crypto.constants</code><span><a class="mark" href="#crypto_crypto_constants" id="crypto_crypto_constants">#</a></span></h4> 1741<div class="api_metadata"> 1742<span>Added in: v6.3.0</span> 1743</div> 1744<ul> 1745<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> An object containing commonly used constants for crypto and 1746security related operations. The specific constants currently defined are 1747described in <a href="#crypto_crypto_constants_1">Crypto constants</a>.</li> 1748</ul> 1749<h4><code>crypto.DEFAULT_ENCODING</code><span><a class="mark" href="#crypto_crypto_default_encoding" id="crypto_crypto_default_encoding">#</a></span></h4> 1750<div class="api_metadata"> 1751<span>Added in: v0.9.3</span><span>Deprecated since: v10.0.0</span> 1752</div> 1753<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated</div><p></p> 1754<p>The default encoding to use for functions that can take either strings 1755or <a href="buffer.html">buffers</a>. The default value is <code>'buffer'</code>, which makes methods 1756default to <a href="buffer.html"><code>Buffer</code></a> objects.</p> 1757<p>The <code>crypto.DEFAULT_ENCODING</code> mechanism is provided for backward compatibility 1758with legacy programs that expect <code>'latin1'</code> to be the default encoding.</p> 1759<p>New applications should expect the default to be <code>'buffer'</code>.</p> 1760<p>This property is deprecated.</p> 1761<h4><code>crypto.fips</code><span><a class="mark" href="#crypto_crypto_fips" id="crypto_crypto_fips">#</a></span></h4> 1762<div class="api_metadata"> 1763<span>Added in: v6.0.0</span><span>Deprecated since: v10.0.0</span> 1764</div> 1765<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated</div><p></p> 1766<p>Property for checking and controlling whether a FIPS compliant crypto provider 1767is currently in use. Setting to true requires a FIPS build of Node.js.</p> 1768<p>This property is deprecated. Please use <code>crypto.setFips()</code> and 1769<code>crypto.getFips()</code> instead.</p> 1770<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> 1771<div class="api_metadata"> 1772<details class="changelog"><summary>History</summary> 1773<table> 1774<tbody><tr><th>Version</th><th>Changes</th></tr> 1775<tr><td>v10.10.0</td> 1776<td><p>Ciphers in OCB mode are now supported.</p></td></tr> 1777<tr><td>v10.2.0</td> 1778<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> 1779<tr><td>v10.0.0</td> 1780<td><p><span>Deprecated since: v10.0.0</span></p></td></tr> 1781<tr><td>v0.1.94</td> 1782<td><p><span>Added in: v0.1.94</span></p></td></tr> 1783</tbody></table> 1784</details> 1785</div> 1786<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> 1787<ul> 1788<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1789<li><code>password</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 1790<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <a href="stream.html#stream_new_stream_transform_options"><code>stream.transform</code> options</a></li> 1791<li>Returns: <a href="crypto.html#crypto_class_cipher" class="type"><Cipher></a></li> 1792</ul> 1793<p>Creates and returns a <code>Cipher</code> object that uses the given <code>algorithm</code> and 1794<code>password</code>.</p> 1795<p>The <code>options</code> argument controls stream behavior and is optional except when a 1796cipher in CCM or OCB mode is used (e.g. <code>'aes-128-ccm'</code>). In that case, the 1797<code>authTagLength</code> option is required and specifies the length of the 1798authentication tag in bytes, see <a href="#crypto_ccm_mode">CCM mode</a>. In GCM mode, the <code>authTagLength</code> 1799option is not required but can be used to set the length of the authentication 1800tag that will be returned by <code>getAuthTag()</code> and defaults to 16 bytes.</p> 1801<p>The <code>algorithm</code> is dependent on OpenSSL, examples are <code>'aes192'</code>, etc. On 1802recent OpenSSL releases, <code>openssl list -cipher-algorithms</code> 1803(<code>openssl list-cipher-algorithms</code> for older versions of OpenSSL) will 1804display the available cipher algorithms.</p> 1805<p>The <code>password</code> is used to derive the cipher key and initialization vector (IV). 1806The value must be either a <code>'latin1'</code> encoded string, a <a href="buffer.html"><code>Buffer</code></a>, a 1807<code>TypedArray</code>, or a <code>DataView</code>.</p> 1808<p>The implementation of <code>crypto.createCipher()</code> derives keys using the OpenSSL 1809function <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 1810iteration, and no salt. The lack of salt allows dictionary attacks as the same 1811password always creates the same key. The low iteration count and 1812non-cryptographically secure hash algorithm allow passwords to be tested very 1813rapidly.</p> 1814<p>In line with OpenSSL's recommendation to use a more modern algorithm instead of 1815<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 1816their 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> 1817to create the <code>Cipher</code> object. Users should not use ciphers with counter mode 1818(e.g. CTR, GCM, or CCM) in <code>crypto.createCipher()</code>. A warning is emitted when 1819they are used in order to avoid the risk of IV reuse that causes 1820vulnerabilities. For the case when IV is reused in GCM, see <a href="https://github.com/nonce-disrespect/nonce-disrespect">Nonce-Disrespecting 1821Adversaries</a> for details.</p> 1822<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> 1823<div class="api_metadata"> 1824<details class="changelog"><summary>History</summary> 1825<table> 1826<tbody><tr><th>Version</th><th>Changes</th></tr> 1827<tr><td>v11.6.0</td> 1828<td><p>The <code>key</code> argument can now be a <code>KeyObject</code>.</p></td></tr> 1829<tr><td>v11.2.0, v10.17.0</td> 1830<td><p>The cipher <code>chacha20-poly1305</code> is now supported.</p></td></tr> 1831<tr><td>v10.10.0</td> 1832<td><p>Ciphers in OCB mode are now supported.</p></td></tr> 1833<tr><td>v10.2.0</td> 1834<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> 1835<tr><td>v9.9.0</td> 1836<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> 1837<tr><td>v0.1.94</td> 1838<td><p><span>Added in: v0.1.94</span></p></td></tr> 1839</tbody></table> 1840</details> 1841</div> 1842<ul> 1843<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1844<li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> | <a href="crypto.html#crypto_class_keyobject" class="type"><KeyObject></a></li> 1845<li><code>iv</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a></li> 1846<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <a href="stream.html#stream_new_stream_transform_options"><code>stream.transform</code> options</a></li> 1847<li>Returns: <a href="crypto.html#crypto_class_cipher" class="type"><Cipher></a></li> 1848</ul> 1849<p>Creates and returns a <code>Cipher</code> object, with the given <code>algorithm</code>, <code>key</code> and 1850initialization vector (<code>iv</code>).</p> 1851<p>The <code>options</code> argument controls stream behavior and is optional except when a 1852cipher in CCM or OCB mode is used (e.g. <code>'aes-128-ccm'</code>). In that case, the 1853<code>authTagLength</code> option is required and specifies the length of the 1854authentication tag in bytes, see <a href="#crypto_ccm_mode">CCM mode</a>. In GCM mode, the <code>authTagLength</code> 1855option is not required but can be used to set the length of the authentication 1856tag that will be returned by <code>getAuthTag()</code> and defaults to 16 bytes.</p> 1857<p>The <code>algorithm</code> is dependent on OpenSSL, examples are <code>'aes192'</code>, etc. On 1858recent OpenSSL releases, <code>openssl list -cipher-algorithms</code> 1859(<code>openssl list-cipher-algorithms</code> for older versions of OpenSSL) will 1860display the available cipher algorithms.</p> 1861<p>The <code>key</code> is the raw key used by the <code>algorithm</code> and <code>iv</code> is an 1862<a href="https://en.wikipedia.org/wiki/Initialization_vector">initialization vector</a>. Both arguments must be <code>'utf8'</code> encoded strings, 1863<a href="buffer.html">Buffers</a>, <code>TypedArray</code>, or <code>DataView</code>s. The <code>key</code> may optionally be 1864a <a href="#crypto_class_keyobject"><code>KeyObject</code></a> of type <code>secret</code>. If the cipher does not need 1865an initialization vector, <code>iv</code> may be <code>null</code>.</p> 1866<p>Initialization vectors should be unpredictable and unique; ideally, they will be 1867cryptographically random. They do not have to be secret: IVs are typically just 1868added to ciphertext messages unencrypted. It may sound contradictory that 1869something has to be unpredictable and unique, but does not have to be secret; 1870remember that an attacker must not be able to predict ahead of time what a 1871given IV will be.</p> 1872<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> 1873<div class="api_metadata"> 1874<details class="changelog"><summary>History</summary> 1875<table> 1876<tbody><tr><th>Version</th><th>Changes</th></tr> 1877<tr><td>v10.10.0</td> 1878<td><p>Ciphers in OCB mode are now supported.</p></td></tr> 1879<tr><td>v10.0.0</td> 1880<td><p><span>Deprecated since: v10.0.0</span></p></td></tr> 1881<tr><td>v0.1.94</td> 1882<td><p><span>Added in: v0.1.94</span></p></td></tr> 1883</tbody></table> 1884</details> 1885</div> 1886<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> 1887<ul> 1888<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1889<li><code>password</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 1890<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <a href="stream.html#stream_new_stream_transform_options"><code>stream.transform</code> options</a></li> 1891<li>Returns: <a href="crypto.html#crypto_class_decipher" class="type"><Decipher></a></li> 1892</ul> 1893<p>Creates and returns a <code>Decipher</code> object that uses the given <code>algorithm</code> and 1894<code>password</code> (key).</p> 1895<p>The <code>options</code> argument controls stream behavior and is optional except when a 1896cipher in CCM or OCB mode is used (e.g. <code>'aes-128-ccm'</code>). In that case, the 1897<code>authTagLength</code> option is required and specifies the length of the 1898authentication tag in bytes, see <a href="#crypto_ccm_mode">CCM mode</a>.</p> 1899<p>The implementation of <code>crypto.createDecipher()</code> derives keys using the OpenSSL 1900function <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 1901iteration, and no salt. The lack of salt allows dictionary attacks as the same 1902password always creates the same key. The low iteration count and 1903non-cryptographically secure hash algorithm allow passwords to be tested very 1904rapidly.</p> 1905<p>In line with OpenSSL's recommendation to use a more modern algorithm instead of 1906<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 1907their 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> 1908to create the <code>Decipher</code> object.</p> 1909<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> 1910<div class="api_metadata"> 1911<details class="changelog"><summary>History</summary> 1912<table> 1913<tbody><tr><th>Version</th><th>Changes</th></tr> 1914<tr><td>v11.6.0</td> 1915<td><p>The <code>key</code> argument can now be a <code>KeyObject</code>.</p></td></tr> 1916<tr><td>v11.2.0, v10.17.0</td> 1917<td><p>The cipher <code>chacha20-poly1305</code> is now supported.</p></td></tr> 1918<tr><td>v10.10.0</td> 1919<td><p>Ciphers in OCB mode are now supported.</p></td></tr> 1920<tr><td>v10.2.0</td> 1921<td><p>The <code>authTagLength</code> option can now be used to restrict accepted GCM authentication tag lengths.</p></td></tr> 1922<tr><td>v9.9.0</td> 1923<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> 1924<tr><td>v0.1.94</td> 1925<td><p><span>Added in: v0.1.94</span></p></td></tr> 1926</tbody></table> 1927</details> 1928</div> 1929<ul> 1930<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1931<li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> | <a href="crypto.html#crypto_class_keyobject" class="type"><KeyObject></a></li> 1932<li><code>iv</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a></li> 1933<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <a href="stream.html#stream_new_stream_transform_options"><code>stream.transform</code> options</a></li> 1934<li>Returns: <a href="crypto.html#crypto_class_decipher" class="type"><Decipher></a></li> 1935</ul> 1936<p>Creates and returns a <code>Decipher</code> object that uses the given <code>algorithm</code>, <code>key</code> 1937and initialization vector (<code>iv</code>).</p> 1938<p>The <code>options</code> argument controls stream behavior and is optional except when a 1939cipher in CCM or OCB mode is used (e.g. <code>'aes-128-ccm'</code>). In that case, the 1940<code>authTagLength</code> option is required and specifies the length of the 1941authentication tag in bytes, see <a href="#crypto_ccm_mode">CCM mode</a>. In GCM mode, the <code>authTagLength</code> 1942option is not required but can be used to restrict accepted authentication tags 1943to those with the specified length.</p> 1944<p>The <code>algorithm</code> is dependent on OpenSSL, examples are <code>'aes192'</code>, etc. On 1945recent OpenSSL releases, <code>openssl list -cipher-algorithms</code> 1946(<code>openssl list-cipher-algorithms</code> for older versions of OpenSSL) will 1947display the available cipher algorithms.</p> 1948<p>The <code>key</code> is the raw key used by the <code>algorithm</code> and <code>iv</code> is an 1949<a href="https://en.wikipedia.org/wiki/Initialization_vector">initialization vector</a>. Both arguments must be <code>'utf8'</code> encoded strings, 1950<a href="buffer.html">Buffers</a>, <code>TypedArray</code>, or <code>DataView</code>s. The <code>key</code> may optionally be 1951a <a href="#crypto_class_keyobject"><code>KeyObject</code></a> of type <code>secret</code>. If the cipher does not need 1952an initialization vector, <code>iv</code> may be <code>null</code>.</p> 1953<p>Initialization vectors should be unpredictable and unique; ideally, they will be 1954cryptographically random. They do not have to be secret: IVs are typically just 1955added to ciphertext messages unencrypted. It may sound contradictory that 1956something has to be unpredictable and unique, but does not have to be secret; 1957remember that an attacker must not be able to predict ahead of time what a given 1958IV will be.</p> 1959<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> 1960<div class="api_metadata"> 1961<details class="changelog"><summary>History</summary> 1962<table> 1963<tbody><tr><th>Version</th><th>Changes</th></tr> 1964<tr><td>v8.0.0</td> 1965<td><p>The <code>prime</code> argument can be any <code>TypedArray</code> or <code>DataView</code> now.</p></td></tr> 1966<tr><td>v8.0.0</td> 1967<td><p>The <code>prime</code> argument can be a <code>Uint8Array</code> now.</p></td></tr> 1968<tr><td>v6.0.0</td> 1969<td><p>The default for the encoding parameters changed from <code>binary</code> to <code>utf8</code>.</p></td></tr> 1970<tr><td>v0.11.12</td> 1971<td><p><span>Added in: v0.11.12</span></p></td></tr> 1972</tbody></table> 1973</details> 1974</div> 1975<ul> 1976<li><code>prime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 1977<li><code>primeEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the <code>prime</code> string.</li> 1978<li><code>generator</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> <strong>Default:</strong> 1979<code>2</code></li> 1980<li><code>generatorEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <a href="buffer.html#buffer_buffers_and_character_encodings">encoding</a> of the <code>generator</code> string.</li> 1981<li>Returns: <a href="crypto.html#crypto_class_diffiehellman" class="type"><DiffieHellman></a></li> 1982</ul> 1983<p>Creates a <code>DiffieHellman</code> key exchange object using the supplied <code>prime</code> and an 1984optional specific <code>generator</code>.</p> 1985<p>The <code>generator</code> argument can be a number, string, or <a href="buffer.html"><code>Buffer</code></a>. If 1986<code>generator</code> is not specified, the value <code>2</code> is used.</p> 1987<p>If <code>primeEncoding</code> is specified, <code>prime</code> is expected to be a string; otherwise 1988a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or <code>DataView</code> is expected.</p> 1989<p>If <code>generatorEncoding</code> is specified, <code>generator</code> is expected to be a string; 1990otherwise a number, <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or <code>DataView</code> is expected.</p> 1991<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> 1992<div class="api_metadata"> 1993<span>Added in: v0.5.0</span> 1994</div> 1995<ul> 1996<li><code>primeLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1997<li><code>generator</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> <strong>Default:</strong> <code>2</code></li> 1998<li>Returns: <a href="crypto.html#crypto_class_diffiehellman" class="type"><DiffieHellman></a></li> 1999</ul> 2000<p>Creates a <code>DiffieHellman</code> key exchange object and generates a prime of 2001<code>primeLength</code> bits using an optional specific numeric <code>generator</code>. 2002If <code>generator</code> is not specified, the value <code>2</code> is used.</p> 2003<h4><code>crypto.createDiffieHellmanGroup(name)</code><span><a class="mark" href="#crypto_crypto_creatediffiehellmangroup_name" id="crypto_crypto_creatediffiehellmangroup_name">#</a></span></h4> 2004<div class="api_metadata"> 2005<span>Added in: v0.9.3</span> 2006</div> 2007<ul> 2008<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2009<li>Returns: <a href="crypto.html#crypto_class_diffiehellmangroup" class="type"><DiffieHellmanGroup></a></li> 2010</ul> 2011<p>An alias for <a href="#crypto_crypto_getdiffiehellman_groupname"><code>crypto.getDiffieHellman()</code></a></p> 2012<h4><code>crypto.createECDH(curveName)</code><span><a class="mark" href="#crypto_crypto_createecdh_curvename" id="crypto_crypto_createecdh_curvename">#</a></span></h4> 2013<div class="api_metadata"> 2014<span>Added in: v0.11.14</span> 2015</div> 2016<ul> 2017<li><code>curveName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2018<li>Returns: <a href="crypto.html#crypto_class_ecdh" class="type"><ECDH></a></li> 2019</ul> 2020<p>Creates an Elliptic Curve Diffie-Hellman (<code>ECDH</code>) key exchange object using a 2021predefined curve specified by the <code>curveName</code> string. Use 2022<a href="#crypto_crypto_getcurves"><code>crypto.getCurves()</code></a> to obtain a list of available curve names. On recent 2023OpenSSL releases, <code>openssl ecparam -list_curves</code> will also display the name 2024and description of each available elliptic curve.</p> 2025<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> 2026<div class="api_metadata"> 2027<details class="changelog"><summary>History</summary> 2028<table> 2029<tbody><tr><th>Version</th><th>Changes</th></tr> 2030<tr><td>v12.8.0</td> 2031<td><p>The <code>outputLength</code> option was added for XOF hash functions.</p></td></tr> 2032<tr><td>v0.1.92</td> 2033<td><p><span>Added in: v0.1.92</span></p></td></tr> 2034</tbody></table> 2035</details> 2036</div> 2037<ul> 2038<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2039<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <a href="stream.html#stream_new_stream_transform_options"><code>stream.transform</code> options</a></li> 2040<li>Returns: <a href="crypto.html#crypto_class_hash" class="type"><Hash></a></li> 2041</ul> 2042<p>Creates and returns a <code>Hash</code> object that can be used to generate hash digests 2043using the given <code>algorithm</code>. Optional <code>options</code> argument controls stream 2044behavior. For XOF hash functions such as <code>'shake256'</code>, the <code>outputLength</code> option 2045can be used to specify the desired output length in bytes.</p> 2046<p>The <code>algorithm</code> is dependent on the available algorithms supported by the 2047version of OpenSSL on the platform. Examples are <code>'sha256'</code>, <code>'sha512'</code>, etc. 2048On recent releases of OpenSSL, <code>openssl list -digest-algorithms</code> 2049(<code>openssl list-message-digest-algorithms</code> for older versions of OpenSSL) will 2050display the available digest algorithms.</p> 2051<p>Example: generating the sha256 sum of a file</p> 2052<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>]; 2053<span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>); 2054<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>); 2055 2056<span class="hljs-keyword">const</span> hash = crypto.<span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>); 2057 2058<span class="hljs-keyword">const</span> input = fs.<span class="hljs-title function_">createReadStream</span>(filename); 2059input.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> { 2060 <span class="hljs-comment">// Only one element is going to be produced by the</span> 2061 <span class="hljs-comment">// hash stream.</span> 2062 <span class="hljs-keyword">const</span> data = input.<span class="hljs-title function_">read</span>(); 2063 <span class="hljs-keyword">if</span> (data) 2064 hash.<span class="hljs-title function_">update</span>(data); 2065 <span class="hljs-keyword">else</span> { 2066 <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>); 2067 } 2068});</code></pre> 2069<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> 2070<div class="api_metadata"> 2071<details class="changelog"><summary>History</summary> 2072<table> 2073<tbody><tr><th>Version</th><th>Changes</th></tr> 2074<tr><td>v11.6.0</td> 2075<td><p>The <code>key</code> argument can now be a <code>KeyObject</code>.</p></td></tr> 2076<tr><td>v0.1.94</td> 2077<td><p><span>Added in: v0.1.94</span></p></td></tr> 2078</tbody></table> 2079</details> 2080</div> 2081<ul> 2082<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2083<li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> | <a href="crypto.html#crypto_class_keyobject" class="type"><KeyObject></a></li> 2084<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <a href="stream.html#stream_new_stream_transform_options"><code>stream.transform</code> options</a></li> 2085<li>Returns: <a href="crypto.html#crypto_class_hmac" class="type"><Hmac></a></li> 2086</ul> 2087<p>Creates and returns an <code>Hmac</code> object that uses the given <code>algorithm</code> and <code>key</code>. 2088Optional <code>options</code> argument controls stream behavior.</p> 2089<p>The <code>algorithm</code> is dependent on the available algorithms supported by the 2090version of OpenSSL on the platform. Examples are <code>'sha256'</code>, <code>'sha512'</code>, etc. 2091On recent releases of OpenSSL, <code>openssl list -digest-algorithms</code> 2092(<code>openssl list-message-digest-algorithms</code> for older versions of OpenSSL) will 2093display the available digest algorithms.</p> 2094<p>The <code>key</code> is the HMAC key used to generate the cryptographic HMAC hash. If it is 2095a <a href="#crypto_class_keyobject"><code>KeyObject</code></a>, its type must be <code>secret</code>.</p> 2096<p>Example: generating the sha256 HMAC of a file</p> 2097<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>]; 2098<span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>); 2099<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>); 2100 2101<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>); 2102 2103<span class="hljs-keyword">const</span> input = fs.<span class="hljs-title function_">createReadStream</span>(filename); 2104input.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> { 2105 <span class="hljs-comment">// Only one element is going to be produced by the</span> 2106 <span class="hljs-comment">// hash stream.</span> 2107 <span class="hljs-keyword">const</span> data = input.<span class="hljs-title function_">read</span>(); 2108 <span class="hljs-keyword">if</span> (data) 2109 hmac.<span class="hljs-title function_">update</span>(data); 2110 <span class="hljs-keyword">else</span> { 2111 <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>); 2112 } 2113});</code></pre> 2114<h4><code>crypto.createPrivateKey(key)</code><span><a class="mark" href="#crypto_crypto_createprivatekey_key" id="crypto_crypto_createprivatekey_key">#</a></span></h4> 2115<div class="api_metadata"> 2116<span>Added in: v11.6.0</span> 2117</div> 2118<ul> 2119<li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> 2120<ul> 2121<li><code>key</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> The key material, either in PEM or DER format.</li> 2122<li><code>format</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Must be <code>'pem'</code> or <code>'der'</code>. <strong>Default:</strong> <code>'pem'</code>.</li> 2123<li><code>type</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Must be <code>'pkcs1'</code>, <code>'pkcs8'</code> or <code>'sec1'</code>. This option is 2124required only if the <code>format</code> is <code>'der'</code> and ignored if it is <code>'pem'</code>.</li> 2125<li><code>passphrase</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> The passphrase to use for decryption.</li> 2126</ul> 2127</li> 2128<li>Returns: <a href="crypto.html#crypto_class_keyobject" class="type"><KeyObject></a></li> 2129</ul> 2130<p>Creates and returns a new key object containing a private key. If <code>key</code> is a 2131string or <code>Buffer</code>, <code>format</code> is assumed to be <code>'pem'</code>; otherwise, <code>key</code> 2132must be an object with the properties described above.</p> 2133<p>If the private key is encrypted, a <code>passphrase</code> must be specified. The length 2134of the passphrase is limited to 1024 bytes.</p> 2135<h4><code>crypto.createPublicKey(key)</code><span><a class="mark" href="#crypto_crypto_createpublickey_key" id="crypto_crypto_createpublickey_key">#</a></span></h4> 2136<div class="api_metadata"> 2137<details class="changelog"><summary>History</summary> 2138<table> 2139<tbody><tr><th>Version</th><th>Changes</th></tr> 2140<tr><td>v11.13.0</td> 2141<td><p>The <code>key</code> argument can now be a <code>KeyObject</code> with type <code>private</code>.</p></td></tr> 2142<tr><td>v11.7.0</td> 2143<td><p>The <code>key</code> argument can now be a private key.</p></td></tr> 2144<tr><td>v11.6.0</td> 2145<td><p><span>Added in: v11.6.0</span></p></td></tr> 2146</tbody></table> 2147</details> 2148</div> 2149<ul> 2150<li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="crypto.html#crypto_class_keyobject" class="type"><KeyObject></a> 2151<ul> 2152<li><code>key</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li> 2153<li><code>format</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Must be <code>'pem'</code> or <code>'der'</code>. <strong>Default:</strong> <code>'pem'</code>.</li> 2154<li><code>type</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Must be <code>'pkcs1'</code> or <code>'spki'</code>. This option is required 2155only if the <code>format</code> is <code>'der'</code>.</li> 2156</ul> 2157</li> 2158<li>Returns: <a href="crypto.html#crypto_class_keyobject" class="type"><KeyObject></a></li> 2159</ul> 2160<p>Creates and returns a new key object containing a public key. If <code>key</code> is a 2161string or <code>Buffer</code>, <code>format</code> is assumed to be <code>'pem'</code>; if <code>key</code> is a <code>KeyObject</code> 2162with type <code>'private'</code>, the public key is derived from the given private key; 2163otherwise, <code>key</code> must be an object with the properties described above.</p> 2164<p>If the format is <code>'pem'</code>, the <code>'key'</code> may also be an X.509 certificate.</p> 2165<p>Because public keys can be derived from private keys, a private key may be 2166passed instead of a public key. In that case, this function behaves as if 2167<a href="#crypto_crypto_createprivatekey_key"><code>crypto.createPrivateKey()</code></a> had been called, except that the type of the 2168returned <code>KeyObject</code> will be <code>'public'</code> and that the private key cannot be 2169extracted from the returned <code>KeyObject</code>. Similarly, if a <code>KeyObject</code> with type 2170<code>'private'</code> is given, a new <code>KeyObject</code> with type <code>'public'</code> will be returned 2171and it will be impossible to extract the private key from the returned object.</p> 2172<h4><code>crypto.createSecretKey(key)</code><span><a class="mark" href="#crypto_crypto_createsecretkey_key" id="crypto_crypto_createsecretkey_key">#</a></span></h4> 2173<div class="api_metadata"> 2174<span>Added in: v11.6.0</span> 2175</div> 2176<ul> 2177<li><code>key</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 2178<li>Returns: <a href="crypto.html#crypto_class_keyobject" class="type"><KeyObject></a></li> 2179</ul> 2180<p>Creates and returns a new key object containing a secret key for symmetric 2181encryption or <code>Hmac</code>.</p> 2182<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> 2183<div class="api_metadata"> 2184<span>Added in: v0.1.92</span> 2185</div> 2186<ul> 2187<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2188<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <a href="stream.html#stream_new_stream_writable_options"><code>stream.Writable</code> options</a></li> 2189<li>Returns: <a href="crypto.html#crypto_class_sign" class="type"><Sign></a></li> 2190</ul> 2191<p>Creates and returns a <code>Sign</code> object that uses the given <code>algorithm</code>. Use 2192<a href="#crypto_crypto_gethashes"><code>crypto.getHashes()</code></a> to obtain the names of the available digest algorithms. 2193Optional <code>options</code> argument controls the <code>stream.Writable</code> behavior.</p> 2194<p>In some cases, a <code>Sign</code> instance can be created using the name of a signature 2195algorithm, such as <code>'RSA-SHA256'</code>, instead of a digest algorithm. This will use 2196the corresponding digest algorithm. This does not work for all signature 2197algorithms, such as <code>'ecdsa-with-SHA256'</code>, so it is best to always use digest 2198algorithm names.</p> 2199<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> 2200<div class="api_metadata"> 2201<span>Added in: v0.1.92</span> 2202</div> 2203<ul> 2204<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2205<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> <a href="stream.html#stream_new_stream_writable_options"><code>stream.Writable</code> options</a></li> 2206<li>Returns: <a href="crypto.html#crypto_class_verify" class="type"><Verify></a></li> 2207</ul> 2208<p>Creates and returns a <code>Verify</code> object that uses the given algorithm. 2209Use <a href="#crypto_crypto_gethashes"><code>crypto.getHashes()</code></a> to obtain an array of names of the available 2210signing algorithms. Optional <code>options</code> argument controls the 2211<code>stream.Writable</code> behavior.</p> 2212<p>In some cases, a <code>Verify</code> instance can be created using the name of a signature 2213algorithm, such as <code>'RSA-SHA256'</code>, instead of a digest algorithm. This will use 2214the corresponding digest algorithm. This does not work for all signature 2215algorithms, such as <code>'ecdsa-with-SHA256'</code>, so it is best to always use digest 2216algorithm names.</p> 2217<h4><code>crypto.diffieHellman(options)</code><span><a class="mark" href="#crypto_crypto_diffiehellman_options" id="crypto_crypto_diffiehellman_options">#</a></span></h4> 2218<div class="api_metadata"> 2219<span>Added in: v13.9.0</span> 2220</div> 2221<ul> 2222<li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2223<ul> 2224<li><code>privateKey</code>: <a href="crypto.html#crypto_class_keyobject" class="type"><KeyObject></a></li> 2225<li><code>publicKey</code>: <a href="crypto.html#crypto_class_keyobject" class="type"><KeyObject></a></li> 2226</ul> 2227</li> 2228<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li> 2229</ul> 2230<p>Computes the Diffie-Hellman secret based on a <code>privateKey</code> and a <code>publicKey</code>. 2231Both keys must have the same <code>asymmetricKeyType</code>, which must be one of <code>'dh'</code> 2232(for Diffie-Hellman), <code>'ec'</code> (for ECDH), <code>'x448'</code>, or <code>'x25519'</code> (for ECDH-ES).</p> 2233<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> 2234<div class="api_metadata"> 2235<details class="changelog"><summary>History</summary> 2236<table> 2237<tbody><tr><th>Version</th><th>Changes</th></tr> 2238<tr><td>v13.9.0</td> 2239<td><p>Add support for Diffie-Hellman.</p></td></tr> 2240<tr><td>v12.0.0</td> 2241<td><p>Add ability to generate X25519 and X448 key pairs.</p></td></tr> 2242<tr><td>v12.0.0</td> 2243<td><p>Add ability to generate Ed25519 and Ed448 key pairs.</p></td></tr> 2244<tr><td>v11.6.0</td> 2245<td><p>The <code>generateKeyPair</code> and <code>generateKeyPairSync</code> functions now produce key objects if no encoding was specified.</p></td></tr> 2246<tr><td>v10.12.0</td> 2247<td><p><span>Added in: v10.12.0</span></p></td></tr> 2248</tbody></table> 2249</details> 2250</div> 2251<ul> 2252<li><code>type</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Must be <code>'rsa'</code>, <code>'dsa'</code>, <code>'ec'</code>, <code>'ed25519'</code>, <code>'ed448'</code>, 2253<code>'x25519'</code>, <code>'x448'</code>, or <code>'dh'</code>.</li> 2254<li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2255<ul> 2256<li><code>modulusLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Key size in bits (RSA, DSA).</li> 2257<li><code>publicExponent</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Public exponent (RSA). <strong>Default:</strong> <code>0x10001</code>.</li> 2258<li><code>divisorLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Size of <code>q</code> in bits (DSA).</li> 2259<li><code>namedCurve</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Name of the curve to use (EC).</li> 2260<li><code>prime</code>: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> The prime parameter (DH).</li> 2261<li><code>primeLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Prime length in bits (DH).</li> 2262<li><code>generator</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Custom generator (DH). <strong>Default:</strong> <code>2</code>.</li> 2263<li><code>groupName</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Diffie-Hellman group name (DH). See 2264<a href="#crypto_crypto_getdiffiehellman_groupname"><code>crypto.getDiffieHellman()</code></a>.</li> 2265<li><code>publicKeyEncoding</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> See <a href="#crypto_keyobject_export_options"><code>keyObject.export()</code></a>.</li> 2266<li><code>privateKeyEncoding</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> See <a href="#crypto_keyobject_export_options"><code>keyObject.export()</code></a>.</li> 2267</ul> 2268</li> 2269<li><code>callback</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 2270<ul> 2271<li><code>err</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 2272<li><code>publicKey</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="crypto.html#crypto_class_keyobject" class="type"><KeyObject></a></li> 2273<li><code>privateKey</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="crypto.html#crypto_class_keyobject" class="type"><KeyObject></a></li> 2274</ul> 2275</li> 2276</ul> 2277<p>Generates a new asymmetric key pair of the given <code>type</code>. RSA, DSA, EC, Ed25519, 2278Ed448, X25519, X448, and DH are currently supported.</p> 2279<p>If a <code>publicKeyEncoding</code> or <code>privateKeyEncoding</code> was specified, this function 2280behaves as if <a href="#crypto_keyobject_export_options"><code>keyObject.export()</code></a> had been called on its result. Otherwise, 2281the respective part of the key is returned as a <a href="#crypto_class_keyobject"><code>KeyObject</code></a>.</p> 2282<p>It is recommended to encode public keys as <code>'spki'</code> and private keys as 2283<code>'pkcs8'</code> with encryption for long-term storage:</p> 2284<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>); 2285<span class="hljs-title function_">generateKeyPair</span>(<span class="hljs-string">'rsa'</span>, { 2286 <span class="hljs-attr">modulusLength</span>: <span class="hljs-number">4096</span>, 2287 <span class="hljs-attr">publicKeyEncoding</span>: { 2288 <span class="hljs-attr">type</span>: <span class="hljs-string">'spki'</span>, 2289 <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span> 2290 }, 2291 <span class="hljs-attr">privateKeyEncoding</span>: { 2292 <span class="hljs-attr">type</span>: <span class="hljs-string">'pkcs8'</span>, 2293 <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>, 2294 <span class="hljs-attr">cipher</span>: <span class="hljs-string">'aes-256-cbc'</span>, 2295 <span class="hljs-attr">passphrase</span>: <span class="hljs-string">'top secret'</span> 2296 } 2297}, <span class="hljs-function">(<span class="hljs-params">err, publicKey, privateKey</span>) =></span> { 2298 <span class="hljs-comment">// Handle errors and use the generated key pair.</span> 2299});</code></pre> 2300<p>On completion, <code>callback</code> will be called with <code>err</code> set to <code>undefined</code> and 2301<code>publicKey</code> / <code>privateKey</code> representing the generated key pair.</p> 2302<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 2303a <code>Promise</code> for an <code>Object</code> with <code>publicKey</code> and <code>privateKey</code> properties.</p> 2304<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> 2305<div class="api_metadata"> 2306<details class="changelog"><summary>History</summary> 2307<table> 2308<tbody><tr><th>Version</th><th>Changes</th></tr> 2309<tr><td>v13.9.0</td> 2310<td><p>Add support for Diffie-Hellman.</p></td></tr> 2311<tr><td>v12.0.0</td> 2312<td><p>Add ability to generate Ed25519 and Ed448 key pairs.</p></td></tr> 2313<tr><td>v11.6.0</td> 2314<td><p>The <code>generateKeyPair</code> and <code>generateKeyPairSync</code> functions now produce key objects if no encoding was specified.</p></td></tr> 2315<tr><td>v10.12.0</td> 2316<td><p><span>Added in: v10.12.0</span></p></td></tr> 2317</tbody></table> 2318</details> 2319</div> 2320<ul> 2321<li><code>type</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Must be <code>'rsa'</code>, <code>'dsa'</code>, <code>'ec'</code>, <code>'ed25519'</code>, <code>'ed448'</code>, 2322<code>'x25519'</code>, <code>'x448'</code>, or <code>'dh'</code>.</li> 2323<li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2324<ul> 2325<li><code>modulusLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Key size in bits (RSA, DSA).</li> 2326<li><code>publicExponent</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Public exponent (RSA). <strong>Default:</strong> <code>0x10001</code>.</li> 2327<li><code>divisorLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Size of <code>q</code> in bits (DSA).</li> 2328<li><code>namedCurve</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Name of the curve to use (EC).</li> 2329<li><code>prime</code>: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> The prime parameter (DH).</li> 2330<li><code>primeLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Prime length in bits (DH).</li> 2331<li><code>generator</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Custom generator (DH). <strong>Default:</strong> <code>2</code>.</li> 2332<li><code>groupName</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Diffie-Hellman group name (DH). See 2333<a href="#crypto_crypto_getdiffiehellman_groupname"><code>crypto.getDiffieHellman()</code></a>.</li> 2334<li><code>publicKeyEncoding</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> See <a href="#crypto_keyobject_export_options"><code>keyObject.export()</code></a>.</li> 2335<li><code>privateKeyEncoding</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> See <a href="#crypto_keyobject_export_options"><code>keyObject.export()</code></a>.</li> 2336</ul> 2337</li> 2338<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2339<ul> 2340<li><code>publicKey</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="crypto.html#crypto_class_keyobject" class="type"><KeyObject></a></li> 2341<li><code>privateKey</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="crypto.html#crypto_class_keyobject" class="type"><KeyObject></a></li> 2342</ul> 2343</li> 2344</ul> 2345<p>Generates a new asymmetric key pair of the given <code>type</code>. RSA, DSA, EC, Ed25519, 2346Ed448, X25519, X448, and DH are currently supported.</p> 2347<p>If a <code>publicKeyEncoding</code> or <code>privateKeyEncoding</code> was specified, this function 2348behaves as if <a href="#crypto_keyobject_export_options"><code>keyObject.export()</code></a> had been called on its result. Otherwise, 2349the respective part of the key is returned as a <a href="#crypto_class_keyobject"><code>KeyObject</code></a>.</p> 2350<p>When encoding public keys, it is recommended to use <code>'spki'</code>. When encoding 2351private keys, it is recommended to use <code>'pkcs8'</code> with a strong passphrase, 2352and to keep the passphrase confidential.</p> 2353<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>); 2354<span class="hljs-keyword">const</span> { publicKey, privateKey } = <span class="hljs-title function_">generateKeyPairSync</span>(<span class="hljs-string">'rsa'</span>, { 2355 <span class="hljs-attr">modulusLength</span>: <span class="hljs-number">4096</span>, 2356 <span class="hljs-attr">publicKeyEncoding</span>: { 2357 <span class="hljs-attr">type</span>: <span class="hljs-string">'spki'</span>, 2358 <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span> 2359 }, 2360 <span class="hljs-attr">privateKeyEncoding</span>: { 2361 <span class="hljs-attr">type</span>: <span class="hljs-string">'pkcs8'</span>, 2362 <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>, 2363 <span class="hljs-attr">cipher</span>: <span class="hljs-string">'aes-256-cbc'</span>, 2364 <span class="hljs-attr">passphrase</span>: <span class="hljs-string">'top secret'</span> 2365 } 2366});</code></pre> 2367<p>The return value <code>{ publicKey, privateKey }</code> represents the generated key pair. 2368When PEM encoding was selected, the respective key will be a string, otherwise 2369it will be a buffer containing the data encoded as DER.</p> 2370<h4><code>crypto.getCiphers()</code><span><a class="mark" href="#crypto_crypto_getciphers" id="crypto_crypto_getciphers">#</a></span></h4> 2371<div class="api_metadata"> 2372<span>Added in: v0.9.3</span> 2373</div> 2374<ul> 2375<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> An array with the names of the supported cipher 2376algorithms.</li> 2377</ul> 2378<pre><code class="language-js"><span class="hljs-keyword">const</span> ciphers = crypto.<span class="hljs-title function_">getCiphers</span>(); 2379<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> 2380<h4><code>crypto.getCurves()</code><span><a class="mark" href="#crypto_crypto_getcurves" id="crypto_crypto_getcurves">#</a></span></h4> 2381<div class="api_metadata"> 2382<span>Added in: v2.3.0</span> 2383</div> 2384<ul> 2385<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> An array with the names of the supported elliptic curves.</li> 2386</ul> 2387<pre><code class="language-js"><span class="hljs-keyword">const</span> curves = crypto.<span class="hljs-title function_">getCurves</span>(); 2388<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> 2389<h4><code>crypto.getDiffieHellman(groupName)</code><span><a class="mark" href="#crypto_crypto_getdiffiehellman_groupname" id="crypto_crypto_getdiffiehellman_groupname">#</a></span></h4> 2390<div class="api_metadata"> 2391<span>Added in: v0.7.5</span> 2392</div> 2393<ul> 2394<li><code>groupName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2395<li>Returns: <a href="crypto.html#crypto_class_diffiehellmangroup" class="type"><DiffieHellmanGroup></a></li> 2396</ul> 2397<p>Creates a predefined <code>DiffieHellmanGroup</code> key exchange object. The 2398supported groups are: <code>'modp1'</code>, <code>'modp2'</code>, <code>'modp5'</code> (defined in 2399<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>, 2400<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 2401returned object mimics the interface of objects created by 2402<a href="#crypto_crypto_creatediffiehellman_prime_primeencoding_generator_generatorencoding"><code>crypto.createDiffieHellman()</code></a>, but will not allow changing 2403the keys (with <a href="#crypto_diffiehellman_setpublickey_publickey_encoding"><code>diffieHellman.setPublicKey()</code></a>, for example). The 2404advantage of using this method is that the parties do not have to 2405generate nor exchange a group modulus beforehand, saving both processor 2406and communication time.</p> 2407<p>Example (obtaining a shared secret):</p> 2408<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>); 2409<span class="hljs-keyword">const</span> alice = crypto.<span class="hljs-title function_">getDiffieHellman</span>(<span class="hljs-string">'modp14'</span>); 2410<span class="hljs-keyword">const</span> bob = crypto.<span class="hljs-title function_">getDiffieHellman</span>(<span class="hljs-string">'modp14'</span>); 2411 2412alice.<span class="hljs-title function_">generateKeys</span>(); 2413bob.<span class="hljs-title function_">generateKeys</span>(); 2414 2415<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>); 2416<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>); 2417 2418<span class="hljs-comment">/* aliceSecret and bobSecret should be the same */</span> 2419<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(aliceSecret === bobSecret);</code></pre> 2420<h4><code>crypto.getFips()</code><span><a class="mark" href="#crypto_crypto_getfips" id="crypto_crypto_getfips">#</a></span></h4> 2421<div class="api_metadata"> 2422<span>Added in: v10.0.0</span> 2423</div> 2424<ul> 2425<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> <code>1</code> if and only if a FIPS compliant crypto provider is 2426currently in use, <code>0</code> otherwise. A future semver-major release may change 2427the return type of this API to a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a>.</li> 2428</ul> 2429<h4><code>crypto.getHashes()</code><span><a class="mark" href="#crypto_crypto_gethashes" id="crypto_crypto_gethashes">#</a></span></h4> 2430<div class="api_metadata"> 2431<span>Added in: v0.9.3</span> 2432</div> 2433<ul> 2434<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> An array of the names of the supported hash algorithms, 2435such as <code>'RSA-SHA256'</code>. Hash algorithms are also called "digest" algorithms.</li> 2436</ul> 2437<pre><code class="language-js"><span class="hljs-keyword">const</span> hashes = crypto.<span class="hljs-title function_">getHashes</span>(); 2438<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> 2439<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> 2440<div class="api_metadata"> 2441<details class="changelog"><summary>History</summary> 2442<table> 2443<tbody><tr><th>Version</th><th>Changes</th></tr> 2444<tr><td>v14.0.0</td> 2445<td><p>The <code>iterations</code> parameter is now restricted to positive values. Earlier releases treated other values as one.</p></td></tr> 2446<tr><td>v8.0.0</td> 2447<td><p>The <code>digest</code> parameter is always required now.</p></td></tr> 2448<tr><td>v6.0.0</td> 2449<td><p>Calling this function without passing the <code>digest</code> parameter is deprecated now and will emit a warning.</p></td></tr> 2450<tr><td>v6.0.0</td> 2451<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> 2452<tr><td>v0.5.5</td> 2453<td><p><span>Added in: v0.5.5</span></p></td></tr> 2454</tbody></table> 2455</details> 2456</div> 2457<ul> 2458<li><code>password</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 2459<li><code>salt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 2460<li><code>iterations</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 2461<li><code>keylen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 2462<li><code>digest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2463<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 2464<ul> 2465<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 2466<li><code>derivedKey</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li> 2467</ul> 2468</li> 2469</ul> 2470<p>Provides an asynchronous Password-Based Key Derivation Function 2 (PBKDF2) 2471implementation. A selected HMAC digest algorithm specified by <code>digest</code> is 2472applied to derive a key of the requested byte length (<code>keylen</code>) from the 2473<code>password</code>, <code>salt</code> and <code>iterations</code>.</p> 2474<p>The supplied <code>callback</code> function is called with two arguments: <code>err</code> and 2475<code>derivedKey</code>. If an error occurs while deriving the key, <code>err</code> will be set; 2476otherwise <code>err</code> will be <code>null</code>. By default, the successfully generated 2477<code>derivedKey</code> will be passed to the callback as a <a href="buffer.html"><code>Buffer</code></a>. An error will be 2478thrown if any of the input arguments specify invalid values or types.</p> 2479<p>If <code>digest</code> is <code>null</code>, <code>'sha1'</code> will be used. This behavior is deprecated, 2480please specify a <code>digest</code> explicitly.</p> 2481<p>The <code>iterations</code> argument must be a number set as high as possible. The 2482higher the number of iterations, the more secure the derived key will be, 2483but will take a longer amount of time to complete.</p> 2484<p>The <code>salt</code> should be as unique as possible. It is recommended that a salt is 2485random 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> 2486<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>); 2487crypto.<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> { 2488 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2489 <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> 2490});</code></pre> 2491<p>The <code>crypto.DEFAULT_ENCODING</code> property can be used to change the way the 2492<code>derivedKey</code> is passed to the callback. This property, however, has been 2493deprecated and use should be avoided.</p> 2494<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>); 2495crypto.<span class="hljs-property">DEFAULT_ENCODING</span> = <span class="hljs-string">'hex'</span>; 2496crypto.<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> { 2497 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2498 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(derivedKey); <span class="hljs-comment">// '3745e48...aa39b34'</span> 2499});</code></pre> 2500<p>An array of supported digest functions can be retrieved using 2501<a href="#crypto_crypto_gethashes"><code>crypto.getHashes()</code></a>.</p> 2502<p>This API uses libuv's threadpool, which can have surprising and 2503negative performance implications for some applications; see the 2504<a href="cli.html#cli_uv_threadpool_size_size"><code>UV_THREADPOOL_SIZE</code></a> documentation for more information.</p> 2505<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> 2506<div class="api_metadata"> 2507<details class="changelog"><summary>History</summary> 2508<table> 2509<tbody><tr><th>Version</th><th>Changes</th></tr> 2510<tr><td>v14.0.0</td> 2511<td><p>The <code>iterations</code> parameter is now restricted to positive values. Earlier releases treated other values as one.</p></td></tr> 2512<tr><td>v6.0.0</td> 2513<td><p>Calling this function without passing the <code>digest</code> parameter is deprecated now and will emit a warning.</p></td></tr> 2514<tr><td>v6.0.0</td> 2515<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> 2516<tr><td>v0.9.3</td> 2517<td><p><span>Added in: v0.9.3</span></p></td></tr> 2518</tbody></table> 2519</details> 2520</div> 2521<ul> 2522<li><code>password</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 2523<li><code>salt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 2524<li><code>iterations</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 2525<li><code>keylen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 2526<li><code>digest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2527<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li> 2528</ul> 2529<p>Provides a synchronous Password-Based Key Derivation Function 2 (PBKDF2) 2530implementation. A selected HMAC digest algorithm specified by <code>digest</code> is 2531applied to derive a key of the requested byte length (<code>keylen</code>) from the 2532<code>password</code>, <code>salt</code> and <code>iterations</code>.</p> 2533<p>If an error occurs an <code>Error</code> will be thrown, otherwise the derived key will be 2534returned as a <a href="buffer.html"><code>Buffer</code></a>.</p> 2535<p>If <code>digest</code> is <code>null</code>, <code>'sha1'</code> will be used. This behavior is deprecated, 2536please specify a <code>digest</code> explicitly.</p> 2537<p>The <code>iterations</code> argument must be a number set as high as possible. The 2538higher the number of iterations, the more secure the derived key will be, 2539but will take a longer amount of time to complete.</p> 2540<p>The <code>salt</code> should be as unique as possible. It is recommended that a salt is 2541random 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> 2542<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>); 2543<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>); 2544<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> 2545<p>The <code>crypto.DEFAULT_ENCODING</code> property may be used to change the way the 2546<code>derivedKey</code> is returned. This property, however, is deprecated and use 2547should be avoided.</p> 2548<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>); 2549crypto.<span class="hljs-property">DEFAULT_ENCODING</span> = <span class="hljs-string">'hex'</span>; 2550<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>); 2551<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(key); <span class="hljs-comment">// '3745e48...aa39b34'</span></code></pre> 2552<p>An array of supported digest functions can be retrieved using 2553<a href="#crypto_crypto_gethashes"><code>crypto.getHashes()</code></a>.</p> 2554<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> 2555<div class="api_metadata"> 2556<details class="changelog"><summary>History</summary> 2557<table> 2558<tbody><tr><th>Version</th><th>Changes</th></tr> 2559<tr><td>v12.11.0</td> 2560<td><p>The <code>oaepLabel</code> option was added.</p></td></tr> 2561<tr><td>v12.9.0</td> 2562<td><p>The <code>oaepHash</code> option was added.</p></td></tr> 2563<tr><td>v11.6.0</td> 2564<td><p>This function now supports key objects.</p></td></tr> 2565<tr><td>v0.11.14</td> 2566<td><p><span>Added in: v0.11.14</span></p></td></tr> 2567</tbody></table> 2568</details> 2569</div> 2570<ul> 2571<li><code>privateKey</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="crypto.html#crypto_class_keyobject" class="type"><KeyObject></a> 2572<ul> 2573<li><code>oaepHash</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The hash function to use for OAEP padding and MGF1. 2574<strong>Default:</strong> <code>'sha1'</code></li> 2575<li><code>oaepLabel</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> The label to use for OAEP 2576padding. If not specified, no label is used.</li> 2577<li><code>padding</code> <a href="crypto.html#crypto_crypto_constants_1" class="type"><crypto.constants></a> An optional padding value defined in 2578<code>crypto.constants</code>, which may be: <code>crypto.constants.RSA_NO_PADDING</code>, 2579<code>crypto.constants.RSA_PKCS1_PADDING</code>, or 2580<code>crypto.constants.RSA_PKCS1_OAEP_PADDING</code>.</li> 2581</ul> 2582</li> 2583<li><code>buffer</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 2584<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> A new <code>Buffer</code> with the decrypted content.</li> 2585</ul> 2586<p>Decrypts <code>buffer</code> with <code>privateKey</code>. <code>buffer</code> was previously encrypted using 2587the corresponding public key, for example using <a href="#crypto_crypto_publicencrypt_key_buffer"><code>crypto.publicEncrypt()</code></a>.</p> 2588<p>If <code>privateKey</code> is not a <a href="#crypto_class_keyobject"><code>KeyObject</code></a>, this function behaves as if 2589<code>privateKey</code> had been passed to <a href="#crypto_crypto_createprivatekey_key"><code>crypto.createPrivateKey()</code></a>. If it is an 2590object, the <code>padding</code> property can be passed. Otherwise, this function uses 2591<code>RSA_PKCS1_OAEP_PADDING</code>.</p> 2592<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> 2593<div class="api_metadata"> 2594<details class="changelog"><summary>History</summary> 2595<table> 2596<tbody><tr><th>Version</th><th>Changes</th></tr> 2597<tr><td>v11.6.0</td> 2598<td><p>This function now supports key objects.</p></td></tr> 2599<tr><td>v1.1.0</td> 2600<td><p><span>Added in: v1.1.0</span></p></td></tr> 2601</tbody></table> 2602</details> 2603</div> 2604<ul> 2605<li><code>privateKey</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="crypto.html#crypto_class_keyobject" class="type"><KeyObject></a> 2606<ul> 2607<li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="crypto.html#crypto_class_keyobject" class="type"><KeyObject></a> A PEM encoded private key.</li> 2608<li><code>passphrase</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> An optional passphrase for the private key.</li> 2609<li><code>padding</code> <a href="crypto.html#crypto_crypto_constants_1" class="type"><crypto.constants></a> An optional padding value defined in 2610<code>crypto.constants</code>, which may be: <code>crypto.constants.RSA_NO_PADDING</code> or 2611<code>crypto.constants.RSA_PKCS1_PADDING</code>.</li> 2612</ul> 2613</li> 2614<li><code>buffer</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 2615<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> A new <code>Buffer</code> with the encrypted content.</li> 2616</ul> 2617<p>Encrypts <code>buffer</code> with <code>privateKey</code>. The returned data can be decrypted using 2618the corresponding public key, for example using <a href="#crypto_crypto_publicdecrypt_key_buffer"><code>crypto.publicDecrypt()</code></a>.</p> 2619<p>If <code>privateKey</code> is not a <a href="#crypto_class_keyobject"><code>KeyObject</code></a>, this function behaves as if 2620<code>privateKey</code> had been passed to <a href="#crypto_crypto_createprivatekey_key"><code>crypto.createPrivateKey()</code></a>. If it is an 2621object, the <code>padding</code> property can be passed. Otherwise, this function uses 2622<code>RSA_PKCS1_PADDING</code>.</p> 2623<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> 2624<div class="api_metadata"> 2625<details class="changelog"><summary>History</summary> 2626<table> 2627<tbody><tr><th>Version</th><th>Changes</th></tr> 2628<tr><td>v11.6.0</td> 2629<td><p>This function now supports key objects.</p></td></tr> 2630<tr><td>v1.1.0</td> 2631<td><p><span>Added in: v1.1.0</span></p></td></tr> 2632</tbody></table> 2633</details> 2634</div> 2635<ul> 2636<li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="crypto.html#crypto_class_keyobject" class="type"><KeyObject></a> 2637<ul> 2638<li><code>passphrase</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> An optional passphrase for the private key.</li> 2639<li><code>padding</code> <a href="crypto.html#crypto_crypto_constants_1" class="type"><crypto.constants></a> An optional padding value defined in 2640<code>crypto.constants</code>, which may be: <code>crypto.constants.RSA_NO_PADDING</code> or 2641<code>crypto.constants.RSA_PKCS1_PADDING</code>.</li> 2642</ul> 2643</li> 2644<li><code>buffer</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 2645<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> A new <code>Buffer</code> with the decrypted content.</li> 2646</ul> 2647<p>Decrypts <code>buffer</code> with <code>key</code>.<code>buffer</code> was previously encrypted using 2648the corresponding private key, for example using <a href="#crypto_crypto_privateencrypt_privatekey_buffer"><code>crypto.privateEncrypt()</code></a>.</p> 2649<p>If <code>key</code> is not a <a href="#crypto_class_keyobject"><code>KeyObject</code></a>, this function behaves as if 2650<code>key</code> had been passed to <a href="#crypto_crypto_createpublickey_key"><code>crypto.createPublicKey()</code></a>. If it is an 2651object, the <code>padding</code> property can be passed. Otherwise, this function uses 2652<code>RSA_PKCS1_PADDING</code>.</p> 2653<p>Because RSA public keys can be derived from private keys, a private key may 2654be passed instead of a public key.</p> 2655<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> 2656<div class="api_metadata"> 2657<details class="changelog"><summary>History</summary> 2658<table> 2659<tbody><tr><th>Version</th><th>Changes</th></tr> 2660<tr><td>v12.11.0</td> 2661<td><p>The <code>oaepLabel</code> option was added.</p></td></tr> 2662<tr><td>v12.9.0</td> 2663<td><p>The <code>oaepHash</code> option was added.</p></td></tr> 2664<tr><td>v11.6.0</td> 2665<td><p>This function now supports key objects.</p></td></tr> 2666<tr><td>v0.11.14</td> 2667<td><p><span>Added in: v0.11.14</span></p></td></tr> 2668</tbody></table> 2669</details> 2670</div> 2671<ul> 2672<li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="crypto.html#crypto_class_keyobject" class="type"><KeyObject></a> 2673<ul> 2674<li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="crypto.html#crypto_class_keyobject" class="type"><KeyObject></a> A PEM encoded public or private key.</li> 2675<li><code>oaepHash</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The hash function to use for OAEP padding and MGF1. 2676<strong>Default:</strong> <code>'sha1'</code></li> 2677<li><code>oaepLabel</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> The label to use for OAEP 2678padding. If not specified, no label is used.</li> 2679<li><code>passphrase</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> An optional passphrase for the private key.</li> 2680<li><code>padding</code> <a href="crypto.html#crypto_crypto_constants_1" class="type"><crypto.constants></a> An optional padding value defined in 2681<code>crypto.constants</code>, which may be: <code>crypto.constants.RSA_NO_PADDING</code>, 2682<code>crypto.constants.RSA_PKCS1_PADDING</code>, or 2683<code>crypto.constants.RSA_PKCS1_OAEP_PADDING</code>.</li> 2684</ul> 2685</li> 2686<li><code>buffer</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 2687<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> A new <code>Buffer</code> with the encrypted content.</li> 2688</ul> 2689<p>Encrypts the content of <code>buffer</code> with <code>key</code> and returns a new 2690<a href="buffer.html"><code>Buffer</code></a> with encrypted content. The returned data can be decrypted using 2691the corresponding private key, for example using <a href="#crypto_crypto_privatedecrypt_privatekey_buffer"><code>crypto.privateDecrypt()</code></a>.</p> 2692<p>If <code>key</code> is not a <a href="#crypto_class_keyobject"><code>KeyObject</code></a>, this function behaves as if 2693<code>key</code> had been passed to <a href="#crypto_crypto_createpublickey_key"><code>crypto.createPublicKey()</code></a>. If it is an 2694object, the <code>padding</code> property can be passed. Otherwise, this function uses 2695<code>RSA_PKCS1_OAEP_PADDING</code>.</p> 2696<p>Because RSA public keys can be derived from private keys, a private key may 2697be passed instead of a public key.</p> 2698<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> 2699<div class="api_metadata"> 2700<details class="changelog"><summary>History</summary> 2701<table> 2702<tbody><tr><th>Version</th><th>Changes</th></tr> 2703<tr><td>v9.0.0</td> 2704<td><p>Passing <code>null</code> as the <code>callback</code> argument now throws <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 2705<tr><td>v0.5.8</td> 2706<td><p><span>Added in: v0.5.8</span></p></td></tr> 2707</tbody></table> 2708</details> 2709</div> 2710<ul> 2711<li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 2712<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 2713<ul> 2714<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 2715<li><code>buf</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li> 2716</ul> 2717</li> 2718<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> if the <code>callback</code> function is not provided.</li> 2719</ul> 2720<p>Generates cryptographically strong pseudo-random data. The <code>size</code> argument 2721is a number indicating the number of bytes to generate.</p> 2722<p>If a <code>callback</code> function is provided, the bytes are generated asynchronously 2723and the <code>callback</code> function is invoked with two arguments: <code>err</code> and <code>buf</code>. 2724If an error occurs, <code>err</code> will be an <code>Error</code> object; otherwise it is <code>null</code>. The 2725<code>buf</code> argument is a <a href="buffer.html"><code>Buffer</code></a> containing the generated bytes.</p> 2726<pre><code class="language-js"><span class="hljs-comment">// Asynchronous</span> 2727<span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>); 2728crypto.<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> { 2729 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2730 <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>); 2731});</code></pre> 2732<p>If the <code>callback</code> function is not provided, the random bytes are generated 2733synchronously and returned as a <a href="buffer.html"><code>Buffer</code></a>. An error will be thrown if 2734there is a problem generating the bytes.</p> 2735<pre><code class="language-js"><span class="hljs-comment">// Synchronous</span> 2736<span class="hljs-keyword">const</span> buf = crypto.<span class="hljs-title function_">randomBytes</span>(<span class="hljs-number">256</span>); 2737<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>( 2738 <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> 2739<p>The <code>crypto.randomBytes()</code> method will not complete until there is 2740sufficient entropy available. 2741This should normally never take longer than a few milliseconds. The only time 2742when generating the random bytes may conceivably block for a longer period of 2743time is right after boot, when the whole system is still low on entropy.</p> 2744<p>This API uses libuv's threadpool, which can have surprising and 2745negative performance implications for some applications; see the 2746<a href="cli.html#cli_uv_threadpool_size_size"><code>UV_THREADPOOL_SIZE</code></a> documentation for more information.</p> 2747<p>The asynchronous version of <code>crypto.randomBytes()</code> is carried out in a single 2748threadpool request. To minimize threadpool task length variation, partition 2749large <code>randomBytes</code> requests when doing so as part of fulfilling a client 2750request.</p> 2751<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> 2752<div class="api_metadata"> 2753<details class="changelog"><summary>History</summary> 2754<table> 2755<tbody><tr><th>Version</th><th>Changes</th></tr> 2756<tr><td>v9.0.0</td> 2757<td><p>The <code>buffer</code> argument may be any <code>TypedArray</code> or <code>DataView</code>.</p></td></tr> 2758<tr><td>v7.10.0, v6.13.0</td> 2759<td><p><span>Added in: v7.10.0, v6.13.0</span></p></td></tr> 2760</tbody></table> 2761</details> 2762</div> 2763<ul> 2764<li><code>buffer</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> Must be supplied.</li> 2765<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> <strong>Default:</strong> <code>0</code></li> 2766<li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> <strong>Default:</strong> <code>buffer.length - offset</code></li> 2767<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> The object passed as <code>buffer</code> argument.</li> 2768</ul> 2769<p>Synchronous version of <a href="#crypto_crypto_randomfill_buffer_offset_size_callback"><code>crypto.randomFill()</code></a>.</p> 2770<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>); 2771<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>)); 2772 2773crypto.<span class="hljs-title function_">randomFillSync</span>(buf, <span class="hljs-number">5</span>); 2774<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>)); 2775 2776<span class="hljs-comment">// The above is equivalent to the following:</span> 2777crypto.<span class="hljs-title function_">randomFillSync</span>(buf, <span class="hljs-number">5</span>, <span class="hljs-number">5</span>); 2778<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> 2779<p>Any <code>TypedArray</code> or <code>DataView</code> instance may be passed as <code>buffer</code>.</p> 2780<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>); 2781<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>, 2782 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>)); 2783 2784<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>); 2785<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>, 2786 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>)); 2787 2788<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>)); 2789<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>, 2790 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> 2791<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> 2792<div class="api_metadata"> 2793<details class="changelog"><summary>History</summary> 2794<table> 2795<tbody><tr><th>Version</th><th>Changes</th></tr> 2796<tr><td>v9.0.0</td> 2797<td><p>The <code>buffer</code> argument may be any <code>TypedArray</code> or <code>DataView</code>.</p></td></tr> 2798<tr><td>v7.10.0, v6.13.0</td> 2799<td><p><span>Added in: v7.10.0, v6.13.0</span></p></td></tr> 2800</tbody></table> 2801</details> 2802</div> 2803<ul> 2804<li><code>buffer</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> Must be supplied.</li> 2805<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> <strong>Default:</strong> <code>0</code></li> 2806<li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> <strong>Default:</strong> <code>buffer.length - offset</code></li> 2807<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <code>function(err, buf) {}</code>.</li> 2808</ul> 2809<p>This function is similar to <a href="#crypto_crypto_randombytes_size_callback"><code>crypto.randomBytes()</code></a> but requires the first 2810argument to be a <a href="buffer.html"><code>Buffer</code></a> that will be filled. It also 2811requires that a callback is passed in.</p> 2812<p>If the <code>callback</code> function is not provided, an error will be thrown.</p> 2813<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>); 2814crypto.<span class="hljs-title function_">randomFill</span>(buf, <span class="hljs-function">(<span class="hljs-params">err, buf</span>) =></span> { 2815 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2816 <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>)); 2817}); 2818 2819crypto.<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> { 2820 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2821 <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>)); 2822}); 2823 2824<span class="hljs-comment">// The above is equivalent to the following:</span> 2825crypto.<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> { 2826 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2827 <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>)); 2828});</code></pre> 2829<p>Any <code>TypedArray</code> or <code>DataView</code> instance may be passed as <code>buffer</code>.</p> 2830<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>); 2831crypto.<span class="hljs-title function_">randomFill</span>(a, <span class="hljs-function">(<span class="hljs-params">err, buf</span>) =></span> { 2832 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2833 <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>) 2834 .<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>)); 2835}); 2836 2837<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>); 2838crypto.<span class="hljs-title function_">randomFill</span>(b, <span class="hljs-function">(<span class="hljs-params">err, buf</span>) =></span> { 2839 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2840 <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>) 2841 .<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>)); 2842}); 2843 2844<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>)); 2845crypto.<span class="hljs-title function_">randomFill</span>(c, <span class="hljs-function">(<span class="hljs-params">err, buf</span>) =></span> { 2846 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2847 <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>) 2848 .<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>)); 2849});</code></pre> 2850<p>This API uses libuv's threadpool, which can have surprising and 2851negative performance implications for some applications; see the 2852<a href="cli.html#cli_uv_threadpool_size_size"><code>UV_THREADPOOL_SIZE</code></a> documentation for more information.</p> 2853<p>The asynchronous version of <code>crypto.randomFill()</code> is carried out in a single 2854threadpool request. To minimize threadpool task length variation, partition 2855large <code>randomFill</code> requests when doing so as part of fulfilling a client 2856request.</p> 2857<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> 2858<div class="api_metadata"> 2859<span>Added in: v14.10.0</span> 2860</div> 2861<ul> 2862<li><code>min</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Start of random range (inclusive). <strong>Default</strong>: <code>0</code>.</li> 2863<li><code>max</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> End of random range (exclusive).</li> 2864<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <code>function(err, n) {}</code>.</li> 2865</ul> 2866<p>Return a random integer <code>n</code> such that <code>min <= n < max</code>. This 2867implementation avoids <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#Modulo_bias">modulo bias</a>.</p> 2868<p>The range (<code>max - min</code>) must be less than 2<sup>48</sup>. <code>min</code> and <code>max</code> must 2869be <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isSafeInteger">safe integers</a>.</p> 2870<p>If the <code>callback</code> function is not provided, the random integer is 2871generated synchronously.</p> 2872<pre><code class="language-js"><span class="hljs-comment">// Asynchronous</span> 2873crypto.<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> { 2874 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2875 <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>); 2876});</code></pre> 2877<pre><code class="language-js"><span class="hljs-comment">// Synchronous</span> 2878<span class="hljs-keyword">const</span> n = crypto.<span class="hljs-title function_">randomInt</span>(<span class="hljs-number">3</span>); 2879<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> 2880<pre><code class="language-js"><span class="hljs-comment">// With `min` argument</span> 2881<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>); 2882<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> 2883<h4><code>crypto.randomUUID([options])</code><span><a class="mark" href="#crypto_crypto_randomuuid_options" id="crypto_crypto_randomuuid_options">#</a></span></h4> 2884<div class="api_metadata"> 2885<span>Added in: v14.17.0</span> 2886</div> 2887<ul> 2888<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2889<ul> 2890<li><code>disableEntropyCache</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> By default, to improve performance, 2891Node.js generates and caches enough random data to generate up to 2892128 random UUIDs. To generate a UUID without using the cache, set 2893<code>disableEntropyCache</code> to <code>true</code>. <strong>Defaults</strong>: <code>false</code>.</li> 2894</ul> 2895</li> 2896<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2897</ul> 2898<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 2899cryptographic pseudorandom number generator.</p> 2900<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> 2901<div class="api_metadata"> 2902<details class="changelog"><summary>History</summary> 2903<table> 2904<tbody><tr><th>Version</th><th>Changes</th></tr> 2905<tr><td>v12.8.0, v10.17.0</td> 2906<td><p>The <code>maxmem</code> value can now be any safe integer.</p></td></tr> 2907<tr><td>v10.9.0</td> 2908<td><p>The <code>cost</code>, <code>blockSize</code> and <code>parallelization</code> option names have been added.</p></td></tr> 2909<tr><td>v10.5.0</td> 2910<td><p><span>Added in: v10.5.0</span></p></td></tr> 2911</tbody></table> 2912</details> 2913</div> 2914<ul> 2915<li><code>password</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 2916<li><code>salt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 2917<li><code>keylen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 2918<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2919<ul> 2920<li><code>cost</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> CPU/memory cost parameter. Must be a power of two greater 2921than one. <strong>Default:</strong> <code>16384</code>.</li> 2922<li><code>blockSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Block size parameter. <strong>Default:</strong> <code>8</code>.</li> 2923<li><code>parallelization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Parallelization parameter. <strong>Default:</strong> <code>1</code>.</li> 2924<li><code>N</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Alias for <code>cost</code>. Only one of both may be specified.</li> 2925<li><code>r</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Alias for <code>blockSize</code>. Only one of both may be specified.</li> 2926<li><code>p</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Alias for <code>parallelization</code>. Only one of both may be specified.</li> 2927<li><code>maxmem</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Memory upper bound. It is an error when (approximately) 2928<code>128 * N * r > maxmem</code>. <strong>Default:</strong> <code>32 * 1024 * 1024</code>.</li> 2929</ul> 2930</li> 2931<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 2932<ul> 2933<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 2934<li><code>derivedKey</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li> 2935</ul> 2936</li> 2937</ul> 2938<p>Provides an asynchronous <a href="https://en.wikipedia.org/wiki/Scrypt">scrypt</a> implementation. Scrypt is a password-based 2939key derivation function that is designed to be expensive computationally and 2940memory-wise in order to make brute-force attacks unrewarding.</p> 2941<p>The <code>salt</code> should be as unique as possible. It is recommended that a salt is 2942random 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> 2943<p>The <code>callback</code> function is called with two arguments: <code>err</code> and <code>derivedKey</code>. 2944<code>err</code> is an exception object when key derivation fails, otherwise <code>err</code> is 2945<code>null</code>. <code>derivedKey</code> is passed to the callback as a <a href="buffer.html"><code>Buffer</code></a>.</p> 2946<p>An exception is thrown when any of the input arguments specify invalid values 2947or types.</p> 2948<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>); 2949<span class="hljs-comment">// Using the factory defaults.</span> 2950crypto.<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> { 2951 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2952 <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> 2953}); 2954<span class="hljs-comment">// Using a custom N parameter. Must be a power of two.</span> 2955crypto.<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> { 2956 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 2957 <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> 2958});</code></pre> 2959<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> 2960<div class="api_metadata"> 2961<details class="changelog"><summary>History</summary> 2962<table> 2963<tbody><tr><th>Version</th><th>Changes</th></tr> 2964<tr><td>v12.8.0, v10.17.0</td> 2965<td><p>The <code>maxmem</code> value can now be any safe integer.</p></td></tr> 2966<tr><td>v10.9.0</td> 2967<td><p>The <code>cost</code>, <code>blockSize</code> and <code>parallelization</code> option names have been added.</p></td></tr> 2968<tr><td>v10.5.0</td> 2969<td><p><span>Added in: v10.5.0</span></p></td></tr> 2970</tbody></table> 2971</details> 2972</div> 2973<ul> 2974<li><code>password</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 2975<li><code>salt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 2976<li><code>keylen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 2977<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2978<ul> 2979<li><code>cost</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> CPU/memory cost parameter. Must be a power of two greater 2980than one. <strong>Default:</strong> <code>16384</code>.</li> 2981<li><code>blockSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Block size parameter. <strong>Default:</strong> <code>8</code>.</li> 2982<li><code>parallelization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Parallelization parameter. <strong>Default:</strong> <code>1</code>.</li> 2983<li><code>N</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Alias for <code>cost</code>. Only one of both may be specified.</li> 2984<li><code>r</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Alias for <code>blockSize</code>. Only one of both may be specified.</li> 2985<li><code>p</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Alias for <code>parallelization</code>. Only one of both may be specified.</li> 2986<li><code>maxmem</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Memory upper bound. It is an error when (approximately) 2987<code>128 * N * r > maxmem</code>. <strong>Default:</strong> <code>32 * 1024 * 1024</code>.</li> 2988</ul> 2989</li> 2990<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li> 2991</ul> 2992<p>Provides a synchronous <a href="https://en.wikipedia.org/wiki/Scrypt">scrypt</a> implementation. Scrypt is a password-based 2993key derivation function that is designed to be expensive computationally and 2994memory-wise in order to make brute-force attacks unrewarding.</p> 2995<p>The <code>salt</code> should be as unique as possible. It is recommended that a salt is 2996random 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> 2997<p>An exception is thrown when key derivation fails, otherwise the derived key is 2998returned as a <a href="buffer.html"><code>Buffer</code></a>.</p> 2999<p>An exception is thrown when any of the input arguments specify invalid values 3000or types.</p> 3001<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>); 3002<span class="hljs-comment">// Using the factory defaults.</span> 3003<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>); 3004<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> 3005<span class="hljs-comment">// Using a custom N parameter. Must be a power of two.</span> 3006<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> }); 3007<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> 3008<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> 3009<div class="api_metadata"> 3010<span>Added in: v0.11.11</span> 3011</div> 3012<ul> 3013<li><code>engine</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3014<li><code>flags</code> <a href="crypto.html#crypto_crypto_constants_1" class="type"><crypto.constants></a> <strong>Default:</strong> <code>crypto.constants.ENGINE_METHOD_ALL</code></li> 3015</ul> 3016<p>Load and set the <code>engine</code> for some or all OpenSSL functions (selected by flags).</p> 3017<p><code>engine</code> could be either an id or a path to the engine's shared library.</p> 3018<p>The optional <code>flags</code> argument uses <code>ENGINE_METHOD_ALL</code> by default. The <code>flags</code> 3019is a bit field taking one of or a mix of the following flags (defined in 3020<code>crypto.constants</code>):</p> 3021<ul> 3022<li><code>crypto.constants.ENGINE_METHOD_RSA</code></li> 3023<li><code>crypto.constants.ENGINE_METHOD_DSA</code></li> 3024<li><code>crypto.constants.ENGINE_METHOD_DH</code></li> 3025<li><code>crypto.constants.ENGINE_METHOD_RAND</code></li> 3026<li><code>crypto.constants.ENGINE_METHOD_EC</code></li> 3027<li><code>crypto.constants.ENGINE_METHOD_CIPHERS</code></li> 3028<li><code>crypto.constants.ENGINE_METHOD_DIGESTS</code></li> 3029<li><code>crypto.constants.ENGINE_METHOD_PKEY_METHS</code></li> 3030<li><code>crypto.constants.ENGINE_METHOD_PKEY_ASN1_METHS</code></li> 3031<li><code>crypto.constants.ENGINE_METHOD_ALL</code></li> 3032<li><code>crypto.constants.ENGINE_METHOD_NONE</code></li> 3033</ul> 3034<p>The flags below are deprecated in OpenSSL-1.1.0.</p> 3035<ul> 3036<li><code>crypto.constants.ENGINE_METHOD_ECDH</code></li> 3037<li><code>crypto.constants.ENGINE_METHOD_ECDSA</code></li> 3038<li><code>crypto.constants.ENGINE_METHOD_STORE</code></li> 3039</ul> 3040<h4><code>crypto.setFips(bool)</code><span><a class="mark" href="#crypto_crypto_setfips_bool" id="crypto_crypto_setfips_bool">#</a></span></h4> 3041<div class="api_metadata"> 3042<span>Added in: v10.0.0</span> 3043</div> 3044<ul> 3045<li><code>bool</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> to enable FIPS mode.</li> 3046</ul> 3047<p>Enables the FIPS compliant crypto provider in a FIPS-enabled Node.js build. 3048Throws an error if FIPS mode is not available.</p> 3049<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> 3050<div class="api_metadata"> 3051<details class="changelog"><summary>History</summary> 3052<table> 3053<tbody><tr><th>Version</th><th>Changes</th></tr> 3054<tr><td>v13.2.0, v12.16.0</td> 3055<td><p>This function now supports IEEE-P1363 DSA and ECDSA signatures.</p></td></tr> 3056<tr><td>v12.0.0</td> 3057<td><p><span>Added in: v12.0.0</span></p></td></tr> 3058</tbody></table> 3059</details> 3060</div> 3061<ul> 3062<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a></li> 3063<li><code>data</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 3064<li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="crypto.html#crypto_class_keyobject" class="type"><KeyObject></a></li> 3065<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a></li> 3066</ul> 3067<p>Calculates and returns the signature for <code>data</code> using the given private key and 3068algorithm. If <code>algorithm</code> is <code>null</code> or <code>undefined</code>, then the algorithm is 3069dependent upon the key type (especially Ed25519 and Ed448).</p> 3070<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 3071passed to <a href="#crypto_crypto_createprivatekey_key"><code>crypto.createPrivateKey()</code></a>. If it is an object, the following 3072additional properties can be passed:</p> 3073<ul> 3074<li> 3075<p><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> For DSA and ECDSA, this option specifies the 3076format of the generated signature. It can be one of the following:</p> 3077<ul> 3078<li><code>'der'</code> (default): DER-encoded ASN.1 signature structure encoding <code>(r, s)</code>.</li> 3079<li><code>'ieee-p1363'</code>: Signature format <code>r || s</code> as proposed in IEEE-P1363.</li> 3080</ul> 3081</li> 3082<li> 3083<p><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Optional padding value for RSA, one of the following:</p> 3084<ul> 3085<li><code>crypto.constants.RSA_PKCS1_PADDING</code> (default)</li> 3086<li><code>crypto.constants.RSA_PKCS1_PSS_PADDING</code></li> 3087</ul> 3088<p><code>RSA_PKCS1_PSS_PADDING</code> will use MGF1 with the same hash function 3089used 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> 3090</li> 3091<li> 3092<p><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Salt length for when padding is 3093<code>RSA_PKCS1_PSS_PADDING</code>. The special value 3094<code>crypto.constants.RSA_PSS_SALTLEN_DIGEST</code> sets the salt length to the digest 3095size, <code>crypto.constants.RSA_PSS_SALTLEN_MAX_SIGN</code> (default) sets it to the 3096maximum permissible value.</p> 3097</li> 3098</ul> 3099<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> 3100<div class="api_metadata"> 3101<span>Added in: v6.6.0</span> 3102</div> 3103<ul> 3104<li><code>a</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 3105<li><code>b</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 3106<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3107</ul> 3108<p>This function is based on a constant-time algorithm. 3109Returns true if <code>a</code> is equal to <code>b</code>, without leaking timing information that 3110would allow an attacker to guess one of the values. This is suitable for 3111comparing HMAC digests or secret values like authentication cookies or 3112<a href="https://www.w3.org/TR/capability-urls/">capability urls</a>.</p> 3113<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 3114must have the same byte length.</p> 3115<p>If at least one of <code>a</code> and <code>b</code> is a <code>TypedArray</code> with more than one byte per 3116entry, such as <code>Uint16Array</code>, the result will be computed using the platform 3117byte order.</p> 3118<p>Use of <code>crypto.timingSafeEqual</code> does not guarantee that the <em>surrounding</em> code 3119is timing-safe. Care should be taken to ensure that the surrounding code does 3120not introduce timing vulnerabilities.</p> 3121<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> 3122<div class="api_metadata"> 3123<details class="changelog"><summary>History</summary> 3124<table> 3125<tbody><tr><th>Version</th><th>Changes</th></tr> 3126<tr><td>v13.2.0, v12.16.0</td> 3127<td><p>This function now supports IEEE-P1363 DSA and ECDSA signatures.</p></td></tr> 3128<tr><td>v12.0.0</td> 3129<td><p><span>Added in: v12.0.0</span></p></td></tr> 3130</tbody></table> 3131</details> 3132</div> 3133<ul> 3134<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a></li> 3135<li><code>data</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 3136<li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="crypto.html#crypto_class_keyobject" class="type"><KeyObject></a></li> 3137<li><code>signature</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li> 3138<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3139</ul> 3140<p>Verifies the given signature for <code>data</code> using the given key and algorithm. If 3141<code>algorithm</code> is <code>null</code> or <code>undefined</code>, then the algorithm is dependent upon the 3142key type (especially Ed25519 and Ed448).</p> 3143<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 3144passed to <a href="#crypto_crypto_createpublickey_key"><code>crypto.createPublicKey()</code></a>. If it is an object, the following 3145additional properties can be passed:</p> 3146<ul> 3147<li> 3148<p><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> For DSA and ECDSA, this option specifies the 3149format of the signature. It can be one of the following:</p> 3150<ul> 3151<li><code>'der'</code> (default): DER-encoded ASN.1 signature structure encoding <code>(r, s)</code>.</li> 3152<li><code>'ieee-p1363'</code>: Signature format <code>r || s</code> as proposed in IEEE-P1363.</li> 3153</ul> 3154</li> 3155<li> 3156<p><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Optional padding value for RSA, one of the following:</p> 3157<ul> 3158<li><code>crypto.constants.RSA_PKCS1_PADDING</code> (default)</li> 3159<li><code>crypto.constants.RSA_PKCS1_PSS_PADDING</code></li> 3160</ul> 3161<p><code>RSA_PKCS1_PSS_PADDING</code> will use MGF1 with the same hash function 3162used 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> 3163</li> 3164<li> 3165<p><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Salt length for when padding is 3166<code>RSA_PKCS1_PSS_PADDING</code>. The special value 3167<code>crypto.constants.RSA_PSS_SALTLEN_DIGEST</code> sets the salt length to the digest 3168size, <code>crypto.constants.RSA_PSS_SALTLEN_MAX_SIGN</code> (default) sets it to the 3169maximum permissible value.</p> 3170</li> 3171</ul> 3172<p>The <code>signature</code> argument is the previously calculated signature for the <code>data</code>.</p> 3173<p>Because public keys can be derived from private keys, a private key or a public 3174key may be passed for <code>key</code>.</p> 3175</section><section><h3>Notes<span><a class="mark" href="#crypto_notes" id="crypto_notes">#</a></span></h3> 3176<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> 3177<p>The Crypto module was added to Node.js before there was the concept of a 3178unified Stream API, and before there were <a href="buffer.html"><code>Buffer</code></a> objects for handling 3179binary data. As such, the many of the <code>crypto</code> defined classes have methods not 3180typically found on other Node.js classes that implement the <a href="stream.html">streams</a> 3181API (e.g. <code>update()</code>, <code>final()</code>, or <code>digest()</code>). Also, many methods accepted 3182and returned <code>'latin1'</code> encoded strings by default rather than <code>Buffer</code>s. This 3183default was changed after Node.js v0.8 to use <a href="buffer.html"><code>Buffer</code></a> objects by default 3184instead.</p> 3185<h4>Recent ECDH changes<span><a class="mark" href="#crypto_recent_ecdh_changes" id="crypto_recent_ecdh_changes">#</a></span></h4> 3186<p>Usage of <code>ECDH</code> with non-dynamically generated key pairs has been simplified. 3187Now, <a href="#crypto_ecdh_setprivatekey_privatekey_encoding"><code>ecdh.setPrivateKey()</code></a> can be called with a preselected private key 3188and the associated public point (key) will be computed and stored in the object. 3189This allows code to only store and provide the private part of the EC key pair. 3190<a href="#crypto_ecdh_setprivatekey_privatekey_encoding"><code>ecdh.setPrivateKey()</code></a> now also validates that the private key is valid for 3191the selected curve.</p> 3192<p>The <a href="#crypto_ecdh_setpublickey_publickey_encoding"><code>ecdh.setPublicKey()</code></a> method is now deprecated as its inclusion in the 3193API is not useful. Either a previously stored private key should be set, which 3194automatically generates the associated public key, or <a href="#crypto_ecdh_generatekeys_encoding_format"><code>ecdh.generateKeys()</code></a> 3195should be called. The main drawback of using <a href="#crypto_ecdh_setpublickey_publickey_encoding"><code>ecdh.setPublicKey()</code></a> is that 3196it can be used to put the ECDH key pair into an inconsistent state.</p> 3197<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> 3198<p>The <code>crypto</code> module still supports some algorithms which are already 3199compromised and are not currently recommended for use. The API also allows 3200the use of ciphers and hashes with a small key size that are too weak for safe 3201use.</p> 3202<p>Users should take full responsibility for selecting the crypto 3203algorithm and key size according to their security requirements.</p> 3204<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> 3205<ul> 3206<li>MD5 and SHA-1 are no longer acceptable where collision resistance is 3207required such as digital signatures.</li> 3208<li>The key used with RSA, DSA, and DH algorithms is recommended to have 3209at least 2048 bits and that of the curve of ECDSA and ECDH at least 3210224 bits, to be safe to use for several years.</li> 3211<li>The DH groups of <code>modp1</code>, <code>modp2</code> and <code>modp5</code> have a key size 3212smaller than 2048 bits and are not recommended.</li> 3213</ul> 3214<p>See the reference for other recommendations and details.</p> 3215<h4>CCM mode<span><a class="mark" href="#crypto_ccm_mode" id="crypto_ccm_mode">#</a></span></h4> 3216<p>CCM is one of the supported <a href="https://en.wikipedia.org/wiki/Authenticated_encryption">AEAD algorithms</a>. Applications which use this 3217mode must adhere to certain restrictions when using the cipher API:</p> 3218<ul> 3219<li>The authentication tag length must be specified during cipher creation by 3220setting the <code>authTagLength</code> option and must be one of 4, 6, 8, 10, 12, 14 or 322116 bytes.</li> 3222<li>The length of the initialization vector (nonce) <code>N</code> must be between 7 and 13 3223bytes (<code>7 ≤ N ≤ 13</code>).</li> 3224<li>The length of the plaintext is limited to <code>2 ** (8 * (15 - N))</code> bytes.</li> 3225<li>When decrypting, the authentication tag must be set via <code>setAuthTag()</code> before 3226calling <code>update()</code>. 3227Otherwise, decryption will fail and <code>final()</code> will throw an error in 3228compliance with section 2.6 of <a href="https://www.rfc-editor.org/rfc/rfc3610.txt">RFC 3610</a>.</li> 3229<li>Using stream methods such as <code>write(data)</code>, <code>end(data)</code> or <code>pipe()</code> in CCM 3230mode might fail as CCM cannot handle more than one chunk of data per instance.</li> 3231<li>When passing additional authenticated data (AAD), the length of the actual 3232message in bytes must be passed to <code>setAAD()</code> via the <code>plaintextLength</code> 3233option. 3234Many crypto libraries include the authentication tag in the ciphertext, 3235which means that they produce ciphertexts of the length 3236<code>plaintextLength + authTagLength</code>. Node.js does not include the authentication 3237tag, so the ciphertext length is always <code>plaintextLength</code>. 3238This is not necessary if no AAD is used.</li> 3239<li>As CCM processes the whole message at once, <code>update()</code> can only be called 3240once.</li> 3241<li>Even though calling <code>update()</code> is sufficient to encrypt/decrypt the message, 3242applications <em>must</em> call <code>final()</code> to compute or verify the 3243authentication tag.</li> 3244</ul> 3245<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>); 3246 3247<span class="hljs-keyword">const</span> key = <span class="hljs-string">'keykeykeykeykeykeykeykey'</span>; 3248<span class="hljs-keyword">const</span> nonce = crypto.<span class="hljs-title function_">randomBytes</span>(<span class="hljs-number">12</span>); 3249 3250<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>); 3251 3252<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, { 3253 <span class="hljs-attr">authTagLength</span>: <span class="hljs-number">16</span> 3254}); 3255<span class="hljs-keyword">const</span> plaintext = <span class="hljs-string">'Hello world'</span>; 3256cipher.<span class="hljs-title function_">setAAD</span>(aad, { 3257 <span class="hljs-attr">plaintextLength</span>: <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">byteLength</span>(plaintext) 3258}); 3259<span class="hljs-keyword">const</span> ciphertext = cipher.<span class="hljs-title function_">update</span>(plaintext, <span class="hljs-string">'utf8'</span>); 3260cipher.<span class="hljs-title function_">final</span>(); 3261<span class="hljs-keyword">const</span> tag = cipher.<span class="hljs-title function_">getAuthTag</span>(); 3262 3263<span class="hljs-comment">// Now transmit { ciphertext, nonce, tag }.</span> 3264 3265<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, { 3266 <span class="hljs-attr">authTagLength</span>: <span class="hljs-number">16</span> 3267}); 3268decipher.<span class="hljs-title function_">setAuthTag</span>(tag); 3269decipher.<span class="hljs-title function_">setAAD</span>(aad, { 3270 <span class="hljs-attr">plaintextLength</span>: ciphertext.<span class="hljs-property">length</span> 3271}); 3272<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>); 3273 3274<span class="hljs-keyword">try</span> { 3275 decipher.<span class="hljs-title function_">final</span>(); 3276} <span class="hljs-keyword">catch</span> (err) { 3277 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'Authentication failed!'</span>); 3278 <span class="hljs-keyword">return</span>; 3279} 3280 3281<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(receivedPlaintext);</code></pre> 3282</section><section><h3>Crypto constants<span><a class="mark" href="#crypto_crypto_constants_1" id="crypto_crypto_constants_1">#</a></span></h3> 3283<p>The following constants exported by <code>crypto.constants</code> apply to various uses of 3284the <code>crypto</code>, <code>tls</code>, and <code>https</code> modules and are generally specific to OpenSSL.</p> 3285<h4>OpenSSL options<span><a class="mark" href="#crypto_openssl_options" id="crypto_openssl_options">#</a></span></h4> 3286<table> 3287 <tbody><tr> 3288 <th>Constant</th> 3289 <th>Description</th> 3290 </tr> 3291 <tr> 3292 <td><code>SSL_OP_ALL</code></td> 3293 <td>Applies multiple bug workarounds within OpenSSL. See 3294 <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> 3295 for detail.</td> 3296 </tr> 3297 <tr> 3298 <td><code>SSL_OP_ALLOW_NO_DHE_KEX</code></td> 3299 <td>Instructs OpenSSL to allow a non-[EC]DHE-based key exchange mode 3300 for TLS v1.3</td> 3301 </tr> 3302 <tr> 3303 <td><code>SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION</code></td> 3304 <td>Allows legacy insecure renegotiation between OpenSSL and unpatched 3305 clients or servers. See 3306 <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> 3307 </tr> 3308 <tr> 3309 <td><code>SSL_OP_CIPHER_SERVER_PREFERENCE</code></td> 3310 <td>Attempts to use the server's preferences instead of the client's when 3311 selecting a cipher. Behavior depends on protocol version. See 3312 <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> 3313 </tr> 3314 <tr> 3315 <td><code>SSL_OP_CISCO_ANYCONNECT</code></td> 3316 <td>Instructs OpenSSL to use Cisco's "speshul" version of DTLS_BAD_VER.</td> 3317 </tr> 3318 <tr> 3319 <td><code>SSL_OP_COOKIE_EXCHANGE</code></td> 3320 <td>Instructs OpenSSL to turn on cookie exchange.</td> 3321 </tr> 3322 <tr> 3323 <td><code>SSL_OP_CRYPTOPRO_TLSEXT_BUG</code></td> 3324 <td>Instructs OpenSSL to add server-hello extension from an early version 3325 of the cryptopro draft.</td> 3326 </tr> 3327 <tr> 3328 <td><code>SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS</code></td> 3329 <td>Instructs OpenSSL to disable a SSL 3.0/TLS 1.0 vulnerability 3330 workaround added in OpenSSL 0.9.6d.</td> 3331 </tr> 3332 <tr> 3333 <td><code>SSL_OP_EPHEMERAL_RSA</code></td> 3334 <td>Instructs OpenSSL to always use the tmp_rsa key when performing RSA 3335 operations.</td> 3336 </tr> 3337 <tr> 3338 <td><code>SSL_OP_LEGACY_SERVER_CONNECT</code></td> 3339 <td>Allows initial connection to servers that do not support RI.</td> 3340 </tr> 3341 <tr> 3342 <td><code>SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER</code></td> 3343 <td></td> 3344 </tr> 3345 <tr> 3346 <td><code>SSL_OP_MICROSOFT_SESS_ID_BUG</code></td> 3347 <td></td> 3348 </tr> 3349 <tr> 3350 <td><code>SSL_OP_MSIE_SSLV2_RSA_PADDING</code></td> 3351 <td>Instructs OpenSSL to disable the workaround for a man-in-the-middle 3352 protocol-version vulnerability in the SSL 2.0 server implementation.</td> 3353 </tr> 3354 <tr> 3355 <td><code>SSL_OP_NETSCAPE_CA_DN_BUG</code></td> 3356 <td></td> 3357 </tr> 3358 <tr> 3359 <td><code>SSL_OP_NETSCAPE_CHALLENGE_BUG</code></td> 3360 <td></td> 3361 </tr> 3362 <tr> 3363 <td><code>SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG</code></td> 3364 <td></td> 3365 </tr> 3366 <tr> 3367 <td><code>SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG</code></td> 3368 <td></td> 3369 </tr> 3370 <tr> 3371 <td><code>SSL_OP_NO_COMPRESSION</code></td> 3372 <td>Instructs OpenSSL to disable support for SSL/TLS compression.</td> 3373 </tr> 3374 <tr> 3375 <td><code>SSL_OP_NO_ENCRYPT_THEN_MAC</code></td> 3376 <td>Instructs OpenSSL to disable encrypt-then-MAC.</td> 3377 </tr> 3378 <tr> 3379 <td><code>SSL_OP_NO_QUERY_MTU</code></td> 3380 <td></td> 3381 </tr> 3382 <tr> 3383 <td><code>SSL_OP_NO_RENEGOTIATION</code></td> 3384 <td>Instructs OpenSSL to disable renegotiation.</td> 3385 </tr> 3386 <tr> 3387 <td><code>SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION</code></td> 3388 <td>Instructs OpenSSL to always start a new session when performing 3389 renegotiation.</td> 3390 </tr> 3391 <tr> 3392 <td><code>SSL_OP_NO_SSLv2</code></td> 3393 <td>Instructs OpenSSL to turn off SSL v2</td> 3394 </tr> 3395 <tr> 3396 <td><code>SSL_OP_NO_SSLv3</code></td> 3397 <td>Instructs OpenSSL to turn off SSL v3</td> 3398 </tr> 3399 <tr> 3400 <td><code>SSL_OP_NO_TICKET</code></td> 3401 <td>Instructs OpenSSL to disable use of RFC4507bis tickets.</td> 3402 </tr> 3403 <tr> 3404 <td><code>SSL_OP_NO_TLSv1</code></td> 3405 <td>Instructs OpenSSL to turn off TLS v1</td> 3406 </tr> 3407 <tr> 3408 <td><code>SSL_OP_NO_TLSv1_1</code></td> 3409 <td>Instructs OpenSSL to turn off TLS v1.1</td> 3410 </tr> 3411 <tr> 3412 <td><code>SSL_OP_NO_TLSv1_2</code></td> 3413 <td>Instructs OpenSSL to turn off TLS v1.2</td> 3414 </tr> 3415 <tr> 3416 <td><code>SSL_OP_NO_TLSv1_3</code></td> 3417 <td>Instructs OpenSSL to turn off TLS v1.3</td> 3418 </tr> 3419 <tr><td><code>SSL_OP_PKCS1_CHECK_1</code></td> 3420 <td></td> 3421 </tr> 3422 <tr> 3423 <td><code>SSL_OP_PKCS1_CHECK_2</code></td> 3424 <td></td> 3425 </tr> 3426 <tr> 3427 <td><code>SSL_OP_PRIORITIZE_CHACHA</code></td> 3428 <td>Instructs OpenSSL server to prioritize ChaCha20Poly1305 3429 when client does. 3430 This option has no effect if 3431 <code>SSL_OP_CIPHER_SERVER_PREFERENCE</code> 3432 is not enabled.</td> 3433 </tr> 3434 <tr> 3435 <td><code>SSL_OP_SINGLE_DH_USE</code></td> 3436 <td>Instructs OpenSSL to always create a new key when using 3437 temporary/ephemeral DH parameters.</td> 3438 </tr> 3439 <tr> 3440 <td><code>SSL_OP_SINGLE_ECDH_USE</code></td> 3441 <td>Instructs OpenSSL to always create a new key when using 3442 temporary/ephemeral ECDH parameters.</td> 3443 </tr> 3444 <tr><td><code>SSL_OP_SSLEAY_080_CLIENT_DH_BUG</code></td> 3445 <td></td> 3446 </tr> 3447 <tr> 3448 <td><code>SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG</code></td> 3449 <td></td> 3450 </tr> 3451 <tr> 3452 <td><code>SSL_OP_TLS_BLOCK_PADDING_BUG</code></td> 3453 <td></td> 3454 </tr> 3455 <tr> 3456 <td><code>SSL_OP_TLS_D5_BUG</code></td> 3457 <td></td> 3458 </tr> 3459 <tr> 3460 <td><code>SSL_OP_TLS_ROLLBACK_BUG</code></td> 3461 <td>Instructs OpenSSL to disable version rollback attack detection.</td> 3462 </tr> 3463</tbody></table> 3464<h4>OpenSSL engine constants<span><a class="mark" href="#crypto_openssl_engine_constants" id="crypto_openssl_engine_constants">#</a></span></h4> 3465<table> 3466 <tbody><tr> 3467 <th>Constant</th> 3468 <th>Description</th> 3469 </tr> 3470 <tr> 3471 <td><code>ENGINE_METHOD_RSA</code></td> 3472 <td>Limit engine usage to RSA</td> 3473 </tr> 3474 <tr> 3475 <td><code>ENGINE_METHOD_DSA</code></td> 3476 <td>Limit engine usage to DSA</td> 3477 </tr> 3478 <tr> 3479 <td><code>ENGINE_METHOD_DH</code></td> 3480 <td>Limit engine usage to DH</td> 3481 </tr> 3482 <tr> 3483 <td><code>ENGINE_METHOD_RAND</code></td> 3484 <td>Limit engine usage to RAND</td> 3485 </tr> 3486 <tr> 3487 <td><code>ENGINE_METHOD_EC</code></td> 3488 <td>Limit engine usage to EC</td> 3489 </tr> 3490 <tr> 3491 <td><code>ENGINE_METHOD_CIPHERS</code></td> 3492 <td>Limit engine usage to CIPHERS</td> 3493 </tr> 3494 <tr> 3495 <td><code>ENGINE_METHOD_DIGESTS</code></td> 3496 <td>Limit engine usage to DIGESTS</td> 3497 </tr> 3498 <tr> 3499 <td><code>ENGINE_METHOD_PKEY_METHS</code></td> 3500 <td>Limit engine usage to PKEY_METHDS</td> 3501 </tr> 3502 <tr> 3503 <td><code>ENGINE_METHOD_PKEY_ASN1_METHS</code></td> 3504 <td>Limit engine usage to PKEY_ASN1_METHS</td> 3505 </tr> 3506 <tr> 3507 <td><code>ENGINE_METHOD_ALL</code></td> 3508 <td></td> 3509 </tr> 3510 <tr> 3511 <td><code>ENGINE_METHOD_NONE</code></td> 3512 <td></td> 3513 </tr> 3514</tbody></table> 3515<h4>Other OpenSSL constants<span><a class="mark" href="#crypto_other_openssl_constants" id="crypto_other_openssl_constants">#</a></span></h4> 3516<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> 3517<table> 3518 <tbody><tr> 3519 <th>Constant</th> 3520 <th>Description</th> 3521 </tr> 3522 <tr> 3523 <td><code>DH_CHECK_P_NOT_SAFE_PRIME</code></td> 3524 <td></td> 3525 </tr> 3526 <tr> 3527 <td><code>DH_CHECK_P_NOT_PRIME</code></td> 3528 <td></td> 3529 </tr> 3530 <tr> 3531 <td><code>DH_UNABLE_TO_CHECK_GENERATOR</code></td> 3532 <td></td> 3533 </tr> 3534 <tr> 3535 <td><code>DH_NOT_SUITABLE_GENERATOR</code></td> 3536 <td></td> 3537 </tr> 3538 <tr> 3539 <td><code>ALPN_ENABLED</code></td> 3540 <td></td> 3541 </tr> 3542 <tr> 3543 <td><code>RSA_PKCS1_PADDING</code></td> 3544 <td></td> 3545 </tr> 3546 <tr> 3547 <td><code>RSA_SSLV23_PADDING</code></td> 3548 <td></td> 3549 </tr> 3550 <tr> 3551 <td><code>RSA_NO_PADDING</code></td> 3552 <td></td> 3553 </tr> 3554 <tr> 3555 <td><code>RSA_PKCS1_OAEP_PADDING</code></td> 3556 <td></td> 3557 </tr> 3558 <tr> 3559 <td><code>RSA_X931_PADDING</code></td> 3560 <td></td> 3561 </tr> 3562 <tr> 3563 <td><code>RSA_PKCS1_PSS_PADDING</code></td> 3564 <td></td> 3565 </tr> 3566 <tr> 3567 <td><code>RSA_PSS_SALTLEN_DIGEST</code></td> 3568 <td>Sets the salt length for <code>RSA_PKCS1_PSS_PADDING</code> to the 3569 digest size when signing or verifying.</td> 3570 </tr> 3571 <tr> 3572 <td><code>RSA_PSS_SALTLEN_MAX_SIGN</code></td> 3573 <td>Sets the salt length for <code>RSA_PKCS1_PSS_PADDING</code> to the 3574 maximum permissible value when signing data.</td> 3575 </tr> 3576 <tr> 3577 <td><code>RSA_PSS_SALTLEN_AUTO</code></td> 3578 <td>Causes the salt length for <code>RSA_PKCS1_PSS_PADDING</code> to be 3579 determined automatically when verifying a signature.</td> 3580 </tr> 3581 <tr> 3582 <td><code>POINT_CONVERSION_COMPRESSED</code></td> 3583 <td></td> 3584 </tr> 3585 <tr> 3586 <td><code>POINT_CONVERSION_UNCOMPRESSED</code></td> 3587 <td></td> 3588 </tr> 3589 <tr> 3590 <td><code>POINT_CONVERSION_HYBRID</code></td> 3591 <td></td> 3592 </tr> 3593</tbody></table> 3594<h4>Node.js crypto constants<span><a class="mark" href="#crypto_node_js_crypto_constants" id="crypto_node_js_crypto_constants">#</a></span></h4> 3595<table> 3596 <tbody><tr> 3597 <th>Constant</th> 3598 <th>Description</th> 3599 </tr> 3600 <tr> 3601 <td><code>defaultCoreCipherList</code></td> 3602 <td>Specifies the built-in default cipher list used by Node.js.</td> 3603 </tr> 3604 <tr> 3605 <td><code>defaultCipherList</code></td> 3606 <td>Specifies the active default cipher list used by the current Node.js 3607 process.</td> 3608 </tr> 3609</tbody></table></section> 3610 <!-- API END --> 3611 </div> 3612 </div> 3613 </div> 3614 <script> 3615 'use strict'; 3616 { 3617 const kCustomPreference = 'customDarkTheme'; 3618 const userSettings = sessionStorage.getItem(kCustomPreference); 3619 const themeToggleButton = document.getElementById('theme-toggle-btn'); 3620 if (userSettings === null && window.matchMedia) { 3621 const mq = window.matchMedia('(prefers-color-scheme: dark)'); 3622 if ('onchange' in mq) { 3623 function mqChangeListener(e) { 3624 document.body.classList.toggle('dark-mode', e.matches); 3625 } 3626 mq.addEventListener('change', mqChangeListener); 3627 if (themeToggleButton) { 3628 themeToggleButton.addEventListener('click', function() { 3629 mq.removeEventListener('change', mqChangeListener); 3630 }, { once: true }); 3631 } 3632 } 3633 if (mq.matches) { 3634 document.body.classList.add('dark-mode'); 3635 } 3636 } else if (userSettings === 'true') { 3637 document.body.classList.add('dark-mode'); 3638 } 3639 if (themeToggleButton) { 3640 themeToggleButton.hidden = false; 3641 themeToggleButton.addEventListener('click', function() { 3642 sessionStorage.setItem( 3643 kCustomPreference, 3644 document.body.classList.toggle('dark-mode') 3645 ); 3646 }); 3647 } 3648 } 3649 </script> 3650</body> 3651</html> 3652