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="v18.20.1"> 7 <title>Crypto | Node.js v18.20.1 Documentation</title> 8 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 9 <link rel="stylesheet" href="assets/style.css"> 10 <link rel="stylesheet" href="assets/hljs.css"> 11 <link rel="canonical" href="https://nodejs.org/api/crypto.html"> 12 <script async defer src="assets/api.js" type="text/javascript"></script> 13 <style>@media(max-width:630px){.with-51-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:638px){.with-52-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:294px){.with-9-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:374px){.with-19-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:518px){.with-37-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:526px){.with-38-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:742px){.with-65-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:326px){.with-13-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:606px){.with-48-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:446px){.with-28-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:566px){.with-43-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:710px){.with-61-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:398px){.with-22-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:670px){.with-56-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:334px){.with-14-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:366px){.with-18-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:318px){.with-12-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:342px){.with-15-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:662px){.with-55-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:558px){.with-42-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}</style> 14</head> 15<body class="alt apidoc" id="api-section-crypto"> 16 <div id="content" class="clearfix"> 17 <div id="column2" class="interior"> 18 <div id="intro" class="interior"> 19 <a href="/" title="Go back to the home page"> 20 Node.js 21 </a> 22 </div> 23 <ul> 24<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 25<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 26</ul> 27<hr class="line"> 28<ul> 29<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 30<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 31<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 32<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 33<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 34<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 35<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 36<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 37<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 38<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 39<li><a href="console.html" class="nav-console">Console</a></li> 40<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 41<li><a href="crypto.html" class="nav-crypto active">Crypto</a></li> 42<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 43<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 44<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 45<li><a href="dns.html" class="nav-dns">DNS</a></li> 46<li><a href="domain.html" class="nav-domain">Domain</a></li> 47<li><a href="errors.html" class="nav-errors">Errors</a></li> 48<li><a href="events.html" class="nav-events">Events</a></li> 49<li><a href="fs.html" class="nav-fs">File system</a></li> 50<li><a href="globals.html" class="nav-globals">Globals</a></li> 51<li><a href="http.html" class="nav-http">HTTP</a></li> 52<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 53<li><a href="https.html" class="nav-https">HTTPS</a></li> 54<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 55<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 56<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 57<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 58<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 59<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 60<li><a href="net.html" class="nav-net">Net</a></li> 61<li><a href="os.html" class="nav-os">OS</a></li> 62<li><a href="path.html" class="nav-path">Path</a></li> 63<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 64<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 65<li><a href="process.html" class="nav-process">Process</a></li> 66<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 67<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 68<li><a href="readline.html" class="nav-readline">Readline</a></li> 69<li><a href="repl.html" class="nav-repl">REPL</a></li> 70<li><a href="report.html" class="nav-report">Report</a></li> 71<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 72<li><a href="stream.html" class="nav-stream">Stream</a></li> 73<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 74<li><a href="test.html" class="nav-test">Test runner</a></li> 75<li><a href="timers.html" class="nav-timers">Timers</a></li> 76<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 77<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 78<li><a href="tty.html" class="nav-tty">TTY</a></li> 79<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 80<li><a href="url.html" class="nav-url">URL</a></li> 81<li><a href="util.html" class="nav-util">Utilities</a></li> 82<li><a href="v8.html" class="nav-v8">V8</a></li> 83<li><a href="vm.html" class="nav-vm">VM</a></li> 84<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 85<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 86<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 87<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 88<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 89</ul> 90<hr class="line"> 91<ul> 92<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 93</ul> 94 </div> 95 96 <div id="column1" data-id="crypto" class="interior"> 97 <header class="header"> 98 <div class="header-container"> 99 <h1>Node.js v18.20.1 documentation</h1> 100 <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> 101 <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> 102 <path fill="none" d="M0 0h24v24H0z" /> 103 <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"/> 104 <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"/> 105 </svg> 106 <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> 107 <path d="M0 0h24v24H0z" fill="none" /> 108 <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"/> 109 </svg> 110 </button> 111 </div> 112 <div id="gtoc"> 113 <ul> 114 <li class="pinned-header">Node.js v18.20.1</li> 115 116 <li class="picker-header"> 117 <a href="#"> 118 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 119 Table of contents 120 </a> 121 122 <div class="picker"><div class="toc"><ul> 123<li><span class="stability_2"><a href="#crypto">Crypto</a></span> 124<ul> 125<li><a href="#determining-if-crypto-support-is-unavailable">Determining if crypto support is unavailable</a></li> 126<li><a href="#class-certificate">Class: <code>Certificate</code></a> 127<ul> 128<li><a href="#static-method-certificateexportchallengespkac-encoding">Static method: <code>Certificate.exportChallenge(spkac[, encoding])</code></a></li> 129<li><a href="#static-method-certificateexportpublickeyspkac-encoding">Static method: <code>Certificate.exportPublicKey(spkac[, encoding])</code></a></li> 130<li><a href="#static-method-certificateverifyspkacspkac-encoding">Static method: <code>Certificate.verifySpkac(spkac[, encoding])</code></a></li> 131<li><span class="stability_0"><a href="#legacy-api">Legacy API</a></span> 132<ul> 133<li><a href="#new-cryptocertificate"><code>new crypto.Certificate()</code></a></li> 134<li><a href="#certificateexportchallengespkac-encoding"><code>certificate.exportChallenge(spkac[, encoding])</code></a></li> 135<li><a href="#certificateexportpublickeyspkac-encoding"><code>certificate.exportPublicKey(spkac[, encoding])</code></a></li> 136<li><a href="#certificateverifyspkacspkac-encoding"><code>certificate.verifySpkac(spkac[, encoding])</code></a></li> 137</ul> 138</li> 139</ul> 140</li> 141<li><a href="#class-cipher">Class: <code>Cipher</code></a> 142<ul> 143<li><a href="#cipherfinaloutputencoding"><code>cipher.final([outputEncoding])</code></a></li> 144<li><a href="#ciphergetauthtag"><code>cipher.getAuthTag()</code></a></li> 145<li><a href="#ciphersetaadbuffer-options"><code>cipher.setAAD(buffer[, options])</code></a></li> 146<li><a href="#ciphersetautopaddingautopadding"><code>cipher.setAutoPadding([autoPadding])</code></a></li> 147<li><a href="#cipherupdatedata-inputencoding-outputencoding"><code>cipher.update(data[, inputEncoding][, outputEncoding])</code></a></li> 148</ul> 149</li> 150<li><a href="#class-decipher">Class: <code>Decipher</code></a> 151<ul> 152<li><a href="#decipherfinaloutputencoding"><code>decipher.final([outputEncoding])</code></a></li> 153<li><a href="#deciphersetaadbuffer-options"><code>decipher.setAAD(buffer[, options])</code></a></li> 154<li><a href="#deciphersetauthtagbuffer-encoding"><code>decipher.setAuthTag(buffer[, encoding])</code></a></li> 155<li><a href="#deciphersetautopaddingautopadding"><code>decipher.setAutoPadding([autoPadding])</code></a></li> 156<li><a href="#decipherupdatedata-inputencoding-outputencoding"><code>decipher.update(data[, inputEncoding][, outputEncoding])</code></a></li> 157</ul> 158</li> 159<li><a href="#class-diffiehellman">Class: <code>DiffieHellman</code></a> 160<ul> 161<li><a href="#diffiehellmancomputesecretotherpublickey-inputencoding-outputencoding"><code>diffieHellman.computeSecret(otherPublicKey[, inputEncoding][, outputEncoding])</code></a></li> 162<li><a href="#diffiehellmangeneratekeysencoding"><code>diffieHellman.generateKeys([encoding])</code></a></li> 163<li><a href="#diffiehellmangetgeneratorencoding"><code>diffieHellman.getGenerator([encoding])</code></a></li> 164<li><a href="#diffiehellmangetprimeencoding"><code>diffieHellman.getPrime([encoding])</code></a></li> 165<li><a href="#diffiehellmangetprivatekeyencoding"><code>diffieHellman.getPrivateKey([encoding])</code></a></li> 166<li><a href="#diffiehellmangetpublickeyencoding"><code>diffieHellman.getPublicKey([encoding])</code></a></li> 167<li><a href="#diffiehellmansetprivatekeyprivatekey-encoding"><code>diffieHellman.setPrivateKey(privateKey[, encoding])</code></a></li> 168<li><a href="#diffiehellmansetpublickeypublickey-encoding"><code>diffieHellman.setPublicKey(publicKey[, encoding])</code></a></li> 169<li><a href="#diffiehellmanverifyerror"><code>diffieHellman.verifyError</code></a></li> 170</ul> 171</li> 172<li><a href="#class-diffiehellmangroup">Class: <code>DiffieHellmanGroup</code></a></li> 173<li><a href="#class-ecdh">Class: <code>ECDH</code></a> 174<ul> 175<li><a href="#static-method-ecdhconvertkeykey-curve-inputencoding-outputencoding-format">Static method: <code>ECDH.convertKey(key, curve[, inputEncoding[, outputEncoding[, format]]])</code></a></li> 176<li><a href="#ecdhcomputesecretotherpublickey-inputencoding-outputencoding"><code>ecdh.computeSecret(otherPublicKey[, inputEncoding][, outputEncoding])</code></a></li> 177<li><a href="#ecdhgeneratekeysencoding-format"><code>ecdh.generateKeys([encoding[, format]])</code></a></li> 178<li><a href="#ecdhgetprivatekeyencoding"><code>ecdh.getPrivateKey([encoding])</code></a></li> 179<li><a href="#ecdhgetpublickeyencoding-format"><code>ecdh.getPublicKey([encoding][, format])</code></a></li> 180<li><a href="#ecdhsetprivatekeyprivatekey-encoding"><code>ecdh.setPrivateKey(privateKey[, encoding])</code></a></li> 181<li><span class="stability_0"><a href="#ecdhsetpublickeypublickey-encoding"><code>ecdh.setPublicKey(publicKey[, encoding])</code></a></span></li> 182</ul> 183</li> 184<li><a href="#class-hash">Class: <code>Hash</code></a> 185<ul> 186<li><a href="#hashcopyoptions"><code>hash.copy([options])</code></a></li> 187<li><a href="#hashdigestencoding"><code>hash.digest([encoding])</code></a></li> 188<li><a href="#hashupdatedata-inputencoding"><code>hash.update(data[, inputEncoding])</code></a></li> 189</ul> 190</li> 191<li><a href="#class-hmac">Class: <code>Hmac</code></a> 192<ul> 193<li><a href="#hmacdigestencoding"><code>hmac.digest([encoding])</code></a></li> 194<li><a href="#hmacupdatedata-inputencoding"><code>hmac.update(data[, inputEncoding])</code></a></li> 195</ul> 196</li> 197<li><a href="#class-keyobject">Class: <code>KeyObject</code></a> 198<ul> 199<li><a href="#static-method-keyobjectfromkey">Static method: <code>KeyObject.from(key)</code></a></li> 200<li><a href="#keyobjectasymmetrickeydetails"><code>keyObject.asymmetricKeyDetails</code></a></li> 201<li><a href="#keyobjectasymmetrickeytype"><code>keyObject.asymmetricKeyType</code></a></li> 202<li><a href="#keyobjectexportoptions"><code>keyObject.export([options])</code></a></li> 203<li><a href="#keyobjectequalsotherkeyobject"><code>keyObject.equals(otherKeyObject)</code></a></li> 204<li><a href="#keyobjectsymmetrickeysize"><code>keyObject.symmetricKeySize</code></a></li> 205<li><a href="#keyobjecttype"><code>keyObject.type</code></a></li> 206</ul> 207</li> 208<li><a href="#class-sign">Class: <code>Sign</code></a> 209<ul> 210<li><a href="#signsignprivatekey-outputencoding"><code>sign.sign(privateKey[, outputEncoding])</code></a></li> 211<li><a href="#signupdatedata-inputencoding"><code>sign.update(data[, inputEncoding])</code></a></li> 212</ul> 213</li> 214<li><a href="#class-verify">Class: <code>Verify</code></a> 215<ul> 216<li><a href="#verifyupdatedata-inputencoding"><code>verify.update(data[, inputEncoding])</code></a></li> 217<li><a href="#verifyverifyobject-signature-signatureencoding"><code>verify.verify(object, signature[, signatureEncoding])</code></a></li> 218</ul> 219</li> 220<li><a href="#class-x509certificate">Class: <code>X509Certificate</code></a> 221<ul> 222<li><a href="#new-x509certificatebuffer"><code>new X509Certificate(buffer)</code></a></li> 223<li><a href="#x509ca"><code>x509.ca</code></a></li> 224<li><a href="#x509checkemailemail-options"><code>x509.checkEmail(email[, options])</code></a></li> 225<li><a href="#x509checkhostname-options"><code>x509.checkHost(name[, options])</code></a></li> 226<li><a href="#x509checkipip"><code>x509.checkIP(ip)</code></a></li> 227<li><a href="#x509checkissuedothercert"><code>x509.checkIssued(otherCert)</code></a></li> 228<li><a href="#x509checkprivatekeyprivatekey"><code>x509.checkPrivateKey(privateKey)</code></a></li> 229<li><a href="#x509fingerprint"><code>x509.fingerprint</code></a></li> 230<li><a href="#x509fingerprint256"><code>x509.fingerprint256</code></a></li> 231<li><a href="#x509fingerprint512"><code>x509.fingerprint512</code></a></li> 232<li><a href="#x509infoaccess"><code>x509.infoAccess</code></a></li> 233<li><a href="#x509issuer"><code>x509.issuer</code></a></li> 234<li><a href="#x509issuercertificate"><code>x509.issuerCertificate</code></a></li> 235<li><a href="#x509keyusage"><code>x509.keyUsage</code></a></li> 236<li><a href="#x509publickey"><code>x509.publicKey</code></a></li> 237<li><a href="#x509raw"><code>x509.raw</code></a></li> 238<li><a href="#x509serialnumber"><code>x509.serialNumber</code></a></li> 239<li><a href="#x509subject"><code>x509.subject</code></a></li> 240<li><a href="#x509subjectaltname"><code>x509.subjectAltName</code></a></li> 241<li><a href="#x509tojson"><code>x509.toJSON()</code></a></li> 242<li><a href="#x509tolegacyobject"><code>x509.toLegacyObject()</code></a></li> 243<li><a href="#x509tostring"><code>x509.toString()</code></a></li> 244<li><a href="#x509validfrom"><code>x509.validFrom</code></a></li> 245<li><a href="#x509validto"><code>x509.validTo</code></a></li> 246<li><a href="#x509verifypublickey"><code>x509.verify(publicKey)</code></a></li> 247</ul> 248</li> 249<li><a href="#nodecrypto-module-methods-and-properties"><code>node:crypto</code> module methods and properties</a> 250<ul> 251<li><a href="#cryptoconstants"><code>crypto.constants</code></a></li> 252<li><span class="stability_0"><a href="#cryptodefault_encoding"><code>crypto.DEFAULT_ENCODING</code></a></span></li> 253<li><span class="stability_0"><a href="#cryptofips"><code>crypto.fips</code></a></span></li> 254<li><a href="#cryptocheckprimecandidate-options-callback"><code>crypto.checkPrime(candidate[, options], callback)</code></a></li> 255<li><a href="#cryptocheckprimesynccandidate-options"><code>crypto.checkPrimeSync(candidate[, options])</code></a></li> 256<li><span class="stability_0"><a href="#cryptocreatecipheralgorithm-password-options"><code>crypto.createCipher(algorithm, password[, options])</code></a></span></li> 257<li><a href="#cryptocreatecipherivalgorithm-key-iv-options"><code>crypto.createCipheriv(algorithm, key, iv[, options])</code></a></li> 258<li><span class="stability_0"><a href="#cryptocreatedecipheralgorithm-password-options"><code>crypto.createDecipher(algorithm, password[, options])</code></a></span></li> 259<li><a href="#cryptocreatedecipherivalgorithm-key-iv-options"><code>crypto.createDecipheriv(algorithm, key, iv[, options])</code></a></li> 260<li><a href="#cryptocreatediffiehellmanprime-primeencoding-generator-generatorencoding"><code>crypto.createDiffieHellman(prime[, primeEncoding][, generator][, generatorEncoding])</code></a></li> 261<li><a href="#cryptocreatediffiehellmanprimelength-generator"><code>crypto.createDiffieHellman(primeLength[, generator])</code></a></li> 262<li><a href="#cryptocreatediffiehellmangroupname"><code>crypto.createDiffieHellmanGroup(name)</code></a></li> 263<li><a href="#cryptocreateecdhcurvename"><code>crypto.createECDH(curveName)</code></a></li> 264<li><a href="#cryptocreatehashalgorithm-options"><code>crypto.createHash(algorithm[, options])</code></a></li> 265<li><a href="#cryptocreatehmacalgorithm-key-options"><code>crypto.createHmac(algorithm, key[, options])</code></a></li> 266<li><a href="#cryptocreateprivatekeykey"><code>crypto.createPrivateKey(key)</code></a></li> 267<li><a href="#cryptocreatepublickeykey"><code>crypto.createPublicKey(key)</code></a></li> 268<li><a href="#cryptocreatesecretkeykey-encoding"><code>crypto.createSecretKey(key[, encoding])</code></a></li> 269<li><a href="#cryptocreatesignalgorithm-options"><code>crypto.createSign(algorithm[, options])</code></a></li> 270<li><a href="#cryptocreateverifyalgorithm-options"><code>crypto.createVerify(algorithm[, options])</code></a></li> 271<li><a href="#cryptodiffiehellmanoptions"><code>crypto.diffieHellman(options)</code></a></li> 272<li><a href="#cryptogeneratekeytype-options-callback"><code>crypto.generateKey(type, options, callback)</code></a></li> 273<li><a href="#cryptogeneratekeypairtype-options-callback"><code>crypto.generateKeyPair(type, options, callback)</code></a></li> 274<li><a href="#cryptogeneratekeypairsynctype-options"><code>crypto.generateKeyPairSync(type, options)</code></a></li> 275<li><a href="#cryptogeneratekeysynctype-options"><code>crypto.generateKeySync(type, options)</code></a></li> 276<li><a href="#cryptogenerateprimesize-options-callback"><code>crypto.generatePrime(size[, options[, callback]])</code></a></li> 277<li><a href="#cryptogenerateprimesyncsize-options"><code>crypto.generatePrimeSync(size[, options])</code></a></li> 278<li><a href="#cryptogetcipherinfonameornid-options"><code>crypto.getCipherInfo(nameOrNid[, options])</code></a></li> 279<li><a href="#cryptogetciphers"><code>crypto.getCiphers()</code></a></li> 280<li><a href="#cryptogetcurves"><code>crypto.getCurves()</code></a></li> 281<li><a href="#cryptogetdiffiehellmangroupname"><code>crypto.getDiffieHellman(groupName)</code></a></li> 282<li><a href="#cryptogetfips"><code>crypto.getFips()</code></a></li> 283<li><a href="#cryptogethashes"><code>crypto.getHashes()</code></a></li> 284<li><a href="#cryptogetrandomvaluestypedarray"><code>crypto.getRandomValues(typedArray)</code></a></li> 285<li><a href="#cryptohkdfdigest-ikm-salt-info-keylen-callback"><code>crypto.hkdf(digest, ikm, salt, info, keylen, callback)</code></a></li> 286<li><a href="#cryptohkdfsyncdigest-ikm-salt-info-keylen"><code>crypto.hkdfSync(digest, ikm, salt, info, keylen)</code></a></li> 287<li><a href="#cryptopbkdf2password-salt-iterations-keylen-digest-callback"><code>crypto.pbkdf2(password, salt, iterations, keylen, digest, callback)</code></a></li> 288<li><a href="#cryptopbkdf2syncpassword-salt-iterations-keylen-digest"><code>crypto.pbkdf2Sync(password, salt, iterations, keylen, digest)</code></a></li> 289<li><a href="#cryptoprivatedecryptprivatekey-buffer"><code>crypto.privateDecrypt(privateKey, buffer)</code></a></li> 290<li><a href="#cryptoprivateencryptprivatekey-buffer"><code>crypto.privateEncrypt(privateKey, buffer)</code></a></li> 291<li><a href="#cryptopublicdecryptkey-buffer"><code>crypto.publicDecrypt(key, buffer)</code></a></li> 292<li><a href="#cryptopublicencryptkey-buffer"><code>crypto.publicEncrypt(key, buffer)</code></a></li> 293<li><a href="#cryptorandombytessize-callback"><code>crypto.randomBytes(size[, callback])</code></a></li> 294<li><a href="#cryptorandomfillsyncbuffer-offset-size"><code>crypto.randomFillSync(buffer[, offset][, size])</code></a></li> 295<li><a href="#cryptorandomfillbuffer-offset-size-callback"><code>crypto.randomFill(buffer[, offset][, size], callback)</code></a></li> 296<li><a href="#cryptorandomintmin-max-callback"><code>crypto.randomInt([min, ]max[, callback])</code></a></li> 297<li><a href="#cryptorandomuuidoptions"><code>crypto.randomUUID([options])</code></a></li> 298<li><a href="#cryptoscryptpassword-salt-keylen-options-callback"><code>crypto.scrypt(password, salt, keylen[, options], callback)</code></a></li> 299<li><a href="#cryptoscryptsyncpassword-salt-keylen-options"><code>crypto.scryptSync(password, salt, keylen[, options])</code></a></li> 300<li><a href="#cryptosecureheapused"><code>crypto.secureHeapUsed()</code></a></li> 301<li><a href="#cryptosetengineengine-flags"><code>crypto.setEngine(engine[, flags])</code></a></li> 302<li><a href="#cryptosetfipsbool"><code>crypto.setFips(bool)</code></a></li> 303<li><a href="#cryptosignalgorithm-data-key-callback"><code>crypto.sign(algorithm, data, key[, callback])</code></a></li> 304<li><a href="#cryptosubtle"><code>crypto.subtle</code></a></li> 305<li><a href="#cryptotimingsafeequala-b"><code>crypto.timingSafeEqual(a, b)</code></a></li> 306<li><a href="#cryptoverifyalgorithm-data-key-signature-callback"><code>crypto.verify(algorithm, data, key, signature[, callback])</code></a></li> 307<li><a href="#cryptowebcrypto"><code>crypto.webcrypto</code></a></li> 308</ul> 309</li> 310<li><a href="#notes">Notes</a> 311<ul> 312<li><a href="#using-strings-as-inputs-to-cryptographic-apis">Using strings as inputs to cryptographic APIs</a></li> 313<li><a href="#legacy-streams-api-prior-to-nodejs-010">Legacy streams API (prior to Node.js 0.10)</a></li> 314<li><a href="#support-for-weak-or-compromised-algorithms">Support for weak or compromised algorithms</a></li> 315<li><a href="#ccm-mode">CCM mode</a></li> 316<li><a href="#fips-mode">FIPS mode</a></li> 317</ul> 318</li> 319<li><a href="#crypto-constants">Crypto constants</a> 320<ul> 321<li><a href="#openssl-options">OpenSSL options</a></li> 322<li><a href="#openssl-engine-constants">OpenSSL engine constants</a></li> 323<li><a href="#other-openssl-constants">Other OpenSSL constants</a></li> 324<li><a href="#nodejs-crypto-constants">Node.js crypto constants</a></li> 325</ul> 326</li> 327</ul> 328</li> 329</ul></div></div> 330 </li> 331 332 333 <li class="picker-header"> 334 <a href="#"> 335 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 336 Index 337 </a> 338 339 <div class="picker"><ul> 340<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 341<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 342 343 <li> 344 <a href="index.html">Index</a> 345 </li> 346 </ul> 347 348<hr class="line"> 349<ul> 350<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 351<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 352<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 353<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 354<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 355<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 356<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 357<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 358<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 359<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 360<li><a href="console.html" class="nav-console">Console</a></li> 361<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 362<li><a href="crypto.html" class="nav-crypto active">Crypto</a></li> 363<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 364<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 365<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 366<li><a href="dns.html" class="nav-dns">DNS</a></li> 367<li><a href="domain.html" class="nav-domain">Domain</a></li> 368<li><a href="errors.html" class="nav-errors">Errors</a></li> 369<li><a href="events.html" class="nav-events">Events</a></li> 370<li><a href="fs.html" class="nav-fs">File system</a></li> 371<li><a href="globals.html" class="nav-globals">Globals</a></li> 372<li><a href="http.html" class="nav-http">HTTP</a></li> 373<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 374<li><a href="https.html" class="nav-https">HTTPS</a></li> 375<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 376<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 377<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 378<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 379<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 380<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 381<li><a href="net.html" class="nav-net">Net</a></li> 382<li><a href="os.html" class="nav-os">OS</a></li> 383<li><a href="path.html" class="nav-path">Path</a></li> 384<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 385<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 386<li><a href="process.html" class="nav-process">Process</a></li> 387<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 388<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 389<li><a href="readline.html" class="nav-readline">Readline</a></li> 390<li><a href="repl.html" class="nav-repl">REPL</a></li> 391<li><a href="report.html" class="nav-report">Report</a></li> 392<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 393<li><a href="stream.html" class="nav-stream">Stream</a></li> 394<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 395<li><a href="test.html" class="nav-test">Test runner</a></li> 396<li><a href="timers.html" class="nav-timers">Timers</a></li> 397<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 398<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 399<li><a href="tty.html" class="nav-tty">TTY</a></li> 400<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 401<li><a href="url.html" class="nav-url">URL</a></li> 402<li><a href="util.html" class="nav-util">Utilities</a></li> 403<li><a href="v8.html" class="nav-v8">V8</a></li> 404<li><a href="vm.html" class="nav-vm">VM</a></li> 405<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 406<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 407<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 408<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 409<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 410</ul> 411<hr class="line"> 412<ul> 413<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 414</ul></div> 415 </li> 416 417 418 <li class="picker-header"> 419 <a href="#"> 420 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 421 Other versions 422 </a> 423 <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/crypto.html">21.x</a></li> 424<li><a href="https://nodejs.org/docs/latest-v20.x/api/crypto.html">20.x <b>LTS</b></a></li> 425<li><a href="https://nodejs.org/docs/latest-v19.x/api/crypto.html">19.x</a></li> 426<li><a href="https://nodejs.org/docs/latest-v18.x/api/crypto.html">18.x <b>LTS</b></a></li> 427<li><a href="https://nodejs.org/docs/latest-v17.x/api/crypto.html">17.x</a></li> 428<li><a href="https://nodejs.org/docs/latest-v16.x/api/crypto.html">16.x</a></li> 429<li><a href="https://nodejs.org/docs/latest-v15.x/api/crypto.html">15.x</a></li> 430<li><a href="https://nodejs.org/docs/latest-v14.x/api/crypto.html">14.x</a></li> 431<li><a href="https://nodejs.org/docs/latest-v13.x/api/crypto.html">13.x</a></li> 432<li><a href="https://nodejs.org/docs/latest-v12.x/api/crypto.html">12.x</a></li> 433<li><a href="https://nodejs.org/docs/latest-v11.x/api/crypto.html">11.x</a></li> 434<li><a href="https://nodejs.org/docs/latest-v10.x/api/crypto.html">10.x</a></li> 435<li><a href="https://nodejs.org/docs/latest-v9.x/api/crypto.html">9.x</a></li> 436<li><a href="https://nodejs.org/docs/latest-v8.x/api/crypto.html">8.x</a></li> 437<li><a href="https://nodejs.org/docs/latest-v7.x/api/crypto.html">7.x</a></li> 438<li><a href="https://nodejs.org/docs/latest-v6.x/api/crypto.html">6.x</a></li> 439<li><a href="https://nodejs.org/docs/latest-v5.x/api/crypto.html">5.x</a></li> 440<li><a href="https://nodejs.org/docs/latest-v4.x/api/crypto.html">4.x</a></li> 441<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/crypto.html">0.12.x</a></li> 442<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/crypto.html">0.10.x</a></li></ol></div> 443 </li> 444 445 <li class="picker-header"> 446 <a href="#"> 447 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 448 Options 449 </a> 450 451 <div class="picker"> 452 <ul> 453 <li> 454 <a href="all.html">View on single page</a> 455 </li> 456 <li> 457 <a href="crypto.json">View as JSON</a> 458 </li> 459 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/crypto.md">Edit on GitHub</a></li> 460 </ul> 461 </div> 462 </li> 463 </ul> 464 </div> 465 <hr> 466 </header> 467 468 <details id="toc" open><summary>Table of contents</summary><ul> 469<li><span class="stability_2"><a href="#crypto">Crypto</a></span> 470<ul> 471<li><a href="#determining-if-crypto-support-is-unavailable">Determining if crypto support is unavailable</a></li> 472<li><a href="#class-certificate">Class: <code>Certificate</code></a> 473<ul> 474<li><a href="#static-method-certificateexportchallengespkac-encoding">Static method: <code>Certificate.exportChallenge(spkac[, encoding])</code></a></li> 475<li><a href="#static-method-certificateexportpublickeyspkac-encoding">Static method: <code>Certificate.exportPublicKey(spkac[, encoding])</code></a></li> 476<li><a href="#static-method-certificateverifyspkacspkac-encoding">Static method: <code>Certificate.verifySpkac(spkac[, encoding])</code></a></li> 477<li><span class="stability_0"><a href="#legacy-api">Legacy API</a></span> 478<ul> 479<li><a href="#new-cryptocertificate"><code>new crypto.Certificate()</code></a></li> 480<li><a href="#certificateexportchallengespkac-encoding"><code>certificate.exportChallenge(spkac[, encoding])</code></a></li> 481<li><a href="#certificateexportpublickeyspkac-encoding"><code>certificate.exportPublicKey(spkac[, encoding])</code></a></li> 482<li><a href="#certificateverifyspkacspkac-encoding"><code>certificate.verifySpkac(spkac[, encoding])</code></a></li> 483</ul> 484</li> 485</ul> 486</li> 487<li><a href="#class-cipher">Class: <code>Cipher</code></a> 488<ul> 489<li><a href="#cipherfinaloutputencoding"><code>cipher.final([outputEncoding])</code></a></li> 490<li><a href="#ciphergetauthtag"><code>cipher.getAuthTag()</code></a></li> 491<li><a href="#ciphersetaadbuffer-options"><code>cipher.setAAD(buffer[, options])</code></a></li> 492<li><a href="#ciphersetautopaddingautopadding"><code>cipher.setAutoPadding([autoPadding])</code></a></li> 493<li><a href="#cipherupdatedata-inputencoding-outputencoding"><code>cipher.update(data[, inputEncoding][, outputEncoding])</code></a></li> 494</ul> 495</li> 496<li><a href="#class-decipher">Class: <code>Decipher</code></a> 497<ul> 498<li><a href="#decipherfinaloutputencoding"><code>decipher.final([outputEncoding])</code></a></li> 499<li><a href="#deciphersetaadbuffer-options"><code>decipher.setAAD(buffer[, options])</code></a></li> 500<li><a href="#deciphersetauthtagbuffer-encoding"><code>decipher.setAuthTag(buffer[, encoding])</code></a></li> 501<li><a href="#deciphersetautopaddingautopadding"><code>decipher.setAutoPadding([autoPadding])</code></a></li> 502<li><a href="#decipherupdatedata-inputencoding-outputencoding"><code>decipher.update(data[, inputEncoding][, outputEncoding])</code></a></li> 503</ul> 504</li> 505<li><a href="#class-diffiehellman">Class: <code>DiffieHellman</code></a> 506<ul> 507<li><a href="#diffiehellmancomputesecretotherpublickey-inputencoding-outputencoding"><code>diffieHellman.computeSecret(otherPublicKey[, inputEncoding][, outputEncoding])</code></a></li> 508<li><a href="#diffiehellmangeneratekeysencoding"><code>diffieHellman.generateKeys([encoding])</code></a></li> 509<li><a href="#diffiehellmangetgeneratorencoding"><code>diffieHellman.getGenerator([encoding])</code></a></li> 510<li><a href="#diffiehellmangetprimeencoding"><code>diffieHellman.getPrime([encoding])</code></a></li> 511<li><a href="#diffiehellmangetprivatekeyencoding"><code>diffieHellman.getPrivateKey([encoding])</code></a></li> 512<li><a href="#diffiehellmangetpublickeyencoding"><code>diffieHellman.getPublicKey([encoding])</code></a></li> 513<li><a href="#diffiehellmansetprivatekeyprivatekey-encoding"><code>diffieHellman.setPrivateKey(privateKey[, encoding])</code></a></li> 514<li><a href="#diffiehellmansetpublickeypublickey-encoding"><code>diffieHellman.setPublicKey(publicKey[, encoding])</code></a></li> 515<li><a href="#diffiehellmanverifyerror"><code>diffieHellman.verifyError</code></a></li> 516</ul> 517</li> 518<li><a href="#class-diffiehellmangroup">Class: <code>DiffieHellmanGroup</code></a></li> 519<li><a href="#class-ecdh">Class: <code>ECDH</code></a> 520<ul> 521<li><a href="#static-method-ecdhconvertkeykey-curve-inputencoding-outputencoding-format">Static method: <code>ECDH.convertKey(key, curve[, inputEncoding[, outputEncoding[, format]]])</code></a></li> 522<li><a href="#ecdhcomputesecretotherpublickey-inputencoding-outputencoding"><code>ecdh.computeSecret(otherPublicKey[, inputEncoding][, outputEncoding])</code></a></li> 523<li><a href="#ecdhgeneratekeysencoding-format"><code>ecdh.generateKeys([encoding[, format]])</code></a></li> 524<li><a href="#ecdhgetprivatekeyencoding"><code>ecdh.getPrivateKey([encoding])</code></a></li> 525<li><a href="#ecdhgetpublickeyencoding-format"><code>ecdh.getPublicKey([encoding][, format])</code></a></li> 526<li><a href="#ecdhsetprivatekeyprivatekey-encoding"><code>ecdh.setPrivateKey(privateKey[, encoding])</code></a></li> 527<li><span class="stability_0"><a href="#ecdhsetpublickeypublickey-encoding"><code>ecdh.setPublicKey(publicKey[, encoding])</code></a></span></li> 528</ul> 529</li> 530<li><a href="#class-hash">Class: <code>Hash</code></a> 531<ul> 532<li><a href="#hashcopyoptions"><code>hash.copy([options])</code></a></li> 533<li><a href="#hashdigestencoding"><code>hash.digest([encoding])</code></a></li> 534<li><a href="#hashupdatedata-inputencoding"><code>hash.update(data[, inputEncoding])</code></a></li> 535</ul> 536</li> 537<li><a href="#class-hmac">Class: <code>Hmac</code></a> 538<ul> 539<li><a href="#hmacdigestencoding"><code>hmac.digest([encoding])</code></a></li> 540<li><a href="#hmacupdatedata-inputencoding"><code>hmac.update(data[, inputEncoding])</code></a></li> 541</ul> 542</li> 543<li><a href="#class-keyobject">Class: <code>KeyObject</code></a> 544<ul> 545<li><a href="#static-method-keyobjectfromkey">Static method: <code>KeyObject.from(key)</code></a></li> 546<li><a href="#keyobjectasymmetrickeydetails"><code>keyObject.asymmetricKeyDetails</code></a></li> 547<li><a href="#keyobjectasymmetrickeytype"><code>keyObject.asymmetricKeyType</code></a></li> 548<li><a href="#keyobjectexportoptions"><code>keyObject.export([options])</code></a></li> 549<li><a href="#keyobjectequalsotherkeyobject"><code>keyObject.equals(otherKeyObject)</code></a></li> 550<li><a href="#keyobjectsymmetrickeysize"><code>keyObject.symmetricKeySize</code></a></li> 551<li><a href="#keyobjecttype"><code>keyObject.type</code></a></li> 552</ul> 553</li> 554<li><a href="#class-sign">Class: <code>Sign</code></a> 555<ul> 556<li><a href="#signsignprivatekey-outputencoding"><code>sign.sign(privateKey[, outputEncoding])</code></a></li> 557<li><a href="#signupdatedata-inputencoding"><code>sign.update(data[, inputEncoding])</code></a></li> 558</ul> 559</li> 560<li><a href="#class-verify">Class: <code>Verify</code></a> 561<ul> 562<li><a href="#verifyupdatedata-inputencoding"><code>verify.update(data[, inputEncoding])</code></a></li> 563<li><a href="#verifyverifyobject-signature-signatureencoding"><code>verify.verify(object, signature[, signatureEncoding])</code></a></li> 564</ul> 565</li> 566<li><a href="#class-x509certificate">Class: <code>X509Certificate</code></a> 567<ul> 568<li><a href="#new-x509certificatebuffer"><code>new X509Certificate(buffer)</code></a></li> 569<li><a href="#x509ca"><code>x509.ca</code></a></li> 570<li><a href="#x509checkemailemail-options"><code>x509.checkEmail(email[, options])</code></a></li> 571<li><a href="#x509checkhostname-options"><code>x509.checkHost(name[, options])</code></a></li> 572<li><a href="#x509checkipip"><code>x509.checkIP(ip)</code></a></li> 573<li><a href="#x509checkissuedothercert"><code>x509.checkIssued(otherCert)</code></a></li> 574<li><a href="#x509checkprivatekeyprivatekey"><code>x509.checkPrivateKey(privateKey)</code></a></li> 575<li><a href="#x509fingerprint"><code>x509.fingerprint</code></a></li> 576<li><a href="#x509fingerprint256"><code>x509.fingerprint256</code></a></li> 577<li><a href="#x509fingerprint512"><code>x509.fingerprint512</code></a></li> 578<li><a href="#x509infoaccess"><code>x509.infoAccess</code></a></li> 579<li><a href="#x509issuer"><code>x509.issuer</code></a></li> 580<li><a href="#x509issuercertificate"><code>x509.issuerCertificate</code></a></li> 581<li><a href="#x509keyusage"><code>x509.keyUsage</code></a></li> 582<li><a href="#x509publickey"><code>x509.publicKey</code></a></li> 583<li><a href="#x509raw"><code>x509.raw</code></a></li> 584<li><a href="#x509serialnumber"><code>x509.serialNumber</code></a></li> 585<li><a href="#x509subject"><code>x509.subject</code></a></li> 586<li><a href="#x509subjectaltname"><code>x509.subjectAltName</code></a></li> 587<li><a href="#x509tojson"><code>x509.toJSON()</code></a></li> 588<li><a href="#x509tolegacyobject"><code>x509.toLegacyObject()</code></a></li> 589<li><a href="#x509tostring"><code>x509.toString()</code></a></li> 590<li><a href="#x509validfrom"><code>x509.validFrom</code></a></li> 591<li><a href="#x509validto"><code>x509.validTo</code></a></li> 592<li><a href="#x509verifypublickey"><code>x509.verify(publicKey)</code></a></li> 593</ul> 594</li> 595<li><a href="#nodecrypto-module-methods-and-properties"><code>node:crypto</code> module methods and properties</a> 596<ul> 597<li><a href="#cryptoconstants"><code>crypto.constants</code></a></li> 598<li><span class="stability_0"><a href="#cryptodefault_encoding"><code>crypto.DEFAULT_ENCODING</code></a></span></li> 599<li><span class="stability_0"><a href="#cryptofips"><code>crypto.fips</code></a></span></li> 600<li><a href="#cryptocheckprimecandidate-options-callback"><code>crypto.checkPrime(candidate[, options], callback)</code></a></li> 601<li><a href="#cryptocheckprimesynccandidate-options"><code>crypto.checkPrimeSync(candidate[, options])</code></a></li> 602<li><span class="stability_0"><a href="#cryptocreatecipheralgorithm-password-options"><code>crypto.createCipher(algorithm, password[, options])</code></a></span></li> 603<li><a href="#cryptocreatecipherivalgorithm-key-iv-options"><code>crypto.createCipheriv(algorithm, key, iv[, options])</code></a></li> 604<li><span class="stability_0"><a href="#cryptocreatedecipheralgorithm-password-options"><code>crypto.createDecipher(algorithm, password[, options])</code></a></span></li> 605<li><a href="#cryptocreatedecipherivalgorithm-key-iv-options"><code>crypto.createDecipheriv(algorithm, key, iv[, options])</code></a></li> 606<li><a href="#cryptocreatediffiehellmanprime-primeencoding-generator-generatorencoding"><code>crypto.createDiffieHellman(prime[, primeEncoding][, generator][, generatorEncoding])</code></a></li> 607<li><a href="#cryptocreatediffiehellmanprimelength-generator"><code>crypto.createDiffieHellman(primeLength[, generator])</code></a></li> 608<li><a href="#cryptocreatediffiehellmangroupname"><code>crypto.createDiffieHellmanGroup(name)</code></a></li> 609<li><a href="#cryptocreateecdhcurvename"><code>crypto.createECDH(curveName)</code></a></li> 610<li><a href="#cryptocreatehashalgorithm-options"><code>crypto.createHash(algorithm[, options])</code></a></li> 611<li><a href="#cryptocreatehmacalgorithm-key-options"><code>crypto.createHmac(algorithm, key[, options])</code></a></li> 612<li><a href="#cryptocreateprivatekeykey"><code>crypto.createPrivateKey(key)</code></a></li> 613<li><a href="#cryptocreatepublickeykey"><code>crypto.createPublicKey(key)</code></a></li> 614<li><a href="#cryptocreatesecretkeykey-encoding"><code>crypto.createSecretKey(key[, encoding])</code></a></li> 615<li><a href="#cryptocreatesignalgorithm-options"><code>crypto.createSign(algorithm[, options])</code></a></li> 616<li><a href="#cryptocreateverifyalgorithm-options"><code>crypto.createVerify(algorithm[, options])</code></a></li> 617<li><a href="#cryptodiffiehellmanoptions"><code>crypto.diffieHellman(options)</code></a></li> 618<li><a href="#cryptogeneratekeytype-options-callback"><code>crypto.generateKey(type, options, callback)</code></a></li> 619<li><a href="#cryptogeneratekeypairtype-options-callback"><code>crypto.generateKeyPair(type, options, callback)</code></a></li> 620<li><a href="#cryptogeneratekeypairsynctype-options"><code>crypto.generateKeyPairSync(type, options)</code></a></li> 621<li><a href="#cryptogeneratekeysynctype-options"><code>crypto.generateKeySync(type, options)</code></a></li> 622<li><a href="#cryptogenerateprimesize-options-callback"><code>crypto.generatePrime(size[, options[, callback]])</code></a></li> 623<li><a href="#cryptogenerateprimesyncsize-options"><code>crypto.generatePrimeSync(size[, options])</code></a></li> 624<li><a href="#cryptogetcipherinfonameornid-options"><code>crypto.getCipherInfo(nameOrNid[, options])</code></a></li> 625<li><a href="#cryptogetciphers"><code>crypto.getCiphers()</code></a></li> 626<li><a href="#cryptogetcurves"><code>crypto.getCurves()</code></a></li> 627<li><a href="#cryptogetdiffiehellmangroupname"><code>crypto.getDiffieHellman(groupName)</code></a></li> 628<li><a href="#cryptogetfips"><code>crypto.getFips()</code></a></li> 629<li><a href="#cryptogethashes"><code>crypto.getHashes()</code></a></li> 630<li><a href="#cryptogetrandomvaluestypedarray"><code>crypto.getRandomValues(typedArray)</code></a></li> 631<li><a href="#cryptohkdfdigest-ikm-salt-info-keylen-callback"><code>crypto.hkdf(digest, ikm, salt, info, keylen, callback)</code></a></li> 632<li><a href="#cryptohkdfsyncdigest-ikm-salt-info-keylen"><code>crypto.hkdfSync(digest, ikm, salt, info, keylen)</code></a></li> 633<li><a href="#cryptopbkdf2password-salt-iterations-keylen-digest-callback"><code>crypto.pbkdf2(password, salt, iterations, keylen, digest, callback)</code></a></li> 634<li><a href="#cryptopbkdf2syncpassword-salt-iterations-keylen-digest"><code>crypto.pbkdf2Sync(password, salt, iterations, keylen, digest)</code></a></li> 635<li><a href="#cryptoprivatedecryptprivatekey-buffer"><code>crypto.privateDecrypt(privateKey, buffer)</code></a></li> 636<li><a href="#cryptoprivateencryptprivatekey-buffer"><code>crypto.privateEncrypt(privateKey, buffer)</code></a></li> 637<li><a href="#cryptopublicdecryptkey-buffer"><code>crypto.publicDecrypt(key, buffer)</code></a></li> 638<li><a href="#cryptopublicencryptkey-buffer"><code>crypto.publicEncrypt(key, buffer)</code></a></li> 639<li><a href="#cryptorandombytessize-callback"><code>crypto.randomBytes(size[, callback])</code></a></li> 640<li><a href="#cryptorandomfillsyncbuffer-offset-size"><code>crypto.randomFillSync(buffer[, offset][, size])</code></a></li> 641<li><a href="#cryptorandomfillbuffer-offset-size-callback"><code>crypto.randomFill(buffer[, offset][, size], callback)</code></a></li> 642<li><a href="#cryptorandomintmin-max-callback"><code>crypto.randomInt([min, ]max[, callback])</code></a></li> 643<li><a href="#cryptorandomuuidoptions"><code>crypto.randomUUID([options])</code></a></li> 644<li><a href="#cryptoscryptpassword-salt-keylen-options-callback"><code>crypto.scrypt(password, salt, keylen[, options], callback)</code></a></li> 645<li><a href="#cryptoscryptsyncpassword-salt-keylen-options"><code>crypto.scryptSync(password, salt, keylen[, options])</code></a></li> 646<li><a href="#cryptosecureheapused"><code>crypto.secureHeapUsed()</code></a></li> 647<li><a href="#cryptosetengineengine-flags"><code>crypto.setEngine(engine[, flags])</code></a></li> 648<li><a href="#cryptosetfipsbool"><code>crypto.setFips(bool)</code></a></li> 649<li><a href="#cryptosignalgorithm-data-key-callback"><code>crypto.sign(algorithm, data, key[, callback])</code></a></li> 650<li><a href="#cryptosubtle"><code>crypto.subtle</code></a></li> 651<li><a href="#cryptotimingsafeequala-b"><code>crypto.timingSafeEqual(a, b)</code></a></li> 652<li><a href="#cryptoverifyalgorithm-data-key-signature-callback"><code>crypto.verify(algorithm, data, key, signature[, callback])</code></a></li> 653<li><a href="#cryptowebcrypto"><code>crypto.webcrypto</code></a></li> 654</ul> 655</li> 656<li><a href="#notes">Notes</a> 657<ul> 658<li><a href="#using-strings-as-inputs-to-cryptographic-apis">Using strings as inputs to cryptographic APIs</a></li> 659<li><a href="#legacy-streams-api-prior-to-nodejs-010">Legacy streams API (prior to Node.js 0.10)</a></li> 660<li><a href="#support-for-weak-or-compromised-algorithms">Support for weak or compromised algorithms</a></li> 661<li><a href="#ccm-mode">CCM mode</a></li> 662<li><a href="#fips-mode">FIPS mode</a></li> 663</ul> 664</li> 665<li><a href="#crypto-constants">Crypto constants</a> 666<ul> 667<li><a href="#openssl-options">OpenSSL options</a></li> 668<li><a href="#openssl-engine-constants">OpenSSL engine constants</a></li> 669<li><a href="#other-openssl-constants">Other OpenSSL constants</a></li> 670<li><a href="#nodejs-crypto-constants">Node.js crypto constants</a></li> 671</ul> 672</li> 673</ul> 674</li> 675</ul></details> 676 677 <div id="apicontent"> 678 <h2>Crypto<span><a class="mark" href="#crypto" id="crypto">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto"></a></h2> 679 680<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p> 681<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.20.1/lib/crypto.js">lib/crypto.js</a></p> 682<p>The <code>node:crypto</code> module provides cryptographic functionality that includes a 683set of wrappers for OpenSSL's hash, HMAC, cipher, decipher, sign, and verify 684functions.</p> 685 686<pre class="with-51-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">const</span> { createHmac } = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 687 688<span class="hljs-keyword">const</span> secret = <span class="hljs-string">'abcdefg'</span>; 689<span class="hljs-keyword">const</span> hash = <span class="hljs-title function_">createHmac</span>(<span class="hljs-string">'sha256'</span>, secret) 690 .<span class="hljs-title function_">update</span>(<span class="hljs-string">'I love cupcakes'</span>) 691 .<span class="hljs-title function_">digest</span>(<span class="hljs-string">'hex'</span>); 692<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(hash); 693<span class="hljs-comment">// Prints:</span> 694<span class="hljs-comment">// c0fa1bc00531bd78ef38c628449c5102aeabd49b5dc3a2a516ea6ea959d6658e</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { createHmac } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 695 696<span class="hljs-keyword">const</span> secret = <span class="hljs-string">'abcdefg'</span>; 697<span class="hljs-keyword">const</span> hash = <span class="hljs-title function_">createHmac</span>(<span class="hljs-string">'sha256'</span>, secret) 698 .<span class="hljs-title function_">update</span>(<span class="hljs-string">'I love cupcakes'</span>) 699 .<span class="hljs-title function_">digest</span>(<span class="hljs-string">'hex'</span>); 700<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(hash); 701<span class="hljs-comment">// Prints:</span> 702<span class="hljs-comment">// c0fa1bc00531bd78ef38c628449c5102aeabd49b5dc3a2a516ea6ea959d6658e</span></code><button class="copy-button">copy</button></pre> 703<section><h3>Determining if crypto support is unavailable<span><a class="mark" href="#determining-if-crypto-support-is-unavailable" id="determining-if-crypto-support-is-unavailable">#</a></span><a aria-hidden="true" class="legacy" id="crypto_determining_if_crypto_support_is_unavailable"></a></h3> 704<p>It is possible for Node.js to be built without including support for the 705<code>node:crypto</code> module. In such cases, attempting to <code>import</code> from <code>crypto</code> or 706calling <code>require('node:crypto')</code> will result in an error being thrown.</p> 707<p>When using CommonJS, the error thrown can be caught using try/catch:</p> 708<!-- eslint-skip --> 709<pre><code class="language-js cjs"><span class="hljs-keyword">let</span> crypto; 710<span class="hljs-keyword">try</span> { 711 crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 712} <span class="hljs-keyword">catch</span> (err) { 713 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'crypto support is disabled!'</span>); 714}</code> <button class="copy-button">copy</button></pre> 715<p>When using the lexical ESM <code>import</code> keyword, the error can only be 716caught if a handler for <code>process.on('uncaughtException')</code> is registered 717<em>before</em> any attempt to load the module is made (using, for instance, 718a preload module).</p> 719<p>When using ESM, if there is a chance that the code may be run on a build 720of Node.js where crypto support is not enabled, consider using the 721<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import"><code>import()</code></a> function instead of the lexical <code>import</code> keyword:</p> 722<pre><code class="language-js mjs"><span class="hljs-keyword">let</span> crypto; 723<span class="hljs-keyword">try</span> { 724 crypto = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 725} <span class="hljs-keyword">catch</span> (err) { 726 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'crypto support is disabled!'</span>); 727}</code> <button class="copy-button">copy</button></pre> 728</section><section><h3>Class: <code>Certificate</code><span><a class="mark" href="#class-certificate" id="class-certificate">#</a></span><a aria-hidden="true" class="legacy" id="crypto_class_certificate"></a></h3> 729<div class="api_metadata"> 730<span>Added in: v0.11.8</span> 731</div> 732<p>SPKAC is a Certificate Signing Request mechanism originally implemented by 733Netscape and was specified formally as part of HTML5's <code>keygen</code> element.</p> 734<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 735should not use this element anymore.</p> 736<p>The <code>node:crypto</code> module provides the <code>Certificate</code> class for working with SPKAC 737data. The most common usage is handling output generated by the HTML5 738<code><keygen></code> element. Node.js uses <a href="https://www.openssl.org/docs/man3.0/man1/openssl-spkac.html">OpenSSL's SPKAC implementation</a> internally.</p> 739<h4>Static method: <code>Certificate.exportChallenge(spkac[, encoding])</code><span><a class="mark" href="#static-method-certificateexportchallengespkac-encoding" id="static-method-certificateexportchallengespkac-encoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_static_method_certificate_exportchallenge_spkac_encoding"></a></h4> 740<div class="api_metadata"> 741<details class="changelog"><summary>History</summary> 742<table> 743<tbody><tr><th>Version</th><th>Changes</th></tr> 744<tr><td>v15.0.0</td> 745<td><p>The spkac argument can be an ArrayBuffer. Limited the size of the spkac argument to a maximum of 2**31 - 1 bytes.</p></td></tr> 746<tr><td>v9.0.0</td> 747<td><p><span>Added in: v9.0.0</span></p></td></tr> 748</tbody></table> 749</details> 750</div> 751<ul> 752<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 753<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#buffers-and-character-encodings">encoding</a> of the <code>spkac</code> string.</li> 754<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> The challenge component of the <code>spkac</code> data structure, which 755includes a public key and a challenge.</li> 756</ul> 757 758<pre class="with-52-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Certificate</span> } = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 759<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>(); 760<span class="hljs-keyword">const</span> challenge = <span class="hljs-title class_">Certificate</span>.<span class="hljs-title function_">exportChallenge</span>(spkac); 761<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>)); 762<span class="hljs-comment">// Prints: the challenge as a UTF8 string</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Certificate</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 763<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>(); 764<span class="hljs-keyword">const</span> challenge = <span class="hljs-title class_">Certificate</span>.<span class="hljs-title function_">exportChallenge</span>(spkac); 765<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>)); 766<span class="hljs-comment">// Prints: the challenge as a UTF8 string</span></code><button class="copy-button">copy</button></pre> 767<h4>Static method: <code>Certificate.exportPublicKey(spkac[, encoding])</code><span><a class="mark" href="#static-method-certificateexportpublickeyspkac-encoding" id="static-method-certificateexportpublickeyspkac-encoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_static_method_certificate_exportpublickey_spkac_encoding"></a></h4> 768<div class="api_metadata"> 769<details class="changelog"><summary>History</summary> 770<table> 771<tbody><tr><th>Version</th><th>Changes</th></tr> 772<tr><td>v15.0.0</td> 773<td><p>The spkac argument can be an ArrayBuffer. Limited the size of the spkac argument to a maximum of 2**31 - 1 bytes.</p></td></tr> 774<tr><td>v9.0.0</td> 775<td><p><span>Added in: v9.0.0</span></p></td></tr> 776</tbody></table> 777</details> 778</div> 779<ul> 780<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 781<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#buffers-and-character-encodings">encoding</a> of the <code>spkac</code> string.</li> 782<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> The public key component of the <code>spkac</code> data structure, 783which includes a public key and a challenge.</li> 784</ul> 785 786<pre class="with-52-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Certificate</span> } = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 787<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>(); 788<span class="hljs-keyword">const</span> publicKey = <span class="hljs-title class_">Certificate</span>.<span class="hljs-title function_">exportPublicKey</span>(spkac); 789<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(publicKey); 790<span class="hljs-comment">// Prints: the public key as <Buffer ...></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Certificate</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 791<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>(); 792<span class="hljs-keyword">const</span> publicKey = <span class="hljs-title class_">Certificate</span>.<span class="hljs-title function_">exportPublicKey</span>(spkac); 793<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(publicKey); 794<span class="hljs-comment">// Prints: the public key as <Buffer ...></span></code><button class="copy-button">copy</button></pre> 795<h4>Static method: <code>Certificate.verifySpkac(spkac[, encoding])</code><span><a class="mark" href="#static-method-certificateverifyspkacspkac-encoding" id="static-method-certificateverifyspkacspkac-encoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_static_method_certificate_verifyspkac_spkac_encoding"></a></h4> 796<div class="api_metadata"> 797<details class="changelog"><summary>History</summary> 798<table> 799<tbody><tr><th>Version</th><th>Changes</th></tr> 800<tr><td>v15.0.0</td> 801<td><p>The spkac argument can be an ArrayBuffer. Added encoding. Limited the size of the spkac argument to a maximum of 2**31 - 1 bytes.</p></td></tr> 802<tr><td>v9.0.0</td> 803<td><p><span>Added in: v9.0.0</span></p></td></tr> 804</tbody></table> 805</details> 806</div> 807<ul> 808<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 809<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#buffers-and-character-encodings">encoding</a> of the <code>spkac</code> string.</li> 810<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, 811<code>false</code> otherwise.</li> 812</ul> 813 814<pre class="with-52-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 815<span class="hljs-keyword">const</span> { <span class="hljs-title class_">Certificate</span> } = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 816 817<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>(); 818<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))); 819<span class="hljs-comment">// Prints: true or false</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>); 820<span class="hljs-keyword">const</span> { <span class="hljs-title class_">Certificate</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 821 822<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>(); 823<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))); 824<span class="hljs-comment">// Prints: true or false</span></code><button class="copy-button">copy</button></pre> 825<h4>Legacy API<span><a class="mark" href="#legacy-api" id="legacy-api">#</a></span><a aria-hidden="true" class="legacy" id="crypto_legacy_api"></a></h4> 826<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated</div><p></p> 827<p>As a legacy interface, it is possible to create new instances of 828the <code>crypto.Certificate</code> class as illustrated in the examples below.</p> 829<h5><code>new crypto.Certificate()</code><span><a class="mark" href="#new-cryptocertificate" id="new-cryptocertificate">#</a></span><a aria-hidden="true" class="legacy" id="crypto_new_crypto_certificate"></a></h5> 830<p>Instances of the <code>Certificate</code> class can be created using the <code>new</code> keyword 831or by calling <code>crypto.Certificate()</code> as a function:</p> 832 833<pre class="with-52-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Certificate</span> } = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 834 835<span class="hljs-keyword">const</span> cert1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Certificate</span>(); 836<span class="hljs-keyword">const</span> cert2 = <span class="hljs-title class_">Certificate</span>();</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Certificate</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 837 838<span class="hljs-keyword">const</span> cert1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Certificate</span>(); 839<span class="hljs-keyword">const</span> cert2 = <span class="hljs-title class_">Certificate</span>();</code><button class="copy-button">copy</button></pre> 840<h5><code>certificate.exportChallenge(spkac[, encoding])</code><span><a class="mark" href="#certificateexportchallengespkac-encoding" id="certificateexportchallengespkac-encoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_certificate_exportchallenge_spkac_encoding"></a></h5> 841<div class="api_metadata"> 842<span>Added in: v0.11.8</span> 843</div> 844<ul> 845<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 846<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#buffers-and-character-encodings">encoding</a> of the <code>spkac</code> string.</li> 847<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> The challenge component of the <code>spkac</code> data structure, which 848includes a public key and a challenge.</li> 849</ul> 850 851<pre class="with-52-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Certificate</span> } = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 852<span class="hljs-keyword">const</span> cert = <span class="hljs-title class_">Certificate</span>(); 853<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>(); 854<span class="hljs-keyword">const</span> challenge = cert.<span class="hljs-title function_">exportChallenge</span>(spkac); 855<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>)); 856<span class="hljs-comment">// Prints: the challenge as a UTF8 string</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Certificate</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 857<span class="hljs-keyword">const</span> cert = <span class="hljs-title class_">Certificate</span>(); 858<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>(); 859<span class="hljs-keyword">const</span> challenge = cert.<span class="hljs-title function_">exportChallenge</span>(spkac); 860<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>)); 861<span class="hljs-comment">// Prints: the challenge as a UTF8 string</span></code><button class="copy-button">copy</button></pre> 862<h5><code>certificate.exportPublicKey(spkac[, encoding])</code><span><a class="mark" href="#certificateexportpublickeyspkac-encoding" id="certificateexportpublickeyspkac-encoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_certificate_exportpublickey_spkac_encoding"></a></h5> 863<div class="api_metadata"> 864<span>Added in: v0.11.8</span> 865</div> 866<ul> 867<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 868<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#buffers-and-character-encodings">encoding</a> of the <code>spkac</code> string.</li> 869<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> The public key component of the <code>spkac</code> data structure, 870which includes a public key and a challenge.</li> 871</ul> 872 873<pre class="with-52-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Certificate</span> } = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 874<span class="hljs-keyword">const</span> cert = <span class="hljs-title class_">Certificate</span>(); 875<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>(); 876<span class="hljs-keyword">const</span> publicKey = cert.<span class="hljs-title function_">exportPublicKey</span>(spkac); 877<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(publicKey); 878<span class="hljs-comment">// Prints: the public key as <Buffer ...></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Certificate</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 879<span class="hljs-keyword">const</span> cert = <span class="hljs-title class_">Certificate</span>(); 880<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>(); 881<span class="hljs-keyword">const</span> publicKey = cert.<span class="hljs-title function_">exportPublicKey</span>(spkac); 882<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(publicKey); 883<span class="hljs-comment">// Prints: the public key as <Buffer ...></span></code><button class="copy-button">copy</button></pre> 884<h5><code>certificate.verifySpkac(spkac[, encoding])</code><span><a class="mark" href="#certificateverifyspkacspkac-encoding" id="certificateverifyspkacspkac-encoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_certificate_verifyspkac_spkac_encoding"></a></h5> 885<div class="api_metadata"> 886<span>Added in: v0.11.8</span> 887</div> 888<ul> 889<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 890<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#buffers-and-character-encodings">encoding</a> of the <code>spkac</code> string.</li> 891<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, 892<code>false</code> otherwise.</li> 893</ul> 894 895<pre class="with-52-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 896<span class="hljs-keyword">const</span> { <span class="hljs-title class_">Certificate</span> } = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 897 898<span class="hljs-keyword">const</span> cert = <span class="hljs-title class_">Certificate</span>(); 899<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>(); 900<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))); 901<span class="hljs-comment">// Prints: true or false</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>); 902<span class="hljs-keyword">const</span> { <span class="hljs-title class_">Certificate</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 903 904<span class="hljs-keyword">const</span> cert = <span class="hljs-title class_">Certificate</span>(); 905<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>(); 906<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))); 907<span class="hljs-comment">// Prints: true or false</span></code><button class="copy-button">copy</button></pre> 908</section><section><h3>Class: <code>Cipher</code><span><a class="mark" href="#class-cipher" id="class-cipher">#</a></span><a aria-hidden="true" class="legacy" id="crypto_class_cipher"></a></h3> 909<div class="api_metadata"> 910<span>Added in: v0.1.94</span> 911</div> 912<ul> 913<li>Extends: <a href="stream.html#class-streamtransform" class="type"><stream.Transform></a></li> 914</ul> 915<p>Instances of the <code>Cipher</code> class are used to encrypt data. The class can be 916used in one of two ways:</p> 917<ul> 918<li>As a <a href="stream.html">stream</a> that is both readable and writable, where plain unencrypted 919data is written to produce encrypted data on the readable side, or</li> 920<li>Using the <a href="#cipherupdatedata-inputencoding-outputencoding"><code>cipher.update()</code></a> and <a href="#cipherfinaloutputencoding"><code>cipher.final()</code></a> methods to produce 921the encrypted data.</li> 922</ul> 923<p>The <a href="#cryptocreatecipheralgorithm-password-options"><code>crypto.createCipher()</code></a> or <a href="#cryptocreatecipherivalgorithm-key-iv-options"><code>crypto.createCipheriv()</code></a> methods are 924used to create <code>Cipher</code> instances. <code>Cipher</code> objects are not to be created 925directly using the <code>new</code> keyword.</p> 926<p>Example: Using <code>Cipher</code> objects as streams:</p> 927 928<pre class="with-9-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">const</span> { 929 scrypt, 930 randomFill, 931 createCipheriv, 932} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 933 934<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>; 935<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>; 936 937<span class="hljs-comment">// First, we'll generate the key. The key length is dependent on the algorithm.</span> 938<span class="hljs-comment">// In this case for aes192, it is 24 bytes (192 bits).</span> 939<span class="hljs-title function_">scrypt</span>(password, <span class="hljs-string">'salt'</span>, <span class="hljs-number">24</span>, <span class="hljs-function">(<span class="hljs-params">err, key</span>) =></span> { 940 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 941 <span class="hljs-comment">// Then, we'll generate a random initialization vector</span> 942 <span class="hljs-title function_">randomFill</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint8Array</span>(<span class="hljs-number">16</span>), <span class="hljs-function">(<span class="hljs-params">err, iv</span>) =></span> { 943 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 944 945 <span class="hljs-comment">// Once we have the key and iv, we can create and use the cipher...</span> 946 <span class="hljs-keyword">const</span> cipher = <span class="hljs-title function_">createCipheriv</span>(algorithm, key, iv); 947 948 <span class="hljs-keyword">let</span> encrypted = <span class="hljs-string">''</span>; 949 cipher.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'hex'</span>); 950 951 cipher.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">chunk</span>) =></span> encrypted += chunk); 952 cipher.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(encrypted)); 953 954 cipher.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some clear text data'</span>); 955 cipher.<span class="hljs-title function_">end</span>(); 956 }); 957});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 958 scrypt, 959 randomFill, 960 createCipheriv, 961} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 962 963<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>; 964<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>; 965 966<span class="hljs-comment">// First, we'll generate the key. The key length is dependent on the algorithm.</span> 967<span class="hljs-comment">// In this case for aes192, it is 24 bytes (192 bits).</span> 968<span class="hljs-title function_">scrypt</span>(password, <span class="hljs-string">'salt'</span>, <span class="hljs-number">24</span>, <span class="hljs-function">(<span class="hljs-params">err, key</span>) =></span> { 969 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 970 <span class="hljs-comment">// Then, we'll generate a random initialization vector</span> 971 <span class="hljs-title function_">randomFill</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint8Array</span>(<span class="hljs-number">16</span>), <span class="hljs-function">(<span class="hljs-params">err, iv</span>) =></span> { 972 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 973 974 <span class="hljs-comment">// Once we have the key and iv, we can create and use the cipher...</span> 975 <span class="hljs-keyword">const</span> cipher = <span class="hljs-title function_">createCipheriv</span>(algorithm, key, iv); 976 977 <span class="hljs-keyword">let</span> encrypted = <span class="hljs-string">''</span>; 978 cipher.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'hex'</span>); 979 980 cipher.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">chunk</span>) =></span> encrypted += chunk); 981 cipher.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(encrypted)); 982 983 cipher.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some clear text data'</span>); 984 cipher.<span class="hljs-title function_">end</span>(); 985 }); 986});</code><button class="copy-button">copy</button></pre> 987<p>Example: Using <code>Cipher</code> and piped streams:</p> 988 989<pre class="with-19-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { 990 createReadStream, 991 createWriteStream, 992} <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 993 994<span class="hljs-keyword">import</span> { 995 pipeline, 996} <span class="hljs-keyword">from</span> <span class="hljs-string">'node:stream'</span>; 997 998<span class="hljs-keyword">const</span> { 999 scrypt, 1000 randomFill, 1001 createCipheriv, 1002} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 1003 1004<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>; 1005<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>; 1006 1007<span class="hljs-comment">// First, we'll generate the key. The key length is dependent on the algorithm.</span> 1008<span class="hljs-comment">// In this case for aes192, it is 24 bytes (192 bits).</span> 1009<span class="hljs-title function_">scrypt</span>(password, <span class="hljs-string">'salt'</span>, <span class="hljs-number">24</span>, <span class="hljs-function">(<span class="hljs-params">err, key</span>) =></span> { 1010 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 1011 <span class="hljs-comment">// Then, we'll generate a random initialization vector</span> 1012 <span class="hljs-title function_">randomFill</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint8Array</span>(<span class="hljs-number">16</span>), <span class="hljs-function">(<span class="hljs-params">err, iv</span>) =></span> { 1013 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 1014 1015 <span class="hljs-keyword">const</span> cipher = <span class="hljs-title function_">createCipheriv</span>(algorithm, key, iv); 1016 1017 <span class="hljs-keyword">const</span> input = <span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'test.js'</span>); 1018 <span class="hljs-keyword">const</span> output = <span class="hljs-title function_">createWriteStream</span>(<span class="hljs-string">'test.enc'</span>); 1019 1020 <span class="hljs-title function_">pipeline</span>(input, cipher, output, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 1021 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 1022 }); 1023 }); 1024});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 1025 createReadStream, 1026 createWriteStream, 1027} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 1028 1029<span class="hljs-keyword">const</span> { 1030 pipeline, 1031} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:stream'</span>); 1032 1033<span class="hljs-keyword">const</span> { 1034 scrypt, 1035 randomFill, 1036 createCipheriv, 1037} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 1038 1039<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>; 1040<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>; 1041 1042<span class="hljs-comment">// First, we'll generate the key. The key length is dependent on the algorithm.</span> 1043<span class="hljs-comment">// In this case for aes192, it is 24 bytes (192 bits).</span> 1044<span class="hljs-title function_">scrypt</span>(password, <span class="hljs-string">'salt'</span>, <span class="hljs-number">24</span>, <span class="hljs-function">(<span class="hljs-params">err, key</span>) =></span> { 1045 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 1046 <span class="hljs-comment">// Then, we'll generate a random initialization vector</span> 1047 <span class="hljs-title function_">randomFill</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint8Array</span>(<span class="hljs-number">16</span>), <span class="hljs-function">(<span class="hljs-params">err, iv</span>) =></span> { 1048 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 1049 1050 <span class="hljs-keyword">const</span> cipher = <span class="hljs-title function_">createCipheriv</span>(algorithm, key, iv); 1051 1052 <span class="hljs-keyword">const</span> input = <span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'test.js'</span>); 1053 <span class="hljs-keyword">const</span> output = <span class="hljs-title function_">createWriteStream</span>(<span class="hljs-string">'test.enc'</span>); 1054 1055 <span class="hljs-title function_">pipeline</span>(input, cipher, output, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 1056 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 1057 }); 1058 }); 1059});</code><button class="copy-button">copy</button></pre> 1060<p>Example: Using the <a href="#cipherupdatedata-inputencoding-outputencoding"><code>cipher.update()</code></a> and <a href="#cipherfinaloutputencoding"><code>cipher.final()</code></a> methods:</p> 1061 1062<pre class="with-9-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">const</span> { 1063 scrypt, 1064 randomFill, 1065 createCipheriv, 1066} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 1067 1068<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>; 1069<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>; 1070 1071<span class="hljs-comment">// First, we'll generate the key. The key length is dependent on the algorithm.</span> 1072<span class="hljs-comment">// In this case for aes192, it is 24 bytes (192 bits).</span> 1073<span class="hljs-title function_">scrypt</span>(password, <span class="hljs-string">'salt'</span>, <span class="hljs-number">24</span>, <span class="hljs-function">(<span class="hljs-params">err, key</span>) =></span> { 1074 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 1075 <span class="hljs-comment">// Then, we'll generate a random initialization vector</span> 1076 <span class="hljs-title function_">randomFill</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint8Array</span>(<span class="hljs-number">16</span>), <span class="hljs-function">(<span class="hljs-params">err, iv</span>) =></span> { 1077 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 1078 1079 <span class="hljs-keyword">const</span> cipher = <span class="hljs-title function_">createCipheriv</span>(algorithm, key, iv); 1080 1081 <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>); 1082 encrypted += cipher.<span class="hljs-title function_">final</span>(<span class="hljs-string">'hex'</span>); 1083 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(encrypted); 1084 }); 1085});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 1086 scrypt, 1087 randomFill, 1088 createCipheriv, 1089} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 1090 1091<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>; 1092<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>; 1093 1094<span class="hljs-comment">// First, we'll generate the key. The key length is dependent on the algorithm.</span> 1095<span class="hljs-comment">// In this case for aes192, it is 24 bytes (192 bits).</span> 1096<span class="hljs-title function_">scrypt</span>(password, <span class="hljs-string">'salt'</span>, <span class="hljs-number">24</span>, <span class="hljs-function">(<span class="hljs-params">err, key</span>) =></span> { 1097 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 1098 <span class="hljs-comment">// Then, we'll generate a random initialization vector</span> 1099 <span class="hljs-title function_">randomFill</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint8Array</span>(<span class="hljs-number">16</span>), <span class="hljs-function">(<span class="hljs-params">err, iv</span>) =></span> { 1100 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 1101 1102 <span class="hljs-keyword">const</span> cipher = <span class="hljs-title function_">createCipheriv</span>(algorithm, key, iv); 1103 1104 <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>); 1105 encrypted += cipher.<span class="hljs-title function_">final</span>(<span class="hljs-string">'hex'</span>); 1106 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(encrypted); 1107 }); 1108});</code><button class="copy-button">copy</button></pre> 1109<h4><code>cipher.final([outputEncoding])</code><span><a class="mark" href="#cipherfinaloutputencoding" id="cipherfinaloutputencoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_cipher_final_outputencoding"></a></h4> 1110<div class="api_metadata"> 1111<span>Added in: v0.1.94</span> 1112</div> 1113<ul> 1114<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#buffers-and-character-encodings">encoding</a> of the return value.</li> 1115<li>Returns: <a href="buffer.html#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. 1116If <code>outputEncoding</code> is specified, a string is 1117returned. If an <code>outputEncoding</code> is not provided, a <a href="buffer.html"><code>Buffer</code></a> is returned.</li> 1118</ul> 1119<p>Once the <code>cipher.final()</code> method has been called, the <code>Cipher</code> object can no 1120longer be used to encrypt data. Attempts to call <code>cipher.final()</code> more than 1121once will result in an error being thrown.</p> 1122<h4><code>cipher.getAuthTag()</code><span><a class="mark" href="#ciphergetauthtag" id="ciphergetauthtag">#</a></span><a aria-hidden="true" class="legacy" id="crypto_cipher_getauthtag"></a></h4> 1123<div class="api_metadata"> 1124<span>Added in: v1.0.0</span> 1125</div> 1126<ul> 1127<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> When using an authenticated encryption mode (<code>GCM</code>, <code>CCM</code>, 1128<code>OCB</code>, and <code>chacha20-poly1305</code> are currently supported), the 1129<code>cipher.getAuthTag()</code> method returns a 1130<a href="buffer.html"><code>Buffer</code></a> containing the <em>authentication tag</em> that has been computed from 1131the given data.</li> 1132</ul> 1133<p>The <code>cipher.getAuthTag()</code> method should only be called after encryption has 1134been completed using the <a href="#cipherfinaloutputencoding"><code>cipher.final()</code></a> method.</p> 1135<p>If the <code>authTagLength</code> option was set during the <code>cipher</code> instance's creation, 1136this function will return exactly <code>authTagLength</code> bytes.</p> 1137<h4><code>cipher.setAAD(buffer[, options])</code><span><a class="mark" href="#ciphersetaadbuffer-options" id="ciphersetaadbuffer-options">#</a></span><a aria-hidden="true" class="legacy" id="crypto_cipher_setaad_buffer_options"></a></h4> 1138<div class="api_metadata"> 1139<span>Added in: v1.0.0</span> 1140</div> 1141<ul> 1142<li><code>buffer</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/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 1143<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#new-streamtransformoptions"><code>stream.transform</code> options</a> 1144<ul> 1145<li><code>plaintextLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1146<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The string encoding to use when <code>buffer</code> is a string.</li> 1147</ul> 1148</li> 1149<li>Returns: <a href="crypto.html#class-cipher" class="type"><Cipher></a> for method chaining.</li> 1150</ul> 1151<p>When using an authenticated encryption mode (<code>GCM</code>, <code>CCM</code>, <code>OCB</code>, and 1152<code>chacha20-poly1305</code> are 1153currently supported), the <code>cipher.setAAD()</code> method sets the value used for the 1154<em>additional authenticated data</em> (AAD) input parameter.</p> 1155<p>The <code>plaintextLength</code> option is optional for <code>GCM</code> and <code>OCB</code>. When using <code>CCM</code>, 1156the <code>plaintextLength</code> option must be specified and its value must match the 1157length of the plaintext in bytes. See <a href="#ccm-mode">CCM mode</a>.</p> 1158<p>The <code>cipher.setAAD()</code> method must be called before <a href="#cipherupdatedata-inputencoding-outputencoding"><code>cipher.update()</code></a>.</p> 1159<h4><code>cipher.setAutoPadding([autoPadding])</code><span><a class="mark" href="#ciphersetautopaddingautopadding" id="ciphersetautopaddingautopadding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_cipher_setautopadding_autopadding"></a></h4> 1160<div class="api_metadata"> 1161<span>Added in: v0.7.1</span> 1162</div> 1163<ul> 1164<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> 1165<li>Returns: <a href="crypto.html#class-cipher" class="type"><Cipher></a> for method chaining.</li> 1166</ul> 1167<p>When using block encryption algorithms, the <code>Cipher</code> class will automatically 1168add padding to the input data to the appropriate block size. To disable the 1169default padding call <code>cipher.setAutoPadding(false)</code>.</p> 1170<p>When <code>autoPadding</code> is <code>false</code>, the length of the entire input data must be a 1171multiple of the cipher's block size or <a href="#cipherfinaloutputencoding"><code>cipher.final()</code></a> will throw an error. 1172Disabling automatic padding is useful for non-standard padding, for instance 1173using <code>0x0</code> instead of PKCS padding.</p> 1174<p>The <code>cipher.setAutoPadding()</code> method must be called before 1175<a href="#cipherfinaloutputencoding"><code>cipher.final()</code></a>.</p> 1176<h4><code>cipher.update(data[, inputEncoding][, outputEncoding])</code><span><a class="mark" href="#cipherupdatedata-inputencoding-outputencoding" id="cipherupdatedata-inputencoding-outputencoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_cipher_update_data_inputencoding_outputencoding"></a></h4> 1177<div class="api_metadata"> 1178<details class="changelog"><summary>History</summary> 1179<table> 1180<tbody><tr><th>Version</th><th>Changes</th></tr> 1181<tr><td>v6.0.0</td> 1182<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p></td></tr> 1183<tr><td>v0.1.94</td> 1184<td><p><span>Added in: v0.1.94</span></p></td></tr> 1185</tbody></table> 1186</details> 1187</div> 1188<ul> 1189<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#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> 1190<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#buffers-and-character-encodings">encoding</a> of the data.</li> 1191<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#buffers-and-character-encodings">encoding</a> of the return value.</li> 1192<li>Returns: <a href="buffer.html#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> 1193</ul> 1194<p>Updates the cipher with <code>data</code>. If the <code>inputEncoding</code> argument is given, 1195the <code>data</code> 1196argument is a string using the specified encoding. If the <code>inputEncoding</code> 1197argument is not given, <code>data</code> must be a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or 1198<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 1199<code>inputEncoding</code> is ignored.</p> 1200<p>The <code>outputEncoding</code> specifies the output format of the enciphered 1201data. If the <code>outputEncoding</code> 1202is specified, a string using the specified encoding is returned. If no 1203<code>outputEncoding</code> is provided, a <a href="buffer.html"><code>Buffer</code></a> is returned.</p> 1204<p>The <code>cipher.update()</code> method can be called multiple times with new data until 1205<a href="#cipherfinaloutputencoding"><code>cipher.final()</code></a> is called. Calling <code>cipher.update()</code> after 1206<a href="#cipherfinaloutputencoding"><code>cipher.final()</code></a> will result in an error being thrown.</p> 1207</section><section><h3>Class: <code>Decipher</code><span><a class="mark" href="#class-decipher" id="class-decipher">#</a></span><a aria-hidden="true" class="legacy" id="crypto_class_decipher"></a></h3> 1208<div class="api_metadata"> 1209<span>Added in: v0.1.94</span> 1210</div> 1211<ul> 1212<li>Extends: <a href="stream.html#class-streamtransform" class="type"><stream.Transform></a></li> 1213</ul> 1214<p>Instances of the <code>Decipher</code> class are used to decrypt data. The class can be 1215used in one of two ways:</p> 1216<ul> 1217<li>As a <a href="stream.html">stream</a> that is both readable and writable, where plain encrypted 1218data is written to produce unencrypted data on the readable side, or</li> 1219<li>Using the <a href="#decipherupdatedata-inputencoding-outputencoding"><code>decipher.update()</code></a> and <a href="#decipherfinaloutputencoding"><code>decipher.final()</code></a> methods to 1220produce the unencrypted data.</li> 1221</ul> 1222<p>The <a href="#cryptocreatedecipheralgorithm-password-options"><code>crypto.createDecipher()</code></a> or <a href="#cryptocreatedecipherivalgorithm-key-iv-options"><code>crypto.createDecipheriv()</code></a> methods are 1223used to create <code>Decipher</code> instances. <code>Decipher</code> objects are not to be created 1224directly using the <code>new</code> keyword.</p> 1225<p>Example: Using <code>Decipher</code> objects as streams:</p> 1226 1227<pre class="with-37-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 1228<span class="hljs-keyword">const</span> { 1229 scryptSync, 1230 createDecipheriv, 1231} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 1232 1233<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>; 1234<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>; 1235<span class="hljs-comment">// Key length is dependent on the algorithm. In this case for aes192, it is</span> 1236<span class="hljs-comment">// 24 bytes (192 bits).</span> 1237<span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span> 1238<span class="hljs-keyword">const</span> key = <span class="hljs-title function_">scryptSync</span>(password, <span class="hljs-string">'salt'</span>, <span class="hljs-number">24</span>); 1239<span class="hljs-comment">// The IV is usually passed along with the ciphertext.</span> 1240<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> 1241 1242<span class="hljs-keyword">const</span> decipher = <span class="hljs-title function_">createDecipheriv</span>(algorithm, key, iv); 1243 1244<span class="hljs-keyword">let</span> decrypted = <span class="hljs-string">''</span>; 1245decipher.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> { 1246 <span class="hljs-keyword">let</span> chunk; 1247 <span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (chunk = decipher.<span class="hljs-title function_">read</span>())) { 1248 decrypted += chunk.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'utf8'</span>); 1249 } 1250}); 1251decipher.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> { 1252 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(decrypted); 1253 <span class="hljs-comment">// Prints: some clear text data</span> 1254}); 1255 1256<span class="hljs-comment">// Encrypted with same algorithm, key and iv.</span> 1257<span class="hljs-keyword">const</span> encrypted = 1258 <span class="hljs-string">'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa'</span>; 1259decipher.<span class="hljs-title function_">write</span>(encrypted, <span class="hljs-string">'hex'</span>); 1260decipher.<span class="hljs-title function_">end</span>();</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 1261 scryptSync, 1262 createDecipheriv, 1263} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 1264<span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>); 1265 1266<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>; 1267<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>; 1268<span class="hljs-comment">// Key length is dependent on the algorithm. In this case for aes192, it is</span> 1269<span class="hljs-comment">// 24 bytes (192 bits).</span> 1270<span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span> 1271<span class="hljs-keyword">const</span> key = <span class="hljs-title function_">scryptSync</span>(password, <span class="hljs-string">'salt'</span>, <span class="hljs-number">24</span>); 1272<span class="hljs-comment">// The IV is usually passed along with the ciphertext.</span> 1273<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> 1274 1275<span class="hljs-keyword">const</span> decipher = <span class="hljs-title function_">createDecipheriv</span>(algorithm, key, iv); 1276 1277<span class="hljs-keyword">let</span> decrypted = <span class="hljs-string">''</span>; 1278decipher.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> { 1279 <span class="hljs-keyword">let</span> chunk; 1280 <span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (chunk = decipher.<span class="hljs-title function_">read</span>())) { 1281 decrypted += chunk.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'utf8'</span>); 1282 } 1283}); 1284decipher.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> { 1285 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(decrypted); 1286 <span class="hljs-comment">// Prints: some clear text data</span> 1287}); 1288 1289<span class="hljs-comment">// Encrypted with same algorithm, key and iv.</span> 1290<span class="hljs-keyword">const</span> encrypted = 1291 <span class="hljs-string">'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa'</span>; 1292decipher.<span class="hljs-title function_">write</span>(encrypted, <span class="hljs-string">'hex'</span>); 1293decipher.<span class="hljs-title function_">end</span>();</code><button class="copy-button">copy</button></pre> 1294<p>Example: Using <code>Decipher</code> and piped streams:</p> 1295 1296<pre class="with-19-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { 1297 createReadStream, 1298 createWriteStream, 1299} <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 1300<span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 1301<span class="hljs-keyword">const</span> { 1302 scryptSync, 1303 createDecipheriv, 1304} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 1305 1306<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>; 1307<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>; 1308<span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span> 1309<span class="hljs-keyword">const</span> key = <span class="hljs-title function_">scryptSync</span>(password, <span class="hljs-string">'salt'</span>, <span class="hljs-number">24</span>); 1310<span class="hljs-comment">// The IV is usually passed along with the ciphertext.</span> 1311<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> 1312 1313<span class="hljs-keyword">const</span> decipher = <span class="hljs-title function_">createDecipheriv</span>(algorithm, key, iv); 1314 1315<span class="hljs-keyword">const</span> input = <span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'test.enc'</span>); 1316<span class="hljs-keyword">const</span> output = <span class="hljs-title function_">createWriteStream</span>(<span class="hljs-string">'test.js'</span>); 1317 1318input.<span class="hljs-title function_">pipe</span>(decipher).<span class="hljs-title function_">pipe</span>(output);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 1319 createReadStream, 1320 createWriteStream, 1321} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 1322<span class="hljs-keyword">const</span> { 1323 scryptSync, 1324 createDecipheriv, 1325} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 1326<span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>); 1327 1328<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>; 1329<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>; 1330<span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span> 1331<span class="hljs-keyword">const</span> key = <span class="hljs-title function_">scryptSync</span>(password, <span class="hljs-string">'salt'</span>, <span class="hljs-number">24</span>); 1332<span class="hljs-comment">// The IV is usually passed along with the ciphertext.</span> 1333<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> 1334 1335<span class="hljs-keyword">const</span> decipher = <span class="hljs-title function_">createDecipheriv</span>(algorithm, key, iv); 1336 1337<span class="hljs-keyword">const</span> input = <span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'test.enc'</span>); 1338<span class="hljs-keyword">const</span> output = <span class="hljs-title function_">createWriteStream</span>(<span class="hljs-string">'test.js'</span>); 1339 1340input.<span class="hljs-title function_">pipe</span>(decipher).<span class="hljs-title function_">pipe</span>(output);</code><button class="copy-button">copy</button></pre> 1341<p>Example: Using the <a href="#decipherupdatedata-inputencoding-outputencoding"><code>decipher.update()</code></a> and <a href="#decipherfinaloutputencoding"><code>decipher.final()</code></a> methods:</p> 1342 1343<pre class="with-37-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 1344<span class="hljs-keyword">const</span> { 1345 scryptSync, 1346 createDecipheriv, 1347} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 1348 1349<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>; 1350<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>; 1351<span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span> 1352<span class="hljs-keyword">const</span> key = <span class="hljs-title function_">scryptSync</span>(password, <span class="hljs-string">'salt'</span>, <span class="hljs-number">24</span>); 1353<span class="hljs-comment">// The IV is usually passed along with the ciphertext.</span> 1354<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> 1355 1356<span class="hljs-keyword">const</span> decipher = <span class="hljs-title function_">createDecipheriv</span>(algorithm, key, iv); 1357 1358<span class="hljs-comment">// Encrypted using same algorithm, key and iv.</span> 1359<span class="hljs-keyword">const</span> encrypted = 1360 <span class="hljs-string">'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa'</span>; 1361<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>); 1362decrypted += decipher.<span class="hljs-title function_">final</span>(<span class="hljs-string">'utf8'</span>); 1363<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(decrypted); 1364<span class="hljs-comment">// Prints: some clear text data</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 1365 scryptSync, 1366 createDecipheriv, 1367} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 1368<span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>); 1369 1370<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>; 1371<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>; 1372<span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span> 1373<span class="hljs-keyword">const</span> key = <span class="hljs-title function_">scryptSync</span>(password, <span class="hljs-string">'salt'</span>, <span class="hljs-number">24</span>); 1374<span class="hljs-comment">// The IV is usually passed along with the ciphertext.</span> 1375<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> 1376 1377<span class="hljs-keyword">const</span> decipher = <span class="hljs-title function_">createDecipheriv</span>(algorithm, key, iv); 1378 1379<span class="hljs-comment">// Encrypted using same algorithm, key and iv.</span> 1380<span class="hljs-keyword">const</span> encrypted = 1381 <span class="hljs-string">'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa'</span>; 1382<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>); 1383decrypted += decipher.<span class="hljs-title function_">final</span>(<span class="hljs-string">'utf8'</span>); 1384<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(decrypted); 1385<span class="hljs-comment">// Prints: some clear text data</span></code><button class="copy-button">copy</button></pre> 1386<h4><code>decipher.final([outputEncoding])</code><span><a class="mark" href="#decipherfinaloutputencoding" id="decipherfinaloutputencoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_decipher_final_outputencoding"></a></h4> 1387<div class="api_metadata"> 1388<span>Added in: v0.1.94</span> 1389</div> 1390<ul> 1391<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#buffers-and-character-encodings">encoding</a> of the return value.</li> 1392<li>Returns: <a href="buffer.html#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. 1393If <code>outputEncoding</code> is specified, a string is 1394returned. If an <code>outputEncoding</code> is not provided, a <a href="buffer.html"><code>Buffer</code></a> is returned.</li> 1395</ul> 1396<p>Once the <code>decipher.final()</code> method has been called, the <code>Decipher</code> object can 1397no longer be used to decrypt data. Attempts to call <code>decipher.final()</code> more 1398than once will result in an error being thrown.</p> 1399<h4><code>decipher.setAAD(buffer[, options])</code><span><a class="mark" href="#deciphersetaadbuffer-options" id="deciphersetaadbuffer-options">#</a></span><a aria-hidden="true" class="legacy" id="crypto_decipher_setaad_buffer_options"></a></h4> 1400<div class="api_metadata"> 1401<details class="changelog"><summary>History</summary> 1402<table> 1403<tbody><tr><th>Version</th><th>Changes</th></tr> 1404<tr><td>v15.0.0</td> 1405<td><p>The buffer argument can be a string or ArrayBuffer and is limited to no more than 2 ** 31 - 1 bytes.</p></td></tr> 1406<tr><td>v7.2.0</td> 1407<td><p>This method now returns a reference to <code>decipher</code>.</p></td></tr> 1408<tr><td>v1.0.0</td> 1409<td><p><span>Added in: v1.0.0</span></p></td></tr> 1410</tbody></table> 1411</details> 1412</div> 1413<ul> 1414<li><code>buffer</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/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 1415<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#new-streamtransformoptions"><code>stream.transform</code> options</a> 1416<ul> 1417<li><code>plaintextLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1418<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> String encoding to use when <code>buffer</code> is a string.</li> 1419</ul> 1420</li> 1421<li>Returns: <a href="crypto.html#class-decipher" class="type"><Decipher></a> for method chaining.</li> 1422</ul> 1423<p>When using an authenticated encryption mode (<code>GCM</code>, <code>CCM</code>, <code>OCB</code>, and 1424<code>chacha20-poly1305</code> are 1425currently supported), the <code>decipher.setAAD()</code> method sets the value used for the 1426<em>additional authenticated data</em> (AAD) input parameter.</p> 1427<p>The <code>options</code> argument is optional for <code>GCM</code>. When using <code>CCM</code>, the 1428<code>plaintextLength</code> option must be specified and its value must match the length 1429of the ciphertext in bytes. See <a href="#ccm-mode">CCM mode</a>.</p> 1430<p>The <code>decipher.setAAD()</code> method must be called before <a href="#decipherupdatedata-inputencoding-outputencoding"><code>decipher.update()</code></a>.</p> 1431<p>When passing a string as the <code>buffer</code>, please consider 1432<a href="#using-strings-as-inputs-to-cryptographic-apis">caveats when using strings as inputs to cryptographic APIs</a>.</p> 1433<h4><code>decipher.setAuthTag(buffer[, encoding])</code><span><a class="mark" href="#deciphersetauthtagbuffer-encoding" id="deciphersetauthtagbuffer-encoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_decipher_setauthtag_buffer_encoding"></a></h4> 1434<div class="api_metadata"> 1435<details class="changelog"><summary>History</summary> 1436<table> 1437<tbody><tr><th>Version</th><th>Changes</th></tr> 1438<tr><td>v15.0.0</td> 1439<td><p>The buffer argument can be a string or ArrayBuffer and is limited to no more than 2 ** 31 - 1 bytes.</p></td></tr> 1440<tr><td>v11.0.0</td> 1441<td><p>This method now throws if the GCM tag length is invalid.</p></td></tr> 1442<tr><td>v7.2.0</td> 1443<td><p>This method now returns a reference to <code>decipher</code>.</p></td></tr> 1444<tr><td>v1.0.0</td> 1445<td><p><span>Added in: v1.0.0</span></p></td></tr> 1446</tbody></table> 1447</details> 1448</div> 1449<ul> 1450<li><code>buffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></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> 1451<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> String encoding to use when <code>buffer</code> is a string.</li> 1452<li>Returns: <a href="crypto.html#class-decipher" class="type"><Decipher></a> for method chaining.</li> 1453</ul> 1454<p>When using an authenticated encryption mode (<code>GCM</code>, <code>CCM</code>, <code>OCB</code>, and 1455<code>chacha20-poly1305</code> are 1456currently supported), the <code>decipher.setAuthTag()</code> method is used to pass in the 1457received <em>authentication tag</em>. If no tag is provided, or if the cipher text 1458has been tampered with, <a href="#decipherfinaloutputencoding"><code>decipher.final()</code></a> will throw, indicating that the 1459cipher text should be discarded due to failed authentication. If the tag length 1460is 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 1461<code>authTagLength</code> option, <code>decipher.setAuthTag()</code> will throw an error.</p> 1462<p>The <code>decipher.setAuthTag()</code> method must be called before <a href="#decipherupdatedata-inputencoding-outputencoding"><code>decipher.update()</code></a> 1463for <code>CCM</code> mode or before <a href="#decipherfinaloutputencoding"><code>decipher.final()</code></a> for <code>GCM</code> and <code>OCB</code> modes and 1464<code>chacha20-poly1305</code>. 1465<code>decipher.setAuthTag()</code> can only be called once.</p> 1466<p>When passing a string as the authentication tag, please consider 1467<a href="#using-strings-as-inputs-to-cryptographic-apis">caveats when using strings as inputs to cryptographic APIs</a>.</p> 1468<h4><code>decipher.setAutoPadding([autoPadding])</code><span><a class="mark" href="#deciphersetautopaddingautopadding" id="deciphersetautopaddingautopadding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_decipher_setautopadding_autopadding"></a></h4> 1469<div class="api_metadata"> 1470<span>Added in: v0.7.1</span> 1471</div> 1472<ul> 1473<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> 1474<li>Returns: <a href="crypto.html#class-decipher" class="type"><Decipher></a> for method chaining.</li> 1475</ul> 1476<p>When data has been encrypted without standard block padding, calling 1477<code>decipher.setAutoPadding(false)</code> will disable automatic padding to prevent 1478<a href="#decipherfinaloutputencoding"><code>decipher.final()</code></a> from checking for and removing padding.</p> 1479<p>Turning auto padding off will only work if the input data's length is a 1480multiple of the ciphers block size.</p> 1481<p>The <code>decipher.setAutoPadding()</code> method must be called before 1482<a href="#decipherfinaloutputencoding"><code>decipher.final()</code></a>.</p> 1483<h4><code>decipher.update(data[, inputEncoding][, outputEncoding])</code><span><a class="mark" href="#decipherupdatedata-inputencoding-outputencoding" id="decipherupdatedata-inputencoding-outputencoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_decipher_update_data_inputencoding_outputencoding"></a></h4> 1484<div class="api_metadata"> 1485<details class="changelog"><summary>History</summary> 1486<table> 1487<tbody><tr><th>Version</th><th>Changes</th></tr> 1488<tr><td>v6.0.0</td> 1489<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p></td></tr> 1490<tr><td>v0.1.94</td> 1491<td><p><span>Added in: v0.1.94</span></p></td></tr> 1492</tbody></table> 1493</details> 1494</div> 1495<ul> 1496<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#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> 1497<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#buffers-and-character-encodings">encoding</a> of the <code>data</code> string.</li> 1498<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#buffers-and-character-encodings">encoding</a> of the return value.</li> 1499<li>Returns: <a href="buffer.html#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> 1500</ul> 1501<p>Updates the decipher with <code>data</code>. If the <code>inputEncoding</code> argument is given, 1502the <code>data</code> 1503argument is a string using the specified encoding. If the <code>inputEncoding</code> 1504argument is not given, <code>data</code> must be a <a href="buffer.html"><code>Buffer</code></a>. If <code>data</code> is a 1505<a href="buffer.html"><code>Buffer</code></a> then <code>inputEncoding</code> is ignored.</p> 1506<p>The <code>outputEncoding</code> specifies the output format of the enciphered 1507data. If the <code>outputEncoding</code> 1508is specified, a string using the specified encoding is returned. If no 1509<code>outputEncoding</code> is provided, a <a href="buffer.html"><code>Buffer</code></a> is returned.</p> 1510<p>The <code>decipher.update()</code> method can be called multiple times with new data until 1511<a href="#decipherfinaloutputencoding"><code>decipher.final()</code></a> is called. Calling <code>decipher.update()</code> after 1512<a href="#decipherfinaloutputencoding"><code>decipher.final()</code></a> will result in an error being thrown.</p> 1513</section><section><h3>Class: <code>DiffieHellman</code><span><a class="mark" href="#class-diffiehellman" id="class-diffiehellman">#</a></span><a aria-hidden="true" class="legacy" id="crypto_class_diffiehellman"></a></h3> 1514<div class="api_metadata"> 1515<span>Added in: v0.5.0</span> 1516</div> 1517<p>The <code>DiffieHellman</code> class is a utility for creating Diffie-Hellman key 1518exchanges.</p> 1519<p>Instances of the <code>DiffieHellman</code> class can be created using the 1520<a href="#cryptocreatediffiehellmanprime-primeencoding-generator-generatorencoding"><code>crypto.createDiffieHellman()</code></a> function.</p> 1521 1522<pre class="with-38-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>; 1523 1524<span class="hljs-keyword">const</span> { 1525 createDiffieHellman, 1526} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 1527 1528<span class="hljs-comment">// Generate Alice's keys...</span> 1529<span class="hljs-keyword">const</span> alice = <span class="hljs-title function_">createDiffieHellman</span>(<span class="hljs-number">2048</span>); 1530<span class="hljs-keyword">const</span> aliceKey = alice.<span class="hljs-title function_">generateKeys</span>(); 1531 1532<span class="hljs-comment">// Generate Bob's keys...</span> 1533<span class="hljs-keyword">const</span> bob = <span class="hljs-title function_">createDiffieHellman</span>(alice.<span class="hljs-title function_">getPrime</span>(), alice.<span class="hljs-title function_">getGenerator</span>()); 1534<span class="hljs-keyword">const</span> bobKey = bob.<span class="hljs-title function_">generateKeys</span>(); 1535 1536<span class="hljs-comment">// Exchange and generate the secret...</span> 1537<span class="hljs-keyword">const</span> aliceSecret = alice.<span class="hljs-title function_">computeSecret</span>(bobKey); 1538<span class="hljs-keyword">const</span> bobSecret = bob.<span class="hljs-title function_">computeSecret</span>(aliceKey); 1539 1540<span class="hljs-comment">// OK</span> 1541assert.<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><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>); 1542 1543<span class="hljs-keyword">const</span> { 1544 createDiffieHellman, 1545} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 1546 1547<span class="hljs-comment">// Generate Alice's keys...</span> 1548<span class="hljs-keyword">const</span> alice = <span class="hljs-title function_">createDiffieHellman</span>(<span class="hljs-number">2048</span>); 1549<span class="hljs-keyword">const</span> aliceKey = alice.<span class="hljs-title function_">generateKeys</span>(); 1550 1551<span class="hljs-comment">// Generate Bob's keys...</span> 1552<span class="hljs-keyword">const</span> bob = <span class="hljs-title function_">createDiffieHellman</span>(alice.<span class="hljs-title function_">getPrime</span>(), alice.<span class="hljs-title function_">getGenerator</span>()); 1553<span class="hljs-keyword">const</span> bobKey = bob.<span class="hljs-title function_">generateKeys</span>(); 1554 1555<span class="hljs-comment">// Exchange and generate the secret...</span> 1556<span class="hljs-keyword">const</span> aliceSecret = alice.<span class="hljs-title function_">computeSecret</span>(bobKey); 1557<span class="hljs-keyword">const</span> bobSecret = bob.<span class="hljs-title function_">computeSecret</span>(aliceKey); 1558 1559<span class="hljs-comment">// OK</span> 1560assert.<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><button class="copy-button">copy</button></pre> 1561<h4><code>diffieHellman.computeSecret(otherPublicKey[, inputEncoding][, outputEncoding])</code><span><a class="mark" href="#diffiehellmancomputesecretotherpublickey-inputencoding-outputencoding" id="diffiehellmancomputesecretotherpublickey-inputencoding-outputencoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_diffiehellman_computesecret_otherpublickey_inputencoding_outputencoding"></a></h4> 1562<div class="api_metadata"> 1563<span>Added in: v0.5.0</span> 1564</div> 1565<ul> 1566<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 1567<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#buffers-and-character-encodings">encoding</a> of an <code>otherPublicKey</code> string.</li> 1568<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#buffers-and-character-encodings">encoding</a> of the return value.</li> 1569<li>Returns: <a href="buffer.html#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> 1570</ul> 1571<p>Computes the shared secret using <code>otherPublicKey</code> as the other 1572party's public key and returns the computed shared secret. The supplied 1573key is interpreted using the specified <code>inputEncoding</code>, and secret is 1574encoded using specified <code>outputEncoding</code>. 1575If the <code>inputEncoding</code> is not 1576provided, <code>otherPublicKey</code> is expected to be a <a href="buffer.html"><code>Buffer</code></a>, 1577<code>TypedArray</code>, or <code>DataView</code>.</p> 1578<p>If <code>outputEncoding</code> is given a string is returned; otherwise, a 1579<a href="buffer.html"><code>Buffer</code></a> is returned.</p> 1580<h4><code>diffieHellman.generateKeys([encoding])</code><span><a class="mark" href="#diffiehellmangeneratekeysencoding" id="diffiehellmangeneratekeysencoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_diffiehellman_generatekeys_encoding"></a></h4> 1581<div class="api_metadata"> 1582<span>Added in: v0.5.0</span> 1583</div> 1584<ul> 1585<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#buffers-and-character-encodings">encoding</a> of the return value.</li> 1586<li>Returns: <a href="buffer.html#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> 1587</ul> 1588<p>Generates private and public Diffie-Hellman key values unless they have been 1589generated or computed already, and returns 1590the public key in the specified <code>encoding</code>. This key should be 1591transferred to the other party. 1592If <code>encoding</code> is provided a string is returned; otherwise a 1593<a href="buffer.html"><code>Buffer</code></a> is returned.</p> 1594<p>This function is a thin wrapper around <a href="https://www.openssl.org/docs/man3.0/man3/DH_generate_key.html"><code>DH_generate_key()</code></a>. In particular, 1595once a private key has been generated or set, calling this function only updates 1596the public key but does not generate a new private key.</p> 1597<h4><code>diffieHellman.getGenerator([encoding])</code><span><a class="mark" href="#diffiehellmangetgeneratorencoding" id="diffiehellmangetgeneratorencoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_diffiehellman_getgenerator_encoding"></a></h4> 1598<div class="api_metadata"> 1599<span>Added in: v0.5.0</span> 1600</div> 1601<ul> 1602<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#buffers-and-character-encodings">encoding</a> of the return value.</li> 1603<li>Returns: <a href="buffer.html#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> 1604</ul> 1605<p>Returns the Diffie-Hellman generator in the specified <code>encoding</code>. 1606If <code>encoding</code> is provided a string is 1607returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is returned.</p> 1608<h4><code>diffieHellman.getPrime([encoding])</code><span><a class="mark" href="#diffiehellmangetprimeencoding" id="diffiehellmangetprimeencoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_diffiehellman_getprime_encoding"></a></h4> 1609<div class="api_metadata"> 1610<span>Added in: v0.5.0</span> 1611</div> 1612<ul> 1613<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#buffers-and-character-encodings">encoding</a> of the return value.</li> 1614<li>Returns: <a href="buffer.html#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> 1615</ul> 1616<p>Returns the Diffie-Hellman prime in the specified <code>encoding</code>. 1617If <code>encoding</code> is provided a string is 1618returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is returned.</p> 1619<h4><code>diffieHellman.getPrivateKey([encoding])</code><span><a class="mark" href="#diffiehellmangetprivatekeyencoding" id="diffiehellmangetprivatekeyencoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_diffiehellman_getprivatekey_encoding"></a></h4> 1620<div class="api_metadata"> 1621<span>Added in: v0.5.0</span> 1622</div> 1623<ul> 1624<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#buffers-and-character-encodings">encoding</a> of the return value.</li> 1625<li>Returns: <a href="buffer.html#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> 1626</ul> 1627<p>Returns the Diffie-Hellman private key in the specified <code>encoding</code>. 1628If <code>encoding</code> is provided a 1629string is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is returned.</p> 1630<h4><code>diffieHellman.getPublicKey([encoding])</code><span><a class="mark" href="#diffiehellmangetpublickeyencoding" id="diffiehellmangetpublickeyencoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_diffiehellman_getpublickey_encoding"></a></h4> 1631<div class="api_metadata"> 1632<span>Added in: v0.5.0</span> 1633</div> 1634<ul> 1635<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#buffers-and-character-encodings">encoding</a> of the return value.</li> 1636<li>Returns: <a href="buffer.html#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> 1637</ul> 1638<p>Returns the Diffie-Hellman public key in the specified <code>encoding</code>. 1639If <code>encoding</code> is provided a 1640string is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is returned.</p> 1641<h4><code>diffieHellman.setPrivateKey(privateKey[, encoding])</code><span><a class="mark" href="#diffiehellmansetprivatekeyprivatekey-encoding" id="diffiehellmansetprivatekeyprivatekey-encoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_diffiehellman_setprivatekey_privatekey_encoding"></a></h4> 1642<div class="api_metadata"> 1643<span>Added in: v0.5.0</span> 1644</div> 1645<ul> 1646<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 1647<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#buffers-and-character-encodings">encoding</a> of the <code>privateKey</code> string.</li> 1648</ul> 1649<p>Sets the Diffie-Hellman private key. If the <code>encoding</code> argument is provided, 1650<code>privateKey</code> is expected 1651to be a string. If no <code>encoding</code> is provided, <code>privateKey</code> is expected 1652to be a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or <code>DataView</code>.</p> 1653<p>This function does not automatically compute the associated public key. Either 1654<a href="#diffiehellmansetpublickeypublickey-encoding"><code>diffieHellman.setPublicKey()</code></a> or <a href="#diffiehellmangeneratekeysencoding"><code>diffieHellman.generateKeys()</code></a> can be 1655used to manually provide the public key or to automatically derive it.</p> 1656<h4><code>diffieHellman.setPublicKey(publicKey[, encoding])</code><span><a class="mark" href="#diffiehellmansetpublickeypublickey-encoding" id="diffiehellmansetpublickeypublickey-encoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_diffiehellman_setpublickey_publickey_encoding"></a></h4> 1657<div class="api_metadata"> 1658<span>Added in: v0.5.0</span> 1659</div> 1660<ul> 1661<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 1662<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#buffers-and-character-encodings">encoding</a> of the <code>publicKey</code> string.</li> 1663</ul> 1664<p>Sets the Diffie-Hellman public key. If the <code>encoding</code> argument is provided, 1665<code>publicKey</code> is expected 1666to be a string. If no <code>encoding</code> is provided, <code>publicKey</code> is expected 1667to be a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or <code>DataView</code>.</p> 1668<h4><code>diffieHellman.verifyError</code><span><a class="mark" href="#diffiehellmanverifyerror" id="diffiehellmanverifyerror">#</a></span><a aria-hidden="true" class="legacy" id="crypto_diffiehellman_verifyerror"></a></h4> 1669<div class="api_metadata"> 1670<span>Added in: v0.11.12</span> 1671</div> 1672<p>A bit field containing any warnings and/or errors resulting from a check 1673performed during initialization of the <code>DiffieHellman</code> object.</p> 1674<p>The following values are valid for this property (as defined in <code>node:constants</code> module):</p> 1675<ul> 1676<li><code>DH_CHECK_P_NOT_SAFE_PRIME</code></li> 1677<li><code>DH_CHECK_P_NOT_PRIME</code></li> 1678<li><code>DH_UNABLE_TO_CHECK_GENERATOR</code></li> 1679<li><code>DH_NOT_SUITABLE_GENERATOR</code></li> 1680</ul> 1681</section><section><h3>Class: <code>DiffieHellmanGroup</code><span><a class="mark" href="#class-diffiehellmangroup" id="class-diffiehellmangroup">#</a></span><a aria-hidden="true" class="legacy" id="crypto_class_diffiehellmangroup"></a></h3> 1682<div class="api_metadata"> 1683<span>Added in: v0.7.5</span> 1684</div> 1685<p>The <code>DiffieHellmanGroup</code> class takes a well-known modp group as its argument. 1686It works the same as <code>DiffieHellman</code>, except that it does not allow changing 1687its keys after creation. In other words, it does not implement <code>setPublicKey()</code> 1688or <code>setPrivateKey()</code> methods.</p> 1689 1690<pre class="with-65-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">const</span> { createDiffieHellmanGroup } = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 1691<span class="hljs-keyword">const</span> dh = <span class="hljs-title function_">createDiffieHellmanGroup</span>(<span class="hljs-string">'modp16'</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { createDiffieHellmanGroup } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 1692<span class="hljs-keyword">const</span> dh = <span class="hljs-title function_">createDiffieHellmanGroup</span>(<span class="hljs-string">'modp16'</span>);</code><button class="copy-button">copy</button></pre> 1693<p>The following groups are supported:</p> 1694<ul> 1695<li><code>'modp14'</code> (2048 bits, <a href="https://www.rfc-editor.org/rfc/rfc3526.txt">RFC 3526</a> Section 3)</li> 1696<li><code>'modp15'</code> (3072 bits, <a href="https://www.rfc-editor.org/rfc/rfc3526.txt">RFC 3526</a> Section 4)</li> 1697<li><code>'modp16'</code> (4096 bits, <a href="https://www.rfc-editor.org/rfc/rfc3526.txt">RFC 3526</a> Section 5)</li> 1698<li><code>'modp17'</code> (6144 bits, <a href="https://www.rfc-editor.org/rfc/rfc3526.txt">RFC 3526</a> Section 6)</li> 1699<li><code>'modp18'</code> (8192 bits, <a href="https://www.rfc-editor.org/rfc/rfc3526.txt">RFC 3526</a> Section 7)</li> 1700</ul> 1701<p>The following groups are still supported but deprecated (see <a href="#support-for-weak-or-compromised-algorithms">Caveats</a>):</p> 1702<ul> 1703<li><code>'modp1'</code> (768 bits, <a href="https://www.rfc-editor.org/rfc/rfc2409.txt">RFC 2409</a> Section 6.1) <span class="deprecated-inline"></span></li> 1704<li><code>'modp2'</code> (1024 bits, <a href="https://www.rfc-editor.org/rfc/rfc2409.txt">RFC 2409</a> Section 6.2) <span class="deprecated-inline"></span></li> 1705<li><code>'modp5'</code> (1536 bits, <a href="https://www.rfc-editor.org/rfc/rfc3526.txt">RFC 3526</a> Section 2) <span class="deprecated-inline"></span></li> 1706</ul> 1707<p>These deprecated groups might be removed in future versions of Node.js.</p> 1708</section><section><h3>Class: <code>ECDH</code><span><a class="mark" href="#class-ecdh" id="class-ecdh">#</a></span><a aria-hidden="true" class="legacy" id="crypto_class_ecdh"></a></h3> 1709<div class="api_metadata"> 1710<span>Added in: v0.11.14</span> 1711</div> 1712<p>The <code>ECDH</code> class is a utility for creating Elliptic Curve Diffie-Hellman (ECDH) 1713key exchanges.</p> 1714<p>Instances of the <code>ECDH</code> class can be created using the 1715<a href="#cryptocreateecdhcurvename"><code>crypto.createECDH()</code></a> function.</p> 1716 1717<pre class="with-38-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> assert <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>; 1718 1719<span class="hljs-keyword">const</span> { 1720 createECDH, 1721} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 1722 1723<span class="hljs-comment">// Generate Alice's keys...</span> 1724<span class="hljs-keyword">const</span> alice = <span class="hljs-title function_">createECDH</span>(<span class="hljs-string">'secp521r1'</span>); 1725<span class="hljs-keyword">const</span> aliceKey = alice.<span class="hljs-title function_">generateKeys</span>(); 1726 1727<span class="hljs-comment">// Generate Bob's keys...</span> 1728<span class="hljs-keyword">const</span> bob = <span class="hljs-title function_">createECDH</span>(<span class="hljs-string">'secp521r1'</span>); 1729<span class="hljs-keyword">const</span> bobKey = bob.<span class="hljs-title function_">generateKeys</span>(); 1730 1731<span class="hljs-comment">// Exchange and generate the secret...</span> 1732<span class="hljs-keyword">const</span> aliceSecret = alice.<span class="hljs-title function_">computeSecret</span>(bobKey); 1733<span class="hljs-keyword">const</span> bobSecret = bob.<span class="hljs-title function_">computeSecret</span>(aliceKey); 1734 1735assert.<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>)); 1736<span class="hljs-comment">// OK</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>); 1737 1738<span class="hljs-keyword">const</span> { 1739 createECDH, 1740} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 1741 1742<span class="hljs-comment">// Generate Alice's keys...</span> 1743<span class="hljs-keyword">const</span> alice = <span class="hljs-title function_">createECDH</span>(<span class="hljs-string">'secp521r1'</span>); 1744<span class="hljs-keyword">const</span> aliceKey = alice.<span class="hljs-title function_">generateKeys</span>(); 1745 1746<span class="hljs-comment">// Generate Bob's keys...</span> 1747<span class="hljs-keyword">const</span> bob = <span class="hljs-title function_">createECDH</span>(<span class="hljs-string">'secp521r1'</span>); 1748<span class="hljs-keyword">const</span> bobKey = bob.<span class="hljs-title function_">generateKeys</span>(); 1749 1750<span class="hljs-comment">// Exchange and generate the secret...</span> 1751<span class="hljs-keyword">const</span> aliceSecret = alice.<span class="hljs-title function_">computeSecret</span>(bobKey); 1752<span class="hljs-keyword">const</span> bobSecret = bob.<span class="hljs-title function_">computeSecret</span>(aliceKey); 1753 1754assert.<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>)); 1755<span class="hljs-comment">// OK</span></code><button class="copy-button">copy</button></pre> 1756<h4>Static method: <code>ECDH.convertKey(key, curve[, inputEncoding[, outputEncoding[, format]]])</code><span><a class="mark" href="#static-method-ecdhconvertkeykey-curve-inputencoding-outputencoding-format" id="static-method-ecdhconvertkeykey-curve-inputencoding-outputencoding-format">#</a></span><a aria-hidden="true" class="legacy" id="crypto_static_method_ecdh_convertkey_key_curve_inputencoding_outputencoding_format"></a></h4> 1757<div class="api_metadata"> 1758<span>Added in: v10.0.0</span> 1759</div> 1760<ul> 1761<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 1762<li><code>curve</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1763<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#buffers-and-character-encodings">encoding</a> of the <code>key</code> string.</li> 1764<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#buffers-and-character-encodings">encoding</a> of the return value.</li> 1765<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> 1766<li>Returns: <a href="buffer.html#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> 1767</ul> 1768<p>Converts the EC Diffie-Hellman public key specified by <code>key</code> and <code>curve</code> to the 1769format specified by <code>format</code>. The <code>format</code> argument specifies point encoding 1770and can be <code>'compressed'</code>, <code>'uncompressed'</code> or <code>'hybrid'</code>. The supplied key is 1771interpreted using the specified <code>inputEncoding</code>, and the returned key is encoded 1772using the specified <code>outputEncoding</code>.</p> 1773<p>Use <a href="#cryptogetcurves"><code>crypto.getCurves()</code></a> to obtain a list of available curve names. 1774On recent OpenSSL releases, <code>openssl ecparam -list_curves</code> will also display 1775the name and description of each available elliptic curve.</p> 1776<p>If <code>format</code> is not specified the point will be returned in <code>'uncompressed'</code> 1777format.</p> 1778<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>, 1779<code>TypedArray</code>, or <code>DataView</code>.</p> 1780<p>Example (uncompressing a key):</p> 1781 1782<pre class="with-13-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">const</span> { 1783 createECDH, 1784 <span class="hljs-variable constant_">ECDH</span>, 1785} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 1786 1787<span class="hljs-keyword">const</span> ecdh = <span class="hljs-title function_">createECDH</span>(<span class="hljs-string">'secp256k1'</span>); 1788ecdh.<span class="hljs-title function_">generateKeys</span>(); 1789 1790<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>); 1791 1792<span class="hljs-keyword">const</span> uncompressedKey = <span class="hljs-variable constant_">ECDH</span>.<span class="hljs-title function_">convertKey</span>(compressedKey, 1793 <span class="hljs-string">'secp256k1'</span>, 1794 <span class="hljs-string">'hex'</span>, 1795 <span class="hljs-string">'hex'</span>, 1796 <span class="hljs-string">'uncompressed'</span>); 1797 1798<span class="hljs-comment">// The converted key and the uncompressed public key should be the same</span> 1799<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><code class="language-js cjs"><span class="hljs-keyword">const</span> { 1800 createECDH, 1801 <span class="hljs-variable constant_">ECDH</span>, 1802} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 1803 1804<span class="hljs-keyword">const</span> ecdh = <span class="hljs-title function_">createECDH</span>(<span class="hljs-string">'secp256k1'</span>); 1805ecdh.<span class="hljs-title function_">generateKeys</span>(); 1806 1807<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>); 1808 1809<span class="hljs-keyword">const</span> uncompressedKey = <span class="hljs-variable constant_">ECDH</span>.<span class="hljs-title function_">convertKey</span>(compressedKey, 1810 <span class="hljs-string">'secp256k1'</span>, 1811 <span class="hljs-string">'hex'</span>, 1812 <span class="hljs-string">'hex'</span>, 1813 <span class="hljs-string">'uncompressed'</span>); 1814 1815<span class="hljs-comment">// The converted key and the uncompressed public key should be the same</span> 1816<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><button class="copy-button">copy</button></pre> 1817<h4><code>ecdh.computeSecret(otherPublicKey[, inputEncoding][, outputEncoding])</code><span><a class="mark" href="#ecdhcomputesecretotherpublickey-inputencoding-outputencoding" id="ecdhcomputesecretotherpublickey-inputencoding-outputencoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_ecdh_computesecret_otherpublickey_inputencoding_outputencoding"></a></h4> 1818<div class="api_metadata"> 1819<details class="changelog"><summary>History</summary> 1820<table> 1821<tbody><tr><th>Version</th><th>Changes</th></tr> 1822<tr><td>v10.0.0</td> 1823<td><p>Changed error format to better support invalid public key error.</p></td></tr> 1824<tr><td>v6.0.0</td> 1825<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p></td></tr> 1826<tr><td>v0.11.14</td> 1827<td><p><span>Added in: v0.11.14</span></p></td></tr> 1828</tbody></table> 1829</details> 1830</div> 1831<ul> 1832<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 1833<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#buffers-and-character-encodings">encoding</a> of the <code>otherPublicKey</code> string.</li> 1834<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#buffers-and-character-encodings">encoding</a> of the return value.</li> 1835<li>Returns: <a href="buffer.html#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> 1836</ul> 1837<p>Computes the shared secret using <code>otherPublicKey</code> as the other 1838party's public key and returns the computed shared secret. The supplied 1839key is interpreted using specified <code>inputEncoding</code>, and the returned secret 1840is encoded using the specified <code>outputEncoding</code>. 1841If the <code>inputEncoding</code> is not 1842provided, <code>otherPublicKey</code> is expected to be a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or 1843<code>DataView</code>.</p> 1844<p>If <code>outputEncoding</code> is given a string will be returned; otherwise a 1845<a href="buffer.html"><code>Buffer</code></a> is returned.</p> 1846<p><code>ecdh.computeSecret</code> will throw an 1847<code>ERR_CRYPTO_ECDH_INVALID_PUBLIC_KEY</code> error when <code>otherPublicKey</code> 1848lies outside of the elliptic curve. Since <code>otherPublicKey</code> is 1849usually supplied from a remote user over an insecure network, 1850be sure to handle this exception accordingly.</p> 1851<h4><code>ecdh.generateKeys([encoding[, format]])</code><span><a class="mark" href="#ecdhgeneratekeysencoding-format" id="ecdhgeneratekeysencoding-format">#</a></span><a aria-hidden="true" class="legacy" id="crypto_ecdh_generatekeys_encoding_format"></a></h4> 1852<div class="api_metadata"> 1853<span>Added in: v0.11.14</span> 1854</div> 1855<ul> 1856<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#buffers-and-character-encodings">encoding</a> of the return value.</li> 1857<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> 1858<li>Returns: <a href="buffer.html#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> 1859</ul> 1860<p>Generates private and public EC Diffie-Hellman key values, and returns 1861the public key in the specified <code>format</code> and <code>encoding</code>. This key should be 1862transferred to the other party.</p> 1863<p>The <code>format</code> argument specifies point encoding and can be <code>'compressed'</code> or 1864<code>'uncompressed'</code>. If <code>format</code> is not specified, the point will be returned in 1865<code>'uncompressed'</code> format.</p> 1866<p>If <code>encoding</code> is provided a string is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> 1867is returned.</p> 1868<h4><code>ecdh.getPrivateKey([encoding])</code><span><a class="mark" href="#ecdhgetprivatekeyencoding" id="ecdhgetprivatekeyencoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_ecdh_getprivatekey_encoding"></a></h4> 1869<div class="api_metadata"> 1870<span>Added in: v0.11.14</span> 1871</div> 1872<ul> 1873<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#buffers-and-character-encodings">encoding</a> of the return value.</li> 1874<li>Returns: <a href="buffer.html#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> 1875</ul> 1876<p>If <code>encoding</code> is specified, a string is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is 1877returned.</p> 1878<h4><code>ecdh.getPublicKey([encoding][, format])</code><span><a class="mark" href="#ecdhgetpublickeyencoding-format" id="ecdhgetpublickeyencoding-format">#</a></span><a aria-hidden="true" class="legacy" id="crypto_ecdh_getpublickey_encoding_format"></a></h4> 1879<div class="api_metadata"> 1880<span>Added in: v0.11.14</span> 1881</div> 1882<ul> 1883<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#buffers-and-character-encodings">encoding</a> of the return value.</li> 1884<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> 1885<li>Returns: <a href="buffer.html#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 1886<code>encoding</code> and <code>format</code>.</li> 1887</ul> 1888<p>The <code>format</code> argument specifies point encoding and can be <code>'compressed'</code> or 1889<code>'uncompressed'</code>. If <code>format</code> is not specified the point will be returned in 1890<code>'uncompressed'</code> format.</p> 1891<p>If <code>encoding</code> is specified, a string is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is 1892returned.</p> 1893<h4><code>ecdh.setPrivateKey(privateKey[, encoding])</code><span><a class="mark" href="#ecdhsetprivatekeyprivatekey-encoding" id="ecdhsetprivatekeyprivatekey-encoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_ecdh_setprivatekey_privatekey_encoding"></a></h4> 1894<div class="api_metadata"> 1895<span>Added in: v0.11.14</span> 1896</div> 1897<ul> 1898<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 1899<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#buffers-and-character-encodings">encoding</a> of the <code>privateKey</code> string.</li> 1900</ul> 1901<p>Sets the EC Diffie-Hellman private key. 1902If <code>encoding</code> is provided, <code>privateKey</code> is expected 1903to be a string; otherwise <code>privateKey</code> is expected to be a <a href="buffer.html"><code>Buffer</code></a>, 1904<code>TypedArray</code>, or <code>DataView</code>.</p> 1905<p>If <code>privateKey</code> is not valid for the curve specified when the <code>ECDH</code> object was 1906created, an error is thrown. Upon setting the private key, the associated 1907public point (key) is also generated and set in the <code>ECDH</code> object.</p> 1908<h4><code>ecdh.setPublicKey(publicKey[, encoding])</code><span><a class="mark" href="#ecdhsetpublickeypublickey-encoding" id="ecdhsetpublickeypublickey-encoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_ecdh_setpublickey_publickey_encoding"></a></h4> 1909<div class="api_metadata"> 1910<span>Added in: v0.11.14</span><span>Deprecated since: v5.2.0</span> 1911</div> 1912<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated</div><p></p> 1913<ul> 1914<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 1915<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#buffers-and-character-encodings">encoding</a> of the <code>publicKey</code> string.</li> 1916</ul> 1917<p>Sets the EC Diffie-Hellman public key. 1918If <code>encoding</code> is provided <code>publicKey</code> is expected to 1919be a string; otherwise a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or <code>DataView</code> is expected.</p> 1920<p>There is not normally a reason to call this method because <code>ECDH</code> 1921only requires a private key and the other party's public key to compute the 1922shared secret. Typically either <a href="#ecdhgeneratekeysencoding-format"><code>ecdh.generateKeys()</code></a> or 1923<a href="#ecdhsetprivatekeyprivatekey-encoding"><code>ecdh.setPrivateKey()</code></a> will be called. The <a href="#ecdhsetprivatekeyprivatekey-encoding"><code>ecdh.setPrivateKey()</code></a> method 1924attempts to generate the public point/key associated with the private key being 1925set.</p> 1926<p>Example (obtaining a shared secret):</p> 1927 1928<pre class="with-13-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">const</span> { 1929 createECDH, 1930 createHash, 1931} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 1932 1933<span class="hljs-keyword">const</span> alice = <span class="hljs-title function_">createECDH</span>(<span class="hljs-string">'secp256k1'</span>); 1934<span class="hljs-keyword">const</span> bob = <span class="hljs-title function_">createECDH</span>(<span class="hljs-string">'secp256k1'</span>); 1935 1936<span class="hljs-comment">// This is a shortcut way of specifying one of Alice's previous private</span> 1937<span class="hljs-comment">// keys. It would be unwise to use such a predictable private key in a real</span> 1938<span class="hljs-comment">// application.</span> 1939alice.<span class="hljs-title function_">setPrivateKey</span>( 1940 <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>(), 1941); 1942 1943<span class="hljs-comment">// Bob uses a newly generated cryptographically strong</span> 1944<span class="hljs-comment">// pseudorandom key pair</span> 1945bob.<span class="hljs-title function_">generateKeys</span>(); 1946 1947<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>); 1948<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>); 1949 1950<span class="hljs-comment">// aliceSecret and bobSecret should be the same shared secret value</span> 1951<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(aliceSecret === bobSecret);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 1952 createECDH, 1953 createHash, 1954} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 1955 1956<span class="hljs-keyword">const</span> alice = <span class="hljs-title function_">createECDH</span>(<span class="hljs-string">'secp256k1'</span>); 1957<span class="hljs-keyword">const</span> bob = <span class="hljs-title function_">createECDH</span>(<span class="hljs-string">'secp256k1'</span>); 1958 1959<span class="hljs-comment">// This is a shortcut way of specifying one of Alice's previous private</span> 1960<span class="hljs-comment">// keys. It would be unwise to use such a predictable private key in a real</span> 1961<span class="hljs-comment">// application.</span> 1962alice.<span class="hljs-title function_">setPrivateKey</span>( 1963 <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>(), 1964); 1965 1966<span class="hljs-comment">// Bob uses a newly generated cryptographically strong</span> 1967<span class="hljs-comment">// pseudorandom key pair</span> 1968bob.<span class="hljs-title function_">generateKeys</span>(); 1969 1970<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>); 1971<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>); 1972 1973<span class="hljs-comment">// aliceSecret and bobSecret should be the same shared secret value</span> 1974<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(aliceSecret === bobSecret);</code><button class="copy-button">copy</button></pre> 1975</section><section><h3>Class: <code>Hash</code><span><a class="mark" href="#class-hash" id="class-hash">#</a></span><a aria-hidden="true" class="legacy" id="crypto_class_hash"></a></h3> 1976<div class="api_metadata"> 1977<span>Added in: v0.1.92</span> 1978</div> 1979<ul> 1980<li>Extends: <a href="stream.html#class-streamtransform" class="type"><stream.Transform></a></li> 1981</ul> 1982<p>The <code>Hash</code> class is a utility for creating hash digests of data. It can be 1983used in one of two ways:</p> 1984<ul> 1985<li>As a <a href="stream.html">stream</a> that is both readable and writable, where data is written 1986to produce a computed hash digest on the readable side, or</li> 1987<li>Using the <a href="#hashupdatedata-inputencoding"><code>hash.update()</code></a> and <a href="#hashdigestencoding"><code>hash.digest()</code></a> methods to produce the 1988computed hash.</li> 1989</ul> 1990<p>The <a href="#cryptocreatehashalgorithm-options"><code>crypto.createHash()</code></a> method is used to create <code>Hash</code> instances. <code>Hash</code> 1991objects are not to be created directly using the <code>new</code> keyword.</p> 1992<p>Example: Using <code>Hash</code> objects as streams:</p> 1993 1994<pre class="with-13-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">const</span> { 1995 createHash, 1996} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 1997 1998<span class="hljs-keyword">const</span> hash = <span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>); 1999 2000hash.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> { 2001 <span class="hljs-comment">// Only one element is going to be produced by the</span> 2002 <span class="hljs-comment">// hash stream.</span> 2003 <span class="hljs-keyword">const</span> data = hash.<span class="hljs-title function_">read</span>(); 2004 <span class="hljs-keyword">if</span> (data) { 2005 <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>)); 2006 <span class="hljs-comment">// Prints:</span> 2007 <span class="hljs-comment">// 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50</span> 2008 } 2009}); 2010 2011hash.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to hash'</span>); 2012hash.<span class="hljs-title function_">end</span>();</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 2013 createHash, 2014} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 2015 2016<span class="hljs-keyword">const</span> hash = <span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>); 2017 2018hash.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> { 2019 <span class="hljs-comment">// Only one element is going to be produced by the</span> 2020 <span class="hljs-comment">// hash stream.</span> 2021 <span class="hljs-keyword">const</span> data = hash.<span class="hljs-title function_">read</span>(); 2022 <span class="hljs-keyword">if</span> (data) { 2023 <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>)); 2024 <span class="hljs-comment">// Prints:</span> 2025 <span class="hljs-comment">// 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50</span> 2026 } 2027}); 2028 2029hash.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to hash'</span>); 2030hash.<span class="hljs-title function_">end</span>();</code><button class="copy-button">copy</button></pre> 2031<p>Example: Using <code>Hash</code> and piped streams:</p> 2032 2033<pre class="with-48-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createReadStream } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 2034<span class="hljs-keyword">import</span> { stdout } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>; 2035<span class="hljs-keyword">const</span> { createHash } = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 2036 2037<span class="hljs-keyword">const</span> hash = <span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>); 2038 2039<span class="hljs-keyword">const</span> input = <span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'test.js'</span>); 2040input.<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>(stdout);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { createReadStream } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 2041<span class="hljs-keyword">const</span> { createHash } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 2042<span class="hljs-keyword">const</span> { stdout } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:process'</span>); 2043 2044<span class="hljs-keyword">const</span> hash = <span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>); 2045 2046<span class="hljs-keyword">const</span> input = <span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'test.js'</span>); 2047input.<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>(stdout);</code><button class="copy-button">copy</button></pre> 2048<p>Example: Using the <a href="#hashupdatedata-inputencoding"><code>hash.update()</code></a> and <a href="#hashdigestencoding"><code>hash.digest()</code></a> methods:</p> 2049 2050<pre class="with-13-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">const</span> { 2051 createHash, 2052} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 2053 2054<span class="hljs-keyword">const</span> hash = <span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>); 2055 2056hash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to hash'</span>); 2057<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>)); 2058<span class="hljs-comment">// Prints:</span> 2059<span class="hljs-comment">// 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 2060 createHash, 2061} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 2062 2063<span class="hljs-keyword">const</span> hash = <span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>); 2064 2065hash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to hash'</span>); 2066<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>)); 2067<span class="hljs-comment">// Prints:</span> 2068<span class="hljs-comment">// 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50</span></code><button class="copy-button">copy</button></pre> 2069<h4><code>hash.copy([options])</code><span><a class="mark" href="#hashcopyoptions" id="hashcopyoptions">#</a></span><a aria-hidden="true" class="legacy" id="crypto_hash_copy_options"></a></h4> 2070<div class="api_metadata"> 2071<span>Added in: v13.1.0</span> 2072</div> 2073<ul> 2074<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#new-streamtransformoptions"><code>stream.transform</code> options</a></li> 2075<li>Returns: <a href="crypto.html#class-hash" class="type"><Hash></a></li> 2076</ul> 2077<p>Creates a new <code>Hash</code> object that contains a deep copy of the internal state 2078of the current <code>Hash</code> object.</p> 2079<p>The optional <code>options</code> argument controls stream behavior. For XOF hash 2080functions such as <code>'shake256'</code>, the <code>outputLength</code> option can be used to 2081specify the desired output length in bytes.</p> 2082<p>An error is thrown when an attempt is made to copy the <code>Hash</code> object after 2083its <a href="#hashdigestencoding"><code>hash.digest()</code></a> method has been called.</p> 2084 2085<pre class="with-28-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-comment">// Calculate a rolling hash.</span> 2086<span class="hljs-keyword">const</span> { 2087 createHash, 2088} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 2089 2090<span class="hljs-keyword">const</span> hash = <span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>); 2091 2092hash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'one'</span>); 2093<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>)); 2094 2095hash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'two'</span>); 2096<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>)); 2097 2098hash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'three'</span>); 2099<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>)); 2100 2101<span class="hljs-comment">// Etc.</span></code><code class="language-js cjs"><span class="hljs-comment">// Calculate a rolling hash.</span> 2102<span class="hljs-keyword">const</span> { 2103 createHash, 2104} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 2105 2106<span class="hljs-keyword">const</span> hash = <span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>); 2107 2108hash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'one'</span>); 2109<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>)); 2110 2111hash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'two'</span>); 2112<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>)); 2113 2114hash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'three'</span>); 2115<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>)); 2116 2117<span class="hljs-comment">// Etc.</span></code><button class="copy-button">copy</button></pre> 2118<h4><code>hash.digest([encoding])</code><span><a class="mark" href="#hashdigestencoding" id="hashdigestencoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_hash_digest_encoding"></a></h4> 2119<div class="api_metadata"> 2120<span>Added in: v0.1.92</span> 2121</div> 2122<ul> 2123<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#buffers-and-character-encodings">encoding</a> of the return value.</li> 2124<li>Returns: <a href="buffer.html#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> 2125</ul> 2126<p>Calculates the digest of all of the data passed to be hashed (using the 2127<a href="#hashupdatedata-inputencoding"><code>hash.update()</code></a> method). 2128If <code>encoding</code> is provided a string will be returned; otherwise 2129a <a href="buffer.html"><code>Buffer</code></a> is returned.</p> 2130<p>The <code>Hash</code> object can not be used again after <code>hash.digest()</code> method has been 2131called. Multiple calls will cause an error to be thrown.</p> 2132<h4><code>hash.update(data[, inputEncoding])</code><span><a class="mark" href="#hashupdatedata-inputencoding" id="hashupdatedata-inputencoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_hash_update_data_inputencoding"></a></h4> 2133<div class="api_metadata"> 2134<details class="changelog"><summary>History</summary> 2135<table> 2136<tbody><tr><th>Version</th><th>Changes</th></tr> 2137<tr><td>v6.0.0</td> 2138<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p></td></tr> 2139<tr><td>v0.1.92</td> 2140<td><p><span>Added in: v0.1.92</span></p></td></tr> 2141</tbody></table> 2142</details> 2143</div> 2144<ul> 2145<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#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> 2146<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#buffers-and-character-encodings">encoding</a> of the <code>data</code> string.</li> 2147</ul> 2148<p>Updates the hash content with the given <code>data</code>, the encoding of which 2149is given in <code>inputEncoding</code>. 2150If <code>encoding</code> is not provided, and the <code>data</code> is a string, an 2151encoding of <code>'utf8'</code> is enforced. If <code>data</code> is a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or 2152<code>DataView</code>, then <code>inputEncoding</code> is ignored.</p> 2153<p>This can be called many times with new data as it is streamed.</p> 2154</section><section><h3>Class: <code>Hmac</code><span><a class="mark" href="#class-hmac" id="class-hmac">#</a></span><a aria-hidden="true" class="legacy" id="crypto_class_hmac"></a></h3> 2155<div class="api_metadata"> 2156<span>Added in: v0.1.94</span> 2157</div> 2158<ul> 2159<li>Extends: <a href="stream.html#class-streamtransform" class="type"><stream.Transform></a></li> 2160</ul> 2161<p>The <code>Hmac</code> class is a utility for creating cryptographic HMAC digests. It can 2162be used in one of two ways:</p> 2163<ul> 2164<li>As a <a href="stream.html">stream</a> that is both readable and writable, where data is written 2165to produce a computed HMAC digest on the readable side, or</li> 2166<li>Using the <a href="#hmacupdatedata-inputencoding"><code>hmac.update()</code></a> and <a href="#hmacdigestencoding"><code>hmac.digest()</code></a> methods to produce the 2167computed HMAC digest.</li> 2168</ul> 2169<p>The <a href="#cryptocreatehmacalgorithm-key-options"><code>crypto.createHmac()</code></a> method is used to create <code>Hmac</code> instances. <code>Hmac</code> 2170objects are not to be created directly using the <code>new</code> keyword.</p> 2171<p>Example: Using <code>Hmac</code> objects as streams:</p> 2172 2173<pre class="with-13-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">const</span> { 2174 createHmac, 2175} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 2176 2177<span class="hljs-keyword">const</span> hmac = <span class="hljs-title function_">createHmac</span>(<span class="hljs-string">'sha256'</span>, <span class="hljs-string">'a secret'</span>); 2178 2179hmac.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> { 2180 <span class="hljs-comment">// Only one element is going to be produced by the</span> 2181 <span class="hljs-comment">// hash stream.</span> 2182 <span class="hljs-keyword">const</span> data = hmac.<span class="hljs-title function_">read</span>(); 2183 <span class="hljs-keyword">if</span> (data) { 2184 <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>)); 2185 <span class="hljs-comment">// Prints:</span> 2186 <span class="hljs-comment">// 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e</span> 2187 } 2188}); 2189 2190hmac.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to hash'</span>); 2191hmac.<span class="hljs-title function_">end</span>();</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 2192 createHmac, 2193} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 2194 2195<span class="hljs-keyword">const</span> hmac = <span class="hljs-title function_">createHmac</span>(<span class="hljs-string">'sha256'</span>, <span class="hljs-string">'a secret'</span>); 2196 2197hmac.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> { 2198 <span class="hljs-comment">// Only one element is going to be produced by the</span> 2199 <span class="hljs-comment">// hash stream.</span> 2200 <span class="hljs-keyword">const</span> data = hmac.<span class="hljs-title function_">read</span>(); 2201 <span class="hljs-keyword">if</span> (data) { 2202 <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>)); 2203 <span class="hljs-comment">// Prints:</span> 2204 <span class="hljs-comment">// 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e</span> 2205 } 2206}); 2207 2208hmac.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to hash'</span>); 2209hmac.<span class="hljs-title function_">end</span>();</code><button class="copy-button">copy</button></pre> 2210<p>Example: Using <code>Hmac</code> and piped streams:</p> 2211 2212<pre class="with-43-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { createReadStream } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 2213<span class="hljs-keyword">import</span> { stdout } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>; 2214<span class="hljs-keyword">const</span> { 2215 createHmac, 2216} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 2217 2218<span class="hljs-keyword">const</span> hmac = <span class="hljs-title function_">createHmac</span>(<span class="hljs-string">'sha256'</span>, <span class="hljs-string">'a secret'</span>); 2219 2220<span class="hljs-keyword">const</span> input = <span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'test.js'</span>); 2221input.<span class="hljs-title function_">pipe</span>(hmac).<span class="hljs-title function_">pipe</span>(stdout);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 2222 createReadStream, 2223} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 2224<span class="hljs-keyword">const</span> { 2225 createHmac, 2226} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 2227<span class="hljs-keyword">const</span> { stdout } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:process'</span>); 2228 2229<span class="hljs-keyword">const</span> hmac = <span class="hljs-title function_">createHmac</span>(<span class="hljs-string">'sha256'</span>, <span class="hljs-string">'a secret'</span>); 2230 2231<span class="hljs-keyword">const</span> input = <span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'test.js'</span>); 2232input.<span class="hljs-title function_">pipe</span>(hmac).<span class="hljs-title function_">pipe</span>(stdout);</code><button class="copy-button">copy</button></pre> 2233<p>Example: Using the <a href="#hmacupdatedata-inputencoding"><code>hmac.update()</code></a> and <a href="#hmacdigestencoding"><code>hmac.digest()</code></a> methods:</p> 2234 2235<pre class="with-13-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">const</span> { 2236 createHmac, 2237} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 2238 2239<span class="hljs-keyword">const</span> hmac = <span class="hljs-title function_">createHmac</span>(<span class="hljs-string">'sha256'</span>, <span class="hljs-string">'a secret'</span>); 2240 2241hmac.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to hash'</span>); 2242<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>)); 2243<span class="hljs-comment">// Prints:</span> 2244<span class="hljs-comment">// 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 2245 createHmac, 2246} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 2247 2248<span class="hljs-keyword">const</span> hmac = <span class="hljs-title function_">createHmac</span>(<span class="hljs-string">'sha256'</span>, <span class="hljs-string">'a secret'</span>); 2249 2250hmac.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to hash'</span>); 2251<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>)); 2252<span class="hljs-comment">// Prints:</span> 2253<span class="hljs-comment">// 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e</span></code><button class="copy-button">copy</button></pre> 2254<h4><code>hmac.digest([encoding])</code><span><a class="mark" href="#hmacdigestencoding" id="hmacdigestencoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_hmac_digest_encoding"></a></h4> 2255<div class="api_metadata"> 2256<span>Added in: v0.1.94</span> 2257</div> 2258<ul> 2259<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#buffers-and-character-encodings">encoding</a> of the return value.</li> 2260<li>Returns: <a href="buffer.html#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> 2261</ul> 2262<p>Calculates the HMAC digest of all of the data passed using <a href="#hmacupdatedata-inputencoding"><code>hmac.update()</code></a>. 2263If <code>encoding</code> is 2264provided a string is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is returned;</p> 2265<p>The <code>Hmac</code> object can not be used again after <code>hmac.digest()</code> has been 2266called. Multiple calls to <code>hmac.digest()</code> will result in an error being thrown.</p> 2267<h4><code>hmac.update(data[, inputEncoding])</code><span><a class="mark" href="#hmacupdatedata-inputencoding" id="hmacupdatedata-inputencoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_hmac_update_data_inputencoding"></a></h4> 2268<div class="api_metadata"> 2269<details class="changelog"><summary>History</summary> 2270<table> 2271<tbody><tr><th>Version</th><th>Changes</th></tr> 2272<tr><td>v6.0.0</td> 2273<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p></td></tr> 2274<tr><td>v0.1.94</td> 2275<td><p><span>Added in: v0.1.94</span></p></td></tr> 2276</tbody></table> 2277</details> 2278</div> 2279<ul> 2280<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#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> 2281<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#buffers-and-character-encodings">encoding</a> of the <code>data</code> string.</li> 2282</ul> 2283<p>Updates the <code>Hmac</code> content with the given <code>data</code>, the encoding of which 2284is given in <code>inputEncoding</code>. 2285If <code>encoding</code> is not provided, and the <code>data</code> is a string, an 2286encoding of <code>'utf8'</code> is enforced. If <code>data</code> is a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or 2287<code>DataView</code>, then <code>inputEncoding</code> is ignored.</p> 2288<p>This can be called many times with new data as it is streamed.</p> 2289</section><section><h3>Class: <code>KeyObject</code><span><a class="mark" href="#class-keyobject" id="class-keyobject">#</a></span><a aria-hidden="true" class="legacy" id="crypto_class_keyobject"></a></h3> 2290<div class="api_metadata"> 2291<details class="changelog"><summary>History</summary> 2292<table> 2293<tbody><tr><th>Version</th><th>Changes</th></tr> 2294<tr><td>v14.5.0, v12.19.0</td> 2295<td><p>Instances of this class can now be passed to worker threads using <code>postMessage</code>.</p></td></tr> 2296<tr><td>v11.13.0</td> 2297<td><p>This class is now exported.</p></td></tr> 2298<tr><td>v11.6.0</td> 2299<td><p><span>Added in: v11.6.0</span></p></td></tr> 2300</tbody></table> 2301</details> 2302</div> 2303<p>Node.js uses a <code>KeyObject</code> class to represent a symmetric or asymmetric key, 2304and each kind of key exposes different functions. The 2305<a href="#cryptocreatesecretkeykey-encoding"><code>crypto.createSecretKey()</code></a>, <a href="#cryptocreatepublickeykey"><code>crypto.createPublicKey()</code></a> and 2306<a href="#cryptocreateprivatekeykey"><code>crypto.createPrivateKey()</code></a> methods are used to create <code>KeyObject</code> 2307instances. <code>KeyObject</code> objects are not to be created directly using the <code>new</code> 2308keyword.</p> 2309<p>Most applications should consider using the new <code>KeyObject</code> API instead of 2310passing keys as strings or <code>Buffer</code>s due to improved security features.</p> 2311<p><code>KeyObject</code> instances can be passed to other threads via <a href="worker_threads.html#portpostmessagevalue-transferlist"><code>postMessage()</code></a>. 2312The receiver obtains a cloned <code>KeyObject</code>, and the <code>KeyObject</code> does not need to 2313be listed in the <code>transferList</code> argument.</p> 2314<h4>Static method: <code>KeyObject.from(key)</code><span><a class="mark" href="#static-method-keyobjectfromkey" id="static-method-keyobjectfromkey">#</a></span><a aria-hidden="true" class="legacy" id="crypto_static_method_keyobject_from_key"></a></h4> 2315<div class="api_metadata"> 2316<span>Added in: v15.0.0</span> 2317</div> 2318<ul> 2319<li><code>key</code> <a href="webcrypto.html#class-cryptokey" class="type"><CryptoKey></a></li> 2320<li>Returns: <a href="crypto.html#class-keyobject" class="type"><KeyObject></a></li> 2321</ul> 2322<p>Example: Converting a <code>CryptoKey</code> instance to a <code>KeyObject</code>:</p> 2323 2324<pre class="with-61-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">const</span> { webcrypto, <span class="hljs-title class_">KeyObject</span> } = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 2325<span class="hljs-keyword">const</span> { subtle } = webcrypto; 2326 2327<span class="hljs-keyword">const</span> key = <span class="hljs-keyword">await</span> subtle.<span class="hljs-title function_">generateKey</span>({ 2328 <span class="hljs-attr">name</span>: <span class="hljs-string">'HMAC'</span>, 2329 <span class="hljs-attr">hash</span>: <span class="hljs-string">'SHA-256'</span>, 2330 <span class="hljs-attr">length</span>: <span class="hljs-number">256</span>, 2331}, <span class="hljs-literal">true</span>, [<span class="hljs-string">'sign'</span>, <span class="hljs-string">'verify'</span>]); 2332 2333<span class="hljs-keyword">const</span> keyObject = <span class="hljs-title class_">KeyObject</span>.<span class="hljs-title function_">from</span>(key); 2334<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(keyObject.<span class="hljs-property">symmetricKeySize</span>); 2335<span class="hljs-comment">// Prints: 32 (symmetric key size in bytes)</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 2336 <span class="hljs-attr">webcrypto</span>: { 2337 subtle, 2338 }, 2339 <span class="hljs-title class_">KeyObject</span>, 2340} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 2341 2342(<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { 2343 <span class="hljs-keyword">const</span> key = <span class="hljs-keyword">await</span> subtle.<span class="hljs-title function_">generateKey</span>({ 2344 <span class="hljs-attr">name</span>: <span class="hljs-string">'HMAC'</span>, 2345 <span class="hljs-attr">hash</span>: <span class="hljs-string">'SHA-256'</span>, 2346 <span class="hljs-attr">length</span>: <span class="hljs-number">256</span>, 2347 }, <span class="hljs-literal">true</span>, [<span class="hljs-string">'sign'</span>, <span class="hljs-string">'verify'</span>]); 2348 2349 <span class="hljs-keyword">const</span> keyObject = <span class="hljs-title class_">KeyObject</span>.<span class="hljs-title function_">from</span>(key); 2350 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(keyObject.<span class="hljs-property">symmetricKeySize</span>); 2351 <span class="hljs-comment">// Prints: 32 (symmetric key size in bytes)</span> 2352})();</code><button class="copy-button">copy</button></pre> 2353<h4><code>keyObject.asymmetricKeyDetails</code><span><a class="mark" href="#keyobjectasymmetrickeydetails" id="keyobjectasymmetrickeydetails">#</a></span><a aria-hidden="true" class="legacy" id="crypto_keyobject_asymmetrickeydetails"></a></h4> 2354<div class="api_metadata"> 2355<details class="changelog"><summary>History</summary> 2356<table> 2357<tbody><tr><th>Version</th><th>Changes</th></tr> 2358<tr><td>v16.9.0</td> 2359<td><p>Expose <code>RSASSA-PSS-params</code> sequence parameters for RSA-PSS keys.</p></td></tr> 2360<tr><td>v15.7.0</td> 2361<td><p><span>Added in: v15.7.0</span></p></td></tr> 2362</tbody></table> 2363</details> 2364</div> 2365<ul> 2366<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2367<ul> 2368<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> 2369<li><code>publicExponent</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a> Public exponent (RSA).</li> 2370<li><code>hashAlgorithm</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Name of the message digest (RSA-PSS).</li> 2371<li><code>mgf1HashAlgorithm</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Name of the message digest used by 2372MGF1 (RSA-PSS).</li> 2373<li><code>saltLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Minimal salt length in bytes (RSA-PSS).</li> 2374<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> 2375<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 (EC).</li> 2376</ul> 2377</li> 2378</ul> 2379<p>This property exists only on asymmetric keys. Depending on the type of the key, 2380this object contains information about the key. None of the information obtained 2381through this property can be used to uniquely identify a key or to compromise 2382the security of the key.</p> 2383<p>For RSA-PSS keys, if the key material contains a <code>RSASSA-PSS-params</code> sequence, 2384the <code>hashAlgorithm</code>, <code>mgf1HashAlgorithm</code>, and <code>saltLength</code> properties will be 2385set.</p> 2386<p>Other key details might be exposed via this API using additional attributes.</p> 2387<h4><code>keyObject.asymmetricKeyType</code><span><a class="mark" href="#keyobjectasymmetrickeytype" id="keyobjectasymmetrickeytype">#</a></span><a aria-hidden="true" class="legacy" id="crypto_keyobject_asymmetrickeytype"></a></h4> 2388<div class="api_metadata"> 2389<details class="changelog"><summary>History</summary> 2390<table> 2391<tbody><tr><th>Version</th><th>Changes</th></tr> 2392<tr><td>v13.9.0, v12.17.0</td> 2393<td><p>Added support for <code>'dh'</code>.</p></td></tr> 2394<tr><td>v12.0.0</td> 2395<td><p>Added support for <code>'rsa-pss'</code>.</p></td></tr> 2396<tr><td>v12.0.0</td> 2397<td><p>This property now returns <code>undefined</code> for KeyObject instances of unrecognized type instead of aborting.</p></td></tr> 2398<tr><td>v12.0.0</td> 2399<td><p>Added support for <code>'x25519'</code> and <code>'x448'</code>.</p></td></tr> 2400<tr><td>v12.0.0</td> 2401<td><p>Added support for <code>'ed25519'</code> and <code>'ed448'</code>.</p></td></tr> 2402<tr><td>v11.6.0</td> 2403<td><p><span>Added in: v11.6.0</span></p></td></tr> 2404</tbody></table> 2405</details> 2406</div> 2407<ul> 2408<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2409</ul> 2410<p>For asymmetric keys, this property represents the type of the key. Supported key 2411types are:</p> 2412<ul> 2413<li><code>'rsa'</code> (OID 1.2.840.113549.1.1.1)</li> 2414<li><code>'rsa-pss'</code> (OID 1.2.840.113549.1.1.10)</li> 2415<li><code>'dsa'</code> (OID 1.2.840.10040.4.1)</li> 2416<li><code>'ec'</code> (OID 1.2.840.10045.2.1)</li> 2417<li><code>'x25519'</code> (OID 1.3.101.110)</li> 2418<li><code>'x448'</code> (OID 1.3.101.111)</li> 2419<li><code>'ed25519'</code> (OID 1.3.101.112)</li> 2420<li><code>'ed448'</code> (OID 1.3.101.113)</li> 2421<li><code>'dh'</code> (OID 1.2.840.113549.1.3.1)</li> 2422</ul> 2423<p>This property is <code>undefined</code> for unrecognized <code>KeyObject</code> types and symmetric 2424keys.</p> 2425<h4><code>keyObject.export([options])</code><span><a class="mark" href="#keyobjectexportoptions" id="keyobjectexportoptions">#</a></span><a aria-hidden="true" class="legacy" id="crypto_keyobject_export_options"></a></h4> 2426<div class="api_metadata"> 2427<details class="changelog"><summary>History</summary> 2428<table> 2429<tbody><tr><th>Version</th><th>Changes</th></tr> 2430<tr><td>v15.9.0</td> 2431<td><p>Added support for <code>'jwk'</code> format.</p></td></tr> 2432<tr><td>v11.6.0</td> 2433<td><p><span>Added in: v11.6.0</span></p></td></tr> 2434</tbody></table> 2435</details> 2436</div> 2437<ul> 2438<li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 2439<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#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 2440</ul> 2441<p>For symmetric keys, the following encoding options can be used:</p> 2442<ul> 2443<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>'buffer'</code> (default) or <code>'jwk'</code>.</li> 2444</ul> 2445<p>For public keys, the following encoding options can be used:</p> 2446<ul> 2447<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> 2448<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>, <code>'der'</code>, or <code>'jwk'</code>.</li> 2449</ul> 2450<p>For private keys, the following encoding options can be used:</p> 2451<ul> 2452<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 2453<code>'sec1'</code> (EC only).</li> 2454<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>, <code>'der'</code>, or <code>'jwk'</code>.</li> 2455<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 2456the given <code>cipher</code> and <code>passphrase</code> using PKCS#5 v2.0 password based 2457encryption.</li> 2458<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#class-buffer" class="type"><Buffer></a> The passphrase to use for encryption, see 2459<code>cipher</code>.</li> 2460</ul> 2461<p>The result type depends on the selected encoding format, when PEM the 2462result is a string, when DER it will be a buffer containing the data 2463encoded as DER, when <a href="https://tools.ietf.org/html/rfc7517">JWK</a> it will be an object.</p> 2464<p>When <a href="https://tools.ietf.org/html/rfc7517">JWK</a> encoding format was selected, all other encoding options are 2465ignored.</p> 2466<p>PKCS#1, SEC1, and PKCS#8 type keys can be encrypted by using a combination of 2467the <code>cipher</code> and <code>format</code> options. The PKCS#8 <code>type</code> can be used with any 2468<code>format</code> to encrypt any key algorithm (RSA, EC, or DH) by specifying a 2469<code>cipher</code>. PKCS#1 and SEC1 can only be encrypted by specifying a <code>cipher</code> 2470when the PEM <code>format</code> is used. For maximum compatibility, use PKCS#8 for 2471encrypted private keys. Since PKCS#8 defines its own 2472encryption mechanism, PEM-level encryption is not supported when encrypting 2473a 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 2474PKCS#1 and SEC1 encryption.</p> 2475<h4><code>keyObject.equals(otherKeyObject)</code><span><a class="mark" href="#keyobjectequalsotherkeyobject" id="keyobjectequalsotherkeyobject">#</a></span><a aria-hidden="true" class="legacy" id="crypto_keyobject_equals_otherkeyobject"></a></h4> 2476<div class="api_metadata"> 2477<span>Added in: v17.7.0</span> 2478</div> 2479<ul> 2480<li><code>otherKeyObject</code>: <a href="crypto.html#class-keyobject" class="type"><KeyObject></a> A <code>KeyObject</code> with which to 2481compare <code>keyObject</code>.</li> 2482<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2483</ul> 2484<p>Returns <code>true</code> or <code>false</code> depending on whether the keys have exactly the same 2485type, value, and parameters. This method is not 2486<a href="https://en.wikipedia.org/wiki/Timing_attack">constant time</a>.</p> 2487<h4><code>keyObject.symmetricKeySize</code><span><a class="mark" href="#keyobjectsymmetrickeysize" id="keyobjectsymmetrickeysize">#</a></span><a aria-hidden="true" class="legacy" id="crypto_keyobject_symmetrickeysize"></a></h4> 2488<div class="api_metadata"> 2489<span>Added in: v11.6.0</span> 2490</div> 2491<ul> 2492<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 2493</ul> 2494<p>For secret keys, this property represents the size of the key in bytes. This 2495property is <code>undefined</code> for asymmetric keys.</p> 2496<h4><code>keyObject.type</code><span><a class="mark" href="#keyobjecttype" id="keyobjecttype">#</a></span><a aria-hidden="true" class="legacy" id="crypto_keyobject_type"></a></h4> 2497<div class="api_metadata"> 2498<span>Added in: v11.6.0</span> 2499</div> 2500<ul> 2501<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2502</ul> 2503<p>Depending on the type of this <code>KeyObject</code>, this property is either 2504<code>'secret'</code> for secret (symmetric) keys, <code>'public'</code> for public (asymmetric) keys 2505or <code>'private'</code> for private (asymmetric) keys.</p> 2506</section><section><h3>Class: <code>Sign</code><span><a class="mark" href="#class-sign" id="class-sign">#</a></span><a aria-hidden="true" class="legacy" id="crypto_class_sign"></a></h3> 2507<div class="api_metadata"> 2508<span>Added in: v0.1.92</span> 2509</div> 2510<ul> 2511<li>Extends: <a href="stream.html#class-streamwritable" class="type"><stream.Writable></a></li> 2512</ul> 2513<p>The <code>Sign</code> class is a utility for generating signatures. It can be used in one 2514of two ways:</p> 2515<ul> 2516<li>As a writable <a href="stream.html">stream</a>, where data to be signed is written and the 2517<a href="#signsignprivatekey-outputencoding"><code>sign.sign()</code></a> method is used to generate and return the signature, or</li> 2518<li>Using the <a href="#signupdatedata-inputencoding"><code>sign.update()</code></a> and <a href="#signsignprivatekey-outputencoding"><code>sign.sign()</code></a> methods to produce the 2519signature.</li> 2520</ul> 2521<p>The <a href="#cryptocreatesignalgorithm-options"><code>crypto.createSign()</code></a> method is used to create <code>Sign</code> instances. The 2522argument is the string name of the hash function to use. <code>Sign</code> objects are not 2523to be created directly using the <code>new</code> keyword.</p> 2524<p>Example: Using <code>Sign</code> and <a href="#class-verify"><code>Verify</code></a> objects as streams:</p> 2525 2526<pre class="with-22-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">const</span> { 2527 generateKeyPairSync, 2528 createSign, 2529 createVerify, 2530} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 2531 2532<span class="hljs-keyword">const</span> { privateKey, publicKey } = <span class="hljs-title function_">generateKeyPairSync</span>(<span class="hljs-string">'ec'</span>, { 2533 <span class="hljs-attr">namedCurve</span>: <span class="hljs-string">'sect239k1'</span>, 2534}); 2535 2536<span class="hljs-keyword">const</span> sign = <span class="hljs-title function_">createSign</span>(<span class="hljs-string">'SHA256'</span>); 2537sign.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to sign'</span>); 2538sign.<span class="hljs-title function_">end</span>(); 2539<span class="hljs-keyword">const</span> signature = sign.<span class="hljs-title function_">sign</span>(privateKey, <span class="hljs-string">'hex'</span>); 2540 2541<span class="hljs-keyword">const</span> verify = <span class="hljs-title function_">createVerify</span>(<span class="hljs-string">'SHA256'</span>); 2542verify.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to sign'</span>); 2543verify.<span class="hljs-title function_">end</span>(); 2544<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>)); 2545<span class="hljs-comment">// Prints: true</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 2546 generateKeyPairSync, 2547 createSign, 2548 createVerify, 2549} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 2550 2551<span class="hljs-keyword">const</span> { privateKey, publicKey } = <span class="hljs-title function_">generateKeyPairSync</span>(<span class="hljs-string">'ec'</span>, { 2552 <span class="hljs-attr">namedCurve</span>: <span class="hljs-string">'sect239k1'</span>, 2553}); 2554 2555<span class="hljs-keyword">const</span> sign = <span class="hljs-title function_">createSign</span>(<span class="hljs-string">'SHA256'</span>); 2556sign.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to sign'</span>); 2557sign.<span class="hljs-title function_">end</span>(); 2558<span class="hljs-keyword">const</span> signature = sign.<span class="hljs-title function_">sign</span>(privateKey, <span class="hljs-string">'hex'</span>); 2559 2560<span class="hljs-keyword">const</span> verify = <span class="hljs-title function_">createVerify</span>(<span class="hljs-string">'SHA256'</span>); 2561verify.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to sign'</span>); 2562verify.<span class="hljs-title function_">end</span>(); 2563<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>)); 2564<span class="hljs-comment">// Prints: true</span></code><button class="copy-button">copy</button></pre> 2565<p>Example: Using the <a href="#signupdatedata-inputencoding"><code>sign.update()</code></a> and <a href="#verifyupdatedata-inputencoding"><code>verify.update()</code></a> methods:</p> 2566 2567<pre class="with-22-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">const</span> { 2568 generateKeyPairSync, 2569 createSign, 2570 createVerify, 2571} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 2572 2573<span class="hljs-keyword">const</span> { privateKey, publicKey } = <span class="hljs-title function_">generateKeyPairSync</span>(<span class="hljs-string">'rsa'</span>, { 2574 <span class="hljs-attr">modulusLength</span>: <span class="hljs-number">2048</span>, 2575}); 2576 2577<span class="hljs-keyword">const</span> sign = <span class="hljs-title function_">createSign</span>(<span class="hljs-string">'SHA256'</span>); 2578sign.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to sign'</span>); 2579sign.<span class="hljs-title function_">end</span>(); 2580<span class="hljs-keyword">const</span> signature = sign.<span class="hljs-title function_">sign</span>(privateKey); 2581 2582<span class="hljs-keyword">const</span> verify = <span class="hljs-title function_">createVerify</span>(<span class="hljs-string">'SHA256'</span>); 2583verify.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to sign'</span>); 2584verify.<span class="hljs-title function_">end</span>(); 2585<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(verify.<span class="hljs-title function_">verify</span>(publicKey, signature)); 2586<span class="hljs-comment">// Prints: true</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 2587 generateKeyPairSync, 2588 createSign, 2589 createVerify, 2590} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 2591 2592<span class="hljs-keyword">const</span> { privateKey, publicKey } = <span class="hljs-title function_">generateKeyPairSync</span>(<span class="hljs-string">'rsa'</span>, { 2593 <span class="hljs-attr">modulusLength</span>: <span class="hljs-number">2048</span>, 2594}); 2595 2596<span class="hljs-keyword">const</span> sign = <span class="hljs-title function_">createSign</span>(<span class="hljs-string">'SHA256'</span>); 2597sign.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to sign'</span>); 2598sign.<span class="hljs-title function_">end</span>(); 2599<span class="hljs-keyword">const</span> signature = sign.<span class="hljs-title function_">sign</span>(privateKey); 2600 2601<span class="hljs-keyword">const</span> verify = <span class="hljs-title function_">createVerify</span>(<span class="hljs-string">'SHA256'</span>); 2602verify.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to sign'</span>); 2603verify.<span class="hljs-title function_">end</span>(); 2604<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(verify.<span class="hljs-title function_">verify</span>(publicKey, signature)); 2605<span class="hljs-comment">// Prints: true</span></code><button class="copy-button">copy</button></pre> 2606<h4><code>sign.sign(privateKey[, outputEncoding])</code><span><a class="mark" href="#signsignprivatekey-outputencoding" id="signsignprivatekey-outputencoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_sign_sign_privatekey_outputencoding"></a></h4> 2607<div class="api_metadata"> 2608<details class="changelog"><summary>History</summary> 2609<table> 2610<tbody><tr><th>Version</th><th>Changes</th></tr> 2611<tr><td>v15.0.0</td> 2612<td><p>The privateKey can also be an ArrayBuffer and CryptoKey.</p></td></tr> 2613<tr><td>v13.2.0, v12.16.0</td> 2614<td><p>This function now supports IEEE-P1363 DSA and ECDSA signatures.</p></td></tr> 2615<tr><td>v12.0.0</td> 2616<td><p>This function now supports RSA-PSS keys.</p></td></tr> 2617<tr><td>v11.6.0</td> 2618<td><p>This function now supports key objects.</p></td></tr> 2619<tr><td>v8.0.0</td> 2620<td><p>Support for RSASSA-PSS and additional options was added.</p></td></tr> 2621<tr><td>v0.1.92</td> 2622<td><p><span>Added in: v0.1.92</span></p></td></tr> 2623</tbody></table> 2624</details> 2625</div> 2626<!--lint disable maximum-line-length remark-lint--> 2627<ul> 2628<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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#class-keyobject" class="type"><KeyObject></a> | <a href="webcrypto.html#class-cryptokey" class="type"><CryptoKey></a> 2629<ul> 2630<li><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2631<li><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 2632<li><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 2633</ul> 2634</li> 2635<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#buffers-and-character-encodings">encoding</a> of the return value.</li> 2636<li>Returns: <a href="buffer.html#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> 2637</ul> 2638<!--lint enable maximum-line-length remark-lint--> 2639<p>Calculates the signature on all the data passed through using either 2640<a href="#signupdatedata-inputencoding"><code>sign.update()</code></a> or <a href="stream.html#writablewritechunk-encoding-callback"><code>sign.write()</code></a>.</p> 2641<p>If <code>privateKey</code> is not a <a href="#class-keyobject"><code>KeyObject</code></a>, this function behaves as if 2642<code>privateKey</code> had been passed to <a href="#cryptocreateprivatekeykey"><code>crypto.createPrivateKey()</code></a>. If it is an 2643object, the following additional properties can be passed:</p> 2644<ul> 2645<li> 2646<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 2647format of the generated signature. It can be one of the following:</p> 2648<ul> 2649<li><code>'der'</code> (default): DER-encoded ASN.1 signature structure encoding <code>(r, s)</code>.</li> 2650<li><code>'ieee-p1363'</code>: Signature format <code>r || s</code> as proposed in IEEE-P1363.</li> 2651</ul> 2652</li> 2653<li> 2654<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> 2655<ul> 2656<li><code>crypto.constants.RSA_PKCS1_PADDING</code> (default)</li> 2657<li><code>crypto.constants.RSA_PKCS1_PSS_PADDING</code></li> 2658</ul> 2659<p><code>RSA_PKCS1_PSS_PADDING</code> will use MGF1 with the same hash function 2660used 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 2661an MGF1 hash function has been specified as part of the key in compliance with 2662section 3.3 of <a href="https://www.rfc-editor.org/rfc/rfc4055.txt">RFC 4055</a>.</p> 2663</li> 2664<li> 2665<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 2666<code>RSA_PKCS1_PSS_PADDING</code>. The special value 2667<code>crypto.constants.RSA_PSS_SALTLEN_DIGEST</code> sets the salt length to the digest 2668size, <code>crypto.constants.RSA_PSS_SALTLEN_MAX_SIGN</code> (default) sets it to the 2669maximum permissible value.</p> 2670</li> 2671</ul> 2672<p>If <code>outputEncoding</code> is provided a string is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> 2673is returned.</p> 2674<p>The <code>Sign</code> object can not be again used after <code>sign.sign()</code> method has been 2675called. Multiple calls to <code>sign.sign()</code> will result in an error being thrown.</p> 2676<h4><code>sign.update(data[, inputEncoding])</code><span><a class="mark" href="#signupdatedata-inputencoding" id="signupdatedata-inputencoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_sign_update_data_inputencoding"></a></h4> 2677<div class="api_metadata"> 2678<details class="changelog"><summary>History</summary> 2679<table> 2680<tbody><tr><th>Version</th><th>Changes</th></tr> 2681<tr><td>v6.0.0</td> 2682<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p></td></tr> 2683<tr><td>v0.1.92</td> 2684<td><p><span>Added in: v0.1.92</span></p></td></tr> 2685</tbody></table> 2686</details> 2687</div> 2688<ul> 2689<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#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> 2690<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#buffers-and-character-encodings">encoding</a> of the <code>data</code> string.</li> 2691</ul> 2692<p>Updates the <code>Sign</code> content with the given <code>data</code>, the encoding of which 2693is given in <code>inputEncoding</code>. 2694If <code>encoding</code> is not provided, and the <code>data</code> is a string, an 2695encoding of <code>'utf8'</code> is enforced. If <code>data</code> is a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or 2696<code>DataView</code>, then <code>inputEncoding</code> is ignored.</p> 2697<p>This can be called many times with new data as it is streamed.</p> 2698</section><section><h3>Class: <code>Verify</code><span><a class="mark" href="#class-verify" id="class-verify">#</a></span><a aria-hidden="true" class="legacy" id="crypto_class_verify"></a></h3> 2699<div class="api_metadata"> 2700<span>Added in: v0.1.92</span> 2701</div> 2702<ul> 2703<li>Extends: <a href="stream.html#class-streamwritable" class="type"><stream.Writable></a></li> 2704</ul> 2705<p>The <code>Verify</code> class is a utility for verifying signatures. It can be used in one 2706of two ways:</p> 2707<ul> 2708<li>As a writable <a href="stream.html">stream</a> where written data is used to validate against the 2709supplied signature, or</li> 2710<li>Using the <a href="#verifyupdatedata-inputencoding"><code>verify.update()</code></a> and <a href="#verifyverifyobject-signature-signatureencoding"><code>verify.verify()</code></a> methods to verify 2711the signature.</li> 2712</ul> 2713<p>The <a href="#cryptocreateverifyalgorithm-options"><code>crypto.createVerify()</code></a> method is used to create <code>Verify</code> instances. 2714<code>Verify</code> objects are not to be created directly using the <code>new</code> keyword.</p> 2715<p>See <a href="#class-sign"><code>Sign</code></a> for examples.</p> 2716<h4><code>verify.update(data[, inputEncoding])</code><span><a class="mark" href="#verifyupdatedata-inputencoding" id="verifyupdatedata-inputencoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_verify_update_data_inputencoding"></a></h4> 2717<div class="api_metadata"> 2718<details class="changelog"><summary>History</summary> 2719<table> 2720<tbody><tr><th>Version</th><th>Changes</th></tr> 2721<tr><td>v6.0.0</td> 2722<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p></td></tr> 2723<tr><td>v0.1.92</td> 2724<td><p><span>Added in: v0.1.92</span></p></td></tr> 2725</tbody></table> 2726</details> 2727</div> 2728<ul> 2729<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#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> 2730<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#buffers-and-character-encodings">encoding</a> of the <code>data</code> string.</li> 2731</ul> 2732<p>Updates the <code>Verify</code> content with the given <code>data</code>, the encoding of which 2733is given in <code>inputEncoding</code>. 2734If <code>inputEncoding</code> is not provided, and the <code>data</code> is a string, an 2735encoding of <code>'utf8'</code> is enforced. If <code>data</code> is a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or 2736<code>DataView</code>, then <code>inputEncoding</code> is ignored.</p> 2737<p>This can be called many times with new data as it is streamed.</p> 2738<h4><code>verify.verify(object, signature[, signatureEncoding])</code><span><a class="mark" href="#verifyverifyobject-signature-signatureencoding" id="verifyverifyobject-signature-signatureencoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_verify_verify_object_signature_signatureencoding"></a></h4> 2739<div class="api_metadata"> 2740<details class="changelog"><summary>History</summary> 2741<table> 2742<tbody><tr><th>Version</th><th>Changes</th></tr> 2743<tr><td>v15.0.0</td> 2744<td><p>The object can also be an ArrayBuffer and CryptoKey.</p></td></tr> 2745<tr><td>v13.2.0, v12.16.0</td> 2746<td><p>This function now supports IEEE-P1363 DSA and ECDSA signatures.</p></td></tr> 2747<tr><td>v12.0.0</td> 2748<td><p>This function now supports RSA-PSS keys.</p></td></tr> 2749<tr><td>v11.7.0</td> 2750<td><p>The key can now be a private key.</p></td></tr> 2751<tr><td>v8.0.0</td> 2752<td><p>Support for RSASSA-PSS and additional options was added.</p></td></tr> 2753<tr><td>v0.1.92</td> 2754<td><p><span>Added in: v0.1.92</span></p></td></tr> 2755</tbody></table> 2756</details> 2757</div> 2758<!--lint disable maximum-line-length remark-lint--> 2759<ul> 2760<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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#class-keyobject" class="type"><KeyObject></a> | <a href="webcrypto.html#class-cryptokey" class="type"><CryptoKey></a> 2761<ul> 2762<li><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2763<li><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 2764<li><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 2765</ul> 2766</li> 2767<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 2768<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#buffers-and-character-encodings">encoding</a> of the <code>signature</code> string.</li> 2769<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 2770signature for the data and public key.</li> 2771</ul> 2772<!--lint enable maximum-line-length remark-lint--> 2773<p>Verifies the provided data using the given <code>object</code> and <code>signature</code>.</p> 2774<p>If <code>object</code> is not a <a href="#class-keyobject"><code>KeyObject</code></a>, this function behaves as if 2775<code>object</code> had been passed to <a href="#cryptocreatepublickeykey"><code>crypto.createPublicKey()</code></a>. If it is an 2776object, the following additional properties can be passed:</p> 2777<ul> 2778<li> 2779<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 2780format of the signature. It can be one of the following:</p> 2781<ul> 2782<li><code>'der'</code> (default): DER-encoded ASN.1 signature structure encoding <code>(r, s)</code>.</li> 2783<li><code>'ieee-p1363'</code>: Signature format <code>r || s</code> as proposed in IEEE-P1363.</li> 2784</ul> 2785</li> 2786<li> 2787<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> 2788<ul> 2789<li><code>crypto.constants.RSA_PKCS1_PADDING</code> (default)</li> 2790<li><code>crypto.constants.RSA_PKCS1_PSS_PADDING</code></li> 2791</ul> 2792<p><code>RSA_PKCS1_PSS_PADDING</code> will use MGF1 with the same hash function 2793used 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 2794an MGF1 hash function has been specified as part of the key in compliance with 2795section 3.3 of <a href="https://www.rfc-editor.org/rfc/rfc4055.txt">RFC 4055</a>.</p> 2796</li> 2797<li> 2798<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 2799<code>RSA_PKCS1_PSS_PADDING</code>. The special value 2800<code>crypto.constants.RSA_PSS_SALTLEN_DIGEST</code> sets the salt length to the digest 2801size, <code>crypto.constants.RSA_PSS_SALTLEN_AUTO</code> (default) causes it to be 2802determined automatically.</p> 2803</li> 2804</ul> 2805<p>The <code>signature</code> argument is the previously calculated signature for the data, in 2806the <code>signatureEncoding</code>. 2807If a <code>signatureEncoding</code> is specified, the <code>signature</code> is expected to be a 2808string; otherwise <code>signature</code> is expected to be a <a href="buffer.html"><code>Buffer</code></a>, 2809<code>TypedArray</code>, or <code>DataView</code>.</p> 2810<p>The <code>verify</code> object can not be used again after <code>verify.verify()</code> has been 2811called. Multiple calls to <code>verify.verify()</code> will result in an error being 2812thrown.</p> 2813<p>Because public keys can be derived from private keys, a private key may 2814be passed instead of a public key.</p> 2815</section><section><h3>Class: <code>X509Certificate</code><span><a class="mark" href="#class-x509certificate" id="class-x509certificate">#</a></span><a aria-hidden="true" class="legacy" id="crypto_class_x509certificate"></a></h3> 2816<div class="api_metadata"> 2817<span>Added in: v15.6.0</span> 2818</div> 2819<p>Encapsulates an X509 certificate and provides read-only access to 2820its information.</p> 2821 2822<pre class="with-56-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">const</span> { X509Certificate } = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 2823 2824<span class="hljs-keyword">const</span> x509 = <span class="hljs-keyword">new</span> <span class="hljs-title function_">X509Certificate</span>(<span class="hljs-string">'{... pem encoded cert ...}'</span>); 2825 2826<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(x509.<span class="hljs-property">subject</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { X509Certificate } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 2827 2828<span class="hljs-keyword">const</span> x509 = <span class="hljs-keyword">new</span> <span class="hljs-title function_">X509Certificate</span>(<span class="hljs-string">'{... pem encoded cert ...}'</span>); 2829 2830<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(x509.<span class="hljs-property">subject</span>);</code><button class="copy-button">copy</button></pre> 2831<h4><code>new X509Certificate(buffer)</code><span><a class="mark" href="#new-x509certificatebuffer" id="new-x509certificatebuffer">#</a></span><a aria-hidden="true" class="legacy" id="crypto_new_x509certificate_buffer"></a></h4> 2832<div class="api_metadata"> 2833<span>Added in: v15.6.0</span> 2834</div> 2835<ul> 2836<li><code>buffer</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/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> A PEM or DER encoded 2837X509 Certificate.</li> 2838</ul> 2839<h4><code>x509.ca</code><span><a class="mark" href="#x509ca" id="x509ca">#</a></span><a aria-hidden="true" class="legacy" id="crypto_x509_ca"></a></h4> 2840<div class="api_metadata"> 2841<span>Added in: v15.6.0</span> 2842</div> 2843<ul> 2844<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Will be <code>true</code> if this is a Certificate Authority (CA) 2845certificate.</li> 2846</ul> 2847<h4><code>x509.checkEmail(email[, options])</code><span><a class="mark" href="#x509checkemailemail-options" id="x509checkemailemail-options">#</a></span><a aria-hidden="true" class="legacy" id="crypto_x509_checkemail_email_options"></a></h4> 2848<div class="api_metadata"> 2849<details class="changelog"><summary>History</summary> 2850<table> 2851<tbody><tr><th>Version</th><th>Changes</th></tr> 2852<tr><td>v18.0.0</td> 2853<td><p>The subject option now defaults to <code>'default'</code>.</p></td></tr> 2854<tr><td>v17.5.0</td> 2855<td><p>The subject option can now be set to <code>'default'</code>.</p></td></tr> 2856<tr><td>v17.5.0, v16.14.1</td> 2857<td><p>The <code>wildcards</code>, <code>partialWildcards</code>, <code>multiLabelWildcards</code>, and <code>singleLabelSubdomains</code> options have been removed since they had no effect.</p></td></tr> 2858<tr><td>v15.6.0</td> 2859<td><p><span>Added in: v15.6.0</span></p></td></tr> 2860</tbody></table> 2861</details> 2862</div> 2863<ul> 2864<li><code>email</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2865<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2866<ul> 2867<li><code>subject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <code>'default'</code>, <code>'always'</code>, or <code>'never'</code>. 2868<strong>Default:</strong> <code>'default'</code>.</li> 2869</ul> 2870</li> 2871<li>Returns: <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#Undefined_type" class="type"><undefined></a> Returns <code>email</code> if the certificate matches, 2872<code>undefined</code> if it does not.</li> 2873</ul> 2874<p>Checks whether the certificate matches the given email address.</p> 2875<p>If the <code>'subject'</code> option is undefined or set to <code>'default'</code>, the certificate 2876subject is only considered if the subject alternative name extension either does 2877not exist or does not contain any email addresses.</p> 2878<p>If the <code>'subject'</code> option is set to <code>'always'</code> and if the subject alternative 2879name extension either does not exist or does not contain a matching email 2880address, the certificate subject is considered.</p> 2881<p>If the <code>'subject'</code> option is set to <code>'never'</code>, the certificate subject is never 2882considered, even if the certificate contains no subject alternative names.</p> 2883<h4><code>x509.checkHost(name[, options])</code><span><a class="mark" href="#x509checkhostname-options" id="x509checkhostname-options">#</a></span><a aria-hidden="true" class="legacy" id="crypto_x509_checkhost_name_options"></a></h4> 2884<div class="api_metadata"> 2885<details class="changelog"><summary>History</summary> 2886<table> 2887<tbody><tr><th>Version</th><th>Changes</th></tr> 2888<tr><td>v18.0.0</td> 2889<td><p>The subject option now defaults to <code>'default'</code>.</p></td></tr> 2890<tr><td>v17.5.0</td> 2891<td><p>The subject option can now be set to <code>'default'</code>.</p></td></tr> 2892<tr><td>v15.6.0</td> 2893<td><p><span>Added in: v15.6.0</span></p></td></tr> 2894</tbody></table> 2895</details> 2896</div> 2897<ul> 2898<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2899<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2900<ul> 2901<li><code>subject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <code>'default'</code>, <code>'always'</code>, or <code>'never'</code>. 2902<strong>Default:</strong> <code>'default'</code>.</li> 2903<li><code>wildcards</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> 2904<li><code>partialWildcards</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> 2905<li><code>multiLabelWildcards</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code>.</li> 2906<li><code>singleLabelSubdomains</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code>.</li> 2907</ul> 2908</li> 2909<li>Returns: <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#Undefined_type" class="type"><undefined></a> Returns a subject name that matches <code>name</code>, 2910or <code>undefined</code> if no subject name matches <code>name</code>.</li> 2911</ul> 2912<p>Checks whether the certificate matches the given host name.</p> 2913<p>If the certificate matches the given host name, the matching subject name is 2914returned. The returned name might be an exact match (e.g., <code>foo.example.com</code>) 2915or it might contain wildcards (e.g., <code>*.example.com</code>). Because host name 2916comparisons are case-insensitive, the returned subject name might also differ 2917from the given <code>name</code> in capitalization.</p> 2918<p>If the <code>'subject'</code> option is undefined or set to <code>'default'</code>, the certificate 2919subject is only considered if the subject alternative name extension either does 2920not exist or does not contain any DNS names. This behavior is consistent with 2921<a href="https://www.rfc-editor.org/rfc/rfc2818.txt">RFC 2818</a> ("HTTP Over TLS").</p> 2922<p>If the <code>'subject'</code> option is set to <code>'always'</code> and if the subject alternative 2923name extension either does not exist or does not contain a matching DNS name, 2924the certificate subject is considered.</p> 2925<p>If the <code>'subject'</code> option is set to <code>'never'</code>, the certificate subject is never 2926considered, even if the certificate contains no subject alternative names.</p> 2927<h4><code>x509.checkIP(ip)</code><span><a class="mark" href="#x509checkipip" id="x509checkipip">#</a></span><a aria-hidden="true" class="legacy" id="crypto_x509_checkip_ip"></a></h4> 2928<div class="api_metadata"> 2929<details class="changelog"><summary>History</summary> 2930<table> 2931<tbody><tr><th>Version</th><th>Changes</th></tr> 2932<tr><td>v17.5.0, v16.14.1</td> 2933<td><p>The <code>options</code> argument has been removed since it had no effect.</p></td></tr> 2934<tr><td>v15.6.0</td> 2935<td><p><span>Added in: v15.6.0</span></p></td></tr> 2936</tbody></table> 2937</details> 2938</div> 2939<ul> 2940<li><code>ip</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2941<li>Returns: <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#Undefined_type" class="type"><undefined></a> Returns <code>ip</code> if the certificate matches, 2942<code>undefined</code> if it does not.</li> 2943</ul> 2944<p>Checks whether the certificate matches the given IP address (IPv4 or IPv6).</p> 2945<p>Only <a href="https://www.rfc-editor.org/rfc/rfc5280.txt">RFC 5280</a> <code>iPAddress</code> subject alternative names are considered, and they 2946must match the given <code>ip</code> address exactly. Other subject alternative names as 2947well as the subject field of the certificate are ignored.</p> 2948<h4><code>x509.checkIssued(otherCert)</code><span><a class="mark" href="#x509checkissuedothercert" id="x509checkissuedothercert">#</a></span><a aria-hidden="true" class="legacy" id="crypto_x509_checkissued_othercert"></a></h4> 2949<div class="api_metadata"> 2950<span>Added in: v15.6.0</span> 2951</div> 2952<ul> 2953<li><code>otherCert</code> <a href="crypto.html#class-x509certificate" class="type"><X509Certificate></a></li> 2954<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2955</ul> 2956<p>Checks whether this certificate was issued by the given <code>otherCert</code>.</p> 2957<h4><code>x509.checkPrivateKey(privateKey)</code><span><a class="mark" href="#x509checkprivatekeyprivatekey" id="x509checkprivatekeyprivatekey">#</a></span><a aria-hidden="true" class="legacy" id="crypto_x509_checkprivatekey_privatekey"></a></h4> 2958<div class="api_metadata"> 2959<span>Added in: v15.6.0</span> 2960</div> 2961<ul> 2962<li><code>privateKey</code> <a href="crypto.html#class-keyobject" class="type"><KeyObject></a> A private key.</li> 2963<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2964</ul> 2965<p>Checks whether the public key for this certificate is consistent with 2966the given private key.</p> 2967<h4><code>x509.fingerprint</code><span><a class="mark" href="#x509fingerprint" id="x509fingerprint">#</a></span><a aria-hidden="true" class="legacy" id="crypto_x509_fingerprint"></a></h4> 2968<div class="api_metadata"> 2969<span>Added in: v15.6.0</span> 2970</div> 2971<ul> 2972<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2973</ul> 2974<p>The SHA-1 fingerprint of this certificate.</p> 2975<p>Because SHA-1 is cryptographically broken and because the security of SHA-1 is 2976significantly worse than that of algorithms that are commonly used to sign 2977certificates, consider using <a href="#x509fingerprint256"><code>x509.fingerprint256</code></a> instead.</p> 2978<h4><code>x509.fingerprint256</code><span><a class="mark" href="#x509fingerprint256" id="x509fingerprint256">#</a></span><a aria-hidden="true" class="legacy" id="crypto_x509_fingerprint256"></a></h4> 2979<div class="api_metadata"> 2980<span>Added in: v15.6.0</span> 2981</div> 2982<ul> 2983<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2984</ul> 2985<p>The SHA-256 fingerprint of this certificate.</p> 2986<h4><code>x509.fingerprint512</code><span><a class="mark" href="#x509fingerprint512" id="x509fingerprint512">#</a></span><a aria-hidden="true" class="legacy" id="crypto_x509_fingerprint512"></a></h4> 2987<div class="api_metadata"> 2988<span>Added in: v17.2.0, v16.14.0</span> 2989</div> 2990<ul> 2991<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2992</ul> 2993<p>The SHA-512 fingerprint of this certificate.</p> 2994<p>Because computing the SHA-256 fingerprint is usually faster and because it is 2995only half the size of the SHA-512 fingerprint, <a href="#x509fingerprint256"><code>x509.fingerprint256</code></a> may be 2996a better choice. While SHA-512 presumably provides a higher level of security in 2997general, the security of SHA-256 matches that of most algorithms that are 2998commonly used to sign certificates.</p> 2999<h4><code>x509.infoAccess</code><span><a class="mark" href="#x509infoaccess" id="x509infoaccess">#</a></span><a aria-hidden="true" class="legacy" id="crypto_x509_infoaccess"></a></h4> 3000<div class="api_metadata"> 3001<details class="changelog"><summary>History</summary> 3002<table> 3003<tbody><tr><th>Version</th><th>Changes</th></tr> 3004<tr><td>v17.3.1, v16.13.2</td> 3005<td><p>Parts of this string may be encoded as JSON string literals in response to CVE-2021-44532.</p></td></tr> 3006<tr><td>v15.6.0</td> 3007<td><p><span>Added in: v15.6.0</span></p></td></tr> 3008</tbody></table> 3009</details> 3010</div> 3011<ul> 3012<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3013</ul> 3014<p>A textual representation of the certificate's authority information access 3015extension.</p> 3016<p>This is a line feed separated list of access descriptions. Each line begins with 3017the access method and the kind of the access location, followed by a colon and 3018the value associated with the access location.</p> 3019<p>After the prefix denoting the access method and the kind of the access location, 3020the remainder of each line might be enclosed in quotes to indicate that the 3021value is a JSON string literal. For backward compatibility, Node.js only uses 3022JSON string literals within this property when necessary to avoid ambiguity. 3023Third-party code should be prepared to handle both possible entry formats.</p> 3024<h4><code>x509.issuer</code><span><a class="mark" href="#x509issuer" id="x509issuer">#</a></span><a aria-hidden="true" class="legacy" id="crypto_x509_issuer"></a></h4> 3025<div class="api_metadata"> 3026<span>Added in: v15.6.0</span> 3027</div> 3028<ul> 3029<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3030</ul> 3031<p>The issuer identification included in this certificate.</p> 3032<h4><code>x509.issuerCertificate</code><span><a class="mark" href="#x509issuercertificate" id="x509issuercertificate">#</a></span><a aria-hidden="true" class="legacy" id="crypto_x509_issuercertificate"></a></h4> 3033<div class="api_metadata"> 3034<span>Added in: v15.9.0</span> 3035</div> 3036<ul> 3037<li>Type: <a href="crypto.html#class-x509certificate" class="type"><X509Certificate></a></li> 3038</ul> 3039<p>The issuer certificate or <code>undefined</code> if the issuer certificate is not 3040available.</p> 3041<h4><code>x509.keyUsage</code><span><a class="mark" href="#x509keyusage" id="x509keyusage">#</a></span><a aria-hidden="true" class="legacy" id="crypto_x509_keyusage"></a></h4> 3042<div class="api_metadata"> 3043<span>Added in: v15.6.0</span> 3044</div> 3045<ul> 3046<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> 3047</ul> 3048<p>An array detailing the key usages for this certificate.</p> 3049<h4><code>x509.publicKey</code><span><a class="mark" href="#x509publickey" id="x509publickey">#</a></span><a aria-hidden="true" class="legacy" id="crypto_x509_publickey"></a></h4> 3050<div class="api_metadata"> 3051<span>Added in: v15.6.0</span> 3052</div> 3053<ul> 3054<li>Type: <a href="crypto.html#class-keyobject" class="type"><KeyObject></a></li> 3055</ul> 3056<p>The public key <a href="crypto.html#class-keyobject" class="type"><KeyObject></a> for this certificate.</p> 3057<h4><code>x509.raw</code><span><a class="mark" href="#x509raw" id="x509raw">#</a></span><a aria-hidden="true" class="legacy" id="crypto_x509_raw"></a></h4> 3058<div class="api_metadata"> 3059<span>Added in: v15.6.0</span> 3060</div> 3061<ul> 3062<li>Type: <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 3063</ul> 3064<p>A <code>Buffer</code> containing the DER encoding of this certificate.</p> 3065<h4><code>x509.serialNumber</code><span><a class="mark" href="#x509serialnumber" id="x509serialnumber">#</a></span><a aria-hidden="true" class="legacy" id="crypto_x509_serialnumber"></a></h4> 3066<div class="api_metadata"> 3067<span>Added in: v15.6.0</span> 3068</div> 3069<ul> 3070<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3071</ul> 3072<p>The serial number of this certificate.</p> 3073<p>Serial numbers are assigned by certificate authorities and do not uniquely 3074identify certificates. Consider using <a href="#x509fingerprint256"><code>x509.fingerprint256</code></a> as a unique 3075identifier instead.</p> 3076<h4><code>x509.subject</code><span><a class="mark" href="#x509subject" id="x509subject">#</a></span><a aria-hidden="true" class="legacy" id="crypto_x509_subject"></a></h4> 3077<div class="api_metadata"> 3078<span>Added in: v15.6.0</span> 3079</div> 3080<ul> 3081<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3082</ul> 3083<p>The complete subject of this certificate.</p> 3084<h4><code>x509.subjectAltName</code><span><a class="mark" href="#x509subjectaltname" id="x509subjectaltname">#</a></span><a aria-hidden="true" class="legacy" id="crypto_x509_subjectaltname"></a></h4> 3085<div class="api_metadata"> 3086<details class="changelog"><summary>History</summary> 3087<table> 3088<tbody><tr><th>Version</th><th>Changes</th></tr> 3089<tr><td>v17.3.1, v16.13.2</td> 3090<td><p>Parts of this string may be encoded as JSON string literals in response to CVE-2021-44532.</p></td></tr> 3091<tr><td>v15.6.0</td> 3092<td><p><span>Added in: v15.6.0</span></p></td></tr> 3093</tbody></table> 3094</details> 3095</div> 3096<ul> 3097<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3098</ul> 3099<p>The subject alternative name specified for this certificate.</p> 3100<p>This is a comma-separated list of subject alternative names. Each entry begins 3101with a string identifying the kind of the subject alternative name followed by 3102a colon and the value associated with the entry.</p> 3103<p>Earlier versions of Node.js incorrectly assumed that it is safe to split this 3104property at the two-character sequence <code>', '</code> (see <a href="https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-44532">CVE-2021-44532</a>). However, 3105both malicious and legitimate certificates can contain subject alternative names 3106that include this sequence when represented as a string.</p> 3107<p>After the prefix denoting the type of the entry, the remainder of each entry 3108might be enclosed in quotes to indicate that the value is a JSON string literal. 3109For backward compatibility, Node.js only uses JSON string literals within this 3110property when necessary to avoid ambiguity. Third-party code should be prepared 3111to handle both possible entry formats.</p> 3112<h4><code>x509.toJSON()</code><span><a class="mark" href="#x509tojson" id="x509tojson">#</a></span><a aria-hidden="true" class="legacy" id="crypto_x509_tojson"></a></h4> 3113<div class="api_metadata"> 3114<span>Added in: v15.6.0</span> 3115</div> 3116<ul> 3117<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3118</ul> 3119<p>There is no standard JSON encoding for X509 certificates. The 3120<code>toJSON()</code> method returns a string containing the PEM encoded 3121certificate.</p> 3122<h4><code>x509.toLegacyObject()</code><span><a class="mark" href="#x509tolegacyobject" id="x509tolegacyobject">#</a></span><a aria-hidden="true" class="legacy" id="crypto_x509_tolegacyobject"></a></h4> 3123<div class="api_metadata"> 3124<span>Added in: v15.6.0</span> 3125</div> 3126<ul> 3127<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 3128</ul> 3129<p>Returns information about this certificate using the legacy 3130<a href="tls.html#certificate-object">certificate object</a> encoding.</p> 3131<h4><code>x509.toString()</code><span><a class="mark" href="#x509tostring" id="x509tostring">#</a></span><a aria-hidden="true" class="legacy" id="crypto_x509_tostring"></a></h4> 3132<div class="api_metadata"> 3133<span>Added in: v15.6.0</span> 3134</div> 3135<ul> 3136<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3137</ul> 3138<p>Returns the PEM-encoded certificate.</p> 3139<h4><code>x509.validFrom</code><span><a class="mark" href="#x509validfrom" id="x509validfrom">#</a></span><a aria-hidden="true" class="legacy" id="crypto_x509_validfrom"></a></h4> 3140<div class="api_metadata"> 3141<span>Added in: v15.6.0</span> 3142</div> 3143<ul> 3144<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3145</ul> 3146<p>The date/time from which this certificate is considered valid.</p> 3147<h4><code>x509.validTo</code><span><a class="mark" href="#x509validto" id="x509validto">#</a></span><a aria-hidden="true" class="legacy" id="crypto_x509_validto"></a></h4> 3148<div class="api_metadata"> 3149<span>Added in: v15.6.0</span> 3150</div> 3151<ul> 3152<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3153</ul> 3154<p>The date/time until which this certificate is considered valid.</p> 3155<h4><code>x509.verify(publicKey)</code><span><a class="mark" href="#x509verifypublickey" id="x509verifypublickey">#</a></span><a aria-hidden="true" class="legacy" id="crypto_x509_verify_publickey"></a></h4> 3156<div class="api_metadata"> 3157<span>Added in: v15.6.0</span> 3158</div> 3159<ul> 3160<li><code>publicKey</code> <a href="crypto.html#class-keyobject" class="type"><KeyObject></a> A public key.</li> 3161<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3162</ul> 3163<p>Verifies that this certificate was signed by the given public key. 3164Does not perform any other validation checks on the certificate.</p> 3165</section><section><h3><code>node:crypto</code> module methods and properties<span><a class="mark" href="#nodecrypto-module-methods-and-properties" id="nodecrypto-module-methods-and-properties">#</a></span><a aria-hidden="true" class="legacy" id="crypto_node_crypto_module_methods_and_properties"></a></h3> 3166<h4><code>crypto.constants</code><span><a class="mark" href="#cryptoconstants" id="cryptoconstants">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_constants"></a></h4> 3167<div class="api_metadata"> 3168<span>Added in: v6.3.0</span> 3169</div> 3170<ul> 3171<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 3172</ul> 3173<p>An object containing commonly used constants for crypto and security related 3174operations. The specific constants currently defined are described in 3175<a href="#crypto-constants">Crypto constants</a>.</p> 3176<h4><code>crypto.DEFAULT_ENCODING</code><span><a class="mark" href="#cryptodefault_encoding" id="cryptodefault_encoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_default_encoding"></a></h4> 3177<div class="api_metadata"> 3178<span>Added in: v0.9.3</span><span>Deprecated since: v10.0.0</span> 3179</div> 3180<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated</div><p></p> 3181<p>The default encoding to use for functions that can take either strings 3182or <a href="buffer.html">buffers</a>. The default value is <code>'buffer'</code>, which makes methods 3183default to <a href="buffer.html"><code>Buffer</code></a> objects.</p> 3184<p>The <code>crypto.DEFAULT_ENCODING</code> mechanism is provided for backward compatibility 3185with legacy programs that expect <code>'latin1'</code> to be the default encoding.</p> 3186<p>New applications should expect the default to be <code>'buffer'</code>.</p> 3187<p>This property is deprecated.</p> 3188<h4><code>crypto.fips</code><span><a class="mark" href="#cryptofips" id="cryptofips">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_fips"></a></h4> 3189<div class="api_metadata"> 3190<span>Added in: v6.0.0</span><span>Deprecated since: v10.0.0</span> 3191</div> 3192<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated</div><p></p> 3193<p>Property for checking and controlling whether a FIPS compliant crypto provider 3194is currently in use. Setting to true requires a FIPS build of Node.js.</p> 3195<p>This property is deprecated. Please use <code>crypto.setFips()</code> and 3196<code>crypto.getFips()</code> instead.</p> 3197<h4><code>crypto.checkPrime(candidate[, options], callback)</code><span><a class="mark" href="#cryptocheckprimecandidate-options-callback" id="cryptocheckprimecandidate-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_checkprime_candidate_options_callback"></a></h4> 3198<div class="api_metadata"> 3199<details class="changelog"><summary>History</summary> 3200<table> 3201<tbody><tr><th>Version</th><th>Changes</th></tr> 3202<tr><td>v18.0.0</td> 3203<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 3204<tr><td>v15.8.0</td> 3205<td><p><span>Added in: v15.8.0</span></p></td></tr> 3206</tbody></table> 3207</details> 3208</div> 3209<ul> 3210<li><code>candidate</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer" class="type"><SharedArrayBuffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></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/Reference/Global_Objects/BigInt" class="type"><bigint></a> 3211A possible prime encoded as a sequence of big endian octets of arbitrary 3212length.</li> 3213<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 3214<ul> 3215<li><code>checks</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of Miller-Rabin probabilistic primality 3216iterations to perform. When the value is <code>0</code> (zero), a number of checks 3217is used that yields a false positive rate of at most 2<sup>-64</sup> for 3218random input. Care must be used when selecting a number of checks. Refer 3219to the OpenSSL documentation for the <a href="https://www.openssl.org/docs/man1.1.1/man3/BN_is_prime_ex.html"><code>BN_is_prime_ex</code></a> function <code>nchecks</code> 3220options for more details. <strong>Default:</strong> <code>0</code></li> 3221</ul> 3222</li> 3223<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3224<ul> 3225<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a> Set to an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a> object if an error occurred during check.</li> 3226<li><code>result</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> if the candidate is a prime with an error 3227probability less than <code>0.25 ** options.checks</code>.</li> 3228</ul> 3229</li> 3230</ul> 3231<p>Checks the primality of the <code>candidate</code>.</p> 3232<h4><code>crypto.checkPrimeSync(candidate[, options])</code><span><a class="mark" href="#cryptocheckprimesynccandidate-options" id="cryptocheckprimesynccandidate-options">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_checkprimesync_candidate_options"></a></h4> 3233<div class="api_metadata"> 3234<span>Added in: v15.8.0</span> 3235</div> 3236<ul> 3237<li><code>candidate</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer" class="type"><SharedArrayBuffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></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/Reference/Global_Objects/BigInt" class="type"><bigint></a> 3238A possible prime encoded as a sequence of big endian octets of arbitrary 3239length.</li> 3240<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 3241<ul> 3242<li><code>checks</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of Miller-Rabin probabilistic primality 3243iterations to perform. When the value is <code>0</code> (zero), a number of checks 3244is used that yields a false positive rate of at most 2<sup>-64</sup> for 3245random input. Care must be used when selecting a number of checks. Refer 3246to the OpenSSL documentation for the <a href="https://www.openssl.org/docs/man1.1.1/man3/BN_is_prime_ex.html"><code>BN_is_prime_ex</code></a> function <code>nchecks</code> 3247options for more details. <strong>Default:</strong> <code>0</code></li> 3248</ul> 3249</li> 3250<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 candidate is a prime with an error 3251probability less than <code>0.25 ** options.checks</code>.</li> 3252</ul> 3253<p>Checks the primality of the <code>candidate</code>.</p> 3254<h4><code>crypto.createCipher(algorithm, password[, options])</code><span><a class="mark" href="#cryptocreatecipheralgorithm-password-options" id="cryptocreatecipheralgorithm-password-options">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_createcipher_algorithm_password_options"></a></h4> 3255<div class="api_metadata"> 3256<details class="changelog"><summary>History</summary> 3257<table> 3258<tbody><tr><th>Version</th><th>Changes</th></tr> 3259<tr><td>v17.9.0</td> 3260<td><p>The <code>authTagLength</code> option is now optional when using the <code>chacha20-poly1305</code> cipher and defaults to 16 bytes.</p></td></tr> 3261<tr><td>v15.0.0</td> 3262<td><p>The password argument can be an ArrayBuffer and is limited to a maximum of 2 ** 31 - 1 bytes.</p></td></tr> 3263<tr><td>v10.10.0</td> 3264<td><p>Ciphers in OCB mode are now supported.</p></td></tr> 3265<tr><td>v10.2.0</td> 3266<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> 3267<tr><td>v10.0.0</td> 3268<td><p><span>Deprecated since: v10.0.0</span></p></td></tr> 3269<tr><td>v0.1.94</td> 3270<td><p><span>Added in: v0.1.94</span></p></td></tr> 3271</tbody></table> 3272</details> 3273</div> 3274<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="#cryptocreatecipherivalgorithm-key-iv-options"><code>crypto.createCipheriv()</code></a> instead.</div><p></p> 3275<ul> 3276<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3277<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 3278<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#new-streamtransformoptions"><code>stream.transform</code> options</a></li> 3279<li>Returns: <a href="crypto.html#class-cipher" class="type"><Cipher></a></li> 3280</ul> 3281<p>Creates and returns a <code>Cipher</code> object that uses the given <code>algorithm</code> and 3282<code>password</code>.</p> 3283<p>The <code>options</code> argument controls stream behavior and is optional except when a 3284cipher in CCM or OCB mode (e.g. <code>'aes-128-ccm'</code>) is used. In that case, the 3285<code>authTagLength</code> option is required and specifies the length of the 3286authentication tag in bytes, see <a href="#ccm-mode">CCM mode</a>. In GCM mode, the <code>authTagLength</code> 3287option is not required but can be used to set the length of the authentication 3288tag that will be returned by <code>getAuthTag()</code> and defaults to 16 bytes. 3289For <code>chacha20-poly1305</code>, the <code>authTagLength</code> option defaults to 16 bytes.</p> 3290<p>The <code>algorithm</code> is dependent on OpenSSL, examples are <code>'aes192'</code>, etc. On 3291recent OpenSSL releases, <code>openssl list -cipher-algorithms</code> will 3292display the available cipher algorithms.</p> 3293<p>The <code>password</code> is used to derive the cipher key and initialization vector (IV). 3294The value must be either a <code>'latin1'</code> encoded string, a <a href="buffer.html"><code>Buffer</code></a>, a 3295<code>TypedArray</code>, or a <code>DataView</code>.</p> 3296<p><strong class="critical">This function is semantically insecure for all 3297supported ciphers and fatally flawed for ciphers in counter mode (such as CTR, 3298GCM, or CCM).</strong></p> 3299<p>The implementation of <code>crypto.createCipher()</code> derives keys using the OpenSSL 3300function <a href="https://www.openssl.org/docs/man3.0/man3/EVP_BytesToKey.html"><code>EVP_BytesToKey</code></a> with the digest algorithm set to MD5, one 3301iteration, and no salt. The lack of salt allows dictionary attacks as the same 3302password always creates the same key. The low iteration count and 3303non-cryptographically secure hash algorithm allow passwords to be tested very 3304rapidly.</p> 3305<p>In line with OpenSSL's recommendation to use a more modern algorithm instead of 3306<a href="https://www.openssl.org/docs/man3.0/man3/EVP_BytesToKey.html"><code>EVP_BytesToKey</code></a> it is recommended that developers derive a key and IV on 3307their own using <a href="#cryptoscryptpassword-salt-keylen-options-callback"><code>crypto.scrypt()</code></a> and to use <a href="#cryptocreatecipherivalgorithm-key-iv-options"><code>crypto.createCipheriv()</code></a> 3308to create the <code>Cipher</code> object. Users should not use ciphers with counter mode 3309(e.g. CTR, GCM, or CCM) in <code>crypto.createCipher()</code>. A warning is emitted when 3310they are used in order to avoid the risk of IV reuse that causes 3311vulnerabilities. For the case when IV is reused in GCM, see <a href="https://github.com/nonce-disrespect/nonce-disrespect">Nonce-Disrespecting 3312Adversaries</a> for details.</p> 3313<h4><code>crypto.createCipheriv(algorithm, key, iv[, options])</code><span><a class="mark" href="#cryptocreatecipherivalgorithm-key-iv-options" id="cryptocreatecipherivalgorithm-key-iv-options">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_createcipheriv_algorithm_key_iv_options"></a></h4> 3314<div class="api_metadata"> 3315<details class="changelog"><summary>History</summary> 3316<table> 3317<tbody><tr><th>Version</th><th>Changes</th></tr> 3318<tr><td>v17.9.0</td> 3319<td><p>The <code>authTagLength</code> option is now optional when using the <code>chacha20-poly1305</code> cipher and defaults to 16 bytes.</p></td></tr> 3320<tr><td>v15.0.0</td> 3321<td><p>The password and iv arguments can be an ArrayBuffer and are each limited to a maximum of 2 ** 31 - 1 bytes.</p></td></tr> 3322<tr><td>v11.6.0</td> 3323<td><p>The <code>key</code> argument can now be a <code>KeyObject</code>.</p></td></tr> 3324<tr><td>v11.2.0, v10.17.0</td> 3325<td><p>The cipher <code>chacha20-poly1305</code> (the IETF variant of ChaCha20-Poly1305) is now supported.</p></td></tr> 3326<tr><td>v10.10.0</td> 3327<td><p>Ciphers in OCB mode are now supported.</p></td></tr> 3328<tr><td>v10.2.0</td> 3329<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> 3330<tr><td>v9.9.0</td> 3331<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> 3332<tr><td>v0.1.94</td> 3333<td><p><span>Added in: v0.1.94</span></p></td></tr> 3334</tbody></table> 3335</details> 3336</div> 3337<ul> 3338<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3339<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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#class-keyobject" class="type"><KeyObject></a> | <a href="webcrypto.html#class-cryptokey" class="type"><CryptoKey></a></li> 3340<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 3341<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#new-streamtransformoptions"><code>stream.transform</code> options</a></li> 3342<li>Returns: <a href="crypto.html#class-cipher" class="type"><Cipher></a></li> 3343</ul> 3344<p>Creates and returns a <code>Cipher</code> object, with the given <code>algorithm</code>, <code>key</code> and 3345initialization vector (<code>iv</code>).</p> 3346<p>The <code>options</code> argument controls stream behavior and is optional except when a 3347cipher in CCM or OCB mode (e.g. <code>'aes-128-ccm'</code>) is used. In that case, the 3348<code>authTagLength</code> option is required and specifies the length of the 3349authentication tag in bytes, see <a href="#ccm-mode">CCM mode</a>. In GCM mode, the <code>authTagLength</code> 3350option is not required but can be used to set the length of the authentication 3351tag that will be returned by <code>getAuthTag()</code> and defaults to 16 bytes. 3352For <code>chacha20-poly1305</code>, the <code>authTagLength</code> option defaults to 16 bytes.</p> 3353<p>The <code>algorithm</code> is dependent on OpenSSL, examples are <code>'aes192'</code>, etc. On 3354recent OpenSSL releases, <code>openssl list -cipher-algorithms</code> will 3355display the available cipher algorithms.</p> 3356<p>The <code>key</code> is the raw key used by the <code>algorithm</code> and <code>iv</code> is an 3357<a href="https://en.wikipedia.org/wiki/Initialization_vector">initialization vector</a>. Both arguments must be <code>'utf8'</code> encoded strings, 3358<a href="buffer.html">Buffers</a>, <code>TypedArray</code>, or <code>DataView</code>s. The <code>key</code> may optionally be 3359a <a href="#class-keyobject"><code>KeyObject</code></a> of type <code>secret</code>. If the cipher does not need 3360an initialization vector, <code>iv</code> may be <code>null</code>.</p> 3361<p>When passing strings for <code>key</code> or <code>iv</code>, please consider 3362<a href="#using-strings-as-inputs-to-cryptographic-apis">caveats when using strings as inputs to cryptographic APIs</a>.</p> 3363<p>Initialization vectors should be unpredictable and unique; ideally, they will be 3364cryptographically random. They do not have to be secret: IVs are typically just 3365added to ciphertext messages unencrypted. It may sound contradictory that 3366something has to be unpredictable and unique, but does not have to be secret; 3367remember that an attacker must not be able to predict ahead of time what a 3368given IV will be.</p> 3369<h4><code>crypto.createDecipher(algorithm, password[, options])</code><span><a class="mark" href="#cryptocreatedecipheralgorithm-password-options" id="cryptocreatedecipheralgorithm-password-options">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_createdecipher_algorithm_password_options"></a></h4> 3370<div class="api_metadata"> 3371<details class="changelog"><summary>History</summary> 3372<table> 3373<tbody><tr><th>Version</th><th>Changes</th></tr> 3374<tr><td>v17.9.0</td> 3375<td><p>The <code>authTagLength</code> option is now optional when using the <code>chacha20-poly1305</code> cipher and defaults to 16 bytes.</p></td></tr> 3376<tr><td>v10.10.0</td> 3377<td><p>Ciphers in OCB mode are now supported.</p></td></tr> 3378<tr><td>v10.0.0</td> 3379<td><p><span>Deprecated since: v10.0.0</span></p></td></tr> 3380<tr><td>v0.1.94</td> 3381<td><p><span>Added in: v0.1.94</span></p></td></tr> 3382</tbody></table> 3383</details> 3384</div> 3385<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="#cryptocreatedecipherivalgorithm-key-iv-options"><code>crypto.createDecipheriv()</code></a> instead.</div><p></p> 3386<ul> 3387<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3388<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 3389<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#new-streamtransformoptions"><code>stream.transform</code> options</a></li> 3390<li>Returns: <a href="crypto.html#class-decipher" class="type"><Decipher></a></li> 3391</ul> 3392<p>Creates and returns a <code>Decipher</code> object that uses the given <code>algorithm</code> and 3393<code>password</code> (key).</p> 3394<p>The <code>options</code> argument controls stream behavior and is optional except when a 3395cipher in CCM or OCB mode (e.g. <code>'aes-128-ccm'</code>) is used. In that case, the 3396<code>authTagLength</code> option is required and specifies the length of the 3397authentication tag in bytes, see <a href="#ccm-mode">CCM mode</a>. 3398For <code>chacha20-poly1305</code>, the <code>authTagLength</code> option defaults to 16 bytes.</p> 3399<p><strong class="critical">This function is semantically insecure for all 3400supported ciphers and fatally flawed for ciphers in counter mode (such as CTR, 3401GCM, or CCM).</strong></p> 3402<p>The implementation of <code>crypto.createDecipher()</code> derives keys using the OpenSSL 3403function <a href="https://www.openssl.org/docs/man3.0/man3/EVP_BytesToKey.html"><code>EVP_BytesToKey</code></a> with the digest algorithm set to MD5, one 3404iteration, and no salt. The lack of salt allows dictionary attacks as the same 3405password always creates the same key. The low iteration count and 3406non-cryptographically secure hash algorithm allow passwords to be tested very 3407rapidly.</p> 3408<p>In line with OpenSSL's recommendation to use a more modern algorithm instead of 3409<a href="https://www.openssl.org/docs/man3.0/man3/EVP_BytesToKey.html"><code>EVP_BytesToKey</code></a> it is recommended that developers derive a key and IV on 3410their own using <a href="#cryptoscryptpassword-salt-keylen-options-callback"><code>crypto.scrypt()</code></a> and to use <a href="#cryptocreatedecipherivalgorithm-key-iv-options"><code>crypto.createDecipheriv()</code></a> 3411to create the <code>Decipher</code> object.</p> 3412<h4><code>crypto.createDecipheriv(algorithm, key, iv[, options])</code><span><a class="mark" href="#cryptocreatedecipherivalgorithm-key-iv-options" id="cryptocreatedecipherivalgorithm-key-iv-options">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_createdecipheriv_algorithm_key_iv_options"></a></h4> 3413<div class="api_metadata"> 3414<details class="changelog"><summary>History</summary> 3415<table> 3416<tbody><tr><th>Version</th><th>Changes</th></tr> 3417<tr><td>v17.9.0</td> 3418<td><p>The <code>authTagLength</code> option is now optional when using the <code>chacha20-poly1305</code> cipher and defaults to 16 bytes.</p></td></tr> 3419<tr><td>v11.6.0</td> 3420<td><p>The <code>key</code> argument can now be a <code>KeyObject</code>.</p></td></tr> 3421<tr><td>v11.2.0, v10.17.0</td> 3422<td><p>The cipher <code>chacha20-poly1305</code> (the IETF variant of ChaCha20-Poly1305) is now supported.</p></td></tr> 3423<tr><td>v10.10.0</td> 3424<td><p>Ciphers in OCB mode are now supported.</p></td></tr> 3425<tr><td>v10.2.0</td> 3426<td><p>The <code>authTagLength</code> option can now be used to restrict accepted GCM authentication tag lengths.</p></td></tr> 3427<tr><td>v9.9.0</td> 3428<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> 3429<tr><td>v0.1.94</td> 3430<td><p><span>Added in: v0.1.94</span></p></td></tr> 3431</tbody></table> 3432</details> 3433</div> 3434<ul> 3435<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3436<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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#class-keyobject" class="type"><KeyObject></a> | <a href="webcrypto.html#class-cryptokey" class="type"><CryptoKey></a></li> 3437<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 3438<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#new-streamtransformoptions"><code>stream.transform</code> options</a></li> 3439<li>Returns: <a href="crypto.html#class-decipher" class="type"><Decipher></a></li> 3440</ul> 3441<p>Creates and returns a <code>Decipher</code> object that uses the given <code>algorithm</code>, <code>key</code> 3442and initialization vector (<code>iv</code>).</p> 3443<p>The <code>options</code> argument controls stream behavior and is optional except when a 3444cipher in CCM or OCB mode (e.g. <code>'aes-128-ccm'</code>) is used. In that case, the 3445<code>authTagLength</code> option is required and specifies the length of the 3446authentication tag in bytes, see <a href="#ccm-mode">CCM mode</a>. In GCM mode, the <code>authTagLength</code> 3447option is not required but can be used to restrict accepted authentication tags 3448to those with the specified length. 3449For <code>chacha20-poly1305</code>, the <code>authTagLength</code> option defaults to 16 bytes.</p> 3450<p>The <code>algorithm</code> is dependent on OpenSSL, examples are <code>'aes192'</code>, etc. On 3451recent OpenSSL releases, <code>openssl list -cipher-algorithms</code> will 3452display the available cipher algorithms.</p> 3453<p>The <code>key</code> is the raw key used by the <code>algorithm</code> and <code>iv</code> is an 3454<a href="https://en.wikipedia.org/wiki/Initialization_vector">initialization vector</a>. Both arguments must be <code>'utf8'</code> encoded strings, 3455<a href="buffer.html">Buffers</a>, <code>TypedArray</code>, or <code>DataView</code>s. The <code>key</code> may optionally be 3456a <a href="#class-keyobject"><code>KeyObject</code></a> of type <code>secret</code>. If the cipher does not need 3457an initialization vector, <code>iv</code> may be <code>null</code>.</p> 3458<p>When passing strings for <code>key</code> or <code>iv</code>, please consider 3459<a href="#using-strings-as-inputs-to-cryptographic-apis">caveats when using strings as inputs to cryptographic APIs</a>.</p> 3460<p>Initialization vectors should be unpredictable and unique; ideally, they will be 3461cryptographically random. They do not have to be secret: IVs are typically just 3462added to ciphertext messages unencrypted. It may sound contradictory that 3463something has to be unpredictable and unique, but does not have to be secret; 3464remember that an attacker must not be able to predict ahead of time what a given 3465IV will be.</p> 3466<h4><code>crypto.createDiffieHellman(prime[, primeEncoding][, generator][, generatorEncoding])</code><span><a class="mark" href="#cryptocreatediffiehellmanprime-primeencoding-generator-generatorencoding" id="cryptocreatediffiehellmanprime-primeencoding-generator-generatorencoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_creatediffiehellman_prime_primeencoding_generator_generatorencoding"></a></h4> 3467<div class="api_metadata"> 3468<details class="changelog"><summary>History</summary> 3469<table> 3470<tbody><tr><th>Version</th><th>Changes</th></tr> 3471<tr><td>v8.0.0</td> 3472<td><p>The <code>prime</code> argument can be any <code>TypedArray</code> or <code>DataView</code> now.</p></td></tr> 3473<tr><td>v8.0.0</td> 3474<td><p>The <code>prime</code> argument can be a <code>Uint8Array</code> now.</p></td></tr> 3475<tr><td>v6.0.0</td> 3476<td><p>The default for the encoding parameters changed from <code>binary</code> to <code>utf8</code>.</p></td></tr> 3477<tr><td>v0.11.12</td> 3478<td><p><span>Added in: v0.11.12</span></p></td></tr> 3479</tbody></table> 3480</details> 3481</div> 3482<ul> 3483<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 3484<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#buffers-and-character-encodings">encoding</a> of the <code>prime</code> string.</li> 3485<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 3486<strong>Default:</strong> <code>2</code></li> 3487<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#buffers-and-character-encodings">encoding</a> of the <code>generator</code> string.</li> 3488<li>Returns: <a href="crypto.html#class-diffiehellman" class="type"><DiffieHellman></a></li> 3489</ul> 3490<p>Creates a <code>DiffieHellman</code> key exchange object using the supplied <code>prime</code> and an 3491optional specific <code>generator</code>.</p> 3492<p>The <code>generator</code> argument can be a number, string, or <a href="buffer.html"><code>Buffer</code></a>. If 3493<code>generator</code> is not specified, the value <code>2</code> is used.</p> 3494<p>If <code>primeEncoding</code> is specified, <code>prime</code> is expected to be a string; otherwise 3495a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or <code>DataView</code> is expected.</p> 3496<p>If <code>generatorEncoding</code> is specified, <code>generator</code> is expected to be a string; 3497otherwise a number, <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or <code>DataView</code> is expected.</p> 3498<h4><code>crypto.createDiffieHellman(primeLength[, generator])</code><span><a class="mark" href="#cryptocreatediffiehellmanprimelength-generator" id="cryptocreatediffiehellmanprimelength-generator">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_creatediffiehellman_primelength_generator"></a></h4> 3499<div class="api_metadata"> 3500<span>Added in: v0.5.0</span> 3501</div> 3502<ul> 3503<li><code>primeLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 3504<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> 3505<li>Returns: <a href="crypto.html#class-diffiehellman" class="type"><DiffieHellman></a></li> 3506</ul> 3507<p>Creates a <code>DiffieHellman</code> key exchange object and generates a prime of 3508<code>primeLength</code> bits using an optional specific numeric <code>generator</code>. 3509If <code>generator</code> is not specified, the value <code>2</code> is used.</p> 3510<h4><code>crypto.createDiffieHellmanGroup(name)</code><span><a class="mark" href="#cryptocreatediffiehellmangroupname" id="cryptocreatediffiehellmangroupname">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_creatediffiehellmangroup_name"></a></h4> 3511<div class="api_metadata"> 3512<span>Added in: v0.9.3</span> 3513</div> 3514<ul> 3515<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3516<li>Returns: <a href="crypto.html#class-diffiehellmangroup" class="type"><DiffieHellmanGroup></a></li> 3517</ul> 3518<p>An alias for <a href="#cryptogetdiffiehellmangroupname"><code>crypto.getDiffieHellman()</code></a></p> 3519<h4><code>crypto.createECDH(curveName)</code><span><a class="mark" href="#cryptocreateecdhcurvename" id="cryptocreateecdhcurvename">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_createecdh_curvename"></a></h4> 3520<div class="api_metadata"> 3521<span>Added in: v0.11.14</span> 3522</div> 3523<ul> 3524<li><code>curveName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3525<li>Returns: <a href="crypto.html#class-ecdh" class="type"><ECDH></a></li> 3526</ul> 3527<p>Creates an Elliptic Curve Diffie-Hellman (<code>ECDH</code>) key exchange object using a 3528predefined curve specified by the <code>curveName</code> string. Use 3529<a href="#cryptogetcurves"><code>crypto.getCurves()</code></a> to obtain a list of available curve names. On recent 3530OpenSSL releases, <code>openssl ecparam -list_curves</code> will also display the name 3531and description of each available elliptic curve.</p> 3532<h4><code>crypto.createHash(algorithm[, options])</code><span><a class="mark" href="#cryptocreatehashalgorithm-options" id="cryptocreatehashalgorithm-options">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_createhash_algorithm_options"></a></h4> 3533<div class="api_metadata"> 3534<details class="changelog"><summary>History</summary> 3535<table> 3536<tbody><tr><th>Version</th><th>Changes</th></tr> 3537<tr><td>v12.8.0</td> 3538<td><p>The <code>outputLength</code> option was added for XOF hash functions.</p></td></tr> 3539<tr><td>v0.1.92</td> 3540<td><p><span>Added in: v0.1.92</span></p></td></tr> 3541</tbody></table> 3542</details> 3543</div> 3544<ul> 3545<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3546<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#new-streamtransformoptions"><code>stream.transform</code> options</a></li> 3547<li>Returns: <a href="crypto.html#class-hash" class="type"><Hash></a></li> 3548</ul> 3549<p>Creates and returns a <code>Hash</code> object that can be used to generate hash digests 3550using the given <code>algorithm</code>. Optional <code>options</code> argument controls stream 3551behavior. For XOF hash functions such as <code>'shake256'</code>, the <code>outputLength</code> option 3552can be used to specify the desired output length in bytes.</p> 3553<p>The <code>algorithm</code> is dependent on the available algorithms supported by the 3554version of OpenSSL on the platform. Examples are <code>'sha256'</code>, <code>'sha512'</code>, etc. 3555On recent releases of OpenSSL, <code>openssl list -digest-algorithms</code> will 3556display the available digest algorithms.</p> 3557<p>Example: generating the sha256 sum of a file</p> 3558 3559<pre class="with-19-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { 3560 createReadStream, 3561} <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 3562<span class="hljs-keyword">import</span> { argv } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>; 3563<span class="hljs-keyword">const</span> { 3564 createHash, 3565} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 3566 3567<span class="hljs-keyword">const</span> filename = argv[<span class="hljs-number">2</span>]; 3568 3569<span class="hljs-keyword">const</span> hash = <span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>); 3570 3571<span class="hljs-keyword">const</span> input = <span class="hljs-title function_">createReadStream</span>(filename); 3572input.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> { 3573 <span class="hljs-comment">// Only one element is going to be produced by the</span> 3574 <span class="hljs-comment">// hash stream.</span> 3575 <span class="hljs-keyword">const</span> data = input.<span class="hljs-title function_">read</span>(); 3576 <span class="hljs-keyword">if</span> (data) 3577 hash.<span class="hljs-title function_">update</span>(data); 3578 <span class="hljs-keyword">else</span> { 3579 <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>); 3580 } 3581});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 3582 createReadStream, 3583} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 3584<span class="hljs-keyword">const</span> { 3585 createHash, 3586} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 3587<span class="hljs-keyword">const</span> { argv } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:process'</span>); 3588 3589<span class="hljs-keyword">const</span> filename = argv[<span class="hljs-number">2</span>]; 3590 3591<span class="hljs-keyword">const</span> hash = <span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>); 3592 3593<span class="hljs-keyword">const</span> input = <span class="hljs-title function_">createReadStream</span>(filename); 3594input.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> { 3595 <span class="hljs-comment">// Only one element is going to be produced by the</span> 3596 <span class="hljs-comment">// hash stream.</span> 3597 <span class="hljs-keyword">const</span> data = input.<span class="hljs-title function_">read</span>(); 3598 <span class="hljs-keyword">if</span> (data) 3599 hash.<span class="hljs-title function_">update</span>(data); 3600 <span class="hljs-keyword">else</span> { 3601 <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>); 3602 } 3603});</code><button class="copy-button">copy</button></pre> 3604<h4><code>crypto.createHmac(algorithm, key[, options])</code><span><a class="mark" href="#cryptocreatehmacalgorithm-key-options" id="cryptocreatehmacalgorithm-key-options">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_createhmac_algorithm_key_options"></a></h4> 3605<div class="api_metadata"> 3606<details class="changelog"><summary>History</summary> 3607<table> 3608<tbody><tr><th>Version</th><th>Changes</th></tr> 3609<tr><td>v15.0.0</td> 3610<td><p>The key can also be an ArrayBuffer or CryptoKey. The encoding option was added. The key cannot contain more than 2 ** 32 - 1 bytes.</p></td></tr> 3611<tr><td>v11.6.0</td> 3612<td><p>The <code>key</code> argument can now be a <code>KeyObject</code>.</p></td></tr> 3613<tr><td>v0.1.94</td> 3614<td><p><span>Added in: v0.1.94</span></p></td></tr> 3615</tbody></table> 3616</details> 3617</div> 3618<ul> 3619<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3620<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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#class-keyobject" class="type"><KeyObject></a> | <a href="webcrypto.html#class-cryptokey" class="type"><CryptoKey></a></li> 3621<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#new-streamtransformoptions"><code>stream.transform</code> options</a> 3622<ul> 3623<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The string encoding to use when <code>key</code> is a string.</li> 3624</ul> 3625</li> 3626<li>Returns: <a href="crypto.html#class-hmac" class="type"><Hmac></a></li> 3627</ul> 3628<p>Creates and returns an <code>Hmac</code> object that uses the given <code>algorithm</code> and <code>key</code>. 3629Optional <code>options</code> argument controls stream behavior.</p> 3630<p>The <code>algorithm</code> is dependent on the available algorithms supported by the 3631version of OpenSSL on the platform. Examples are <code>'sha256'</code>, <code>'sha512'</code>, etc. 3632On recent releases of OpenSSL, <code>openssl list -digest-algorithms</code> will 3633display the available digest algorithms.</p> 3634<p>The <code>key</code> is the HMAC key used to generate the cryptographic HMAC hash. If it is 3635a <a href="#class-keyobject"><code>KeyObject</code></a>, its type must be <code>secret</code>. If it is a string, please consider 3636<a href="#using-strings-as-inputs-to-cryptographic-apis">caveats when using strings as inputs to cryptographic APIs</a>. If it was 3637obtained from a cryptographically secure source of entropy, such as 3638<a href="#cryptorandombytessize-callback"><code>crypto.randomBytes()</code></a> or <a href="#cryptogeneratekeytype-options-callback"><code>crypto.generateKey()</code></a>, its length should not 3639exceed the block size of <code>algorithm</code> (e.g., 512 bits for SHA-256).</p> 3640<p>Example: generating the sha256 HMAC of a file</p> 3641 3642<pre class="with-19-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { 3643 createReadStream, 3644} <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 3645<span class="hljs-keyword">import</span> { argv } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>; 3646<span class="hljs-keyword">const</span> { 3647 createHmac, 3648} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 3649 3650<span class="hljs-keyword">const</span> filename = argv[<span class="hljs-number">2</span>]; 3651 3652<span class="hljs-keyword">const</span> hmac = <span class="hljs-title function_">createHmac</span>(<span class="hljs-string">'sha256'</span>, <span class="hljs-string">'a secret'</span>); 3653 3654<span class="hljs-keyword">const</span> input = <span class="hljs-title function_">createReadStream</span>(filename); 3655input.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> { 3656 <span class="hljs-comment">// Only one element is going to be produced by the</span> 3657 <span class="hljs-comment">// hash stream.</span> 3658 <span class="hljs-keyword">const</span> data = input.<span class="hljs-title function_">read</span>(); 3659 <span class="hljs-keyword">if</span> (data) 3660 hmac.<span class="hljs-title function_">update</span>(data); 3661 <span class="hljs-keyword">else</span> { 3662 <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>); 3663 } 3664});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 3665 createReadStream, 3666} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 3667<span class="hljs-keyword">const</span> { 3668 createHmac, 3669} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 3670<span class="hljs-keyword">const</span> { argv } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:process'</span>); 3671 3672<span class="hljs-keyword">const</span> filename = argv[<span class="hljs-number">2</span>]; 3673 3674<span class="hljs-keyword">const</span> hmac = <span class="hljs-title function_">createHmac</span>(<span class="hljs-string">'sha256'</span>, <span class="hljs-string">'a secret'</span>); 3675 3676<span class="hljs-keyword">const</span> input = <span class="hljs-title function_">createReadStream</span>(filename); 3677input.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> { 3678 <span class="hljs-comment">// Only one element is going to be produced by the</span> 3679 <span class="hljs-comment">// hash stream.</span> 3680 <span class="hljs-keyword">const</span> data = input.<span class="hljs-title function_">read</span>(); 3681 <span class="hljs-keyword">if</span> (data) 3682 hmac.<span class="hljs-title function_">update</span>(data); 3683 <span class="hljs-keyword">else</span> { 3684 <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>); 3685 } 3686});</code><button class="copy-button">copy</button></pre> 3687<h4><code>crypto.createPrivateKey(key)</code><span><a class="mark" href="#cryptocreateprivatekeykey" id="cryptocreateprivatekeykey">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_createprivatekey_key"></a></h4> 3688<div class="api_metadata"> 3689<details class="changelog"><summary>History</summary> 3690<table> 3691<tbody><tr><th>Version</th><th>Changes</th></tr> 3692<tr><td>v15.12.0</td> 3693<td><p>The key can also be a JWK object.</p></td></tr> 3694<tr><td>v15.0.0</td> 3695<td><p>The key can also be an ArrayBuffer. The encoding option was added. The key cannot contain more than 2 ** 32 - 1 bytes.</p></td></tr> 3696<tr><td>v11.6.0</td> 3697<td><p><span>Added in: v11.6.0</span></p></td></tr> 3698</tbody></table> 3699</details> 3700</div> 3701<!--lint disable maximum-line-length remark-lint--> 3702<ul> 3703<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 3704<ul> 3705<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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/Reference/Global_Objects/Object" class="type"><Object></a> The key 3706material, either in PEM, DER, or JWK format.</li> 3707<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>, <code>'der'</code>, or '<code>'jwk'</code>. 3708<strong>Default:</strong> <code>'pem'</code>.</li> 3709<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 3710required only if the <code>format</code> is <code>'der'</code> and ignored otherwise.</li> 3711<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#class-buffer" class="type"><Buffer></a> The passphrase to use for decryption.</li> 3712<li><code>encoding</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The string encoding to use when <code>key</code> is a string.</li> 3713</ul> 3714</li> 3715<li>Returns: <a href="crypto.html#class-keyobject" class="type"><KeyObject></a></li> 3716</ul> 3717<!--lint enable maximum-line-length remark-lint--> 3718<p>Creates and returns a new key object containing a private key. If <code>key</code> is a 3719string or <code>Buffer</code>, <code>format</code> is assumed to be <code>'pem'</code>; otherwise, <code>key</code> 3720must be an object with the properties described above.</p> 3721<p>If the private key is encrypted, a <code>passphrase</code> must be specified. The length 3722of the passphrase is limited to 1024 bytes.</p> 3723<h4><code>crypto.createPublicKey(key)</code><span><a class="mark" href="#cryptocreatepublickeykey" id="cryptocreatepublickeykey">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_createpublickey_key"></a></h4> 3724<div class="api_metadata"> 3725<details class="changelog"><summary>History</summary> 3726<table> 3727<tbody><tr><th>Version</th><th>Changes</th></tr> 3728<tr><td>v15.12.0</td> 3729<td><p>The key can also be a JWK object.</p></td></tr> 3730<tr><td>v15.0.0</td> 3731<td><p>The key can also be an ArrayBuffer. The encoding option was added. The key cannot contain more than 2 ** 32 - 1 bytes.</p></td></tr> 3732<tr><td>v11.13.0</td> 3733<td><p>The <code>key</code> argument can now be a <code>KeyObject</code> with type <code>private</code>.</p></td></tr> 3734<tr><td>v11.7.0</td> 3735<td><p>The <code>key</code> argument can now be a private key.</p></td></tr> 3736<tr><td>v11.6.0</td> 3737<td><p><span>Added in: v11.6.0</span></p></td></tr> 3738</tbody></table> 3739</details> 3740</div> 3741<!--lint disable maximum-line-length remark-lint--> 3742<ul> 3743<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 3744<ul> 3745<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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/Reference/Global_Objects/Object" class="type"><Object></a> The key 3746material, either in PEM, DER, or JWK format.</li> 3747<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>, <code>'der'</code>, or <code>'jwk'</code>. 3748<strong>Default:</strong> <code>'pem'</code>.</li> 3749<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 3750required only if the <code>format</code> is <code>'der'</code> and ignored otherwise.</li> 3751<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The string encoding to use when <code>key</code> is a string.</li> 3752</ul> 3753</li> 3754<li>Returns: <a href="crypto.html#class-keyobject" class="type"><KeyObject></a></li> 3755</ul> 3756<!--lint enable maximum-line-length remark-lint--> 3757<p>Creates and returns a new key object containing a public key. If <code>key</code> is a 3758string or <code>Buffer</code>, <code>format</code> is assumed to be <code>'pem'</code>; if <code>key</code> is a <code>KeyObject</code> 3759with type <code>'private'</code>, the public key is derived from the given private key; 3760otherwise, <code>key</code> must be an object with the properties described above.</p> 3761<p>If the format is <code>'pem'</code>, the <code>'key'</code> may also be an X.509 certificate.</p> 3762<p>Because public keys can be derived from private keys, a private key may be 3763passed instead of a public key. In that case, this function behaves as if 3764<a href="#cryptocreateprivatekeykey"><code>crypto.createPrivateKey()</code></a> had been called, except that the type of the 3765returned <code>KeyObject</code> will be <code>'public'</code> and that the private key cannot be 3766extracted from the returned <code>KeyObject</code>. Similarly, if a <code>KeyObject</code> with type 3767<code>'private'</code> is given, a new <code>KeyObject</code> with type <code>'public'</code> will be returned 3768and it will be impossible to extract the private key from the returned object.</p> 3769<h4><code>crypto.createSecretKey(key[, encoding])</code><span><a class="mark" href="#cryptocreatesecretkeykey-encoding" id="cryptocreatesecretkeykey-encoding">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_createsecretkey_key_encoding"></a></h4> 3770<div class="api_metadata"> 3771<details class="changelog"><summary>History</summary> 3772<table> 3773<tbody><tr><th>Version</th><th>Changes</th></tr> 3774<tr><td>v18.8.0</td> 3775<td><p>The key can now be zero-length.</p></td></tr> 3776<tr><td>v15.0.0</td> 3777<td><p>The key can also be an ArrayBuffer or string. The encoding argument was added. The key cannot contain more than 2 ** 32 - 1 bytes.</p></td></tr> 3778<tr><td>v11.6.0</td> 3779<td><p><span>Added in: v11.6.0</span></p></td></tr> 3780</tbody></table> 3781</details> 3782</div> 3783<ul> 3784<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 3785<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The string encoding when <code>key</code> is a string.</li> 3786<li>Returns: <a href="crypto.html#class-keyobject" class="type"><KeyObject></a></li> 3787</ul> 3788<p>Creates and returns a new key object containing a secret key for symmetric 3789encryption or <code>Hmac</code>.</p> 3790<h4><code>crypto.createSign(algorithm[, options])</code><span><a class="mark" href="#cryptocreatesignalgorithm-options" id="cryptocreatesignalgorithm-options">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_createsign_algorithm_options"></a></h4> 3791<div class="api_metadata"> 3792<span>Added in: v0.1.92</span> 3793</div> 3794<ul> 3795<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3796<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#new-streamwritableoptions"><code>stream.Writable</code> options</a></li> 3797<li>Returns: <a href="crypto.html#class-sign" class="type"><Sign></a></li> 3798</ul> 3799<p>Creates and returns a <code>Sign</code> object that uses the given <code>algorithm</code>. Use 3800<a href="#cryptogethashes"><code>crypto.getHashes()</code></a> to obtain the names of the available digest algorithms. 3801Optional <code>options</code> argument controls the <code>stream.Writable</code> behavior.</p> 3802<p>In some cases, a <code>Sign</code> instance can be created using the name of a signature 3803algorithm, such as <code>'RSA-SHA256'</code>, instead of a digest algorithm. This will use 3804the corresponding digest algorithm. This does not work for all signature 3805algorithms, such as <code>'ecdsa-with-SHA256'</code>, so it is best to always use digest 3806algorithm names.</p> 3807<h4><code>crypto.createVerify(algorithm[, options])</code><span><a class="mark" href="#cryptocreateverifyalgorithm-options" id="cryptocreateverifyalgorithm-options">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_createverify_algorithm_options"></a></h4> 3808<div class="api_metadata"> 3809<span>Added in: v0.1.92</span> 3810</div> 3811<ul> 3812<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3813<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#new-streamwritableoptions"><code>stream.Writable</code> options</a></li> 3814<li>Returns: <a href="crypto.html#class-verify" class="type"><Verify></a></li> 3815</ul> 3816<p>Creates and returns a <code>Verify</code> object that uses the given algorithm. 3817Use <a href="#cryptogethashes"><code>crypto.getHashes()</code></a> to obtain an array of names of the available 3818signing algorithms. Optional <code>options</code> argument controls the 3819<code>stream.Writable</code> behavior.</p> 3820<p>In some cases, a <code>Verify</code> instance can be created using the name of a signature 3821algorithm, such as <code>'RSA-SHA256'</code>, instead of a digest algorithm. This will use 3822the corresponding digest algorithm. This does not work for all signature 3823algorithms, such as <code>'ecdsa-with-SHA256'</code>, so it is best to always use digest 3824algorithm names.</p> 3825<h4><code>crypto.diffieHellman(options)</code><span><a class="mark" href="#cryptodiffiehellmanoptions" id="cryptodiffiehellmanoptions">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_diffiehellman_options"></a></h4> 3826<div class="api_metadata"> 3827<span>Added in: v13.9.0, v12.17.0</span> 3828</div> 3829<ul> 3830<li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 3831<ul> 3832<li><code>privateKey</code>: <a href="crypto.html#class-keyobject" class="type"><KeyObject></a></li> 3833<li><code>publicKey</code>: <a href="crypto.html#class-keyobject" class="type"><KeyObject></a></li> 3834</ul> 3835</li> 3836<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 3837</ul> 3838<p>Computes the Diffie-Hellman secret based on a <code>privateKey</code> and a <code>publicKey</code>. 3839Both keys must have the same <code>asymmetricKeyType</code>, which must be one of <code>'dh'</code> 3840(for Diffie-Hellman), <code>'ec'</code> (for ECDH), <code>'x448'</code>, or <code>'x25519'</code> (for ECDH-ES).</p> 3841<h4><code>crypto.generateKey(type, options, callback)</code><span><a class="mark" href="#cryptogeneratekeytype-options-callback" id="cryptogeneratekeytype-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_generatekey_type_options_callback"></a></h4> 3842<div class="api_metadata"> 3843<details class="changelog"><summary>History</summary> 3844<table> 3845<tbody><tr><th>Version</th><th>Changes</th></tr> 3846<tr><td>v18.0.0</td> 3847<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 3848<tr><td>v15.0.0</td> 3849<td><p><span>Added in: v15.0.0</span></p></td></tr> 3850</tbody></table> 3851</details> 3852</div> 3853<ul> 3854<li><code>type</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The intended use of the generated secret key. Currently 3855accepted values are <code>'hmac'</code> and <code>'aes'</code>.</li> 3856<li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 3857<ul> 3858<li><code>length</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The bit length of the key to generate. This must be a 3859value greater than 0. 3860<ul> 3861<li>If <code>type</code> is <code>'hmac'</code>, the minimum is 8, and the maximum length is 38622<sup>31</sup>-1. If the value is not a multiple of 8, the generated 3863key will be truncated to <code>Math.floor(length / 8)</code>.</li> 3864<li>If <code>type</code> is <code>'aes'</code>, the length must be one of <code>128</code>, <code>192</code>, or <code>256</code>.</li> 3865</ul> 3866</li> 3867</ul> 3868</li> 3869<li><code>callback</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3870<ul> 3871<li><code>err</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3872<li><code>key</code>: <a href="crypto.html#class-keyobject" class="type"><KeyObject></a></li> 3873</ul> 3874</li> 3875</ul> 3876<p>Asynchronously generates a new random secret key of the given <code>length</code>. The 3877<code>type</code> will determine which validations will be performed on the <code>length</code>.</p> 3878 3879<pre class="with-14-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">const</span> { 3880 generateKey, 3881} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 3882 3883<span class="hljs-title function_">generateKey</span>(<span class="hljs-string">'hmac'</span>, { <span class="hljs-attr">length</span>: <span class="hljs-number">512</span> }, <span class="hljs-function">(<span class="hljs-params">err, key</span>) =></span> { 3884 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 3885 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(key.<span class="hljs-title function_">export</span>().<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>)); <span class="hljs-comment">// 46e..........620</span> 3886});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 3887 generateKey, 3888} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 3889 3890<span class="hljs-title function_">generateKey</span>(<span class="hljs-string">'hmac'</span>, { <span class="hljs-attr">length</span>: <span class="hljs-number">512</span> }, <span class="hljs-function">(<span class="hljs-params">err, key</span>) =></span> { 3891 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 3892 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(key.<span class="hljs-title function_">export</span>().<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>)); <span class="hljs-comment">// 46e..........620</span> 3893});</code><button class="copy-button">copy</button></pre> 3894<p>The size of a generated HMAC key should not exceed the block size of the 3895underlying hash function. See <a href="#cryptocreatehmacalgorithm-key-options"><code>crypto.createHmac()</code></a> for more information.</p> 3896<h4><code>crypto.generateKeyPair(type, options, callback)</code><span><a class="mark" href="#cryptogeneratekeypairtype-options-callback" id="cryptogeneratekeypairtype-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_generatekeypair_type_options_callback"></a></h4> 3897<div class="api_metadata"> 3898<details class="changelog"><summary>History</summary> 3899<table> 3900<tbody><tr><th>Version</th><th>Changes</th></tr> 3901<tr><td>v18.0.0</td> 3902<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 3903<tr><td>v16.10.0</td> 3904<td><p>Add ability to define <code>RSASSA-PSS-params</code> sequence parameters for RSA-PSS keys pairs.</p></td></tr> 3905<tr><td>v13.9.0, v12.17.0</td> 3906<td><p>Add support for Diffie-Hellman.</p></td></tr> 3907<tr><td>v12.0.0</td> 3908<td><p>Add support for RSA-PSS key pairs.</p></td></tr> 3909<tr><td>v12.0.0</td> 3910<td><p>Add ability to generate X25519 and X448 key pairs.</p></td></tr> 3911<tr><td>v12.0.0</td> 3912<td><p>Add ability to generate Ed25519 and Ed448 key pairs.</p></td></tr> 3913<tr><td>v11.6.0</td> 3914<td><p>The <code>generateKeyPair</code> and <code>generateKeyPairSync</code> functions now produce key objects if no encoding was specified.</p></td></tr> 3915<tr><td>v10.12.0</td> 3916<td><p><span>Added in: v10.12.0</span></p></td></tr> 3917</tbody></table> 3918</details> 3919</div> 3920<ul> 3921<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>'rsa-pss'</code>, <code>'dsa'</code>, <code>'ec'</code>, <code>'ed25519'</code>, 3922<code>'ed448'</code>, <code>'x25519'</code>, <code>'x448'</code>, or <code>'dh'</code>.</li> 3923<li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 3924<ul> 3925<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> 3926<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> 3927<li><code>hashAlgorithm</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Name of the message digest (RSA-PSS).</li> 3928<li><code>mgf1HashAlgorithm</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Name of the message digest used by 3929MGF1 (RSA-PSS).</li> 3930<li><code>saltLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Minimal salt length in bytes (RSA-PSS).</li> 3931<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> 3932<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> 3933<li><code>prime</code>: <a href="buffer.html#class-buffer" class="type"><Buffer></a> The prime parameter (DH).</li> 3934<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> 3935<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> 3936<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 3937<a href="#cryptogetdiffiehellmangroupname"><code>crypto.getDiffieHellman()</code></a>.</li> 3938<li><code>paramEncoding</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Must be <code>'named'</code> or <code>'explicit'</code> (EC). 3939<strong>Default:</strong> <code>'named'</code>.</li> 3940<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="#keyobjectexportoptions"><code>keyObject.export()</code></a>.</li> 3941<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="#keyobjectexportoptions"><code>keyObject.export()</code></a>.</li> 3942</ul> 3943</li> 3944<li><code>callback</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 3945<ul> 3946<li><code>err</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 3947<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#class-buffer" class="type"><Buffer></a> | <a href="crypto.html#class-keyobject" class="type"><KeyObject></a></li> 3948<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#class-buffer" class="type"><Buffer></a> | <a href="crypto.html#class-keyobject" class="type"><KeyObject></a></li> 3949</ul> 3950</li> 3951</ul> 3952<p>Generates a new asymmetric key pair of the given <code>type</code>. RSA, RSA-PSS, DSA, EC, 3953Ed25519, Ed448, X25519, X448, and DH are currently supported.</p> 3954<p>If a <code>publicKeyEncoding</code> or <code>privateKeyEncoding</code> was specified, this function 3955behaves as if <a href="#keyobjectexportoptions"><code>keyObject.export()</code></a> had been called on its result. Otherwise, 3956the respective part of the key is returned as a <a href="#class-keyobject"><code>KeyObject</code></a>.</p> 3957<p>It is recommended to encode public keys as <code>'spki'</code> and private keys as 3958<code>'pkcs8'</code> with encryption for long-term storage:</p> 3959 3960<pre class="with-18-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">const</span> { 3961 generateKeyPair, 3962} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 3963 3964<span class="hljs-title function_">generateKeyPair</span>(<span class="hljs-string">'rsa'</span>, { 3965 <span class="hljs-attr">modulusLength</span>: <span class="hljs-number">4096</span>, 3966 <span class="hljs-attr">publicKeyEncoding</span>: { 3967 <span class="hljs-attr">type</span>: <span class="hljs-string">'spki'</span>, 3968 <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>, 3969 }, 3970 <span class="hljs-attr">privateKeyEncoding</span>: { 3971 <span class="hljs-attr">type</span>: <span class="hljs-string">'pkcs8'</span>, 3972 <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>, 3973 <span class="hljs-attr">cipher</span>: <span class="hljs-string">'aes-256-cbc'</span>, 3974 <span class="hljs-attr">passphrase</span>: <span class="hljs-string">'top secret'</span>, 3975 }, 3976}, <span class="hljs-function">(<span class="hljs-params">err, publicKey, privateKey</span>) =></span> { 3977 <span class="hljs-comment">// Handle errors and use the generated key pair.</span> 3978});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 3979 generateKeyPair, 3980} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 3981 3982<span class="hljs-title function_">generateKeyPair</span>(<span class="hljs-string">'rsa'</span>, { 3983 <span class="hljs-attr">modulusLength</span>: <span class="hljs-number">4096</span>, 3984 <span class="hljs-attr">publicKeyEncoding</span>: { 3985 <span class="hljs-attr">type</span>: <span class="hljs-string">'spki'</span>, 3986 <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>, 3987 }, 3988 <span class="hljs-attr">privateKeyEncoding</span>: { 3989 <span class="hljs-attr">type</span>: <span class="hljs-string">'pkcs8'</span>, 3990 <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>, 3991 <span class="hljs-attr">cipher</span>: <span class="hljs-string">'aes-256-cbc'</span>, 3992 <span class="hljs-attr">passphrase</span>: <span class="hljs-string">'top secret'</span>, 3993 }, 3994}, <span class="hljs-function">(<span class="hljs-params">err, publicKey, privateKey</span>) =></span> { 3995 <span class="hljs-comment">// Handle errors and use the generated key pair.</span> 3996});</code><button class="copy-button">copy</button></pre> 3997<p>On completion, <code>callback</code> will be called with <code>err</code> set to <code>undefined</code> and 3998<code>publicKey</code> / <code>privateKey</code> representing the generated key pair.</p> 3999<p>If this method is invoked as its <a href="util.html#utilpromisifyoriginal"><code>util.promisify()</code></a>ed version, it returns 4000a <code>Promise</code> for an <code>Object</code> with <code>publicKey</code> and <code>privateKey</code> properties.</p> 4001<h4><code>crypto.generateKeyPairSync(type, options)</code><span><a class="mark" href="#cryptogeneratekeypairsynctype-options" id="cryptogeneratekeypairsynctype-options">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_generatekeypairsync_type_options"></a></h4> 4002<div class="api_metadata"> 4003<details class="changelog"><summary>History</summary> 4004<table> 4005<tbody><tr><th>Version</th><th>Changes</th></tr> 4006<tr><td>v16.10.0</td> 4007<td><p>Add ability to define <code>RSASSA-PSS-params</code> sequence parameters for RSA-PSS keys pairs.</p></td></tr> 4008<tr><td>v13.9.0, v12.17.0</td> 4009<td><p>Add support for Diffie-Hellman.</p></td></tr> 4010<tr><td>v12.0.0</td> 4011<td><p>Add support for RSA-PSS key pairs.</p></td></tr> 4012<tr><td>v12.0.0</td> 4013<td><p>Add ability to generate X25519 and X448 key pairs.</p></td></tr> 4014<tr><td>v12.0.0</td> 4015<td><p>Add ability to generate Ed25519 and Ed448 key pairs.</p></td></tr> 4016<tr><td>v11.6.0</td> 4017<td><p>The <code>generateKeyPair</code> and <code>generateKeyPairSync</code> functions now produce key objects if no encoding was specified.</p></td></tr> 4018<tr><td>v10.12.0</td> 4019<td><p><span>Added in: v10.12.0</span></p></td></tr> 4020</tbody></table> 4021</details> 4022</div> 4023<ul> 4024<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>'rsa-pss'</code>, <code>'dsa'</code>, <code>'ec'</code>, <code>'ed25519'</code>, 4025<code>'ed448'</code>, <code>'x25519'</code>, <code>'x448'</code>, or <code>'dh'</code>.</li> 4026<li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 4027<ul> 4028<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> 4029<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> 4030<li><code>hashAlgorithm</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Name of the message digest (RSA-PSS).</li> 4031<li><code>mgf1HashAlgorithm</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Name of the message digest used by 4032MGF1 (RSA-PSS).</li> 4033<li><code>saltLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Minimal salt length in bytes (RSA-PSS).</li> 4034<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> 4035<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> 4036<li><code>prime</code>: <a href="buffer.html#class-buffer" class="type"><Buffer></a> The prime parameter (DH).</li> 4037<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> 4038<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> 4039<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 4040<a href="#cryptogetdiffiehellmangroupname"><code>crypto.getDiffieHellman()</code></a>.</li> 4041<li><code>paramEncoding</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Must be <code>'named'</code> or <code>'explicit'</code> (EC). 4042<strong>Default:</strong> <code>'named'</code>.</li> 4043<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="#keyobjectexportoptions"><code>keyObject.export()</code></a>.</li> 4044<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="#keyobjectexportoptions"><code>keyObject.export()</code></a>.</li> 4045</ul> 4046</li> 4047<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 4048<ul> 4049<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#class-buffer" class="type"><Buffer></a> | <a href="crypto.html#class-keyobject" class="type"><KeyObject></a></li> 4050<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#class-buffer" class="type"><Buffer></a> | <a href="crypto.html#class-keyobject" class="type"><KeyObject></a></li> 4051</ul> 4052</li> 4053</ul> 4054<p>Generates a new asymmetric key pair of the given <code>type</code>. RSA, RSA-PSS, DSA, EC, 4055Ed25519, Ed448, X25519, X448, and DH are currently supported.</p> 4056<p>If a <code>publicKeyEncoding</code> or <code>privateKeyEncoding</code> was specified, this function 4057behaves as if <a href="#keyobjectexportoptions"><code>keyObject.export()</code></a> had been called on its result. Otherwise, 4058the respective part of the key is returned as a <a href="#class-keyobject"><code>KeyObject</code></a>.</p> 4059<p>When encoding public keys, it is recommended to use <code>'spki'</code>. When encoding 4060private keys, it is recommended to use <code>'pkcs8'</code> with a strong passphrase, 4061and to keep the passphrase confidential.</p> 4062 4063<pre class="with-22-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">const</span> { 4064 generateKeyPairSync, 4065} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 4066 4067<span class="hljs-keyword">const</span> { 4068 publicKey, 4069 privateKey, 4070} = <span class="hljs-title function_">generateKeyPairSync</span>(<span class="hljs-string">'rsa'</span>, { 4071 <span class="hljs-attr">modulusLength</span>: <span class="hljs-number">4096</span>, 4072 <span class="hljs-attr">publicKeyEncoding</span>: { 4073 <span class="hljs-attr">type</span>: <span class="hljs-string">'spki'</span>, 4074 <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>, 4075 }, 4076 <span class="hljs-attr">privateKeyEncoding</span>: { 4077 <span class="hljs-attr">type</span>: <span class="hljs-string">'pkcs8'</span>, 4078 <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>, 4079 <span class="hljs-attr">cipher</span>: <span class="hljs-string">'aes-256-cbc'</span>, 4080 <span class="hljs-attr">passphrase</span>: <span class="hljs-string">'top secret'</span>, 4081 }, 4082});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 4083 generateKeyPairSync, 4084} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 4085 4086<span class="hljs-keyword">const</span> { 4087 publicKey, 4088 privateKey, 4089} = <span class="hljs-title function_">generateKeyPairSync</span>(<span class="hljs-string">'rsa'</span>, { 4090 <span class="hljs-attr">modulusLength</span>: <span class="hljs-number">4096</span>, 4091 <span class="hljs-attr">publicKeyEncoding</span>: { 4092 <span class="hljs-attr">type</span>: <span class="hljs-string">'spki'</span>, 4093 <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>, 4094 }, 4095 <span class="hljs-attr">privateKeyEncoding</span>: { 4096 <span class="hljs-attr">type</span>: <span class="hljs-string">'pkcs8'</span>, 4097 <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>, 4098 <span class="hljs-attr">cipher</span>: <span class="hljs-string">'aes-256-cbc'</span>, 4099 <span class="hljs-attr">passphrase</span>: <span class="hljs-string">'top secret'</span>, 4100 }, 4101});</code><button class="copy-button">copy</button></pre> 4102<p>The return value <code>{ publicKey, privateKey }</code> represents the generated key pair. 4103When PEM encoding was selected, the respective key will be a string, otherwise 4104it will be a buffer containing the data encoded as DER.</p> 4105<h4><code>crypto.generateKeySync(type, options)</code><span><a class="mark" href="#cryptogeneratekeysynctype-options" id="cryptogeneratekeysynctype-options">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_generatekeysync_type_options"></a></h4> 4106<div class="api_metadata"> 4107<span>Added in: v15.0.0</span> 4108</div> 4109<ul> 4110<li><code>type</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The intended use of the generated secret key. Currently 4111accepted values are <code>'hmac'</code> and <code>'aes'</code>.</li> 4112<li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 4113<ul> 4114<li><code>length</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The bit length of the key to generate. 4115<ul> 4116<li>If <code>type</code> is <code>'hmac'</code>, the minimum is 8, and the maximum length is 41172<sup>31</sup>-1. If the value is not a multiple of 8, the generated 4118key will be truncated to <code>Math.floor(length / 8)</code>.</li> 4119<li>If <code>type</code> is <code>'aes'</code>, the length must be one of <code>128</code>, <code>192</code>, or <code>256</code>.</li> 4120</ul> 4121</li> 4122</ul> 4123</li> 4124<li>Returns: <a href="crypto.html#class-keyobject" class="type"><KeyObject></a></li> 4125</ul> 4126<p>Synchronously generates a new random secret key of the given <code>length</code>. The 4127<code>type</code> will determine which validations will be performed on the <code>length</code>.</p> 4128 4129<pre class="with-18-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">const</span> { 4130 generateKeySync, 4131} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 4132 4133<span class="hljs-keyword">const</span> key = <span class="hljs-title function_">generateKeySync</span>(<span class="hljs-string">'hmac'</span>, { <span class="hljs-attr">length</span>: <span class="hljs-number">512</span> }); 4134<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(key.<span class="hljs-title function_">export</span>().<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>)); <span class="hljs-comment">// e89..........41e</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 4135 generateKeySync, 4136} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 4137 4138<span class="hljs-keyword">const</span> key = <span class="hljs-title function_">generateKeySync</span>(<span class="hljs-string">'hmac'</span>, { <span class="hljs-attr">length</span>: <span class="hljs-number">512</span> }); 4139<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(key.<span class="hljs-title function_">export</span>().<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>)); <span class="hljs-comment">// e89..........41e</span></code><button class="copy-button">copy</button></pre> 4140<p>The size of a generated HMAC key should not exceed the block size of the 4141underlying hash function. See <a href="#cryptocreatehmacalgorithm-key-options"><code>crypto.createHmac()</code></a> for more information.</p> 4142<h4><code>crypto.generatePrime(size[, options[, callback]])</code><span><a class="mark" href="#cryptogenerateprimesize-options-callback" id="cryptogenerateprimesize-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_generateprime_size_options_callback"></a></h4> 4143<div class="api_metadata"> 4144<details class="changelog"><summary>History</summary> 4145<table> 4146<tbody><tr><th>Version</th><th>Changes</th></tr> 4147<tr><td>v18.0.0</td> 4148<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 4149<tr><td>v15.8.0</td> 4150<td><p><span>Added in: v15.8.0</span></p></td></tr> 4151</tbody></table> 4152</details> 4153</div> 4154<ul> 4155<li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The size (in bits) of the prime to generate.</li> 4156<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 4157<ul> 4158<li><code>add</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer" class="type"><SharedArrayBuffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></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/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 4159<li><code>rem</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer" class="type"><SharedArrayBuffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></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/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 4160<li><code>safe</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code>.</li> 4161<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code>, the generated prime is returned 4162as a <code>bigint</code>.</li> 4163</ul> 4164</li> 4165<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4166<ul> 4167<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4168<li><code>prime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 4169</ul> 4170</li> 4171</ul> 4172<p>Generates a pseudorandom prime of <code>size</code> bits.</p> 4173<p>If <code>options.safe</code> is <code>true</code>, the prime will be a safe prime -- that is, 4174<code>(prime - 1) / 2</code> will also be a prime.</p> 4175<p>The <code>options.add</code> and <code>options.rem</code> parameters can be used to enforce additional 4176requirements, e.g., for Diffie-Hellman:</p> 4177<ul> 4178<li>If <code>options.add</code> and <code>options.rem</code> are both set, the prime will satisfy the 4179condition that <code>prime % add = rem</code>.</li> 4180<li>If only <code>options.add</code> is set and <code>options.safe</code> is not <code>true</code>, the prime will 4181satisfy the condition that <code>prime % add = 1</code>.</li> 4182<li>If only <code>options.add</code> is set and <code>options.safe</code> is set to <code>true</code>, the prime 4183will instead satisfy the condition that <code>prime % add = 3</code>. This is necessary 4184because <code>prime % add = 1</code> for <code>options.add > 2</code> would contradict the condition 4185enforced by <code>options.safe</code>.</li> 4186<li><code>options.rem</code> is ignored if <code>options.add</code> is not given.</li> 4187</ul> 4188<p>Both <code>options.add</code> and <code>options.rem</code> must be encoded as big-endian sequences 4189if given as an <code>ArrayBuffer</code>, <code>SharedArrayBuffer</code>, <code>TypedArray</code>, <code>Buffer</code>, or 4190<code>DataView</code>.</p> 4191<p>By default, the prime is encoded as a big-endian sequence of octets 4192in an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a>. If the <code>bigint</code> option is <code>true</code>, then a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a> 4193is provided.</p> 4194<h4><code>crypto.generatePrimeSync(size[, options])</code><span><a class="mark" href="#cryptogenerateprimesyncsize-options" id="cryptogenerateprimesyncsize-options">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_generateprimesync_size_options"></a></h4> 4195<div class="api_metadata"> 4196<span>Added in: v15.8.0</span> 4197</div> 4198<ul> 4199<li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The size (in bits) of the prime to generate.</li> 4200<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 4201<ul> 4202<li><code>add</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer" class="type"><SharedArrayBuffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></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/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 4203<li><code>rem</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer" class="type"><SharedArrayBuffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></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/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 4204<li><code>safe</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code>.</li> 4205<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code>, the generated prime is returned 4206as a <code>bigint</code>.</li> 4207</ul> 4208</li> 4209<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li> 4210</ul> 4211<p>Generates a pseudorandom prime of <code>size</code> bits.</p> 4212<p>If <code>options.safe</code> is <code>true</code>, the prime will be a safe prime -- that is, 4213<code>(prime - 1) / 2</code> will also be a prime.</p> 4214<p>The <code>options.add</code> and <code>options.rem</code> parameters can be used to enforce additional 4215requirements, e.g., for Diffie-Hellman:</p> 4216<ul> 4217<li>If <code>options.add</code> and <code>options.rem</code> are both set, the prime will satisfy the 4218condition that <code>prime % add = rem</code>.</li> 4219<li>If only <code>options.add</code> is set and <code>options.safe</code> is not <code>true</code>, the prime will 4220satisfy the condition that <code>prime % add = 1</code>.</li> 4221<li>If only <code>options.add</code> is set and <code>options.safe</code> is set to <code>true</code>, the prime 4222will instead satisfy the condition that <code>prime % add = 3</code>. This is necessary 4223because <code>prime % add = 1</code> for <code>options.add > 2</code> would contradict the condition 4224enforced by <code>options.safe</code>.</li> 4225<li><code>options.rem</code> is ignored if <code>options.add</code> is not given.</li> 4226</ul> 4227<p>Both <code>options.add</code> and <code>options.rem</code> must be encoded as big-endian sequences 4228if given as an <code>ArrayBuffer</code>, <code>SharedArrayBuffer</code>, <code>TypedArray</code>, <code>Buffer</code>, or 4229<code>DataView</code>.</p> 4230<p>By default, the prime is encoded as a big-endian sequence of octets 4231in an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a>. If the <code>bigint</code> option is <code>true</code>, then a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a> 4232is provided.</p> 4233<h4><code>crypto.getCipherInfo(nameOrNid[, options])</code><span><a class="mark" href="#cryptogetcipherinfonameornid-options" id="cryptogetcipherinfonameornid-options">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_getcipherinfo_nameornid_options"></a></h4> 4234<div class="api_metadata"> 4235<span>Added in: v15.0.0</span> 4236</div> 4237<ul> 4238<li><code>nameOrNid</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#Number_type" class="type"><number></a> The name or nid of the cipher to query.</li> 4239<li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 4240<ul> 4241<li><code>keyLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> A test key length.</li> 4242<li><code>ivLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> A test IV length.</li> 4243</ul> 4244</li> 4245<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 4246<ul> 4247<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The name of the cipher</li> 4248<li><code>nid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The nid of the cipher</li> 4249<li><code>blockSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The block size of the cipher in bytes. This property 4250is omitted when <code>mode</code> is <code>'stream'</code>.</li> 4251<li><code>ivLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The expected or default initialization vector length in 4252bytes. This property is omitted if the cipher does not use an initialization 4253vector.</li> 4254<li><code>keyLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The expected or default key length in bytes.</li> 4255<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The cipher mode. One of <code>'cbc'</code>, <code>'ccm'</code>, <code>'cfb'</code>, <code>'ctr'</code>, 4256<code>'ecb'</code>, <code>'gcm'</code>, <code>'ocb'</code>, <code>'ofb'</code>, <code>'stream'</code>, <code>'wrap'</code>, <code>'xts'</code>.</li> 4257</ul> 4258</li> 4259</ul> 4260<p>Returns information about a given cipher.</p> 4261<p>Some ciphers accept variable length keys and initialization vectors. By default, 4262the <code>crypto.getCipherInfo()</code> method will return the default values for these 4263ciphers. To test if a given key length or iv length is acceptable for given 4264cipher, use the <code>keyLength</code> and <code>ivLength</code> options. If the given values are 4265unacceptable, <code>undefined</code> will be returned.</p> 4266<h4><code>crypto.getCiphers()</code><span><a class="mark" href="#cryptogetciphers" id="cryptogetciphers">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_getciphers"></a></h4> 4267<div class="api_metadata"> 4268<span>Added in: v0.9.3</span> 4269</div> 4270<ul> 4271<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 4272algorithms.</li> 4273</ul> 4274 4275<pre class="with-13-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">const</span> { 4276 getCiphers, 4277} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 4278 4279<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">getCiphers</span>()); <span class="hljs-comment">// ['aes-128-cbc', 'aes-128-ccm', ...]</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 4280 getCiphers, 4281} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 4282 4283<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">getCiphers</span>()); <span class="hljs-comment">// ['aes-128-cbc', 'aes-128-ccm', ...]</span></code><button class="copy-button">copy</button></pre> 4284<h4><code>crypto.getCurves()</code><span><a class="mark" href="#cryptogetcurves" id="cryptogetcurves">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_getcurves"></a></h4> 4285<div class="api_metadata"> 4286<span>Added in: v2.3.0</span> 4287</div> 4288<ul> 4289<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> 4290</ul> 4291 4292<pre class="with-12-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">const</span> { 4293 getCurves, 4294} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 4295 4296<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">getCurves</span>()); <span class="hljs-comment">// ['Oakley-EC2N-3', 'Oakley-EC2N-4', ...]</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 4297 getCurves, 4298} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 4299 4300<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">getCurves</span>()); <span class="hljs-comment">// ['Oakley-EC2N-3', 'Oakley-EC2N-4', ...]</span></code><button class="copy-button">copy</button></pre> 4301<h4><code>crypto.getDiffieHellman(groupName)</code><span><a class="mark" href="#cryptogetdiffiehellmangroupname" id="cryptogetdiffiehellmangroupname">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_getdiffiehellman_groupname"></a></h4> 4302<div class="api_metadata"> 4303<span>Added in: v0.7.5</span> 4304</div> 4305<ul> 4306<li><code>groupName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 4307<li>Returns: <a href="crypto.html#class-diffiehellmangroup" class="type"><DiffieHellmanGroup></a></li> 4308</ul> 4309<p>Creates a predefined <code>DiffieHellmanGroup</code> key exchange object. The 4310supported groups are listed in the documentation for <a href="#class-diffiehellmangroup"><code>DiffieHellmanGroup</code></a>.</p> 4311<p>The returned object mimics the interface of objects created by 4312<a href="#cryptocreatediffiehellmanprime-primeencoding-generator-generatorencoding"><code>crypto.createDiffieHellman()</code></a>, but will not allow changing 4313the keys (with <a href="#diffiehellmansetpublickeypublickey-encoding"><code>diffieHellman.setPublicKey()</code></a>, for example). The 4314advantage of using this method is that the parties do not have to 4315generate nor exchange a group modulus beforehand, saving both processor 4316and communication time.</p> 4317<p>Example (obtaining a shared secret):</p> 4318 4319<pre class="with-19-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">const</span> { 4320 getDiffieHellman, 4321} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 4322<span class="hljs-keyword">const</span> alice = <span class="hljs-title function_">getDiffieHellman</span>(<span class="hljs-string">'modp14'</span>); 4323<span class="hljs-keyword">const</span> bob = <span class="hljs-title function_">getDiffieHellman</span>(<span class="hljs-string">'modp14'</span>); 4324 4325alice.<span class="hljs-title function_">generateKeys</span>(); 4326bob.<span class="hljs-title function_">generateKeys</span>(); 4327 4328<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>); 4329<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>); 4330 4331<span class="hljs-comment">/* aliceSecret and bobSecret should be the same */</span> 4332<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(aliceSecret === bobSecret);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 4333 getDiffieHellman, 4334} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 4335 4336<span class="hljs-keyword">const</span> alice = <span class="hljs-title function_">getDiffieHellman</span>(<span class="hljs-string">'modp14'</span>); 4337<span class="hljs-keyword">const</span> bob = <span class="hljs-title function_">getDiffieHellman</span>(<span class="hljs-string">'modp14'</span>); 4338 4339alice.<span class="hljs-title function_">generateKeys</span>(); 4340bob.<span class="hljs-title function_">generateKeys</span>(); 4341 4342<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>); 4343<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>); 4344 4345<span class="hljs-comment">/* aliceSecret and bobSecret should be the same */</span> 4346<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(aliceSecret === bobSecret);</code><button class="copy-button">copy</button></pre> 4347<h4><code>crypto.getFips()</code><span><a class="mark" href="#cryptogetfips" id="cryptogetfips">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_getfips"></a></h4> 4348<div class="api_metadata"> 4349<span>Added in: v10.0.0</span> 4350</div> 4351<ul> 4352<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 4353currently in use, <code>0</code> otherwise. A future semver-major release may change 4354the 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> 4355</ul> 4356<h4><code>crypto.getHashes()</code><span><a class="mark" href="#cryptogethashes" id="cryptogethashes">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_gethashes"></a></h4> 4357<div class="api_metadata"> 4358<span>Added in: v0.9.3</span> 4359</div> 4360<ul> 4361<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, 4362such as <code>'RSA-SHA256'</code>. Hash algorithms are also called "digest" algorithms.</li> 4363</ul> 4364 4365<pre class="with-12-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">const</span> { 4366 getHashes, 4367} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 4368 4369<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">getHashes</span>()); <span class="hljs-comment">// ['DSA', 'DSA-SHA', 'DSA-SHA1', ...]</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 4370 getHashes, 4371} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 4372 4373<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">getHashes</span>()); <span class="hljs-comment">// ['DSA', 'DSA-SHA', 'DSA-SHA1', ...]</span></code><button class="copy-button">copy</button></pre> 4374<h4><code>crypto.getRandomValues(typedArray)</code><span><a class="mark" href="#cryptogetrandomvaluestypedarray" id="cryptogetrandomvaluestypedarray">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_getrandomvalues_typedarray"></a></h4> 4375<div class="api_metadata"> 4376<span>Added in: v17.4.0</span> 4377</div> 4378<ul> 4379<li><code>typedArray</code> <a href="buffer.html#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/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a></li> 4380<li>Returns: <a href="buffer.html#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/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> Returns <code>typedArray</code>.</li> 4381</ul> 4382<p>A convenient alias for <a href="webcrypto.html#cryptogetrandomvaluestypedarray"><code>crypto.webcrypto.getRandomValues()</code></a>. This 4383implementation is not compliant with the Web Crypto spec, to write 4384web-compatible code use <a href="webcrypto.html#cryptogetrandomvaluestypedarray"><code>crypto.webcrypto.getRandomValues()</code></a> instead.</p> 4385<h4><code>crypto.hkdf(digest, ikm, salt, info, keylen, callback)</code><span><a class="mark" href="#cryptohkdfdigest-ikm-salt-info-keylen-callback" id="cryptohkdfdigest-ikm-salt-info-keylen-callback">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_hkdf_digest_ikm_salt_info_keylen_callback"></a></h4> 4386<div class="api_metadata"> 4387<details class="changelog"><summary>History</summary> 4388<table> 4389<tbody><tr><th>Version</th><th>Changes</th></tr> 4390<tr><td>v18.8.0</td> 4391<td><p>The input keying material can now be zero-length.</p></td></tr> 4392<tr><td>v18.0.0</td> 4393<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 4394<tr><td>v15.0.0</td> 4395<td><p><span>Added in: v15.0.0</span></p></td></tr> 4396</tbody></table> 4397</details> 4398</div> 4399<ul> 4400<li><code>digest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The digest algorithm to use.</li> 4401<li><code>ikm</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/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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#class-keyobject" class="type"><KeyObject></a> The input 4402keying material. Must be provided but can be zero-length.</li> 4403<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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 salt value. Must 4404be provided but can be zero-length.</li> 4405<li><code>info</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/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> Additional info value. 4406Must be provided but can be zero-length, and cannot be more than 1024 bytes.</li> 4407<li><code>keylen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The length of the key to generate. Must be greater than 0. 4408The maximum allowable value is <code>255</code> times the number of bytes produced by 4409the selected digest function (e.g. <code>sha512</code> generates 64-byte hashes, making 4410the maximum HKDF output 16320 bytes).</li> 4411<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4412<ul> 4413<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4414<li><code>derivedKey</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a></li> 4415</ul> 4416</li> 4417</ul> 4418<p>HKDF is a simple key derivation function defined in RFC 5869. The given <code>ikm</code>, 4419<code>salt</code> and <code>info</code> are used with the <code>digest</code> to derive a key of <code>keylen</code> bytes.</p> 4420<p>The supplied <code>callback</code> function is called with two arguments: <code>err</code> and 4421<code>derivedKey</code>. If an errors occurs while deriving the key, <code>err</code> will be set; 4422otherwise <code>err</code> will be <code>null</code>. The successfully generated <code>derivedKey</code> will 4423be passed to the callback as an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a>. An error will be thrown if any 4424of the input arguments specify invalid values or types.</p> 4425 4426<pre class="with-37-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 4427<span class="hljs-keyword">const</span> { 4428 hkdf, 4429} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 4430 4431<span class="hljs-title function_">hkdf</span>(<span class="hljs-string">'sha512'</span>, <span class="hljs-string">'key'</span>, <span class="hljs-string">'salt'</span>, <span class="hljs-string">'info'</span>, <span class="hljs-number">64</span>, <span class="hljs-function">(<span class="hljs-params">err, derivedKey</span>) =></span> { 4432 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 4433 <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>(derivedKey).<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>)); <span class="hljs-comment">// '24156e2...5391653'</span> 4434});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 4435 hkdf, 4436} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 4437<span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>); 4438 4439<span class="hljs-title function_">hkdf</span>(<span class="hljs-string">'sha512'</span>, <span class="hljs-string">'key'</span>, <span class="hljs-string">'salt'</span>, <span class="hljs-string">'info'</span>, <span class="hljs-number">64</span>, <span class="hljs-function">(<span class="hljs-params">err, derivedKey</span>) =></span> { 4440 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 4441 <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>(derivedKey).<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>)); <span class="hljs-comment">// '24156e2...5391653'</span> 4442});</code><button class="copy-button">copy</button></pre> 4443<h4><code>crypto.hkdfSync(digest, ikm, salt, info, keylen)</code><span><a class="mark" href="#cryptohkdfsyncdigest-ikm-salt-info-keylen" id="cryptohkdfsyncdigest-ikm-salt-info-keylen">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_hkdfsync_digest_ikm_salt_info_keylen"></a></h4> 4444<div class="api_metadata"> 4445<details class="changelog"><summary>History</summary> 4446<table> 4447<tbody><tr><th>Version</th><th>Changes</th></tr> 4448<tr><td>v18.8.0</td> 4449<td><p>The input keying material can now be zero-length.</p></td></tr> 4450<tr><td>v15.0.0</td> 4451<td><p><span>Added in: v15.0.0</span></p></td></tr> 4452</tbody></table> 4453</details> 4454</div> 4455<ul> 4456<li><code>digest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The digest algorithm to use.</li> 4457<li><code>ikm</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/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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#class-keyobject" class="type"><KeyObject></a> The input 4458keying material. Must be provided but can be zero-length.</li> 4459<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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 salt value. Must 4460be provided but can be zero-length.</li> 4461<li><code>info</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/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> Additional info value. 4462Must be provided but can be zero-length, and cannot be more than 1024 bytes.</li> 4463<li><code>keylen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The length of the key to generate. Must be greater than 0. 4464The maximum allowable value is <code>255</code> times the number of bytes produced by 4465the selected digest function (e.g. <code>sha512</code> generates 64-byte hashes, making 4466the maximum HKDF output 16320 bytes).</li> 4467<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a></li> 4468</ul> 4469<p>Provides a synchronous HKDF key derivation function as defined in RFC 5869. The 4470given <code>ikm</code>, <code>salt</code> and <code>info</code> are used with the <code>digest</code> to derive a key of 4471<code>keylen</code> bytes.</p> 4472<p>The successfully generated <code>derivedKey</code> will be returned as an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a>.</p> 4473<p>An error will be thrown if any of the input arguments specify invalid values or 4474types, or if the derived key cannot be generated.</p> 4475 4476<pre class="with-37-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 4477<span class="hljs-keyword">const</span> { 4478 hkdfSync, 4479} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 4480 4481<span class="hljs-keyword">const</span> derivedKey = <span class="hljs-title function_">hkdfSync</span>(<span class="hljs-string">'sha512'</span>, <span class="hljs-string">'key'</span>, <span class="hljs-string">'salt'</span>, <span class="hljs-string">'info'</span>, <span class="hljs-number">64</span>); 4482<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>(derivedKey).<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>)); <span class="hljs-comment">// '24156e2...5391653'</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 4483 hkdfSync, 4484} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 4485<span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>); 4486 4487<span class="hljs-keyword">const</span> derivedKey = <span class="hljs-title function_">hkdfSync</span>(<span class="hljs-string">'sha512'</span>, <span class="hljs-string">'key'</span>, <span class="hljs-string">'salt'</span>, <span class="hljs-string">'info'</span>, <span class="hljs-number">64</span>); 4488<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>(derivedKey).<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>)); <span class="hljs-comment">// '24156e2...5391653'</span></code><button class="copy-button">copy</button></pre> 4489<h4><code>crypto.pbkdf2(password, salt, iterations, keylen, digest, callback)</code><span><a class="mark" href="#cryptopbkdf2password-salt-iterations-keylen-digest-callback" id="cryptopbkdf2password-salt-iterations-keylen-digest-callback">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_pbkdf2_password_salt_iterations_keylen_digest_callback"></a></h4> 4490<div class="api_metadata"> 4491<details class="changelog"><summary>History</summary> 4492<table> 4493<tbody><tr><th>Version</th><th>Changes</th></tr> 4494<tr><td>v18.0.0</td> 4495<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 4496<tr><td>v15.0.0</td> 4497<td><p>The password and salt arguments can also be ArrayBuffer instances.</p></td></tr> 4498<tr><td>v14.0.0</td> 4499<td><p>The <code>iterations</code> parameter is now restricted to positive values. Earlier releases treated other values as one.</p></td></tr> 4500<tr><td>v8.0.0</td> 4501<td><p>The <code>digest</code> parameter is always required now.</p></td></tr> 4502<tr><td>v6.0.0</td> 4503<td><p>Calling this function without passing the <code>digest</code> parameter is deprecated now and will emit a warning.</p></td></tr> 4504<tr><td>v6.0.0</td> 4505<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> 4506<tr><td>v0.5.5</td> 4507<td><p><span>Added in: v0.5.5</span></p></td></tr> 4508</tbody></table> 4509</details> 4510</div> 4511<ul> 4512<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 4513<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 4514<li><code>iterations</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 4515<li><code>keylen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 4516<li><code>digest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 4517<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4518<ul> 4519<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4520<li><code>derivedKey</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 4521</ul> 4522</li> 4523</ul> 4524<p>Provides an asynchronous Password-Based Key Derivation Function 2 (PBKDF2) 4525implementation. A selected HMAC digest algorithm specified by <code>digest</code> is 4526applied to derive a key of the requested byte length (<code>keylen</code>) from the 4527<code>password</code>, <code>salt</code> and <code>iterations</code>.</p> 4528<p>The supplied <code>callback</code> function is called with two arguments: <code>err</code> and 4529<code>derivedKey</code>. If an error occurs while deriving the key, <code>err</code> will be set; 4530otherwise <code>err</code> will be <code>null</code>. By default, the successfully generated 4531<code>derivedKey</code> will be passed to the callback as a <a href="buffer.html"><code>Buffer</code></a>. An error will be 4532thrown if any of the input arguments specify invalid values or types.</p> 4533<p>The <code>iterations</code> argument must be a number set as high as possible. The 4534higher the number of iterations, the more secure the derived key will be, 4535but will take a longer amount of time to complete.</p> 4536<p>The <code>salt</code> should be as unique as possible. It is recommended that a salt is 4537random 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> 4538<p>When passing strings for <code>password</code> or <code>salt</code>, please consider 4539<a href="#using-strings-as-inputs-to-cryptographic-apis">caveats when using strings as inputs to cryptographic APIs</a>.</p> 4540 4541<pre class="with-9-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">const</span> { 4542 pbkdf2, 4543} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 4544 4545<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> { 4546 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 4547 <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> 4548});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 4549 pbkdf2, 4550} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 4551 4552<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> { 4553 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 4554 <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> 4555});</code><button class="copy-button">copy</button></pre> 4556<p>An array of supported digest functions can be retrieved using 4557<a href="#cryptogethashes"><code>crypto.getHashes()</code></a>.</p> 4558<p>This API uses libuv's threadpool, which can have surprising and 4559negative performance implications for some applications; see the 4560<a href="cli.html#uv_threadpool_sizesize"><code>UV_THREADPOOL_SIZE</code></a> documentation for more information.</p> 4561<h4><code>crypto.pbkdf2Sync(password, salt, iterations, keylen, digest)</code><span><a class="mark" href="#cryptopbkdf2syncpassword-salt-iterations-keylen-digest" id="cryptopbkdf2syncpassword-salt-iterations-keylen-digest">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_pbkdf2sync_password_salt_iterations_keylen_digest"></a></h4> 4562<div class="api_metadata"> 4563<details class="changelog"><summary>History</summary> 4564<table> 4565<tbody><tr><th>Version</th><th>Changes</th></tr> 4566<tr><td>v14.0.0</td> 4567<td><p>The <code>iterations</code> parameter is now restricted to positive values. Earlier releases treated other values as one.</p></td></tr> 4568<tr><td>v6.0.0</td> 4569<td><p>Calling this function without passing the <code>digest</code> parameter is deprecated now and will emit a warning.</p></td></tr> 4570<tr><td>v6.0.0</td> 4571<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> 4572<tr><td>v0.9.3</td> 4573<td><p><span>Added in: v0.9.3</span></p></td></tr> 4574</tbody></table> 4575</details> 4576</div> 4577<ul> 4578<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#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> 4579<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#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> 4580<li><code>iterations</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 4581<li><code>keylen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 4582<li><code>digest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 4583<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 4584</ul> 4585<p>Provides a synchronous Password-Based Key Derivation Function 2 (PBKDF2) 4586implementation. A selected HMAC digest algorithm specified by <code>digest</code> is 4587applied to derive a key of the requested byte length (<code>keylen</code>) from the 4588<code>password</code>, <code>salt</code> and <code>iterations</code>.</p> 4589<p>If an error occurs an <code>Error</code> will be thrown, otherwise the derived key will be 4590returned as a <a href="buffer.html"><code>Buffer</code></a>.</p> 4591<p>The <code>iterations</code> argument must be a number set as high as possible. The 4592higher the number of iterations, the more secure the derived key will be, 4593but will take a longer amount of time to complete.</p> 4594<p>The <code>salt</code> should be as unique as possible. It is recommended that a salt is 4595random 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> 4596<p>When passing strings for <code>password</code> or <code>salt</code>, please consider 4597<a href="#using-strings-as-inputs-to-cryptographic-apis">caveats when using strings as inputs to cryptographic APIs</a>.</p> 4598 4599<pre class="with-13-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">const</span> { 4600 pbkdf2Sync, 4601} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 4602 4603<span class="hljs-keyword">const</span> key = <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>); 4604<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><code class="language-js cjs"><span class="hljs-keyword">const</span> { 4605 pbkdf2Sync, 4606} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 4607 4608<span class="hljs-keyword">const</span> key = <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>); 4609<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><button class="copy-button">copy</button></pre> 4610<p>An array of supported digest functions can be retrieved using 4611<a href="#cryptogethashes"><code>crypto.getHashes()</code></a>.</p> 4612<h4><code>crypto.privateDecrypt(privateKey, buffer)</code><span><a class="mark" href="#cryptoprivatedecryptprivatekey-buffer" id="cryptoprivatedecryptprivatekey-buffer">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_privatedecrypt_privatekey_buffer"></a></h4> 4613<div class="api_metadata"> 4614<details class="changelog"><summary>History</summary> 4615<table> 4616<tbody><tr><th>Version</th><th>Changes</th></tr> 4617<tr><td>v21.6.2, v20.11.1, v18.19.1</td> 4618<td><p>The <code>RSA_PKCS1_PADDING</code> padding was disabled unless the OpenSSL build supports implicit rejection.</p></td></tr> 4619<tr><td>v15.0.0</td> 4620<td><p>Added string, ArrayBuffer, and CryptoKey as allowable key types. The oaepLabel can be an ArrayBuffer. The buffer can be a string or ArrayBuffer. All types that accept buffers are limited to a maximum of 2 ** 31 - 1 bytes.</p></td></tr> 4621<tr><td>v12.11.0</td> 4622<td><p>The <code>oaepLabel</code> option was added.</p></td></tr> 4623<tr><td>v12.9.0</td> 4624<td><p>The <code>oaepHash</code> option was added.</p></td></tr> 4625<tr><td>v11.6.0</td> 4626<td><p>This function now supports key objects.</p></td></tr> 4627<tr><td>v0.11.14</td> 4628<td><p><span>Added in: v0.11.14</span></p></td></tr> 4629</tbody></table> 4630</details> 4631</div> 4632<!--lint disable maximum-line-length remark-lint--> 4633<ul> 4634<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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#class-keyobject" class="type"><KeyObject></a> | <a href="webcrypto.html#class-cryptokey" class="type"><CryptoKey></a> 4635<ul> 4636<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. 4637<strong>Default:</strong> <code>'sha1'</code></li> 4638<li><code>oaepLabel</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/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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 4639use for OAEP padding. If not specified, no label is used.</li> 4640<li><code>padding</code> <a href="crypto.html#cryptoconstants" class="type"><crypto.constants></a> An optional padding value defined in 4641<code>crypto.constants</code>, which may be: <code>crypto.constants.RSA_NO_PADDING</code>, 4642<code>crypto.constants.RSA_PKCS1_PADDING</code>, or 4643<code>crypto.constants.RSA_PKCS1_OAEP_PADDING</code>.</li> 4644</ul> 4645</li> 4646<li><code>buffer</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/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 4647<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> A new <code>Buffer</code> with the decrypted content.</li> 4648</ul> 4649<!--lint enable maximum-line-length remark-lint--> 4650<p>Decrypts <code>buffer</code> with <code>privateKey</code>. <code>buffer</code> was previously encrypted using 4651the corresponding public key, for example using <a href="#cryptopublicencryptkey-buffer"><code>crypto.publicEncrypt()</code></a>.</p> 4652<p>If <code>privateKey</code> is not a <a href="#class-keyobject"><code>KeyObject</code></a>, this function behaves as if 4653<code>privateKey</code> had been passed to <a href="#cryptocreateprivatekeykey"><code>crypto.createPrivateKey()</code></a>. If it is an 4654object, the <code>padding</code> property can be passed. Otherwise, this function uses 4655<code>RSA_PKCS1_OAEP_PADDING</code>.</p> 4656<p>Using <code>crypto.constants.RSA_PKCS1_PADDING</code> in <a href="#cryptoprivatedecryptprivatekey-buffer"><code>crypto.privateDecrypt()</code></a> 4657requires OpenSSL to support implicit rejection (<code>rsa_pkcs1_implicit_rejection</code>). 4658If the version of OpenSSL used by Node.js does not support this feature, 4659attempting to use <code>RSA_PKCS1_PADDING</code> will fail.</p> 4660<h4><code>crypto.privateEncrypt(privateKey, buffer)</code><span><a class="mark" href="#cryptoprivateencryptprivatekey-buffer" id="cryptoprivateencryptprivatekey-buffer">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_privateencrypt_privatekey_buffer"></a></h4> 4661<div class="api_metadata"> 4662<details class="changelog"><summary>History</summary> 4663<table> 4664<tbody><tr><th>Version</th><th>Changes</th></tr> 4665<tr><td>v15.0.0</td> 4666<td><p>Added string, ArrayBuffer, and CryptoKey as allowable key types. The passphrase can be an ArrayBuffer. The buffer can be a string or ArrayBuffer. All types that accept buffers are limited to a maximum of 2 ** 31 - 1 bytes.</p></td></tr> 4667<tr><td>v11.6.0</td> 4668<td><p>This function now supports key objects.</p></td></tr> 4669<tr><td>v1.1.0</td> 4670<td><p><span>Added in: v1.1.0</span></p></td></tr> 4671</tbody></table> 4672</details> 4673</div> 4674<!--lint disable maximum-line-length remark-lint--> 4675<ul> 4676<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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#class-keyobject" class="type"><KeyObject></a> | <a href="webcrypto.html#class-cryptokey" class="type"><CryptoKey></a> 4677<ul> 4678<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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#class-keyobject" class="type"><KeyObject></a> | <a href="webcrypto.html#class-cryptokey" class="type"><CryptoKey></a> 4679A PEM encoded private key.</li> 4680<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> An optional 4681passphrase for the private key.</li> 4682<li><code>padding</code> <a href="crypto.html#cryptoconstants" class="type"><crypto.constants></a> An optional padding value defined in 4683<code>crypto.constants</code>, which may be: <code>crypto.constants.RSA_NO_PADDING</code> or 4684<code>crypto.constants.RSA_PKCS1_PADDING</code>.</li> 4685<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The string encoding to use when <code>buffer</code>, <code>key</code>, 4686or <code>passphrase</code> are strings.</li> 4687</ul> 4688</li> 4689<li><code>buffer</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/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 4690<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> A new <code>Buffer</code> with the encrypted content.</li> 4691</ul> 4692<!--lint enable maximum-line-length remark-lint--> 4693<p>Encrypts <code>buffer</code> with <code>privateKey</code>. The returned data can be decrypted using 4694the corresponding public key, for example using <a href="#cryptopublicdecryptkey-buffer"><code>crypto.publicDecrypt()</code></a>.</p> 4695<p>If <code>privateKey</code> is not a <a href="#class-keyobject"><code>KeyObject</code></a>, this function behaves as if 4696<code>privateKey</code> had been passed to <a href="#cryptocreateprivatekeykey"><code>crypto.createPrivateKey()</code></a>. If it is an 4697object, the <code>padding</code> property can be passed. Otherwise, this function uses 4698<code>RSA_PKCS1_PADDING</code>.</p> 4699<h4><code>crypto.publicDecrypt(key, buffer)</code><span><a class="mark" href="#cryptopublicdecryptkey-buffer" id="cryptopublicdecryptkey-buffer">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_publicdecrypt_key_buffer"></a></h4> 4700<div class="api_metadata"> 4701<details class="changelog"><summary>History</summary> 4702<table> 4703<tbody><tr><th>Version</th><th>Changes</th></tr> 4704<tr><td>v15.0.0</td> 4705<td><p>Added string, ArrayBuffer, and CryptoKey as allowable key types. The passphrase can be an ArrayBuffer. The buffer can be a string or ArrayBuffer. All types that accept buffers are limited to a maximum of 2 ** 31 - 1 bytes.</p></td></tr> 4706<tr><td>v11.6.0</td> 4707<td><p>This function now supports key objects.</p></td></tr> 4708<tr><td>v1.1.0</td> 4709<td><p><span>Added in: v1.1.0</span></p></td></tr> 4710</tbody></table> 4711</details> 4712</div> 4713<!--lint disable maximum-line-length remark-lint--> 4714<ul> 4715<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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#class-keyobject" class="type"><KeyObject></a> | <a href="webcrypto.html#class-cryptokey" class="type"><CryptoKey></a> 4716<ul> 4717<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> An optional 4718passphrase for the private key.</li> 4719<li><code>padding</code> <a href="crypto.html#cryptoconstants" class="type"><crypto.constants></a> An optional padding value defined in 4720<code>crypto.constants</code>, which may be: <code>crypto.constants.RSA_NO_PADDING</code> or 4721<code>crypto.constants.RSA_PKCS1_PADDING</code>.</li> 4722<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The string encoding to use when <code>buffer</code>, <code>key</code>, 4723or <code>passphrase</code> are strings.</li> 4724</ul> 4725</li> 4726<li><code>buffer</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/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 4727<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> A new <code>Buffer</code> with the decrypted content.</li> 4728</ul> 4729<!--lint enable maximum-line-length remark-lint--> 4730<p>Decrypts <code>buffer</code> with <code>key</code>.<code>buffer</code> was previously encrypted using 4731the corresponding private key, for example using <a href="#cryptoprivateencryptprivatekey-buffer"><code>crypto.privateEncrypt()</code></a>.</p> 4732<p>If <code>key</code> is not a <a href="#class-keyobject"><code>KeyObject</code></a>, this function behaves as if 4733<code>key</code> had been passed to <a href="#cryptocreatepublickeykey"><code>crypto.createPublicKey()</code></a>. If it is an 4734object, the <code>padding</code> property can be passed. Otherwise, this function uses 4735<code>RSA_PKCS1_PADDING</code>.</p> 4736<p>Because RSA public keys can be derived from private keys, a private key may 4737be passed instead of a public key.</p> 4738<h4><code>crypto.publicEncrypt(key, buffer)</code><span><a class="mark" href="#cryptopublicencryptkey-buffer" id="cryptopublicencryptkey-buffer">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_publicencrypt_key_buffer"></a></h4> 4739<div class="api_metadata"> 4740<details class="changelog"><summary>History</summary> 4741<table> 4742<tbody><tr><th>Version</th><th>Changes</th></tr> 4743<tr><td>v15.0.0</td> 4744<td><p>Added string, ArrayBuffer, and CryptoKey as allowable key types. The oaepLabel and passphrase can be ArrayBuffers. The buffer can be a string or ArrayBuffer. All types that accept buffers are limited to a maximum of 2 ** 31 - 1 bytes.</p></td></tr> 4745<tr><td>v12.11.0</td> 4746<td><p>The <code>oaepLabel</code> option was added.</p></td></tr> 4747<tr><td>v12.9.0</td> 4748<td><p>The <code>oaepHash</code> option was added.</p></td></tr> 4749<tr><td>v11.6.0</td> 4750<td><p>This function now supports key objects.</p></td></tr> 4751<tr><td>v0.11.14</td> 4752<td><p><span>Added in: v0.11.14</span></p></td></tr> 4753</tbody></table> 4754</details> 4755</div> 4756<!--lint disable maximum-line-length remark-lint--> 4757<ul> 4758<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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#class-keyobject" class="type"><KeyObject></a> | <a href="webcrypto.html#class-cryptokey" class="type"><CryptoKey></a> 4759<ul> 4760<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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#class-keyobject" class="type"><KeyObject></a> | <a href="webcrypto.html#class-cryptokey" class="type"><CryptoKey></a> 4761A PEM encoded public or private key, <a href="crypto.html#class-keyobject" class="type"><KeyObject></a>, or <a href="webcrypto.html#class-cryptokey" class="type"><CryptoKey></a>.</li> 4762<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. 4763<strong>Default:</strong> <code>'sha1'</code></li> 4764<li><code>oaepLabel</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/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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 4765use for OAEP padding. If not specified, no label is used.</li> 4766<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> An optional 4767passphrase for the private key.</li> 4768<li><code>padding</code> <a href="crypto.html#cryptoconstants" class="type"><crypto.constants></a> An optional padding value defined in 4769<code>crypto.constants</code>, which may be: <code>crypto.constants.RSA_NO_PADDING</code>, 4770<code>crypto.constants.RSA_PKCS1_PADDING</code>, or 4771<code>crypto.constants.RSA_PKCS1_OAEP_PADDING</code>.</li> 4772<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The string encoding to use when <code>buffer</code>, <code>key</code>, 4773<code>oaepLabel</code>, or <code>passphrase</code> are strings.</li> 4774</ul> 4775</li> 4776<li><code>buffer</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/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 4777<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> A new <code>Buffer</code> with the encrypted content.</li> 4778</ul> 4779<!--lint enable maximum-line-length remark-lint--> 4780<p>Encrypts the content of <code>buffer</code> with <code>key</code> and returns a new 4781<a href="buffer.html"><code>Buffer</code></a> with encrypted content. The returned data can be decrypted using 4782the corresponding private key, for example using <a href="#cryptoprivatedecryptprivatekey-buffer"><code>crypto.privateDecrypt()</code></a>.</p> 4783<p>If <code>key</code> is not a <a href="#class-keyobject"><code>KeyObject</code></a>, this function behaves as if 4784<code>key</code> had been passed to <a href="#cryptocreatepublickeykey"><code>crypto.createPublicKey()</code></a>. If it is an 4785object, the <code>padding</code> property can be passed. Otherwise, this function uses 4786<code>RSA_PKCS1_OAEP_PADDING</code>.</p> 4787<p>Because RSA public keys can be derived from private keys, a private key may 4788be passed instead of a public key.</p> 4789<h4><code>crypto.randomBytes(size[, callback])</code><span><a class="mark" href="#cryptorandombytessize-callback" id="cryptorandombytessize-callback">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_randombytes_size_callback"></a></h4> 4790<div class="api_metadata"> 4791<details class="changelog"><summary>History</summary> 4792<table> 4793<tbody><tr><th>Version</th><th>Changes</th></tr> 4794<tr><td>v18.0.0</td> 4795<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 4796<tr><td>v9.0.0</td> 4797<td><p>Passing <code>null</code> as the <code>callback</code> argument now throws <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 4798<tr><td>v0.5.8</td> 4799<td><p><span>Added in: v0.5.8</span></p></td></tr> 4800</tbody></table> 4801</details> 4802</div> 4803<ul> 4804<li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of bytes to generate. The <code>size</code> must 4805not be larger than <code>2**31 - 1</code>.</li> 4806<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 4807<ul> 4808<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 4809<li><code>buf</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 4810</ul> 4811</li> 4812<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> if the <code>callback</code> function is not provided.</li> 4813</ul> 4814<p>Generates cryptographically strong pseudorandom data. The <code>size</code> argument 4815is a number indicating the number of bytes to generate.</p> 4816<p>If a <code>callback</code> function is provided, the bytes are generated asynchronously 4817and the <code>callback</code> function is invoked with two arguments: <code>err</code> and <code>buf</code>. 4818If an error occurs, <code>err</code> will be an <code>Error</code> object; otherwise it is <code>null</code>. The 4819<code>buf</code> argument is a <a href="buffer.html"><code>Buffer</code></a> containing the generated bytes.</p> 4820 4821<pre class="with-15-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-comment">// Asynchronous</span> 4822<span class="hljs-keyword">const</span> { 4823 randomBytes, 4824} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 4825 4826<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> { 4827 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 4828 <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>); 4829});</code><code class="language-js cjs"><span class="hljs-comment">// Asynchronous</span> 4830<span class="hljs-keyword">const</span> { 4831 randomBytes, 4832} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 4833 4834<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> { 4835 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 4836 <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>); 4837});</code><button class="copy-button">copy</button></pre> 4838<p>If the <code>callback</code> function is not provided, the random bytes are generated 4839synchronously and returned as a <a href="buffer.html"><code>Buffer</code></a>. An error will be thrown if 4840there is a problem generating the bytes.</p> 4841 4842<pre class="with-14-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-comment">// Synchronous</span> 4843<span class="hljs-keyword">const</span> { 4844 randomBytes, 4845} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 4846 4847<span class="hljs-keyword">const</span> buf = <span class="hljs-title function_">randomBytes</span>(<span class="hljs-number">256</span>); 4848<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>( 4849 <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><code class="language-js cjs"><span class="hljs-comment">// Synchronous</span> 4850<span class="hljs-keyword">const</span> { 4851 randomBytes, 4852} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 4853 4854<span class="hljs-keyword">const</span> buf = <span class="hljs-title function_">randomBytes</span>(<span class="hljs-number">256</span>); 4855<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>( 4856 <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><button class="copy-button">copy</button></pre> 4857<p>The <code>crypto.randomBytes()</code> method will not complete until there is 4858sufficient entropy available. 4859This should normally never take longer than a few milliseconds. The only time 4860when generating the random bytes may conceivably block for a longer period of 4861time is right after boot, when the whole system is still low on entropy.</p> 4862<p>This API uses libuv's threadpool, which can have surprising and 4863negative performance implications for some applications; see the 4864<a href="cli.html#uv_threadpool_sizesize"><code>UV_THREADPOOL_SIZE</code></a> documentation for more information.</p> 4865<p>The asynchronous version of <code>crypto.randomBytes()</code> is carried out in a single 4866threadpool request. To minimize threadpool task length variation, partition 4867large <code>randomBytes</code> requests when doing so as part of fulfilling a client 4868request.</p> 4869<h4><code>crypto.randomFillSync(buffer[, offset][, size])</code><span><a class="mark" href="#cryptorandomfillsyncbuffer-offset-size" id="cryptorandomfillsyncbuffer-offset-size">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_randomfillsync_buffer_offset_size"></a></h4> 4870<div class="api_metadata"> 4871<details class="changelog"><summary>History</summary> 4872<table> 4873<tbody><tr><th>Version</th><th>Changes</th></tr> 4874<tr><td>v9.0.0</td> 4875<td><p>The <code>buffer</code> argument may be any <code>TypedArray</code> or <code>DataView</code>.</p></td></tr> 4876<tr><td>v7.10.0, v6.13.0</td> 4877<td><p><span>Added in: v7.10.0, v6.13.0</span></p></td></tr> 4878</tbody></table> 4879</details> 4880</div> 4881<ul> 4882<li><code>buffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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. The 4883size of the provided <code>buffer</code> must not be larger than <code>2**31 - 1</code>.</li> 4884<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> 4885<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>. The <code>size</code> must 4886not be larger than <code>2**31 - 1</code>.</li> 4887<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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 4888<code>buffer</code> argument.</li> 4889</ul> 4890<p>Synchronous version of <a href="#cryptorandomfillbuffer-offset-size-callback"><code>crypto.randomFill()</code></a>.</p> 4891 4892<pre class="with-55-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 4893<span class="hljs-keyword">const</span> { randomFillSync } = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 4894 4895<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>); 4896<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">randomFillSync</span>(buf).<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>)); 4897 4898<span class="hljs-title function_">randomFillSync</span>(buf, <span class="hljs-number">5</span>); 4899<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>)); 4900 4901<span class="hljs-comment">// The above is equivalent to the following:</span> 4902<span class="hljs-title function_">randomFillSync</span>(buf, <span class="hljs-number">5</span>, <span class="hljs-number">5</span>); 4903<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><code class="language-js cjs"><span class="hljs-keyword">const</span> { randomFillSync } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 4904<span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>); 4905 4906<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>); 4907<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">randomFillSync</span>(buf).<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>)); 4908 4909<span class="hljs-title function_">randomFillSync</span>(buf, <span class="hljs-number">5</span>); 4910<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>)); 4911 4912<span class="hljs-comment">// The above is equivalent to the following:</span> 4913<span class="hljs-title function_">randomFillSync</span>(buf, <span class="hljs-number">5</span>, <span class="hljs-number">5</span>); 4914<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><button class="copy-button">copy</button></pre> 4915<p>Any <code>ArrayBuffer</code>, <code>TypedArray</code> or <code>DataView</code> instance may be passed as 4916<code>buffer</code>.</p> 4917 4918<pre class="with-55-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 4919<span class="hljs-keyword">const</span> { randomFillSync } = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 4920 4921<span class="hljs-keyword">const</span> a = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint32Array</span>(<span class="hljs-number">10</span>); 4922<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>(<span class="hljs-title function_">randomFillSync</span>(a).<span class="hljs-property">buffer</span>, 4923 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>)); 4924 4925<span class="hljs-keyword">const</span> b = <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>)); 4926<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>(<span class="hljs-title function_">randomFillSync</span>(b).<span class="hljs-property">buffer</span>, 4927 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>)); 4928 4929<span class="hljs-keyword">const</span> c = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayBuffer</span>(<span class="hljs-number">10</span>); 4930<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>(<span class="hljs-title function_">randomFillSync</span>(c)).<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { randomFillSync } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 4931<span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>); 4932 4933<span class="hljs-keyword">const</span> a = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint32Array</span>(<span class="hljs-number">10</span>); 4934<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>(<span class="hljs-title function_">randomFillSync</span>(a).<span class="hljs-property">buffer</span>, 4935 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>)); 4936 4937<span class="hljs-keyword">const</span> b = <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>)); 4938<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>(<span class="hljs-title function_">randomFillSync</span>(b).<span class="hljs-property">buffer</span>, 4939 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>)); 4940 4941<span class="hljs-keyword">const</span> c = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayBuffer</span>(<span class="hljs-number">10</span>); 4942<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>(<span class="hljs-title function_">randomFillSync</span>(c)).<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));</code><button class="copy-button">copy</button></pre> 4943<h4><code>crypto.randomFill(buffer[, offset][, size], callback)</code><span><a class="mark" href="#cryptorandomfillbuffer-offset-size-callback" id="cryptorandomfillbuffer-offset-size-callback">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_randomfill_buffer_offset_size_callback"></a></h4> 4944<div class="api_metadata"> 4945<details class="changelog"><summary>History</summary> 4946<table> 4947<tbody><tr><th>Version</th><th>Changes</th></tr> 4948<tr><td>v18.0.0</td> 4949<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 4950<tr><td>v9.0.0</td> 4951<td><p>The <code>buffer</code> argument may be any <code>TypedArray</code> or <code>DataView</code>.</p></td></tr> 4952<tr><td>v7.10.0, v6.13.0</td> 4953<td><p><span>Added in: v7.10.0, v6.13.0</span></p></td></tr> 4954</tbody></table> 4955</details> 4956</div> 4957<ul> 4958<li><code>buffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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. The 4959size of the provided <code>buffer</code> must not be larger than <code>2**31 - 1</code>.</li> 4960<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> 4961<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>. The <code>size</code> must 4962not be larger than <code>2**31 - 1</code>.</li> 4963<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> 4964</ul> 4965<p>This function is similar to <a href="#cryptorandombytessize-callback"><code>crypto.randomBytes()</code></a> but requires the first 4966argument to be a <a href="buffer.html"><code>Buffer</code></a> that will be filled. It also 4967requires that a callback is passed in.</p> 4968<p>If the <code>callback</code> function is not provided, an error will be thrown.</p> 4969 4970<pre class="with-51-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 4971<span class="hljs-keyword">const</span> { randomFill } = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 4972 4973<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>); 4974<span class="hljs-title function_">randomFill</span>(buf, <span class="hljs-function">(<span class="hljs-params">err, buf</span>) =></span> { 4975 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 4976 <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>)); 4977}); 4978 4979<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> { 4980 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 4981 <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>)); 4982}); 4983 4984<span class="hljs-comment">// The above is equivalent to the following:</span> 4985<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> { 4986 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 4987 <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>)); 4988});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { randomFill } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 4989<span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>); 4990 4991<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>); 4992<span class="hljs-title function_">randomFill</span>(buf, <span class="hljs-function">(<span class="hljs-params">err, buf</span>) =></span> { 4993 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 4994 <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>)); 4995}); 4996 4997<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> { 4998 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 4999 <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>)); 5000}); 5001 5002<span class="hljs-comment">// The above is equivalent to the following:</span> 5003<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> { 5004 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 5005 <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>)); 5006});</code><button class="copy-button">copy</button></pre> 5007<p>Any <code>ArrayBuffer</code>, <code>TypedArray</code>, or <code>DataView</code> instance may be passed as 5008<code>buffer</code>.</p> 5009<p>While this includes instances of <code>Float32Array</code> and <code>Float64Array</code>, this 5010function should not be used to generate random floating-point numbers. The 5011result may contain <code>+Infinity</code>, <code>-Infinity</code>, and <code>NaN</code>, and even if the array 5012contains finite numbers only, they are not drawn from a uniform random 5013distribution and have no meaningful lower or upper bounds.</p> 5014 5015<pre class="with-51-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 5016<span class="hljs-keyword">const</span> { randomFill } = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 5017 5018<span class="hljs-keyword">const</span> a = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint32Array</span>(<span class="hljs-number">10</span>); 5019<span class="hljs-title function_">randomFill</span>(a, <span class="hljs-function">(<span class="hljs-params">err, buf</span>) =></span> { 5020 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 5021 <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>) 5022 .<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>)); 5023}); 5024 5025<span class="hljs-keyword">const</span> b = <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>)); 5026<span class="hljs-title function_">randomFill</span>(b, <span class="hljs-function">(<span class="hljs-params">err, buf</span>) =></span> { 5027 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 5028 <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>) 5029 .<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>)); 5030}); 5031 5032<span class="hljs-keyword">const</span> c = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayBuffer</span>(<span class="hljs-number">10</span>); 5033<span class="hljs-title function_">randomFill</span>(c, <span class="hljs-function">(<span class="hljs-params">err, buf</span>) =></span> { 5034 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 5035 <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-title function_">toString</span>(<span class="hljs-string">'hex'</span>)); 5036});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { randomFill } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 5037<span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>); 5038 5039<span class="hljs-keyword">const</span> a = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint32Array</span>(<span class="hljs-number">10</span>); 5040<span class="hljs-title function_">randomFill</span>(a, <span class="hljs-function">(<span class="hljs-params">err, buf</span>) =></span> { 5041 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 5042 <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>) 5043 .<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>)); 5044}); 5045 5046<span class="hljs-keyword">const</span> b = <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>)); 5047<span class="hljs-title function_">randomFill</span>(b, <span class="hljs-function">(<span class="hljs-params">err, buf</span>) =></span> { 5048 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 5049 <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>) 5050 .<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>)); 5051}); 5052 5053<span class="hljs-keyword">const</span> c = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayBuffer</span>(<span class="hljs-number">10</span>); 5054<span class="hljs-title function_">randomFill</span>(c, <span class="hljs-function">(<span class="hljs-params">err, buf</span>) =></span> { 5055 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 5056 <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-title function_">toString</span>(<span class="hljs-string">'hex'</span>)); 5057});</code><button class="copy-button">copy</button></pre> 5058<p>This API uses libuv's threadpool, which can have surprising and 5059negative performance implications for some applications; see the 5060<a href="cli.html#uv_threadpool_sizesize"><code>UV_THREADPOOL_SIZE</code></a> documentation for more information.</p> 5061<p>The asynchronous version of <code>crypto.randomFill()</code> is carried out in a single 5062threadpool request. To minimize threadpool task length variation, partition 5063large <code>randomFill</code> requests when doing so as part of fulfilling a client 5064request.</p> 5065<h4><code>crypto.randomInt([min, ]max[, callback])</code><span><a class="mark" href="#cryptorandomintmin-max-callback" id="cryptorandomintmin-max-callback">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_randomint_min_max_callback"></a></h4> 5066<div class="api_metadata"> 5067<details class="changelog"><summary>History</summary> 5068<table> 5069<tbody><tr><th>Version</th><th>Changes</th></tr> 5070<tr><td>v18.0.0</td> 5071<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 5072<tr><td>v14.10.0, v12.19.0</td> 5073<td><p><span>Added in: v14.10.0, v12.19.0</span></p></td></tr> 5074</tbody></table> 5075</details> 5076</div> 5077<ul> 5078<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> 5079<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> 5080<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> 5081</ul> 5082<p>Return a random integer <code>n</code> such that <code>min <= n < max</code>. This 5083implementation avoids <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#Modulo_bias">modulo bias</a>.</p> 5084<p>The range (<code>max - min</code>) must be less than 2<sup>48</sup>. <code>min</code> and <code>max</code> must 5085be <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isSafeInteger">safe integers</a>.</p> 5086<p>If the <code>callback</code> function is not provided, the random integer is 5087generated synchronously.</p> 5088 5089<pre class="with-15-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-comment">// Asynchronous</span> 5090<span class="hljs-keyword">const</span> { 5091 randomInt, 5092} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 5093 5094<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> { 5095 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 5096 <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>); 5097});</code><code class="language-js cjs"><span class="hljs-comment">// Asynchronous</span> 5098<span class="hljs-keyword">const</span> { 5099 randomInt, 5100} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 5101 5102<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> { 5103 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 5104 <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>); 5105});</code><button class="copy-button">copy</button></pre> 5106 5107<pre class="with-14-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-comment">// Synchronous</span> 5108<span class="hljs-keyword">const</span> { 5109 randomInt, 5110} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 5111 5112<span class="hljs-keyword">const</span> n = <span class="hljs-title function_">randomInt</span>(<span class="hljs-number">3</span>); 5113<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><code class="language-js cjs"><span class="hljs-comment">// Synchronous</span> 5114<span class="hljs-keyword">const</span> { 5115 randomInt, 5116} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 5117 5118<span class="hljs-keyword">const</span> n = <span class="hljs-title function_">randomInt</span>(<span class="hljs-number">3</span>); 5119<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><button class="copy-button">copy</button></pre> 5120 5121<pre class="with-22-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-comment">// With `min` argument</span> 5122<span class="hljs-keyword">const</span> { 5123 randomInt, 5124} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 5125 5126<span class="hljs-keyword">const</span> n = <span class="hljs-title function_">randomInt</span>(<span class="hljs-number">1</span>, <span class="hljs-number">7</span>); 5127<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><code class="language-js cjs"><span class="hljs-comment">// With `min` argument</span> 5128<span class="hljs-keyword">const</span> { 5129 randomInt, 5130} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 5131 5132<span class="hljs-keyword">const</span> n = <span class="hljs-title function_">randomInt</span>(<span class="hljs-number">1</span>, <span class="hljs-number">7</span>); 5133<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><button class="copy-button">copy</button></pre> 5134<h4><code>crypto.randomUUID([options])</code><span><a class="mark" href="#cryptorandomuuidoptions" id="cryptorandomuuidoptions">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_randomuuid_options"></a></h4> 5135<div class="api_metadata"> 5136<span>Added in: v15.6.0, v14.17.0</span> 5137</div> 5138<ul> 5139<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 5140<ul> 5141<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, 5142Node.js generates and caches enough 5143random data to generate up to 128 random UUIDs. To generate a UUID 5144without using the cache, set <code>disableEntropyCache</code> to <code>true</code>. 5145<strong>Default:</strong> <code>false</code>.</li> 5146</ul> 5147</li> 5148<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 5149</ul> 5150<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 5151cryptographic pseudorandom number generator.</p> 5152<h4><code>crypto.scrypt(password, salt, keylen[, options], callback)</code><span><a class="mark" href="#cryptoscryptpassword-salt-keylen-options-callback" id="cryptoscryptpassword-salt-keylen-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_scrypt_password_salt_keylen_options_callback"></a></h4> 5153<div class="api_metadata"> 5154<details class="changelog"><summary>History</summary> 5155<table> 5156<tbody><tr><th>Version</th><th>Changes</th></tr> 5157<tr><td>v18.0.0</td> 5158<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 5159<tr><td>v15.0.0</td> 5160<td><p>The password and salt arguments can also be ArrayBuffer instances.</p></td></tr> 5161<tr><td>v12.8.0, v10.17.0</td> 5162<td><p>The <code>maxmem</code> value can now be any safe integer.</p></td></tr> 5163<tr><td>v10.9.0</td> 5164<td><p>The <code>cost</code>, <code>blockSize</code> and <code>parallelization</code> option names have been added.</p></td></tr> 5165<tr><td>v10.5.0</td> 5166<td><p><span>Added in: v10.5.0</span></p></td></tr> 5167</tbody></table> 5168</details> 5169</div> 5170<ul> 5171<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 5172<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 5173<li><code>keylen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 5174<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 5175<ul> 5176<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 5177than one. <strong>Default:</strong> <code>16384</code>.</li> 5178<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> 5179<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> 5180<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> 5181<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> 5182<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> 5183<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) 5184<code>128 * N * r > maxmem</code>. <strong>Default:</strong> <code>32 * 1024 * 1024</code>.</li> 5185</ul> 5186</li> 5187<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 5188<ul> 5189<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 5190<li><code>derivedKey</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 5191</ul> 5192</li> 5193</ul> 5194<p>Provides an asynchronous <a href="https://en.wikipedia.org/wiki/Scrypt">scrypt</a> implementation. Scrypt is a password-based 5195key derivation function that is designed to be expensive computationally and 5196memory-wise in order to make brute-force attacks unrewarding.</p> 5197<p>The <code>salt</code> should be as unique as possible. It is recommended that a salt is 5198random 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> 5199<p>When passing strings for <code>password</code> or <code>salt</code>, please consider 5200<a href="#using-strings-as-inputs-to-cryptographic-apis">caveats when using strings as inputs to cryptographic APIs</a>.</p> 5201<p>The <code>callback</code> function is called with two arguments: <code>err</code> and <code>derivedKey</code>. 5202<code>err</code> is an exception object when key derivation fails, otherwise <code>err</code> is 5203<code>null</code>. <code>derivedKey</code> is passed to the callback as a <a href="buffer.html"><code>Buffer</code></a>.</p> 5204<p>An exception is thrown when any of the input arguments specify invalid values 5205or types.</p> 5206 5207<pre class="with-9-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">const</span> { 5208 scrypt, 5209} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 5210 5211<span class="hljs-comment">// Using the factory defaults.</span> 5212<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> { 5213 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 5214 <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> 5215}); 5216<span class="hljs-comment">// Using a custom N parameter. Must be a power of two.</span> 5217<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> { 5218 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 5219 <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> 5220});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 5221 scrypt, 5222} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 5223 5224<span class="hljs-comment">// Using the factory defaults.</span> 5225<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> { 5226 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 5227 <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> 5228}); 5229<span class="hljs-comment">// Using a custom N parameter. Must be a power of two.</span> 5230<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> { 5231 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 5232 <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> 5233});</code><button class="copy-button">copy</button></pre> 5234<h4><code>crypto.scryptSync(password, salt, keylen[, options])</code><span><a class="mark" href="#cryptoscryptsyncpassword-salt-keylen-options" id="cryptoscryptsyncpassword-salt-keylen-options">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_scryptsync_password_salt_keylen_options"></a></h4> 5235<div class="api_metadata"> 5236<details class="changelog"><summary>History</summary> 5237<table> 5238<tbody><tr><th>Version</th><th>Changes</th></tr> 5239<tr><td>v12.8.0, v10.17.0</td> 5240<td><p>The <code>maxmem</code> value can now be any safe integer.</p></td></tr> 5241<tr><td>v10.9.0</td> 5242<td><p>The <code>cost</code>, <code>blockSize</code> and <code>parallelization</code> option names have been added.</p></td></tr> 5243<tr><td>v10.5.0</td> 5244<td><p><span>Added in: v10.5.0</span></p></td></tr> 5245</tbody></table> 5246</details> 5247</div> 5248<ul> 5249<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#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> 5250<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#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> 5251<li><code>keylen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 5252<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 5253<ul> 5254<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 5255than one. <strong>Default:</strong> <code>16384</code>.</li> 5256<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> 5257<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> 5258<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> 5259<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> 5260<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> 5261<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) 5262<code>128 * N * r > maxmem</code>. <strong>Default:</strong> <code>32 * 1024 * 1024</code>.</li> 5263</ul> 5264</li> 5265<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 5266</ul> 5267<p>Provides a synchronous <a href="https://en.wikipedia.org/wiki/Scrypt">scrypt</a> implementation. Scrypt is a password-based 5268key derivation function that is designed to be expensive computationally and 5269memory-wise in order to make brute-force attacks unrewarding.</p> 5270<p>The <code>salt</code> should be as unique as possible. It is recommended that a salt is 5271random 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> 5272<p>When passing strings for <code>password</code> or <code>salt</code>, please consider 5273<a href="#using-strings-as-inputs-to-cryptographic-apis">caveats when using strings as inputs to cryptographic APIs</a>.</p> 5274<p>An exception is thrown when key derivation fails, otherwise the derived key is 5275returned as a <a href="buffer.html"><code>Buffer</code></a>.</p> 5276<p>An exception is thrown when any of the input arguments specify invalid values 5277or types.</p> 5278 5279<pre class="with-13-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">const</span> { 5280 scryptSync, 5281} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 5282<span class="hljs-comment">// Using the factory defaults.</span> 5283 5284<span class="hljs-keyword">const</span> key1 = <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>); 5285<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> 5286<span class="hljs-comment">// Using a custom N parameter. Must be a power of two.</span> 5287<span class="hljs-keyword">const</span> key2 = <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> }); 5288<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><code class="language-js cjs"><span class="hljs-keyword">const</span> { 5289 scryptSync, 5290} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 5291<span class="hljs-comment">// Using the factory defaults.</span> 5292 5293<span class="hljs-keyword">const</span> key1 = <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>); 5294<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> 5295<span class="hljs-comment">// Using a custom N parameter. Must be a power of two.</span> 5296<span class="hljs-keyword">const</span> key2 = <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> }); 5297<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><button class="copy-button">copy</button></pre> 5298<h4><code>crypto.secureHeapUsed()</code><span><a class="mark" href="#cryptosecureheapused" id="cryptosecureheapused">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_secureheapused"></a></h4> 5299<div class="api_metadata"> 5300<span>Added in: v15.6.0</span> 5301</div> 5302<ul> 5303<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 5304<ul> 5305<li><code>total</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The total allocated secure heap size as specified 5306using the <code>--secure-heap=n</code> command-line flag.</li> 5307<li><code>min</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The minimum allocation from the secure heap as 5308specified using the <code>--secure-heap-min</code> command-line flag.</li> 5309<li><code>used</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The total number of bytes currently allocated from 5310the secure heap.</li> 5311<li><code>utilization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The calculated ratio of <code>used</code> to <code>total</code> 5312allocated bytes.</li> 5313</ul> 5314</li> 5315</ul> 5316<h4><code>crypto.setEngine(engine[, flags])</code><span><a class="mark" href="#cryptosetengineengine-flags" id="cryptosetengineengine-flags">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_setengine_engine_flags"></a></h4> 5317<div class="api_metadata"> 5318<span>Added in: v0.11.11</span> 5319</div> 5320<ul> 5321<li><code>engine</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 5322<li><code>flags</code> <a href="crypto.html#cryptoconstants" class="type"><crypto.constants></a> <strong>Default:</strong> <code>crypto.constants.ENGINE_METHOD_ALL</code></li> 5323</ul> 5324<p>Load and set the <code>engine</code> for some or all OpenSSL functions (selected by flags).</p> 5325<p><code>engine</code> could be either an id or a path to the engine's shared library.</p> 5326<p>The optional <code>flags</code> argument uses <code>ENGINE_METHOD_ALL</code> by default. The <code>flags</code> 5327is a bit field taking one of or a mix of the following flags (defined in 5328<code>crypto.constants</code>):</p> 5329<ul> 5330<li><code>crypto.constants.ENGINE_METHOD_RSA</code></li> 5331<li><code>crypto.constants.ENGINE_METHOD_DSA</code></li> 5332<li><code>crypto.constants.ENGINE_METHOD_DH</code></li> 5333<li><code>crypto.constants.ENGINE_METHOD_RAND</code></li> 5334<li><code>crypto.constants.ENGINE_METHOD_EC</code></li> 5335<li><code>crypto.constants.ENGINE_METHOD_CIPHERS</code></li> 5336<li><code>crypto.constants.ENGINE_METHOD_DIGESTS</code></li> 5337<li><code>crypto.constants.ENGINE_METHOD_PKEY_METHS</code></li> 5338<li><code>crypto.constants.ENGINE_METHOD_PKEY_ASN1_METHS</code></li> 5339<li><code>crypto.constants.ENGINE_METHOD_ALL</code></li> 5340<li><code>crypto.constants.ENGINE_METHOD_NONE</code></li> 5341</ul> 5342<h4><code>crypto.setFips(bool)</code><span><a class="mark" href="#cryptosetfipsbool" id="cryptosetfipsbool">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_setfips_bool"></a></h4> 5343<div class="api_metadata"> 5344<span>Added in: v10.0.0</span> 5345</div> 5346<ul> 5347<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> 5348</ul> 5349<p>Enables the FIPS compliant crypto provider in a FIPS-enabled Node.js build. 5350Throws an error if FIPS mode is not available.</p> 5351<h4><code>crypto.sign(algorithm, data, key[, callback])</code><span><a class="mark" href="#cryptosignalgorithm-data-key-callback" id="cryptosignalgorithm-data-key-callback">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_sign_algorithm_data_key_callback"></a></h4> 5352<div class="api_metadata"> 5353<details class="changelog"><summary>History</summary> 5354<table> 5355<tbody><tr><th>Version</th><th>Changes</th></tr> 5356<tr><td>v18.0.0</td> 5357<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 5358<tr><td>v15.12.0</td> 5359<td><p>Optional callback argument added.</p></td></tr> 5360<tr><td>v13.2.0, v12.16.0</td> 5361<td><p>This function now supports IEEE-P1363 DSA and ECDSA signatures.</p></td></tr> 5362<tr><td>v12.0.0</td> 5363<td><p><span>Added in: v12.0.0</span></p></td></tr> 5364</tbody></table> 5365</details> 5366</div> 5367<!--lint disable maximum-line-length remark-lint--> 5368<ul> 5369<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> 5370<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 5371<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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#class-keyobject" class="type"><KeyObject></a> | <a href="webcrypto.html#class-cryptokey" class="type"><CryptoKey></a></li> 5372<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 5373<ul> 5374<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 5375<li><code>signature</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 5376</ul> 5377</li> 5378<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> if the <code>callback</code> function is not provided.</li> 5379</ul> 5380<!--lint enable maximum-line-length remark-lint--> 5381<p>Calculates and returns the signature for <code>data</code> using the given private key and 5382algorithm. If <code>algorithm</code> is <code>null</code> or <code>undefined</code>, then the algorithm is 5383dependent upon the key type (especially Ed25519 and Ed448).</p> 5384<p>If <code>key</code> is not a <a href="#class-keyobject"><code>KeyObject</code></a>, this function behaves as if <code>key</code> had been 5385passed to <a href="#cryptocreateprivatekeykey"><code>crypto.createPrivateKey()</code></a>. If it is an object, the following 5386additional properties can be passed:</p> 5387<ul> 5388<li> 5389<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 5390format of the generated signature. It can be one of the following:</p> 5391<ul> 5392<li><code>'der'</code> (default): DER-encoded ASN.1 signature structure encoding <code>(r, s)</code>.</li> 5393<li><code>'ieee-p1363'</code>: Signature format <code>r || s</code> as proposed in IEEE-P1363.</li> 5394</ul> 5395</li> 5396<li> 5397<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> 5398<ul> 5399<li><code>crypto.constants.RSA_PKCS1_PADDING</code> (default)</li> 5400<li><code>crypto.constants.RSA_PKCS1_PSS_PADDING</code></li> 5401</ul> 5402<p><code>RSA_PKCS1_PSS_PADDING</code> will use MGF1 with the same hash function 5403used 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> 5404</li> 5405<li> 5406<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 5407<code>RSA_PKCS1_PSS_PADDING</code>. The special value 5408<code>crypto.constants.RSA_PSS_SALTLEN_DIGEST</code> sets the salt length to the digest 5409size, <code>crypto.constants.RSA_PSS_SALTLEN_MAX_SIGN</code> (default) sets it to the 5410maximum permissible value.</p> 5411</li> 5412</ul> 5413<p>If the <code>callback</code> function is provided this function uses libuv's threadpool.</p> 5414<h4><code>crypto.subtle</code><span><a class="mark" href="#cryptosubtle" id="cryptosubtle">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_subtle"></a></h4> 5415<div class="api_metadata"> 5416<span>Added in: v17.4.0</span> 5417</div> 5418<ul> 5419<li>Type: <a href="webcrypto.html#class-subtlecrypto" class="type"><SubtleCrypto></a></li> 5420</ul> 5421<p>A convenient alias for <a href="webcrypto.html#class-subtlecrypto"><code>crypto.webcrypto.subtle</code></a>.</p> 5422<h4><code>crypto.timingSafeEqual(a, b)</code><span><a class="mark" href="#cryptotimingsafeequala-b" id="cryptotimingsafeequala-b">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_timingsafeequal_a_b"></a></h4> 5423<div class="api_metadata"> 5424<details class="changelog"><summary>History</summary> 5425<table> 5426<tbody><tr><th>Version</th><th>Changes</th></tr> 5427<tr><td>v15.0.0</td> 5428<td><p>The a and b arguments can also be ArrayBuffer.</p></td></tr> 5429<tr><td>v6.6.0</td> 5430<td><p><span>Added in: v6.6.0</span></p></td></tr> 5431</tbody></table> 5432</details> 5433</div> 5434<ul> 5435<li><code>a</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 5436<li><code>b</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 5437<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 5438</ul> 5439<p>This function compares the underlying bytes that represent the given 5440<code>ArrayBuffer</code>, <code>TypedArray</code>, or <code>DataView</code> instances using a constant-time 5441algorithm.</p> 5442<p>This function does not leak timing information that 5443would allow an attacker to guess one of the values. This is suitable for 5444comparing HMAC digests or secret values like authentication cookies or 5445<a href="https://www.w3.org/TR/capability-urls/">capability urls</a>.</p> 5446<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 5447must have the same byte length. An error is thrown if <code>a</code> and <code>b</code> have 5448different byte lengths.</p> 5449<p>If at least one of <code>a</code> and <code>b</code> is a <code>TypedArray</code> with more than one byte per 5450entry, such as <code>Uint16Array</code>, the result will be computed using the platform 5451byte order.</p> 5452<p><strong class="critical">When both of the inputs are <code>Float32Array</code>s or 5453<code>Float64Array</code>s, this function might return unexpected results due to IEEE 754 5454encoding of floating-point numbers. In particular, neither <code>x === y</code> nor 5455<code>Object.is(x, y)</code> implies that the byte representations of two floating-point 5456numbers <code>x</code> and <code>y</code> are equal.</strong></p> 5457<p>Use of <code>crypto.timingSafeEqual</code> does not guarantee that the <em>surrounding</em> code 5458is timing-safe. Care should be taken to ensure that the surrounding code does 5459not introduce timing vulnerabilities.</p> 5460<h4><code>crypto.verify(algorithm, data, key, signature[, callback])</code><span><a class="mark" href="#cryptoverifyalgorithm-data-key-signature-callback" id="cryptoverifyalgorithm-data-key-signature-callback">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_verify_algorithm_data_key_signature_callback"></a></h4> 5461<div class="api_metadata"> 5462<details class="changelog"><summary>History</summary> 5463<table> 5464<tbody><tr><th>Version</th><th>Changes</th></tr> 5465<tr><td>v18.0.0</td> 5466<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 5467<tr><td>v15.12.0</td> 5468<td><p>Optional callback argument added.</p></td></tr> 5469<tr><td>v15.0.0</td> 5470<td><p>The data, key, and signature arguments can also be ArrayBuffer.</p></td></tr> 5471<tr><td>v13.2.0, v12.16.0</td> 5472<td><p>This function now supports IEEE-P1363 DSA and ECDSA signatures.</p></td></tr> 5473<tr><td>v12.0.0</td> 5474<td><p><span>Added in: v12.0.0</span></p></td></tr> 5475</tbody></table> 5476</details> 5477</div> 5478<!--lint disable maximum-line-length remark-lint--> 5479<ul> 5480<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> 5481<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 5482<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="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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#class-keyobject" class="type"><KeyObject></a> | <a href="webcrypto.html#class-cryptokey" class="type"><CryptoKey></a></li> 5483<li><code>signature</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="buffer.html#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> 5484<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 5485<ul> 5486<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 5487<li><code>result</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 5488</ul> 5489</li> 5490<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 5491signature for the data and public key if the <code>callback</code> function is not 5492provided.</li> 5493</ul> 5494<!--lint enable maximum-line-length remark-lint--> 5495<p>Verifies the given signature for <code>data</code> using the given key and algorithm. If 5496<code>algorithm</code> is <code>null</code> or <code>undefined</code>, then the algorithm is dependent upon the 5497key type (especially Ed25519 and Ed448).</p> 5498<p>If <code>key</code> is not a <a href="#class-keyobject"><code>KeyObject</code></a>, this function behaves as if <code>key</code> had been 5499passed to <a href="#cryptocreatepublickeykey"><code>crypto.createPublicKey()</code></a>. If it is an object, the following 5500additional properties can be passed:</p> 5501<ul> 5502<li> 5503<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 5504format of the signature. It can be one of the following:</p> 5505<ul> 5506<li><code>'der'</code> (default): DER-encoded ASN.1 signature structure encoding <code>(r, s)</code>.</li> 5507<li><code>'ieee-p1363'</code>: Signature format <code>r || s</code> as proposed in IEEE-P1363.</li> 5508</ul> 5509</li> 5510<li> 5511<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> 5512<ul> 5513<li><code>crypto.constants.RSA_PKCS1_PADDING</code> (default)</li> 5514<li><code>crypto.constants.RSA_PKCS1_PSS_PADDING</code></li> 5515</ul> 5516<p><code>RSA_PKCS1_PSS_PADDING</code> will use MGF1 with the same hash function 5517used 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> 5518</li> 5519<li> 5520<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 5521<code>RSA_PKCS1_PSS_PADDING</code>. The special value 5522<code>crypto.constants.RSA_PSS_SALTLEN_DIGEST</code> sets the salt length to the digest 5523size, <code>crypto.constants.RSA_PSS_SALTLEN_MAX_SIGN</code> (default) sets it to the 5524maximum permissible value.</p> 5525</li> 5526</ul> 5527<p>The <code>signature</code> argument is the previously calculated signature for the <code>data</code>.</p> 5528<p>Because public keys can be derived from private keys, a private key or a public 5529key may be passed for <code>key</code>.</p> 5530<p>If the <code>callback</code> function is provided this function uses libuv's threadpool.</p> 5531<h4><code>crypto.webcrypto</code><span><a class="mark" href="#cryptowebcrypto" id="cryptowebcrypto">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_webcrypto"></a></h4> 5532<div class="api_metadata"> 5533<span>Added in: v15.0.0</span> 5534</div> 5535<p>Type: <a href="webcrypto.html#class-crypto" class="type"><Crypto></a> An implementation of the Web Crypto API standard.</p> 5536<p>See the <a href="webcrypto.html">Web Crypto API documentation</a> for details.</p> 5537</section><section><h3>Notes<span><a class="mark" href="#notes" id="notes">#</a></span><a aria-hidden="true" class="legacy" id="crypto_notes"></a></h3> 5538<h4>Using strings as inputs to cryptographic APIs<span><a class="mark" href="#using-strings-as-inputs-to-cryptographic-apis" id="using-strings-as-inputs-to-cryptographic-apis">#</a></span><a aria-hidden="true" class="legacy" id="crypto_using_strings_as_inputs_to_cryptographic_apis"></a></h4> 5539<p>For historical reasons, many cryptographic APIs provided by Node.js accept 5540strings as inputs where the underlying cryptographic algorithm works on byte 5541sequences. These instances include plaintexts, ciphertexts, symmetric keys, 5542initialization vectors, passphrases, salts, authentication tags, 5543and additional authenticated data.</p> 5544<p>When passing strings to cryptographic APIs, consider the following factors.</p> 5545<ul> 5546<li> 5547<p>Not all byte sequences are valid UTF-8 strings. Therefore, when a byte 5548sequence of length <code>n</code> is derived from a string, its entropy is generally 5549lower than the entropy of a random or pseudorandom <code>n</code> byte sequence. 5550For example, no UTF-8 string will result in the byte sequence <code>c0 af</code>. Secret 5551keys should almost exclusively be random or pseudorandom byte sequences.</p> 5552</li> 5553<li> 5554<p>Similarly, when converting random or pseudorandom byte sequences to UTF-8 5555strings, subsequences that do not represent valid code points may be replaced 5556by the Unicode replacement character (<code>U+FFFD</code>). The byte representation of 5557the resulting Unicode string may, therefore, not be equal to the byte sequence 5558that the string was created from.</p> 5559<pre><code class="language-js"><span class="hljs-keyword">const</span> original = [<span class="hljs-number">0xc0</span>, <span class="hljs-number">0xaf</span>]; 5560<span class="hljs-keyword">const</span> bytesAsString = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(original).<span class="hljs-title function_">toString</span>(<span class="hljs-string">'utf8'</span>); 5561<span class="hljs-keyword">const</span> stringAsBytes = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(bytesAsString, <span class="hljs-string">'utf8'</span>); 5562<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(stringAsBytes); 5563<span class="hljs-comment">// Prints '<Buffer ef bf bd ef bf bd>'.</span></code> <button class="copy-button">copy</button></pre> 5564<p>The outputs of ciphers, hash functions, signature algorithms, and key 5565derivation functions are pseudorandom byte sequences and should not be 5566used as Unicode strings.</p> 5567</li> 5568<li> 5569<p>When strings are obtained from user input, some Unicode characters can be 5570represented in multiple equivalent ways that result in different byte 5571sequences. For example, when passing a user passphrase to a key derivation 5572function, such as PBKDF2 or scrypt, the result of the key derivation function 5573depends on whether the string uses composed or decomposed characters. Node.js 5574does not normalize character representations. Developers should consider using 5575<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize"><code>String.prototype.normalize()</code></a> on user inputs before passing them to 5576cryptographic APIs.</p> 5577</li> 5578</ul> 5579<h4>Legacy streams API (prior to Node.js 0.10)<span><a class="mark" href="#legacy-streams-api-prior-to-nodejs-010" id="legacy-streams-api-prior-to-nodejs-010">#</a></span><a aria-hidden="true" class="legacy" id="crypto_legacy_streams_api_prior_to_node_js_0_10"></a></h4> 5580<p>The Crypto module was added to Node.js before there was the concept of a 5581unified Stream API, and before there were <a href="buffer.html"><code>Buffer</code></a> objects for handling 5582binary data. As such, many <code>crypto</code> classes have methods not 5583typically found on other Node.js classes that implement the <a href="stream.html">streams</a> 5584API (e.g. <code>update()</code>, <code>final()</code>, or <code>digest()</code>). Also, many methods accepted 5585and returned <code>'latin1'</code> encoded strings by default rather than <code>Buffer</code>s. This 5586default was changed after Node.js v0.8 to use <a href="buffer.html"><code>Buffer</code></a> objects by default 5587instead.</p> 5588<h4>Support for weak or compromised algorithms<span><a class="mark" href="#support-for-weak-or-compromised-algorithms" id="support-for-weak-or-compromised-algorithms">#</a></span><a aria-hidden="true" class="legacy" id="crypto_support_for_weak_or_compromised_algorithms"></a></h4> 5589<p>The <code>node:crypto</code> module still supports some algorithms which are already 5590compromised and are not recommended for use. The API also allows 5591the use of ciphers and hashes with a small key size that are too weak for safe 5592use.</p> 5593<p>Users should take full responsibility for selecting the crypto 5594algorithm and key size according to their security requirements.</p> 5595<p>Based on the recommendations of <a href="https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf">NIST SP 800-131A</a>:</p> 5596<ul> 5597<li>MD5 and SHA-1 are no longer acceptable where collision resistance is 5598required such as digital signatures.</li> 5599<li>The key used with RSA, DSA, and DH algorithms is recommended to have 5600at least 2048 bits and that of the curve of ECDSA and ECDH at least 5601224 bits, to be safe to use for several years.</li> 5602<li>The DH groups of <code>modp1</code>, <code>modp2</code> and <code>modp5</code> have a key size 5603smaller than 2048 bits and are not recommended.</li> 5604</ul> 5605<p>See the reference for other recommendations and details.</p> 5606<p>Some algorithms that have known weaknesses and are of little relevance in 5607practice are only available through the <a href="cli.html#--openssl-legacy-provider">legacy provider</a>, which is not 5608enabled by default.</p> 5609<h4>CCM mode<span><a class="mark" href="#ccm-mode" id="ccm-mode">#</a></span><a aria-hidden="true" class="legacy" id="crypto_ccm_mode"></a></h4> 5610<p>CCM is one of the supported <a href="https://en.wikipedia.org/wiki/Authenticated_encryption">AEAD algorithms</a>. Applications which use this 5611mode must adhere to certain restrictions when using the cipher API:</p> 5612<ul> 5613<li>The authentication tag length must be specified during cipher creation by 5614setting the <code>authTagLength</code> option and must be one of 4, 6, 8, 10, 12, 14 or 561516 bytes.</li> 5616<li>The length of the initialization vector (nonce) <code>N</code> must be between 7 and 13 5617bytes (<code>7 ≤ N ≤ 13</code>).</li> 5618<li>The length of the plaintext is limited to <code>2 ** (8 * (15 - N))</code> bytes.</li> 5619<li>When decrypting, the authentication tag must be set via <code>setAuthTag()</code> before 5620calling <code>update()</code>. 5621Otherwise, decryption will fail and <code>final()</code> will throw an error in 5622compliance with section 2.6 of <a href="https://www.rfc-editor.org/rfc/rfc3610.txt">RFC 3610</a>.</li> 5623<li>Using stream methods such as <code>write(data)</code>, <code>end(data)</code> or <code>pipe()</code> in CCM 5624mode might fail as CCM cannot handle more than one chunk of data per instance.</li> 5625<li>When passing additional authenticated data (AAD), the length of the actual 5626message in bytes must be passed to <code>setAAD()</code> via the <code>plaintextLength</code> 5627option. 5628Many crypto libraries include the authentication tag in the ciphertext, 5629which means that they produce ciphertexts of the length 5630<code>plaintextLength + authTagLength</code>. Node.js does not include the authentication 5631tag, so the ciphertext length is always <code>plaintextLength</code>. 5632This is not necessary if no AAD is used.</li> 5633<li>As CCM processes the whole message at once, <code>update()</code> must be called exactly 5634once.</li> 5635<li>Even though calling <code>update()</code> is sufficient to encrypt/decrypt the message, 5636applications <em>must</em> call <code>final()</code> to compute or verify the 5637authentication tag.</li> 5638</ul> 5639 5640<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>; 5641<span class="hljs-keyword">const</span> { 5642 createCipheriv, 5643 createDecipheriv, 5644 randomBytes, 5645} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 5646 5647<span class="hljs-keyword">const</span> key = <span class="hljs-string">'keykeykeykeykeykeykeykey'</span>; 5648<span class="hljs-keyword">const</span> nonce = <span class="hljs-title function_">randomBytes</span>(<span class="hljs-number">12</span>); 5649 5650<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>); 5651 5652<span class="hljs-keyword">const</span> cipher = <span class="hljs-title function_">createCipheriv</span>(<span class="hljs-string">'aes-192-ccm'</span>, key, nonce, { 5653 <span class="hljs-attr">authTagLength</span>: <span class="hljs-number">16</span>, 5654}); 5655<span class="hljs-keyword">const</span> plaintext = <span class="hljs-string">'Hello world'</span>; 5656cipher.<span class="hljs-title function_">setAAD</span>(aad, { 5657 <span class="hljs-attr">plaintextLength</span>: <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">byteLength</span>(plaintext), 5658}); 5659<span class="hljs-keyword">const</span> ciphertext = cipher.<span class="hljs-title function_">update</span>(plaintext, <span class="hljs-string">'utf8'</span>); 5660cipher.<span class="hljs-title function_">final</span>(); 5661<span class="hljs-keyword">const</span> tag = cipher.<span class="hljs-title function_">getAuthTag</span>(); 5662 5663<span class="hljs-comment">// Now transmit { ciphertext, nonce, tag }.</span> 5664 5665<span class="hljs-keyword">const</span> decipher = <span class="hljs-title function_">createDecipheriv</span>(<span class="hljs-string">'aes-192-ccm'</span>, key, nonce, { 5666 <span class="hljs-attr">authTagLength</span>: <span class="hljs-number">16</span>, 5667}); 5668decipher.<span class="hljs-title function_">setAuthTag</span>(tag); 5669decipher.<span class="hljs-title function_">setAAD</span>(aad, { 5670 <span class="hljs-attr">plaintextLength</span>: ciphertext.<span class="hljs-property">length</span>, 5671}); 5672<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>); 5673 5674<span class="hljs-keyword">try</span> { 5675 decipher.<span class="hljs-title function_">final</span>(); 5676} <span class="hljs-keyword">catch</span> (err) { 5677 <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'Authentication failed!'</span>, { <span class="hljs-attr">cause</span>: err }); 5678} 5679 5680<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(receivedPlaintext);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>); 5681<span class="hljs-keyword">const</span> { 5682 createCipheriv, 5683 createDecipheriv, 5684 randomBytes, 5685} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 5686 5687<span class="hljs-keyword">const</span> key = <span class="hljs-string">'keykeykeykeykeykeykeykey'</span>; 5688<span class="hljs-keyword">const</span> nonce = <span class="hljs-title function_">randomBytes</span>(<span class="hljs-number">12</span>); 5689 5690<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>); 5691 5692<span class="hljs-keyword">const</span> cipher = <span class="hljs-title function_">createCipheriv</span>(<span class="hljs-string">'aes-192-ccm'</span>, key, nonce, { 5693 <span class="hljs-attr">authTagLength</span>: <span class="hljs-number">16</span>, 5694}); 5695<span class="hljs-keyword">const</span> plaintext = <span class="hljs-string">'Hello world'</span>; 5696cipher.<span class="hljs-title function_">setAAD</span>(aad, { 5697 <span class="hljs-attr">plaintextLength</span>: <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">byteLength</span>(plaintext), 5698}); 5699<span class="hljs-keyword">const</span> ciphertext = cipher.<span class="hljs-title function_">update</span>(plaintext, <span class="hljs-string">'utf8'</span>); 5700cipher.<span class="hljs-title function_">final</span>(); 5701<span class="hljs-keyword">const</span> tag = cipher.<span class="hljs-title function_">getAuthTag</span>(); 5702 5703<span class="hljs-comment">// Now transmit { ciphertext, nonce, tag }.</span> 5704 5705<span class="hljs-keyword">const</span> decipher = <span class="hljs-title function_">createDecipheriv</span>(<span class="hljs-string">'aes-192-ccm'</span>, key, nonce, { 5706 <span class="hljs-attr">authTagLength</span>: <span class="hljs-number">16</span>, 5707}); 5708decipher.<span class="hljs-title function_">setAuthTag</span>(tag); 5709decipher.<span class="hljs-title function_">setAAD</span>(aad, { 5710 <span class="hljs-attr">plaintextLength</span>: ciphertext.<span class="hljs-property">length</span>, 5711}); 5712<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>); 5713 5714<span class="hljs-keyword">try</span> { 5715 decipher.<span class="hljs-title function_">final</span>(); 5716} <span class="hljs-keyword">catch</span> (err) { 5717 <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'Authentication failed!'</span>, { <span class="hljs-attr">cause</span>: err }); 5718} 5719 5720<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(receivedPlaintext);</code><button class="copy-button">copy</button></pre> 5721<h4>FIPS mode<span><a class="mark" href="#fips-mode" id="fips-mode">#</a></span><a aria-hidden="true" class="legacy" id="crypto_fips_mode"></a></h4> 5722<p>When using OpenSSL 3, Node.js supports FIPS 140-2 when used with an appropriate 5723OpenSSL 3 provider, such as the <a href="https://www.openssl.org/docs/man3.0/man7/crypto.html#FIPS-provider">FIPS provider from OpenSSL 3</a> which can be 5724installed by following the instructions in <a href="https://github.com/openssl/openssl/blob/openssl-3.0/README-FIPS.md">OpenSSL's FIPS README file</a>.</p> 5725<p>For FIPS support in Node.js you will need:</p> 5726<ul> 5727<li>A correctly installed OpenSSL 3 FIPS provider.</li> 5728<li>An OpenSSL 3 <a href="https://www.openssl.org/docs/man3.0/man5/fips_config.html">FIPS module configuration file</a>.</li> 5729<li>An OpenSSL 3 configuration file that references the FIPS module 5730configuration file.</li> 5731</ul> 5732<p>Node.js will need to be configured with an OpenSSL configuration file that 5733points to the FIPS provider. An example configuration file looks like this:</p> 5734<pre><code class="language-text">nodejs_conf = nodejs_init 5735 5736.include /<absolute path>/fipsmodule.cnf 5737 5738[nodejs_init] 5739providers = provider_sect 5740 5741[provider_sect] 5742default = default_sect 5743# The fips section name should match the section name inside the 5744# included fipsmodule.cnf. 5745fips = fips_sect 5746 5747[default_sect] 5748activate = 1</code> <button class="copy-button">copy</button></pre> 5749<p>where <code>fipsmodule.cnf</code> is the FIPS module configuration file generated from the 5750FIPS provider installation step:</p> 5751<pre><code class="language-bash">openssl fipsinstall</code> <button class="copy-button">copy</button></pre> 5752<p>Set the <code>OPENSSL_CONF</code> environment variable to point to 5753your configuration file and <code>OPENSSL_MODULES</code> to the location of the FIPS 5754provider dynamic library. e.g.</p> 5755<pre><code class="language-bash"><span class="hljs-built_in">export</span> OPENSSL_CONF=/<path to configuration file>/nodejs.cnf 5756<span class="hljs-built_in">export</span> OPENSSL_MODULES=/<path to openssl lib>/ossl-modules</code> <button class="copy-button">copy</button></pre> 5757<p>FIPS mode can then be enabled in Node.js either by:</p> 5758<ul> 5759<li>Starting Node.js with <code>--enable-fips</code> or <code>--force-fips</code> command line flags.</li> 5760<li>Programmatically calling <code>crypto.setFips(true)</code>.</li> 5761</ul> 5762<p>Optionally FIPS mode can be enabled in Node.js via the OpenSSL configuration 5763file. e.g.</p> 5764<pre><code class="language-text">nodejs_conf = nodejs_init 5765 5766.include /<absolute path>/fipsmodule.cnf 5767 5768[nodejs_init] 5769providers = provider_sect 5770alg_section = algorithm_sect 5771 5772[provider_sect] 5773default = default_sect 5774# The fips section name should match the section name inside the 5775# included fipsmodule.cnf. 5776fips = fips_sect 5777 5778[default_sect] 5779activate = 1 5780 5781[algorithm_sect] 5782default_properties = fips=yes</code> <button class="copy-button">copy</button></pre> 5783</section><section><h3>Crypto constants<span><a class="mark" href="#crypto-constants" id="crypto-constants">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto_constants_1"></a></h3> 5784<p>The following constants exported by <code>crypto.constants</code> apply to various uses of 5785the <code>node:crypto</code>, <code>node:tls</code>, and <code>node:https</code> modules and are generally 5786specific to OpenSSL.</p> 5787<h4>OpenSSL options<span><a class="mark" href="#openssl-options" id="openssl-options">#</a></span><a aria-hidden="true" class="legacy" id="crypto_openssl_options"></a></h4> 5788<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> 5789<table> 5790 <tbody><tr> 5791 <th>Constant</th> 5792 <th>Description</th> 5793 </tr> 5794 <tr> 5795 <td><code>SSL_OP_ALL</code></td> 5796 <td>Applies multiple bug workarounds within OpenSSL. See 5797 <a href="https://www.openssl.org/docs/man3.0/man3/SSL_CTX_set_options.html">https://www.openssl.org/docs/man3.0/man3/SSL_CTX_set_options.html</a> 5798 for detail.</td> 5799 </tr> 5800 <tr> 5801 <td><code>SSL_OP_ALLOW_NO_DHE_KEX</code></td> 5802 <td>Instructs OpenSSL to allow a non-[EC]DHE-based key exchange mode 5803 for TLS v1.3</td> 5804 </tr> 5805 <tr> 5806 <td><code>SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION</code></td> 5807 <td>Allows legacy insecure renegotiation between OpenSSL and unpatched 5808 clients or servers. See 5809 <a href="https://www.openssl.org/docs/man3.0/man3/SSL_CTX_set_options.html">https://www.openssl.org/docs/man3.0/man3/SSL_CTX_set_options.html</a>.</td> 5810 </tr> 5811 <tr> 5812 <td><code>SSL_OP_CIPHER_SERVER_PREFERENCE</code></td> 5813 <td>Attempts to use the server's preferences instead of the client's when 5814 selecting a cipher. Behavior depends on protocol version. See 5815 <a href="https://www.openssl.org/docs/man3.0/man3/SSL_CTX_set_options.html">https://www.openssl.org/docs/man3.0/man3/SSL_CTX_set_options.html</a>.</td> 5816 </tr> 5817 <tr> 5818 <td><code>SSL_OP_CISCO_ANYCONNECT</code></td> 5819 <td>Instructs OpenSSL to use Cisco's "speshul" version of DTLS_BAD_VER.</td> 5820 </tr> 5821 <tr> 5822 <td><code>SSL_OP_COOKIE_EXCHANGE</code></td> 5823 <td>Instructs OpenSSL to turn on cookie exchange.</td> 5824 </tr> 5825 <tr> 5826 <td><code>SSL_OP_CRYPTOPRO_TLSEXT_BUG</code></td> 5827 <td>Instructs OpenSSL to add server-hello extension from an early version 5828 of the cryptopro draft.</td> 5829 </tr> 5830 <tr> 5831 <td><code>SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS</code></td> 5832 <td>Instructs OpenSSL to disable a SSL 3.0/TLS 1.0 vulnerability 5833 workaround added in OpenSSL 0.9.6d.</td> 5834 </tr> 5835 <tr> 5836 <td><code>SSL_OP_LEGACY_SERVER_CONNECT</code></td> 5837 <td>Allows initial connection to servers that do not support RI.</td> 5838 </tr> 5839 <tr> 5840 <td><code>SSL_OP_NO_COMPRESSION</code></td> 5841 <td>Instructs OpenSSL to disable support for SSL/TLS compression.</td> 5842 </tr> 5843 <tr> 5844 <td><code>SSL_OP_NO_ENCRYPT_THEN_MAC</code></td> 5845 <td>Instructs OpenSSL to disable encrypt-then-MAC.</td> 5846 </tr> 5847 <tr> 5848 <td><code>SSL_OP_NO_QUERY_MTU</code></td> 5849 <td></td> 5850 </tr> 5851 <tr> 5852 <td><code>SSL_OP_NO_RENEGOTIATION</code></td> 5853 <td>Instructs OpenSSL to disable renegotiation.</td> 5854 </tr> 5855 <tr> 5856 <td><code>SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION</code></td> 5857 <td>Instructs OpenSSL to always start a new session when performing 5858 renegotiation.</td> 5859 </tr> 5860 <tr> 5861 <td><code>SSL_OP_NO_SSLv2</code></td> 5862 <td>Instructs OpenSSL to turn off SSL v2</td> 5863 </tr> 5864 <tr> 5865 <td><code>SSL_OP_NO_SSLv3</code></td> 5866 <td>Instructs OpenSSL to turn off SSL v3</td> 5867 </tr> 5868 <tr> 5869 <td><code>SSL_OP_NO_TICKET</code></td> 5870 <td>Instructs OpenSSL to disable use of RFC4507bis tickets.</td> 5871 </tr> 5872 <tr> 5873 <td><code>SSL_OP_NO_TLSv1</code></td> 5874 <td>Instructs OpenSSL to turn off TLS v1</td> 5875 </tr> 5876 <tr> 5877 <td><code>SSL_OP_NO_TLSv1_1</code></td> 5878 <td>Instructs OpenSSL to turn off TLS v1.1</td> 5879 </tr> 5880 <tr> 5881 <td><code>SSL_OP_NO_TLSv1_2</code></td> 5882 <td>Instructs OpenSSL to turn off TLS v1.2</td> 5883 </tr> 5884 <tr> 5885 <td><code>SSL_OP_NO_TLSv1_3</code></td> 5886 <td>Instructs OpenSSL to turn off TLS v1.3</td> 5887 </tr> 5888 <tr> 5889 <td><code>SSL_OP_PRIORITIZE_CHACHA</code></td> 5890 <td>Instructs OpenSSL server to prioritize ChaCha20-Poly1305 5891 when the client does. 5892 This option has no effect if 5893 <code>SSL_OP_CIPHER_SERVER_PREFERENCE</code> 5894 is not enabled.</td> 5895 </tr> 5896 <tr> 5897 <td><code>SSL_OP_TLS_ROLLBACK_BUG</code></td> 5898 <td>Instructs OpenSSL to disable version rollback attack detection.</td> 5899 </tr> 5900</tbody></table> 5901<h4>OpenSSL engine constants<span><a class="mark" href="#openssl-engine-constants" id="openssl-engine-constants">#</a></span><a aria-hidden="true" class="legacy" id="crypto_openssl_engine_constants"></a></h4> 5902<table> 5903 <tbody><tr> 5904 <th>Constant</th> 5905 <th>Description</th> 5906 </tr> 5907 <tr> 5908 <td><code>ENGINE_METHOD_RSA</code></td> 5909 <td>Limit engine usage to RSA</td> 5910 </tr> 5911 <tr> 5912 <td><code>ENGINE_METHOD_DSA</code></td> 5913 <td>Limit engine usage to DSA</td> 5914 </tr> 5915 <tr> 5916 <td><code>ENGINE_METHOD_DH</code></td> 5917 <td>Limit engine usage to DH</td> 5918 </tr> 5919 <tr> 5920 <td><code>ENGINE_METHOD_RAND</code></td> 5921 <td>Limit engine usage to RAND</td> 5922 </tr> 5923 <tr> 5924 <td><code>ENGINE_METHOD_EC</code></td> 5925 <td>Limit engine usage to EC</td> 5926 </tr> 5927 <tr> 5928 <td><code>ENGINE_METHOD_CIPHERS</code></td> 5929 <td>Limit engine usage to CIPHERS</td> 5930 </tr> 5931 <tr> 5932 <td><code>ENGINE_METHOD_DIGESTS</code></td> 5933 <td>Limit engine usage to DIGESTS</td> 5934 </tr> 5935 <tr> 5936 <td><code>ENGINE_METHOD_PKEY_METHS</code></td> 5937 <td>Limit engine usage to PKEY_METHDS</td> 5938 </tr> 5939 <tr> 5940 <td><code>ENGINE_METHOD_PKEY_ASN1_METHS</code></td> 5941 <td>Limit engine usage to PKEY_ASN1_METHS</td> 5942 </tr> 5943 <tr> 5944 <td><code>ENGINE_METHOD_ALL</code></td> 5945 <td></td> 5946 </tr> 5947 <tr> 5948 <td><code>ENGINE_METHOD_NONE</code></td> 5949 <td></td> 5950 </tr> 5951</tbody></table> 5952<h4>Other OpenSSL constants<span><a class="mark" href="#other-openssl-constants" id="other-openssl-constants">#</a></span><a aria-hidden="true" class="legacy" id="crypto_other_openssl_constants"></a></h4> 5953<table> 5954 <tbody><tr> 5955 <th>Constant</th> 5956 <th>Description</th> 5957 </tr> 5958 <tr> 5959 <td><code>DH_CHECK_P_NOT_SAFE_PRIME</code></td> 5960 <td></td> 5961 </tr> 5962 <tr> 5963 <td><code>DH_CHECK_P_NOT_PRIME</code></td> 5964 <td></td> 5965 </tr> 5966 <tr> 5967 <td><code>DH_UNABLE_TO_CHECK_GENERATOR</code></td> 5968 <td></td> 5969 </tr> 5970 <tr> 5971 <td><code>DH_NOT_SUITABLE_GENERATOR</code></td> 5972 <td></td> 5973 </tr> 5974 <tr> 5975 <td><code>ALPN_ENABLED</code></td> 5976 <td></td> 5977 </tr> 5978 <tr> 5979 <td><code>RSA_PKCS1_PADDING</code></td> 5980 <td></td> 5981 </tr> 5982 <tr> 5983 <td><code>RSA_SSLV23_PADDING</code></td> 5984 <td></td> 5985 </tr> 5986 <tr> 5987 <td><code>RSA_NO_PADDING</code></td> 5988 <td></td> 5989 </tr> 5990 <tr> 5991 <td><code>RSA_PKCS1_OAEP_PADDING</code></td> 5992 <td></td> 5993 </tr> 5994 <tr> 5995 <td><code>RSA_X931_PADDING</code></td> 5996 <td></td> 5997 </tr> 5998 <tr> 5999 <td><code>RSA_PKCS1_PSS_PADDING</code></td> 6000 <td></td> 6001 </tr> 6002 <tr> 6003 <td><code>RSA_PSS_SALTLEN_DIGEST</code></td> 6004 <td>Sets the salt length for <code>RSA_PKCS1_PSS_PADDING</code> to the 6005 digest size when signing or verifying.</td> 6006 </tr> 6007 <tr> 6008 <td><code>RSA_PSS_SALTLEN_MAX_SIGN</code></td> 6009 <td>Sets the salt length for <code>RSA_PKCS1_PSS_PADDING</code> to the 6010 maximum permissible value when signing data.</td> 6011 </tr> 6012 <tr> 6013 <td><code>RSA_PSS_SALTLEN_AUTO</code></td> 6014 <td>Causes the salt length for <code>RSA_PKCS1_PSS_PADDING</code> to be 6015 determined automatically when verifying a signature.</td> 6016 </tr> 6017 <tr> 6018 <td><code>POINT_CONVERSION_COMPRESSED</code></td> 6019 <td></td> 6020 </tr> 6021 <tr> 6022 <td><code>POINT_CONVERSION_UNCOMPRESSED</code></td> 6023 <td></td> 6024 </tr> 6025 <tr> 6026 <td><code>POINT_CONVERSION_HYBRID</code></td> 6027 <td></td> 6028 </tr> 6029</tbody></table> 6030<h4>Node.js crypto constants<span><a class="mark" href="#nodejs-crypto-constants" id="nodejs-crypto-constants">#</a></span><a aria-hidden="true" class="legacy" id="crypto_node_js_crypto_constants"></a></h4> 6031<table> 6032 <tbody><tr> 6033 <th>Constant</th> 6034 <th>Description</th> 6035 </tr> 6036 <tr> 6037 <td><code>defaultCoreCipherList</code></td> 6038 <td>Specifies the built-in default cipher list used by Node.js.</td> 6039 </tr> 6040 <tr> 6041 <td><code>defaultCipherList</code></td> 6042 <td>Specifies the active default cipher list used by the current Node.js 6043 process.</td> 6044 </tr> 6045</tbody></table></section> 6046 <!-- API END --> 6047 </div> 6048 </div> 6049 </div> 6050</body> 6051</html> 6052