11cb0ef41Sopenharmony_ci<!DOCTYPE html> 21cb0ef41Sopenharmony_ci<html lang="en"> 31cb0ef41Sopenharmony_ci<head> 41cb0ef41Sopenharmony_ci <meta charset="utf-8"> 51cb0ef41Sopenharmony_ci <meta name="viewport" content="width=device-width"> 61cb0ef41Sopenharmony_ci <meta name="nodejs.org:node-version" content="v18.20.1"> 71cb0ef41Sopenharmony_ci <title>Crypto | Node.js v18.20.1 Documentation</title> 81cb0ef41Sopenharmony_ci <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 91cb0ef41Sopenharmony_ci <link rel="stylesheet" href="assets/style.css"> 101cb0ef41Sopenharmony_ci <link rel="stylesheet" href="assets/hljs.css"> 111cb0ef41Sopenharmony_ci <link rel="canonical" href="https://nodejs.org/api/crypto.html"> 121cb0ef41Sopenharmony_ci <script async defer src="assets/api.js" type="text/javascript"></script> 131cb0ef41Sopenharmony_ci <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> 141cb0ef41Sopenharmony_ci</head> 151cb0ef41Sopenharmony_ci<body class="alt apidoc" id="api-section-crypto"> 161cb0ef41Sopenharmony_ci <div id="content" class="clearfix"> 171cb0ef41Sopenharmony_ci <div id="column2" class="interior"> 181cb0ef41Sopenharmony_ci <div id="intro" class="interior"> 191cb0ef41Sopenharmony_ci <a href="/" title="Go back to the home page"> 201cb0ef41Sopenharmony_ci Node.js 211cb0ef41Sopenharmony_ci </a> 221cb0ef41Sopenharmony_ci </div> 231cb0ef41Sopenharmony_ci <ul> 241cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 251cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 261cb0ef41Sopenharmony_ci</ul> 271cb0ef41Sopenharmony_ci<hr class="line"> 281cb0ef41Sopenharmony_ci<ul> 291cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 301cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 311cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 321cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 331cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 341cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 351cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 361cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 371cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 381cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 391cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li> 401cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 411cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto active">Crypto</a></li> 421cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 431cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 441cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 451cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li> 461cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li> 471cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li> 481cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li> 491cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li> 501cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li> 511cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li> 521cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 531cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li> 541cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 551cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 561cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 571cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 581cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 591cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 601cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li> 611cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li> 621cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li> 631cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 641cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 651cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li> 661cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 671cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 681cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li> 691cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li> 701cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li> 711cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 721cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li> 731cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 741cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li> 751cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li> 761cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 771cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 781cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li> 791cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 801cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li> 811cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li> 821cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li> 831cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li> 841cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 851cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 861cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 871cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 881cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 891cb0ef41Sopenharmony_ci</ul> 901cb0ef41Sopenharmony_ci<hr class="line"> 911cb0ef41Sopenharmony_ci<ul> 921cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 931cb0ef41Sopenharmony_ci</ul> 941cb0ef41Sopenharmony_ci </div> 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci <div id="column1" data-id="crypto" class="interior"> 971cb0ef41Sopenharmony_ci <header class="header"> 981cb0ef41Sopenharmony_ci <div class="header-container"> 991cb0ef41Sopenharmony_ci <h1>Node.js v18.20.1 documentation</h1> 1001cb0ef41Sopenharmony_ci <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> 1011cb0ef41Sopenharmony_ci <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> 1021cb0ef41Sopenharmony_ci <path fill="none" d="M0 0h24v24H0z" /> 1031cb0ef41Sopenharmony_ci <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"/> 1041cb0ef41Sopenharmony_ci <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"/> 1051cb0ef41Sopenharmony_ci </svg> 1061cb0ef41Sopenharmony_ci <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> 1071cb0ef41Sopenharmony_ci <path d="M0 0h24v24H0z" fill="none" /> 1081cb0ef41Sopenharmony_ci <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"/> 1091cb0ef41Sopenharmony_ci </svg> 1101cb0ef41Sopenharmony_ci </button> 1111cb0ef41Sopenharmony_ci </div> 1121cb0ef41Sopenharmony_ci <div id="gtoc"> 1131cb0ef41Sopenharmony_ci <ul> 1141cb0ef41Sopenharmony_ci <li class="pinned-header">Node.js v18.20.1</li> 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci <li class="picker-header"> 1171cb0ef41Sopenharmony_ci <a href="#"> 1181cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 1191cb0ef41Sopenharmony_ci Table of contents 1201cb0ef41Sopenharmony_ci </a> 1211cb0ef41Sopenharmony_ci 1221cb0ef41Sopenharmony_ci <div class="picker"><div class="toc"><ul> 1231cb0ef41Sopenharmony_ci<li><span class="stability_2"><a href="#crypto">Crypto</a></span> 1241cb0ef41Sopenharmony_ci<ul> 1251cb0ef41Sopenharmony_ci<li><a href="#determining-if-crypto-support-is-unavailable">Determining if crypto support is unavailable</a></li> 1261cb0ef41Sopenharmony_ci<li><a href="#class-certificate">Class: <code>Certificate</code></a> 1271cb0ef41Sopenharmony_ci<ul> 1281cb0ef41Sopenharmony_ci<li><a href="#static-method-certificateexportchallengespkac-encoding">Static method: <code>Certificate.exportChallenge(spkac[, encoding])</code></a></li> 1291cb0ef41Sopenharmony_ci<li><a href="#static-method-certificateexportpublickeyspkac-encoding">Static method: <code>Certificate.exportPublicKey(spkac[, encoding])</code></a></li> 1301cb0ef41Sopenharmony_ci<li><a href="#static-method-certificateverifyspkacspkac-encoding">Static method: <code>Certificate.verifySpkac(spkac[, encoding])</code></a></li> 1311cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#legacy-api">Legacy API</a></span> 1321cb0ef41Sopenharmony_ci<ul> 1331cb0ef41Sopenharmony_ci<li><a href="#new-cryptocertificate"><code>new crypto.Certificate()</code></a></li> 1341cb0ef41Sopenharmony_ci<li><a href="#certificateexportchallengespkac-encoding"><code>certificate.exportChallenge(spkac[, encoding])</code></a></li> 1351cb0ef41Sopenharmony_ci<li><a href="#certificateexportpublickeyspkac-encoding"><code>certificate.exportPublicKey(spkac[, encoding])</code></a></li> 1361cb0ef41Sopenharmony_ci<li><a href="#certificateverifyspkacspkac-encoding"><code>certificate.verifySpkac(spkac[, encoding])</code></a></li> 1371cb0ef41Sopenharmony_ci</ul> 1381cb0ef41Sopenharmony_ci</li> 1391cb0ef41Sopenharmony_ci</ul> 1401cb0ef41Sopenharmony_ci</li> 1411cb0ef41Sopenharmony_ci<li><a href="#class-cipher">Class: <code>Cipher</code></a> 1421cb0ef41Sopenharmony_ci<ul> 1431cb0ef41Sopenharmony_ci<li><a href="#cipherfinaloutputencoding"><code>cipher.final([outputEncoding])</code></a></li> 1441cb0ef41Sopenharmony_ci<li><a href="#ciphergetauthtag"><code>cipher.getAuthTag()</code></a></li> 1451cb0ef41Sopenharmony_ci<li><a href="#ciphersetaadbuffer-options"><code>cipher.setAAD(buffer[, options])</code></a></li> 1461cb0ef41Sopenharmony_ci<li><a href="#ciphersetautopaddingautopadding"><code>cipher.setAutoPadding([autoPadding])</code></a></li> 1471cb0ef41Sopenharmony_ci<li><a href="#cipherupdatedata-inputencoding-outputencoding"><code>cipher.update(data[, inputEncoding][, outputEncoding])</code></a></li> 1481cb0ef41Sopenharmony_ci</ul> 1491cb0ef41Sopenharmony_ci</li> 1501cb0ef41Sopenharmony_ci<li><a href="#class-decipher">Class: <code>Decipher</code></a> 1511cb0ef41Sopenharmony_ci<ul> 1521cb0ef41Sopenharmony_ci<li><a href="#decipherfinaloutputencoding"><code>decipher.final([outputEncoding])</code></a></li> 1531cb0ef41Sopenharmony_ci<li><a href="#deciphersetaadbuffer-options"><code>decipher.setAAD(buffer[, options])</code></a></li> 1541cb0ef41Sopenharmony_ci<li><a href="#deciphersetauthtagbuffer-encoding"><code>decipher.setAuthTag(buffer[, encoding])</code></a></li> 1551cb0ef41Sopenharmony_ci<li><a href="#deciphersetautopaddingautopadding"><code>decipher.setAutoPadding([autoPadding])</code></a></li> 1561cb0ef41Sopenharmony_ci<li><a href="#decipherupdatedata-inputencoding-outputencoding"><code>decipher.update(data[, inputEncoding][, outputEncoding])</code></a></li> 1571cb0ef41Sopenharmony_ci</ul> 1581cb0ef41Sopenharmony_ci</li> 1591cb0ef41Sopenharmony_ci<li><a href="#class-diffiehellman">Class: <code>DiffieHellman</code></a> 1601cb0ef41Sopenharmony_ci<ul> 1611cb0ef41Sopenharmony_ci<li><a href="#diffiehellmancomputesecretotherpublickey-inputencoding-outputencoding"><code>diffieHellman.computeSecret(otherPublicKey[, inputEncoding][, outputEncoding])</code></a></li> 1621cb0ef41Sopenharmony_ci<li><a href="#diffiehellmangeneratekeysencoding"><code>diffieHellman.generateKeys([encoding])</code></a></li> 1631cb0ef41Sopenharmony_ci<li><a href="#diffiehellmangetgeneratorencoding"><code>diffieHellman.getGenerator([encoding])</code></a></li> 1641cb0ef41Sopenharmony_ci<li><a href="#diffiehellmangetprimeencoding"><code>diffieHellman.getPrime([encoding])</code></a></li> 1651cb0ef41Sopenharmony_ci<li><a href="#diffiehellmangetprivatekeyencoding"><code>diffieHellman.getPrivateKey([encoding])</code></a></li> 1661cb0ef41Sopenharmony_ci<li><a href="#diffiehellmangetpublickeyencoding"><code>diffieHellman.getPublicKey([encoding])</code></a></li> 1671cb0ef41Sopenharmony_ci<li><a href="#diffiehellmansetprivatekeyprivatekey-encoding"><code>diffieHellman.setPrivateKey(privateKey[, encoding])</code></a></li> 1681cb0ef41Sopenharmony_ci<li><a href="#diffiehellmansetpublickeypublickey-encoding"><code>diffieHellman.setPublicKey(publicKey[, encoding])</code></a></li> 1691cb0ef41Sopenharmony_ci<li><a href="#diffiehellmanverifyerror"><code>diffieHellman.verifyError</code></a></li> 1701cb0ef41Sopenharmony_ci</ul> 1711cb0ef41Sopenharmony_ci</li> 1721cb0ef41Sopenharmony_ci<li><a href="#class-diffiehellmangroup">Class: <code>DiffieHellmanGroup</code></a></li> 1731cb0ef41Sopenharmony_ci<li><a href="#class-ecdh">Class: <code>ECDH</code></a> 1741cb0ef41Sopenharmony_ci<ul> 1751cb0ef41Sopenharmony_ci<li><a href="#static-method-ecdhconvertkeykey-curve-inputencoding-outputencoding-format">Static method: <code>ECDH.convertKey(key, curve[, inputEncoding[, outputEncoding[, format]]])</code></a></li> 1761cb0ef41Sopenharmony_ci<li><a href="#ecdhcomputesecretotherpublickey-inputencoding-outputencoding"><code>ecdh.computeSecret(otherPublicKey[, inputEncoding][, outputEncoding])</code></a></li> 1771cb0ef41Sopenharmony_ci<li><a href="#ecdhgeneratekeysencoding-format"><code>ecdh.generateKeys([encoding[, format]])</code></a></li> 1781cb0ef41Sopenharmony_ci<li><a href="#ecdhgetprivatekeyencoding"><code>ecdh.getPrivateKey([encoding])</code></a></li> 1791cb0ef41Sopenharmony_ci<li><a href="#ecdhgetpublickeyencoding-format"><code>ecdh.getPublicKey([encoding][, format])</code></a></li> 1801cb0ef41Sopenharmony_ci<li><a href="#ecdhsetprivatekeyprivatekey-encoding"><code>ecdh.setPrivateKey(privateKey[, encoding])</code></a></li> 1811cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#ecdhsetpublickeypublickey-encoding"><code>ecdh.setPublicKey(publicKey[, encoding])</code></a></span></li> 1821cb0ef41Sopenharmony_ci</ul> 1831cb0ef41Sopenharmony_ci</li> 1841cb0ef41Sopenharmony_ci<li><a href="#class-hash">Class: <code>Hash</code></a> 1851cb0ef41Sopenharmony_ci<ul> 1861cb0ef41Sopenharmony_ci<li><a href="#hashcopyoptions"><code>hash.copy([options])</code></a></li> 1871cb0ef41Sopenharmony_ci<li><a href="#hashdigestencoding"><code>hash.digest([encoding])</code></a></li> 1881cb0ef41Sopenharmony_ci<li><a href="#hashupdatedata-inputencoding"><code>hash.update(data[, inputEncoding])</code></a></li> 1891cb0ef41Sopenharmony_ci</ul> 1901cb0ef41Sopenharmony_ci</li> 1911cb0ef41Sopenharmony_ci<li><a href="#class-hmac">Class: <code>Hmac</code></a> 1921cb0ef41Sopenharmony_ci<ul> 1931cb0ef41Sopenharmony_ci<li><a href="#hmacdigestencoding"><code>hmac.digest([encoding])</code></a></li> 1941cb0ef41Sopenharmony_ci<li><a href="#hmacupdatedata-inputencoding"><code>hmac.update(data[, inputEncoding])</code></a></li> 1951cb0ef41Sopenharmony_ci</ul> 1961cb0ef41Sopenharmony_ci</li> 1971cb0ef41Sopenharmony_ci<li><a href="#class-keyobject">Class: <code>KeyObject</code></a> 1981cb0ef41Sopenharmony_ci<ul> 1991cb0ef41Sopenharmony_ci<li><a href="#static-method-keyobjectfromkey">Static method: <code>KeyObject.from(key)</code></a></li> 2001cb0ef41Sopenharmony_ci<li><a href="#keyobjectasymmetrickeydetails"><code>keyObject.asymmetricKeyDetails</code></a></li> 2011cb0ef41Sopenharmony_ci<li><a href="#keyobjectasymmetrickeytype"><code>keyObject.asymmetricKeyType</code></a></li> 2021cb0ef41Sopenharmony_ci<li><a href="#keyobjectexportoptions"><code>keyObject.export([options])</code></a></li> 2031cb0ef41Sopenharmony_ci<li><a href="#keyobjectequalsotherkeyobject"><code>keyObject.equals(otherKeyObject)</code></a></li> 2041cb0ef41Sopenharmony_ci<li><a href="#keyobjectsymmetrickeysize"><code>keyObject.symmetricKeySize</code></a></li> 2051cb0ef41Sopenharmony_ci<li><a href="#keyobjecttype"><code>keyObject.type</code></a></li> 2061cb0ef41Sopenharmony_ci</ul> 2071cb0ef41Sopenharmony_ci</li> 2081cb0ef41Sopenharmony_ci<li><a href="#class-sign">Class: <code>Sign</code></a> 2091cb0ef41Sopenharmony_ci<ul> 2101cb0ef41Sopenharmony_ci<li><a href="#signsignprivatekey-outputencoding"><code>sign.sign(privateKey[, outputEncoding])</code></a></li> 2111cb0ef41Sopenharmony_ci<li><a href="#signupdatedata-inputencoding"><code>sign.update(data[, inputEncoding])</code></a></li> 2121cb0ef41Sopenharmony_ci</ul> 2131cb0ef41Sopenharmony_ci</li> 2141cb0ef41Sopenharmony_ci<li><a href="#class-verify">Class: <code>Verify</code></a> 2151cb0ef41Sopenharmony_ci<ul> 2161cb0ef41Sopenharmony_ci<li><a href="#verifyupdatedata-inputencoding"><code>verify.update(data[, inputEncoding])</code></a></li> 2171cb0ef41Sopenharmony_ci<li><a href="#verifyverifyobject-signature-signatureencoding"><code>verify.verify(object, signature[, signatureEncoding])</code></a></li> 2181cb0ef41Sopenharmony_ci</ul> 2191cb0ef41Sopenharmony_ci</li> 2201cb0ef41Sopenharmony_ci<li><a href="#class-x509certificate">Class: <code>X509Certificate</code></a> 2211cb0ef41Sopenharmony_ci<ul> 2221cb0ef41Sopenharmony_ci<li><a href="#new-x509certificatebuffer"><code>new X509Certificate(buffer)</code></a></li> 2231cb0ef41Sopenharmony_ci<li><a href="#x509ca"><code>x509.ca</code></a></li> 2241cb0ef41Sopenharmony_ci<li><a href="#x509checkemailemail-options"><code>x509.checkEmail(email[, options])</code></a></li> 2251cb0ef41Sopenharmony_ci<li><a href="#x509checkhostname-options"><code>x509.checkHost(name[, options])</code></a></li> 2261cb0ef41Sopenharmony_ci<li><a href="#x509checkipip"><code>x509.checkIP(ip)</code></a></li> 2271cb0ef41Sopenharmony_ci<li><a href="#x509checkissuedothercert"><code>x509.checkIssued(otherCert)</code></a></li> 2281cb0ef41Sopenharmony_ci<li><a href="#x509checkprivatekeyprivatekey"><code>x509.checkPrivateKey(privateKey)</code></a></li> 2291cb0ef41Sopenharmony_ci<li><a href="#x509fingerprint"><code>x509.fingerprint</code></a></li> 2301cb0ef41Sopenharmony_ci<li><a href="#x509fingerprint256"><code>x509.fingerprint256</code></a></li> 2311cb0ef41Sopenharmony_ci<li><a href="#x509fingerprint512"><code>x509.fingerprint512</code></a></li> 2321cb0ef41Sopenharmony_ci<li><a href="#x509infoaccess"><code>x509.infoAccess</code></a></li> 2331cb0ef41Sopenharmony_ci<li><a href="#x509issuer"><code>x509.issuer</code></a></li> 2341cb0ef41Sopenharmony_ci<li><a href="#x509issuercertificate"><code>x509.issuerCertificate</code></a></li> 2351cb0ef41Sopenharmony_ci<li><a href="#x509keyusage"><code>x509.keyUsage</code></a></li> 2361cb0ef41Sopenharmony_ci<li><a href="#x509publickey"><code>x509.publicKey</code></a></li> 2371cb0ef41Sopenharmony_ci<li><a href="#x509raw"><code>x509.raw</code></a></li> 2381cb0ef41Sopenharmony_ci<li><a href="#x509serialnumber"><code>x509.serialNumber</code></a></li> 2391cb0ef41Sopenharmony_ci<li><a href="#x509subject"><code>x509.subject</code></a></li> 2401cb0ef41Sopenharmony_ci<li><a href="#x509subjectaltname"><code>x509.subjectAltName</code></a></li> 2411cb0ef41Sopenharmony_ci<li><a href="#x509tojson"><code>x509.toJSON()</code></a></li> 2421cb0ef41Sopenharmony_ci<li><a href="#x509tolegacyobject"><code>x509.toLegacyObject()</code></a></li> 2431cb0ef41Sopenharmony_ci<li><a href="#x509tostring"><code>x509.toString()</code></a></li> 2441cb0ef41Sopenharmony_ci<li><a href="#x509validfrom"><code>x509.validFrom</code></a></li> 2451cb0ef41Sopenharmony_ci<li><a href="#x509validto"><code>x509.validTo</code></a></li> 2461cb0ef41Sopenharmony_ci<li><a href="#x509verifypublickey"><code>x509.verify(publicKey)</code></a></li> 2471cb0ef41Sopenharmony_ci</ul> 2481cb0ef41Sopenharmony_ci</li> 2491cb0ef41Sopenharmony_ci<li><a href="#nodecrypto-module-methods-and-properties"><code>node:crypto</code> module methods and properties</a> 2501cb0ef41Sopenharmony_ci<ul> 2511cb0ef41Sopenharmony_ci<li><a href="#cryptoconstants"><code>crypto.constants</code></a></li> 2521cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#cryptodefault_encoding"><code>crypto.DEFAULT_ENCODING</code></a></span></li> 2531cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#cryptofips"><code>crypto.fips</code></a></span></li> 2541cb0ef41Sopenharmony_ci<li><a href="#cryptocheckprimecandidate-options-callback"><code>crypto.checkPrime(candidate[, options], callback)</code></a></li> 2551cb0ef41Sopenharmony_ci<li><a href="#cryptocheckprimesynccandidate-options"><code>crypto.checkPrimeSync(candidate[, options])</code></a></li> 2561cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#cryptocreatecipheralgorithm-password-options"><code>crypto.createCipher(algorithm, password[, options])</code></a></span></li> 2571cb0ef41Sopenharmony_ci<li><a href="#cryptocreatecipherivalgorithm-key-iv-options"><code>crypto.createCipheriv(algorithm, key, iv[, options])</code></a></li> 2581cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#cryptocreatedecipheralgorithm-password-options"><code>crypto.createDecipher(algorithm, password[, options])</code></a></span></li> 2591cb0ef41Sopenharmony_ci<li><a href="#cryptocreatedecipherivalgorithm-key-iv-options"><code>crypto.createDecipheriv(algorithm, key, iv[, options])</code></a></li> 2601cb0ef41Sopenharmony_ci<li><a href="#cryptocreatediffiehellmanprime-primeencoding-generator-generatorencoding"><code>crypto.createDiffieHellman(prime[, primeEncoding][, generator][, generatorEncoding])</code></a></li> 2611cb0ef41Sopenharmony_ci<li><a href="#cryptocreatediffiehellmanprimelength-generator"><code>crypto.createDiffieHellman(primeLength[, generator])</code></a></li> 2621cb0ef41Sopenharmony_ci<li><a href="#cryptocreatediffiehellmangroupname"><code>crypto.createDiffieHellmanGroup(name)</code></a></li> 2631cb0ef41Sopenharmony_ci<li><a href="#cryptocreateecdhcurvename"><code>crypto.createECDH(curveName)</code></a></li> 2641cb0ef41Sopenharmony_ci<li><a href="#cryptocreatehashalgorithm-options"><code>crypto.createHash(algorithm[, options])</code></a></li> 2651cb0ef41Sopenharmony_ci<li><a href="#cryptocreatehmacalgorithm-key-options"><code>crypto.createHmac(algorithm, key[, options])</code></a></li> 2661cb0ef41Sopenharmony_ci<li><a href="#cryptocreateprivatekeykey"><code>crypto.createPrivateKey(key)</code></a></li> 2671cb0ef41Sopenharmony_ci<li><a href="#cryptocreatepublickeykey"><code>crypto.createPublicKey(key)</code></a></li> 2681cb0ef41Sopenharmony_ci<li><a href="#cryptocreatesecretkeykey-encoding"><code>crypto.createSecretKey(key[, encoding])</code></a></li> 2691cb0ef41Sopenharmony_ci<li><a href="#cryptocreatesignalgorithm-options"><code>crypto.createSign(algorithm[, options])</code></a></li> 2701cb0ef41Sopenharmony_ci<li><a href="#cryptocreateverifyalgorithm-options"><code>crypto.createVerify(algorithm[, options])</code></a></li> 2711cb0ef41Sopenharmony_ci<li><a href="#cryptodiffiehellmanoptions"><code>crypto.diffieHellman(options)</code></a></li> 2721cb0ef41Sopenharmony_ci<li><a href="#cryptogeneratekeytype-options-callback"><code>crypto.generateKey(type, options, callback)</code></a></li> 2731cb0ef41Sopenharmony_ci<li><a href="#cryptogeneratekeypairtype-options-callback"><code>crypto.generateKeyPair(type, options, callback)</code></a></li> 2741cb0ef41Sopenharmony_ci<li><a href="#cryptogeneratekeypairsynctype-options"><code>crypto.generateKeyPairSync(type, options)</code></a></li> 2751cb0ef41Sopenharmony_ci<li><a href="#cryptogeneratekeysynctype-options"><code>crypto.generateKeySync(type, options)</code></a></li> 2761cb0ef41Sopenharmony_ci<li><a href="#cryptogenerateprimesize-options-callback"><code>crypto.generatePrime(size[, options[, callback]])</code></a></li> 2771cb0ef41Sopenharmony_ci<li><a href="#cryptogenerateprimesyncsize-options"><code>crypto.generatePrimeSync(size[, options])</code></a></li> 2781cb0ef41Sopenharmony_ci<li><a href="#cryptogetcipherinfonameornid-options"><code>crypto.getCipherInfo(nameOrNid[, options])</code></a></li> 2791cb0ef41Sopenharmony_ci<li><a href="#cryptogetciphers"><code>crypto.getCiphers()</code></a></li> 2801cb0ef41Sopenharmony_ci<li><a href="#cryptogetcurves"><code>crypto.getCurves()</code></a></li> 2811cb0ef41Sopenharmony_ci<li><a href="#cryptogetdiffiehellmangroupname"><code>crypto.getDiffieHellman(groupName)</code></a></li> 2821cb0ef41Sopenharmony_ci<li><a href="#cryptogetfips"><code>crypto.getFips()</code></a></li> 2831cb0ef41Sopenharmony_ci<li><a href="#cryptogethashes"><code>crypto.getHashes()</code></a></li> 2841cb0ef41Sopenharmony_ci<li><a href="#cryptogetrandomvaluestypedarray"><code>crypto.getRandomValues(typedArray)</code></a></li> 2851cb0ef41Sopenharmony_ci<li><a href="#cryptohkdfdigest-ikm-salt-info-keylen-callback"><code>crypto.hkdf(digest, ikm, salt, info, keylen, callback)</code></a></li> 2861cb0ef41Sopenharmony_ci<li><a href="#cryptohkdfsyncdigest-ikm-salt-info-keylen"><code>crypto.hkdfSync(digest, ikm, salt, info, keylen)</code></a></li> 2871cb0ef41Sopenharmony_ci<li><a href="#cryptopbkdf2password-salt-iterations-keylen-digest-callback"><code>crypto.pbkdf2(password, salt, iterations, keylen, digest, callback)</code></a></li> 2881cb0ef41Sopenharmony_ci<li><a href="#cryptopbkdf2syncpassword-salt-iterations-keylen-digest"><code>crypto.pbkdf2Sync(password, salt, iterations, keylen, digest)</code></a></li> 2891cb0ef41Sopenharmony_ci<li><a href="#cryptoprivatedecryptprivatekey-buffer"><code>crypto.privateDecrypt(privateKey, buffer)</code></a></li> 2901cb0ef41Sopenharmony_ci<li><a href="#cryptoprivateencryptprivatekey-buffer"><code>crypto.privateEncrypt(privateKey, buffer)</code></a></li> 2911cb0ef41Sopenharmony_ci<li><a href="#cryptopublicdecryptkey-buffer"><code>crypto.publicDecrypt(key, buffer)</code></a></li> 2921cb0ef41Sopenharmony_ci<li><a href="#cryptopublicencryptkey-buffer"><code>crypto.publicEncrypt(key, buffer)</code></a></li> 2931cb0ef41Sopenharmony_ci<li><a href="#cryptorandombytessize-callback"><code>crypto.randomBytes(size[, callback])</code></a></li> 2941cb0ef41Sopenharmony_ci<li><a href="#cryptorandomfillsyncbuffer-offset-size"><code>crypto.randomFillSync(buffer[, offset][, size])</code></a></li> 2951cb0ef41Sopenharmony_ci<li><a href="#cryptorandomfillbuffer-offset-size-callback"><code>crypto.randomFill(buffer[, offset][, size], callback)</code></a></li> 2961cb0ef41Sopenharmony_ci<li><a href="#cryptorandomintmin-max-callback"><code>crypto.randomInt([min, ]max[, callback])</code></a></li> 2971cb0ef41Sopenharmony_ci<li><a href="#cryptorandomuuidoptions"><code>crypto.randomUUID([options])</code></a></li> 2981cb0ef41Sopenharmony_ci<li><a href="#cryptoscryptpassword-salt-keylen-options-callback"><code>crypto.scrypt(password, salt, keylen[, options], callback)</code></a></li> 2991cb0ef41Sopenharmony_ci<li><a href="#cryptoscryptsyncpassword-salt-keylen-options"><code>crypto.scryptSync(password, salt, keylen[, options])</code></a></li> 3001cb0ef41Sopenharmony_ci<li><a href="#cryptosecureheapused"><code>crypto.secureHeapUsed()</code></a></li> 3011cb0ef41Sopenharmony_ci<li><a href="#cryptosetengineengine-flags"><code>crypto.setEngine(engine[, flags])</code></a></li> 3021cb0ef41Sopenharmony_ci<li><a href="#cryptosetfipsbool"><code>crypto.setFips(bool)</code></a></li> 3031cb0ef41Sopenharmony_ci<li><a href="#cryptosignalgorithm-data-key-callback"><code>crypto.sign(algorithm, data, key[, callback])</code></a></li> 3041cb0ef41Sopenharmony_ci<li><a href="#cryptosubtle"><code>crypto.subtle</code></a></li> 3051cb0ef41Sopenharmony_ci<li><a href="#cryptotimingsafeequala-b"><code>crypto.timingSafeEqual(a, b)</code></a></li> 3061cb0ef41Sopenharmony_ci<li><a href="#cryptoverifyalgorithm-data-key-signature-callback"><code>crypto.verify(algorithm, data, key, signature[, callback])</code></a></li> 3071cb0ef41Sopenharmony_ci<li><a href="#cryptowebcrypto"><code>crypto.webcrypto</code></a></li> 3081cb0ef41Sopenharmony_ci</ul> 3091cb0ef41Sopenharmony_ci</li> 3101cb0ef41Sopenharmony_ci<li><a href="#notes">Notes</a> 3111cb0ef41Sopenharmony_ci<ul> 3121cb0ef41Sopenharmony_ci<li><a href="#using-strings-as-inputs-to-cryptographic-apis">Using strings as inputs to cryptographic APIs</a></li> 3131cb0ef41Sopenharmony_ci<li><a href="#legacy-streams-api-prior-to-nodejs-010">Legacy streams API (prior to Node.js 0.10)</a></li> 3141cb0ef41Sopenharmony_ci<li><a href="#support-for-weak-or-compromised-algorithms">Support for weak or compromised algorithms</a></li> 3151cb0ef41Sopenharmony_ci<li><a href="#ccm-mode">CCM mode</a></li> 3161cb0ef41Sopenharmony_ci<li><a href="#fips-mode">FIPS mode</a></li> 3171cb0ef41Sopenharmony_ci</ul> 3181cb0ef41Sopenharmony_ci</li> 3191cb0ef41Sopenharmony_ci<li><a href="#crypto-constants">Crypto constants</a> 3201cb0ef41Sopenharmony_ci<ul> 3211cb0ef41Sopenharmony_ci<li><a href="#openssl-options">OpenSSL options</a></li> 3221cb0ef41Sopenharmony_ci<li><a href="#openssl-engine-constants">OpenSSL engine constants</a></li> 3231cb0ef41Sopenharmony_ci<li><a href="#other-openssl-constants">Other OpenSSL constants</a></li> 3241cb0ef41Sopenharmony_ci<li><a href="#nodejs-crypto-constants">Node.js crypto constants</a></li> 3251cb0ef41Sopenharmony_ci</ul> 3261cb0ef41Sopenharmony_ci</li> 3271cb0ef41Sopenharmony_ci</ul> 3281cb0ef41Sopenharmony_ci</li> 3291cb0ef41Sopenharmony_ci</ul></div></div> 3301cb0ef41Sopenharmony_ci </li> 3311cb0ef41Sopenharmony_ci 3321cb0ef41Sopenharmony_ci 3331cb0ef41Sopenharmony_ci <li class="picker-header"> 3341cb0ef41Sopenharmony_ci <a href="#"> 3351cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 3361cb0ef41Sopenharmony_ci Index 3371cb0ef41Sopenharmony_ci </a> 3381cb0ef41Sopenharmony_ci 3391cb0ef41Sopenharmony_ci <div class="picker"><ul> 3401cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 3411cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 3421cb0ef41Sopenharmony_ci 3431cb0ef41Sopenharmony_ci <li> 3441cb0ef41Sopenharmony_ci <a href="index.html">Index</a> 3451cb0ef41Sopenharmony_ci </li> 3461cb0ef41Sopenharmony_ci </ul> 3471cb0ef41Sopenharmony_ci 3481cb0ef41Sopenharmony_ci<hr class="line"> 3491cb0ef41Sopenharmony_ci<ul> 3501cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 3511cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 3521cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 3531cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 3541cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 3551cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 3561cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 3571cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 3581cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 3591cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 3601cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li> 3611cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 3621cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto active">Crypto</a></li> 3631cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 3641cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 3651cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 3661cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li> 3671cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li> 3681cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li> 3691cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li> 3701cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li> 3711cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li> 3721cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li> 3731cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 3741cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li> 3751cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 3761cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 3771cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 3781cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 3791cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 3801cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 3811cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li> 3821cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li> 3831cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li> 3841cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 3851cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 3861cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li> 3871cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 3881cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 3891cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li> 3901cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li> 3911cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li> 3921cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 3931cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li> 3941cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 3951cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li> 3961cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li> 3971cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 3981cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 3991cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li> 4001cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 4011cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li> 4021cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li> 4031cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li> 4041cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li> 4051cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 4061cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 4071cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 4081cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 4091cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 4101cb0ef41Sopenharmony_ci</ul> 4111cb0ef41Sopenharmony_ci<hr class="line"> 4121cb0ef41Sopenharmony_ci<ul> 4131cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 4141cb0ef41Sopenharmony_ci</ul></div> 4151cb0ef41Sopenharmony_ci </li> 4161cb0ef41Sopenharmony_ci 4171cb0ef41Sopenharmony_ci 4181cb0ef41Sopenharmony_ci <li class="picker-header"> 4191cb0ef41Sopenharmony_ci <a href="#"> 4201cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 4211cb0ef41Sopenharmony_ci Other versions 4221cb0ef41Sopenharmony_ci </a> 4231cb0ef41Sopenharmony_ci <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/crypto.html">21.x</a></li> 4241cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v20.x/api/crypto.html">20.x <b>LTS</b></a></li> 4251cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v19.x/api/crypto.html">19.x</a></li> 4261cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v18.x/api/crypto.html">18.x <b>LTS</b></a></li> 4271cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v17.x/api/crypto.html">17.x</a></li> 4281cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v16.x/api/crypto.html">16.x</a></li> 4291cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v15.x/api/crypto.html">15.x</a></li> 4301cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v14.x/api/crypto.html">14.x</a></li> 4311cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v13.x/api/crypto.html">13.x</a></li> 4321cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v12.x/api/crypto.html">12.x</a></li> 4331cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v11.x/api/crypto.html">11.x</a></li> 4341cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v10.x/api/crypto.html">10.x</a></li> 4351cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v9.x/api/crypto.html">9.x</a></li> 4361cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v8.x/api/crypto.html">8.x</a></li> 4371cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v7.x/api/crypto.html">7.x</a></li> 4381cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v6.x/api/crypto.html">6.x</a></li> 4391cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v5.x/api/crypto.html">5.x</a></li> 4401cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v4.x/api/crypto.html">4.x</a></li> 4411cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/crypto.html">0.12.x</a></li> 4421cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/crypto.html">0.10.x</a></li></ol></div> 4431cb0ef41Sopenharmony_ci </li> 4441cb0ef41Sopenharmony_ci 4451cb0ef41Sopenharmony_ci <li class="picker-header"> 4461cb0ef41Sopenharmony_ci <a href="#"> 4471cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 4481cb0ef41Sopenharmony_ci Options 4491cb0ef41Sopenharmony_ci </a> 4501cb0ef41Sopenharmony_ci 4511cb0ef41Sopenharmony_ci <div class="picker"> 4521cb0ef41Sopenharmony_ci <ul> 4531cb0ef41Sopenharmony_ci <li> 4541cb0ef41Sopenharmony_ci <a href="all.html">View on single page</a> 4551cb0ef41Sopenharmony_ci </li> 4561cb0ef41Sopenharmony_ci <li> 4571cb0ef41Sopenharmony_ci <a href="crypto.json">View as JSON</a> 4581cb0ef41Sopenharmony_ci </li> 4591cb0ef41Sopenharmony_ci <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/crypto.md">Edit on GitHub</a></li> 4601cb0ef41Sopenharmony_ci </ul> 4611cb0ef41Sopenharmony_ci </div> 4621cb0ef41Sopenharmony_ci </li> 4631cb0ef41Sopenharmony_ci </ul> 4641cb0ef41Sopenharmony_ci </div> 4651cb0ef41Sopenharmony_ci <hr> 4661cb0ef41Sopenharmony_ci </header> 4671cb0ef41Sopenharmony_ci 4681cb0ef41Sopenharmony_ci <details id="toc" open><summary>Table of contents</summary><ul> 4691cb0ef41Sopenharmony_ci<li><span class="stability_2"><a href="#crypto">Crypto</a></span> 4701cb0ef41Sopenharmony_ci<ul> 4711cb0ef41Sopenharmony_ci<li><a href="#determining-if-crypto-support-is-unavailable">Determining if crypto support is unavailable</a></li> 4721cb0ef41Sopenharmony_ci<li><a href="#class-certificate">Class: <code>Certificate</code></a> 4731cb0ef41Sopenharmony_ci<ul> 4741cb0ef41Sopenharmony_ci<li><a href="#static-method-certificateexportchallengespkac-encoding">Static method: <code>Certificate.exportChallenge(spkac[, encoding])</code></a></li> 4751cb0ef41Sopenharmony_ci<li><a href="#static-method-certificateexportpublickeyspkac-encoding">Static method: <code>Certificate.exportPublicKey(spkac[, encoding])</code></a></li> 4761cb0ef41Sopenharmony_ci<li><a href="#static-method-certificateverifyspkacspkac-encoding">Static method: <code>Certificate.verifySpkac(spkac[, encoding])</code></a></li> 4771cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#legacy-api">Legacy API</a></span> 4781cb0ef41Sopenharmony_ci<ul> 4791cb0ef41Sopenharmony_ci<li><a href="#new-cryptocertificate"><code>new crypto.Certificate()</code></a></li> 4801cb0ef41Sopenharmony_ci<li><a href="#certificateexportchallengespkac-encoding"><code>certificate.exportChallenge(spkac[, encoding])</code></a></li> 4811cb0ef41Sopenharmony_ci<li><a href="#certificateexportpublickeyspkac-encoding"><code>certificate.exportPublicKey(spkac[, encoding])</code></a></li> 4821cb0ef41Sopenharmony_ci<li><a href="#certificateverifyspkacspkac-encoding"><code>certificate.verifySpkac(spkac[, encoding])</code></a></li> 4831cb0ef41Sopenharmony_ci</ul> 4841cb0ef41Sopenharmony_ci</li> 4851cb0ef41Sopenharmony_ci</ul> 4861cb0ef41Sopenharmony_ci</li> 4871cb0ef41Sopenharmony_ci<li><a href="#class-cipher">Class: <code>Cipher</code></a> 4881cb0ef41Sopenharmony_ci<ul> 4891cb0ef41Sopenharmony_ci<li><a href="#cipherfinaloutputencoding"><code>cipher.final([outputEncoding])</code></a></li> 4901cb0ef41Sopenharmony_ci<li><a href="#ciphergetauthtag"><code>cipher.getAuthTag()</code></a></li> 4911cb0ef41Sopenharmony_ci<li><a href="#ciphersetaadbuffer-options"><code>cipher.setAAD(buffer[, options])</code></a></li> 4921cb0ef41Sopenharmony_ci<li><a href="#ciphersetautopaddingautopadding"><code>cipher.setAutoPadding([autoPadding])</code></a></li> 4931cb0ef41Sopenharmony_ci<li><a href="#cipherupdatedata-inputencoding-outputencoding"><code>cipher.update(data[, inputEncoding][, outputEncoding])</code></a></li> 4941cb0ef41Sopenharmony_ci</ul> 4951cb0ef41Sopenharmony_ci</li> 4961cb0ef41Sopenharmony_ci<li><a href="#class-decipher">Class: <code>Decipher</code></a> 4971cb0ef41Sopenharmony_ci<ul> 4981cb0ef41Sopenharmony_ci<li><a href="#decipherfinaloutputencoding"><code>decipher.final([outputEncoding])</code></a></li> 4991cb0ef41Sopenharmony_ci<li><a href="#deciphersetaadbuffer-options"><code>decipher.setAAD(buffer[, options])</code></a></li> 5001cb0ef41Sopenharmony_ci<li><a href="#deciphersetauthtagbuffer-encoding"><code>decipher.setAuthTag(buffer[, encoding])</code></a></li> 5011cb0ef41Sopenharmony_ci<li><a href="#deciphersetautopaddingautopadding"><code>decipher.setAutoPadding([autoPadding])</code></a></li> 5021cb0ef41Sopenharmony_ci<li><a href="#decipherupdatedata-inputencoding-outputencoding"><code>decipher.update(data[, inputEncoding][, outputEncoding])</code></a></li> 5031cb0ef41Sopenharmony_ci</ul> 5041cb0ef41Sopenharmony_ci</li> 5051cb0ef41Sopenharmony_ci<li><a href="#class-diffiehellman">Class: <code>DiffieHellman</code></a> 5061cb0ef41Sopenharmony_ci<ul> 5071cb0ef41Sopenharmony_ci<li><a href="#diffiehellmancomputesecretotherpublickey-inputencoding-outputencoding"><code>diffieHellman.computeSecret(otherPublicKey[, inputEncoding][, outputEncoding])</code></a></li> 5081cb0ef41Sopenharmony_ci<li><a href="#diffiehellmangeneratekeysencoding"><code>diffieHellman.generateKeys([encoding])</code></a></li> 5091cb0ef41Sopenharmony_ci<li><a href="#diffiehellmangetgeneratorencoding"><code>diffieHellman.getGenerator([encoding])</code></a></li> 5101cb0ef41Sopenharmony_ci<li><a href="#diffiehellmangetprimeencoding"><code>diffieHellman.getPrime([encoding])</code></a></li> 5111cb0ef41Sopenharmony_ci<li><a href="#diffiehellmangetprivatekeyencoding"><code>diffieHellman.getPrivateKey([encoding])</code></a></li> 5121cb0ef41Sopenharmony_ci<li><a href="#diffiehellmangetpublickeyencoding"><code>diffieHellman.getPublicKey([encoding])</code></a></li> 5131cb0ef41Sopenharmony_ci<li><a href="#diffiehellmansetprivatekeyprivatekey-encoding"><code>diffieHellman.setPrivateKey(privateKey[, encoding])</code></a></li> 5141cb0ef41Sopenharmony_ci<li><a href="#diffiehellmansetpublickeypublickey-encoding"><code>diffieHellman.setPublicKey(publicKey[, encoding])</code></a></li> 5151cb0ef41Sopenharmony_ci<li><a href="#diffiehellmanverifyerror"><code>diffieHellman.verifyError</code></a></li> 5161cb0ef41Sopenharmony_ci</ul> 5171cb0ef41Sopenharmony_ci</li> 5181cb0ef41Sopenharmony_ci<li><a href="#class-diffiehellmangroup">Class: <code>DiffieHellmanGroup</code></a></li> 5191cb0ef41Sopenharmony_ci<li><a href="#class-ecdh">Class: <code>ECDH</code></a> 5201cb0ef41Sopenharmony_ci<ul> 5211cb0ef41Sopenharmony_ci<li><a href="#static-method-ecdhconvertkeykey-curve-inputencoding-outputencoding-format">Static method: <code>ECDH.convertKey(key, curve[, inputEncoding[, outputEncoding[, format]]])</code></a></li> 5221cb0ef41Sopenharmony_ci<li><a href="#ecdhcomputesecretotherpublickey-inputencoding-outputencoding"><code>ecdh.computeSecret(otherPublicKey[, inputEncoding][, outputEncoding])</code></a></li> 5231cb0ef41Sopenharmony_ci<li><a href="#ecdhgeneratekeysencoding-format"><code>ecdh.generateKeys([encoding[, format]])</code></a></li> 5241cb0ef41Sopenharmony_ci<li><a href="#ecdhgetprivatekeyencoding"><code>ecdh.getPrivateKey([encoding])</code></a></li> 5251cb0ef41Sopenharmony_ci<li><a href="#ecdhgetpublickeyencoding-format"><code>ecdh.getPublicKey([encoding][, format])</code></a></li> 5261cb0ef41Sopenharmony_ci<li><a href="#ecdhsetprivatekeyprivatekey-encoding"><code>ecdh.setPrivateKey(privateKey[, encoding])</code></a></li> 5271cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#ecdhsetpublickeypublickey-encoding"><code>ecdh.setPublicKey(publicKey[, encoding])</code></a></span></li> 5281cb0ef41Sopenharmony_ci</ul> 5291cb0ef41Sopenharmony_ci</li> 5301cb0ef41Sopenharmony_ci<li><a href="#class-hash">Class: <code>Hash</code></a> 5311cb0ef41Sopenharmony_ci<ul> 5321cb0ef41Sopenharmony_ci<li><a href="#hashcopyoptions"><code>hash.copy([options])</code></a></li> 5331cb0ef41Sopenharmony_ci<li><a href="#hashdigestencoding"><code>hash.digest([encoding])</code></a></li> 5341cb0ef41Sopenharmony_ci<li><a href="#hashupdatedata-inputencoding"><code>hash.update(data[, inputEncoding])</code></a></li> 5351cb0ef41Sopenharmony_ci</ul> 5361cb0ef41Sopenharmony_ci</li> 5371cb0ef41Sopenharmony_ci<li><a href="#class-hmac">Class: <code>Hmac</code></a> 5381cb0ef41Sopenharmony_ci<ul> 5391cb0ef41Sopenharmony_ci<li><a href="#hmacdigestencoding"><code>hmac.digest([encoding])</code></a></li> 5401cb0ef41Sopenharmony_ci<li><a href="#hmacupdatedata-inputencoding"><code>hmac.update(data[, inputEncoding])</code></a></li> 5411cb0ef41Sopenharmony_ci</ul> 5421cb0ef41Sopenharmony_ci</li> 5431cb0ef41Sopenharmony_ci<li><a href="#class-keyobject">Class: <code>KeyObject</code></a> 5441cb0ef41Sopenharmony_ci<ul> 5451cb0ef41Sopenharmony_ci<li><a href="#static-method-keyobjectfromkey">Static method: <code>KeyObject.from(key)</code></a></li> 5461cb0ef41Sopenharmony_ci<li><a href="#keyobjectasymmetrickeydetails"><code>keyObject.asymmetricKeyDetails</code></a></li> 5471cb0ef41Sopenharmony_ci<li><a href="#keyobjectasymmetrickeytype"><code>keyObject.asymmetricKeyType</code></a></li> 5481cb0ef41Sopenharmony_ci<li><a href="#keyobjectexportoptions"><code>keyObject.export([options])</code></a></li> 5491cb0ef41Sopenharmony_ci<li><a href="#keyobjectequalsotherkeyobject"><code>keyObject.equals(otherKeyObject)</code></a></li> 5501cb0ef41Sopenharmony_ci<li><a href="#keyobjectsymmetrickeysize"><code>keyObject.symmetricKeySize</code></a></li> 5511cb0ef41Sopenharmony_ci<li><a href="#keyobjecttype"><code>keyObject.type</code></a></li> 5521cb0ef41Sopenharmony_ci</ul> 5531cb0ef41Sopenharmony_ci</li> 5541cb0ef41Sopenharmony_ci<li><a href="#class-sign">Class: <code>Sign</code></a> 5551cb0ef41Sopenharmony_ci<ul> 5561cb0ef41Sopenharmony_ci<li><a href="#signsignprivatekey-outputencoding"><code>sign.sign(privateKey[, outputEncoding])</code></a></li> 5571cb0ef41Sopenharmony_ci<li><a href="#signupdatedata-inputencoding"><code>sign.update(data[, inputEncoding])</code></a></li> 5581cb0ef41Sopenharmony_ci</ul> 5591cb0ef41Sopenharmony_ci</li> 5601cb0ef41Sopenharmony_ci<li><a href="#class-verify">Class: <code>Verify</code></a> 5611cb0ef41Sopenharmony_ci<ul> 5621cb0ef41Sopenharmony_ci<li><a href="#verifyupdatedata-inputencoding"><code>verify.update(data[, inputEncoding])</code></a></li> 5631cb0ef41Sopenharmony_ci<li><a href="#verifyverifyobject-signature-signatureencoding"><code>verify.verify(object, signature[, signatureEncoding])</code></a></li> 5641cb0ef41Sopenharmony_ci</ul> 5651cb0ef41Sopenharmony_ci</li> 5661cb0ef41Sopenharmony_ci<li><a href="#class-x509certificate">Class: <code>X509Certificate</code></a> 5671cb0ef41Sopenharmony_ci<ul> 5681cb0ef41Sopenharmony_ci<li><a href="#new-x509certificatebuffer"><code>new X509Certificate(buffer)</code></a></li> 5691cb0ef41Sopenharmony_ci<li><a href="#x509ca"><code>x509.ca</code></a></li> 5701cb0ef41Sopenharmony_ci<li><a href="#x509checkemailemail-options"><code>x509.checkEmail(email[, options])</code></a></li> 5711cb0ef41Sopenharmony_ci<li><a href="#x509checkhostname-options"><code>x509.checkHost(name[, options])</code></a></li> 5721cb0ef41Sopenharmony_ci<li><a href="#x509checkipip"><code>x509.checkIP(ip)</code></a></li> 5731cb0ef41Sopenharmony_ci<li><a href="#x509checkissuedothercert"><code>x509.checkIssued(otherCert)</code></a></li> 5741cb0ef41Sopenharmony_ci<li><a href="#x509checkprivatekeyprivatekey"><code>x509.checkPrivateKey(privateKey)</code></a></li> 5751cb0ef41Sopenharmony_ci<li><a href="#x509fingerprint"><code>x509.fingerprint</code></a></li> 5761cb0ef41Sopenharmony_ci<li><a href="#x509fingerprint256"><code>x509.fingerprint256</code></a></li> 5771cb0ef41Sopenharmony_ci<li><a href="#x509fingerprint512"><code>x509.fingerprint512</code></a></li> 5781cb0ef41Sopenharmony_ci<li><a href="#x509infoaccess"><code>x509.infoAccess</code></a></li> 5791cb0ef41Sopenharmony_ci<li><a href="#x509issuer"><code>x509.issuer</code></a></li> 5801cb0ef41Sopenharmony_ci<li><a href="#x509issuercertificate"><code>x509.issuerCertificate</code></a></li> 5811cb0ef41Sopenharmony_ci<li><a href="#x509keyusage"><code>x509.keyUsage</code></a></li> 5821cb0ef41Sopenharmony_ci<li><a href="#x509publickey"><code>x509.publicKey</code></a></li> 5831cb0ef41Sopenharmony_ci<li><a href="#x509raw"><code>x509.raw</code></a></li> 5841cb0ef41Sopenharmony_ci<li><a href="#x509serialnumber"><code>x509.serialNumber</code></a></li> 5851cb0ef41Sopenharmony_ci<li><a href="#x509subject"><code>x509.subject</code></a></li> 5861cb0ef41Sopenharmony_ci<li><a href="#x509subjectaltname"><code>x509.subjectAltName</code></a></li> 5871cb0ef41Sopenharmony_ci<li><a href="#x509tojson"><code>x509.toJSON()</code></a></li> 5881cb0ef41Sopenharmony_ci<li><a href="#x509tolegacyobject"><code>x509.toLegacyObject()</code></a></li> 5891cb0ef41Sopenharmony_ci<li><a href="#x509tostring"><code>x509.toString()</code></a></li> 5901cb0ef41Sopenharmony_ci<li><a href="#x509validfrom"><code>x509.validFrom</code></a></li> 5911cb0ef41Sopenharmony_ci<li><a href="#x509validto"><code>x509.validTo</code></a></li> 5921cb0ef41Sopenharmony_ci<li><a href="#x509verifypublickey"><code>x509.verify(publicKey)</code></a></li> 5931cb0ef41Sopenharmony_ci</ul> 5941cb0ef41Sopenharmony_ci</li> 5951cb0ef41Sopenharmony_ci<li><a href="#nodecrypto-module-methods-and-properties"><code>node:crypto</code> module methods and properties</a> 5961cb0ef41Sopenharmony_ci<ul> 5971cb0ef41Sopenharmony_ci<li><a href="#cryptoconstants"><code>crypto.constants</code></a></li> 5981cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#cryptodefault_encoding"><code>crypto.DEFAULT_ENCODING</code></a></span></li> 5991cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#cryptofips"><code>crypto.fips</code></a></span></li> 6001cb0ef41Sopenharmony_ci<li><a href="#cryptocheckprimecandidate-options-callback"><code>crypto.checkPrime(candidate[, options], callback)</code></a></li> 6011cb0ef41Sopenharmony_ci<li><a href="#cryptocheckprimesynccandidate-options"><code>crypto.checkPrimeSync(candidate[, options])</code></a></li> 6021cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#cryptocreatecipheralgorithm-password-options"><code>crypto.createCipher(algorithm, password[, options])</code></a></span></li> 6031cb0ef41Sopenharmony_ci<li><a href="#cryptocreatecipherivalgorithm-key-iv-options"><code>crypto.createCipheriv(algorithm, key, iv[, options])</code></a></li> 6041cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#cryptocreatedecipheralgorithm-password-options"><code>crypto.createDecipher(algorithm, password[, options])</code></a></span></li> 6051cb0ef41Sopenharmony_ci<li><a href="#cryptocreatedecipherivalgorithm-key-iv-options"><code>crypto.createDecipheriv(algorithm, key, iv[, options])</code></a></li> 6061cb0ef41Sopenharmony_ci<li><a href="#cryptocreatediffiehellmanprime-primeencoding-generator-generatorencoding"><code>crypto.createDiffieHellman(prime[, primeEncoding][, generator][, generatorEncoding])</code></a></li> 6071cb0ef41Sopenharmony_ci<li><a href="#cryptocreatediffiehellmanprimelength-generator"><code>crypto.createDiffieHellman(primeLength[, generator])</code></a></li> 6081cb0ef41Sopenharmony_ci<li><a href="#cryptocreatediffiehellmangroupname"><code>crypto.createDiffieHellmanGroup(name)</code></a></li> 6091cb0ef41Sopenharmony_ci<li><a href="#cryptocreateecdhcurvename"><code>crypto.createECDH(curveName)</code></a></li> 6101cb0ef41Sopenharmony_ci<li><a href="#cryptocreatehashalgorithm-options"><code>crypto.createHash(algorithm[, options])</code></a></li> 6111cb0ef41Sopenharmony_ci<li><a href="#cryptocreatehmacalgorithm-key-options"><code>crypto.createHmac(algorithm, key[, options])</code></a></li> 6121cb0ef41Sopenharmony_ci<li><a href="#cryptocreateprivatekeykey"><code>crypto.createPrivateKey(key)</code></a></li> 6131cb0ef41Sopenharmony_ci<li><a href="#cryptocreatepublickeykey"><code>crypto.createPublicKey(key)</code></a></li> 6141cb0ef41Sopenharmony_ci<li><a href="#cryptocreatesecretkeykey-encoding"><code>crypto.createSecretKey(key[, encoding])</code></a></li> 6151cb0ef41Sopenharmony_ci<li><a href="#cryptocreatesignalgorithm-options"><code>crypto.createSign(algorithm[, options])</code></a></li> 6161cb0ef41Sopenharmony_ci<li><a href="#cryptocreateverifyalgorithm-options"><code>crypto.createVerify(algorithm[, options])</code></a></li> 6171cb0ef41Sopenharmony_ci<li><a href="#cryptodiffiehellmanoptions"><code>crypto.diffieHellman(options)</code></a></li> 6181cb0ef41Sopenharmony_ci<li><a href="#cryptogeneratekeytype-options-callback"><code>crypto.generateKey(type, options, callback)</code></a></li> 6191cb0ef41Sopenharmony_ci<li><a href="#cryptogeneratekeypairtype-options-callback"><code>crypto.generateKeyPair(type, options, callback)</code></a></li> 6201cb0ef41Sopenharmony_ci<li><a href="#cryptogeneratekeypairsynctype-options"><code>crypto.generateKeyPairSync(type, options)</code></a></li> 6211cb0ef41Sopenharmony_ci<li><a href="#cryptogeneratekeysynctype-options"><code>crypto.generateKeySync(type, options)</code></a></li> 6221cb0ef41Sopenharmony_ci<li><a href="#cryptogenerateprimesize-options-callback"><code>crypto.generatePrime(size[, options[, callback]])</code></a></li> 6231cb0ef41Sopenharmony_ci<li><a href="#cryptogenerateprimesyncsize-options"><code>crypto.generatePrimeSync(size[, options])</code></a></li> 6241cb0ef41Sopenharmony_ci<li><a href="#cryptogetcipherinfonameornid-options"><code>crypto.getCipherInfo(nameOrNid[, options])</code></a></li> 6251cb0ef41Sopenharmony_ci<li><a href="#cryptogetciphers"><code>crypto.getCiphers()</code></a></li> 6261cb0ef41Sopenharmony_ci<li><a href="#cryptogetcurves"><code>crypto.getCurves()</code></a></li> 6271cb0ef41Sopenharmony_ci<li><a href="#cryptogetdiffiehellmangroupname"><code>crypto.getDiffieHellman(groupName)</code></a></li> 6281cb0ef41Sopenharmony_ci<li><a href="#cryptogetfips"><code>crypto.getFips()</code></a></li> 6291cb0ef41Sopenharmony_ci<li><a href="#cryptogethashes"><code>crypto.getHashes()</code></a></li> 6301cb0ef41Sopenharmony_ci<li><a href="#cryptogetrandomvaluestypedarray"><code>crypto.getRandomValues(typedArray)</code></a></li> 6311cb0ef41Sopenharmony_ci<li><a href="#cryptohkdfdigest-ikm-salt-info-keylen-callback"><code>crypto.hkdf(digest, ikm, salt, info, keylen, callback)</code></a></li> 6321cb0ef41Sopenharmony_ci<li><a href="#cryptohkdfsyncdigest-ikm-salt-info-keylen"><code>crypto.hkdfSync(digest, ikm, salt, info, keylen)</code></a></li> 6331cb0ef41Sopenharmony_ci<li><a href="#cryptopbkdf2password-salt-iterations-keylen-digest-callback"><code>crypto.pbkdf2(password, salt, iterations, keylen, digest, callback)</code></a></li> 6341cb0ef41Sopenharmony_ci<li><a href="#cryptopbkdf2syncpassword-salt-iterations-keylen-digest"><code>crypto.pbkdf2Sync(password, salt, iterations, keylen, digest)</code></a></li> 6351cb0ef41Sopenharmony_ci<li><a href="#cryptoprivatedecryptprivatekey-buffer"><code>crypto.privateDecrypt(privateKey, buffer)</code></a></li> 6361cb0ef41Sopenharmony_ci<li><a href="#cryptoprivateencryptprivatekey-buffer"><code>crypto.privateEncrypt(privateKey, buffer)</code></a></li> 6371cb0ef41Sopenharmony_ci<li><a href="#cryptopublicdecryptkey-buffer"><code>crypto.publicDecrypt(key, buffer)</code></a></li> 6381cb0ef41Sopenharmony_ci<li><a href="#cryptopublicencryptkey-buffer"><code>crypto.publicEncrypt(key, buffer)</code></a></li> 6391cb0ef41Sopenharmony_ci<li><a href="#cryptorandombytessize-callback"><code>crypto.randomBytes(size[, callback])</code></a></li> 6401cb0ef41Sopenharmony_ci<li><a href="#cryptorandomfillsyncbuffer-offset-size"><code>crypto.randomFillSync(buffer[, offset][, size])</code></a></li> 6411cb0ef41Sopenharmony_ci<li><a href="#cryptorandomfillbuffer-offset-size-callback"><code>crypto.randomFill(buffer[, offset][, size], callback)</code></a></li> 6421cb0ef41Sopenharmony_ci<li><a href="#cryptorandomintmin-max-callback"><code>crypto.randomInt([min, ]max[, callback])</code></a></li> 6431cb0ef41Sopenharmony_ci<li><a href="#cryptorandomuuidoptions"><code>crypto.randomUUID([options])</code></a></li> 6441cb0ef41Sopenharmony_ci<li><a href="#cryptoscryptpassword-salt-keylen-options-callback"><code>crypto.scrypt(password, salt, keylen[, options], callback)</code></a></li> 6451cb0ef41Sopenharmony_ci<li><a href="#cryptoscryptsyncpassword-salt-keylen-options"><code>crypto.scryptSync(password, salt, keylen[, options])</code></a></li> 6461cb0ef41Sopenharmony_ci<li><a href="#cryptosecureheapused"><code>crypto.secureHeapUsed()</code></a></li> 6471cb0ef41Sopenharmony_ci<li><a href="#cryptosetengineengine-flags"><code>crypto.setEngine(engine[, flags])</code></a></li> 6481cb0ef41Sopenharmony_ci<li><a href="#cryptosetfipsbool"><code>crypto.setFips(bool)</code></a></li> 6491cb0ef41Sopenharmony_ci<li><a href="#cryptosignalgorithm-data-key-callback"><code>crypto.sign(algorithm, data, key[, callback])</code></a></li> 6501cb0ef41Sopenharmony_ci<li><a href="#cryptosubtle"><code>crypto.subtle</code></a></li> 6511cb0ef41Sopenharmony_ci<li><a href="#cryptotimingsafeequala-b"><code>crypto.timingSafeEqual(a, b)</code></a></li> 6521cb0ef41Sopenharmony_ci<li><a href="#cryptoverifyalgorithm-data-key-signature-callback"><code>crypto.verify(algorithm, data, key, signature[, callback])</code></a></li> 6531cb0ef41Sopenharmony_ci<li><a href="#cryptowebcrypto"><code>crypto.webcrypto</code></a></li> 6541cb0ef41Sopenharmony_ci</ul> 6551cb0ef41Sopenharmony_ci</li> 6561cb0ef41Sopenharmony_ci<li><a href="#notes">Notes</a> 6571cb0ef41Sopenharmony_ci<ul> 6581cb0ef41Sopenharmony_ci<li><a href="#using-strings-as-inputs-to-cryptographic-apis">Using strings as inputs to cryptographic APIs</a></li> 6591cb0ef41Sopenharmony_ci<li><a href="#legacy-streams-api-prior-to-nodejs-010">Legacy streams API (prior to Node.js 0.10)</a></li> 6601cb0ef41Sopenharmony_ci<li><a href="#support-for-weak-or-compromised-algorithms">Support for weak or compromised algorithms</a></li> 6611cb0ef41Sopenharmony_ci<li><a href="#ccm-mode">CCM mode</a></li> 6621cb0ef41Sopenharmony_ci<li><a href="#fips-mode">FIPS mode</a></li> 6631cb0ef41Sopenharmony_ci</ul> 6641cb0ef41Sopenharmony_ci</li> 6651cb0ef41Sopenharmony_ci<li><a href="#crypto-constants">Crypto constants</a> 6661cb0ef41Sopenharmony_ci<ul> 6671cb0ef41Sopenharmony_ci<li><a href="#openssl-options">OpenSSL options</a></li> 6681cb0ef41Sopenharmony_ci<li><a href="#openssl-engine-constants">OpenSSL engine constants</a></li> 6691cb0ef41Sopenharmony_ci<li><a href="#other-openssl-constants">Other OpenSSL constants</a></li> 6701cb0ef41Sopenharmony_ci<li><a href="#nodejs-crypto-constants">Node.js crypto constants</a></li> 6711cb0ef41Sopenharmony_ci</ul> 6721cb0ef41Sopenharmony_ci</li> 6731cb0ef41Sopenharmony_ci</ul> 6741cb0ef41Sopenharmony_ci</li> 6751cb0ef41Sopenharmony_ci</ul></details> 6761cb0ef41Sopenharmony_ci 6771cb0ef41Sopenharmony_ci <div id="apicontent"> 6781cb0ef41Sopenharmony_ci <h2>Crypto<span><a class="mark" href="#crypto" id="crypto">#</a></span><a aria-hidden="true" class="legacy" id="crypto_crypto"></a></h2> 6791cb0ef41Sopenharmony_ci 6801cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p> 6811cb0ef41Sopenharmony_ci<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.20.1/lib/crypto.js">lib/crypto.js</a></p> 6821cb0ef41Sopenharmony_ci<p>The <code>node:crypto</code> module provides cryptographic functionality that includes a 6831cb0ef41Sopenharmony_ciset of wrappers for OpenSSL's hash, HMAC, cipher, decipher, sign, and verify 6841cb0ef41Sopenharmony_cifunctions.</p> 6851cb0ef41Sopenharmony_ci 6861cb0ef41Sopenharmony_ci<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>); 6871cb0ef41Sopenharmony_ci 6881cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> secret = <span class="hljs-string">'abcdefg'</span>; 6891cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> hash = <span class="hljs-title function_">createHmac</span>(<span class="hljs-string">'sha256'</span>, secret) 6901cb0ef41Sopenharmony_ci .<span class="hljs-title function_">update</span>(<span class="hljs-string">'I love cupcakes'</span>) 6911cb0ef41Sopenharmony_ci .<span class="hljs-title function_">digest</span>(<span class="hljs-string">'hex'</span>); 6921cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(hash); 6931cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 6941cb0ef41Sopenharmony_ci<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>); 6951cb0ef41Sopenharmony_ci 6961cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> secret = <span class="hljs-string">'abcdefg'</span>; 6971cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> hash = <span class="hljs-title function_">createHmac</span>(<span class="hljs-string">'sha256'</span>, secret) 6981cb0ef41Sopenharmony_ci .<span class="hljs-title function_">update</span>(<span class="hljs-string">'I love cupcakes'</span>) 6991cb0ef41Sopenharmony_ci .<span class="hljs-title function_">digest</span>(<span class="hljs-string">'hex'</span>); 7001cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(hash); 7011cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 7021cb0ef41Sopenharmony_ci<span class="hljs-comment">// c0fa1bc00531bd78ef38c628449c5102aeabd49b5dc3a2a516ea6ea959d6658e</span></code><button class="copy-button">copy</button></pre> 7031cb0ef41Sopenharmony_ci<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> 7041cb0ef41Sopenharmony_ci<p>It is possible for Node.js to be built without including support for the 7051cb0ef41Sopenharmony_ci<code>node:crypto</code> module. In such cases, attempting to <code>import</code> from <code>crypto</code> or 7061cb0ef41Sopenharmony_cicalling <code>require('node:crypto')</code> will result in an error being thrown.</p> 7071cb0ef41Sopenharmony_ci<p>When using CommonJS, the error thrown can be caught using try/catch:</p> 7081cb0ef41Sopenharmony_ci<!-- eslint-skip --> 7091cb0ef41Sopenharmony_ci<pre><code class="language-js cjs"><span class="hljs-keyword">let</span> crypto; 7101cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> { 7111cb0ef41Sopenharmony_ci crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 7121cb0ef41Sopenharmony_ci} <span class="hljs-keyword">catch</span> (err) { 7131cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'crypto support is disabled!'</span>); 7141cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre> 7151cb0ef41Sopenharmony_ci<p>When using the lexical ESM <code>import</code> keyword, the error can only be 7161cb0ef41Sopenharmony_cicaught if a handler for <code>process.on('uncaughtException')</code> is registered 7171cb0ef41Sopenharmony_ci<em>before</em> any attempt to load the module is made (using, for instance, 7181cb0ef41Sopenharmony_cia preload module).</p> 7191cb0ef41Sopenharmony_ci<p>When using ESM, if there is a chance that the code may be run on a build 7201cb0ef41Sopenharmony_ciof Node.js where crypto support is not enabled, consider using the 7211cb0ef41Sopenharmony_ci<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> 7221cb0ef41Sopenharmony_ci<pre><code class="language-js mjs"><span class="hljs-keyword">let</span> crypto; 7231cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> { 7241cb0ef41Sopenharmony_ci crypto = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 7251cb0ef41Sopenharmony_ci} <span class="hljs-keyword">catch</span> (err) { 7261cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'crypto support is disabled!'</span>); 7271cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre> 7281cb0ef41Sopenharmony_ci</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> 7291cb0ef41Sopenharmony_ci<div class="api_metadata"> 7301cb0ef41Sopenharmony_ci<span>Added in: v0.11.8</span> 7311cb0ef41Sopenharmony_ci</div> 7321cb0ef41Sopenharmony_ci<p>SPKAC is a Certificate Signing Request mechanism originally implemented by 7331cb0ef41Sopenharmony_ciNetscape and was specified formally as part of HTML5's <code>keygen</code> element.</p> 7341cb0ef41Sopenharmony_ci<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 7351cb0ef41Sopenharmony_cishould not use this element anymore.</p> 7361cb0ef41Sopenharmony_ci<p>The <code>node:crypto</code> module provides the <code>Certificate</code> class for working with SPKAC 7371cb0ef41Sopenharmony_cidata. The most common usage is handling output generated by the HTML5 7381cb0ef41Sopenharmony_ci<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> 7391cb0ef41Sopenharmony_ci<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> 7401cb0ef41Sopenharmony_ci<div class="api_metadata"> 7411cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 7421cb0ef41Sopenharmony_ci<table> 7431cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 7441cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 7451cb0ef41Sopenharmony_ci<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> 7461cb0ef41Sopenharmony_ci<tr><td>v9.0.0</td> 7471cb0ef41Sopenharmony_ci<td><p><span>Added in: v9.0.0</span></p></td></tr> 7481cb0ef41Sopenharmony_ci</tbody></table> 7491cb0ef41Sopenharmony_ci</details> 7501cb0ef41Sopenharmony_ci</div> 7511cb0ef41Sopenharmony_ci<ul> 7521cb0ef41Sopenharmony_ci<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> 7531cb0ef41Sopenharmony_ci<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> 7541cb0ef41Sopenharmony_ci<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> The challenge component of the <code>spkac</code> data structure, which 7551cb0ef41Sopenharmony_ciincludes a public key and a challenge.</li> 7561cb0ef41Sopenharmony_ci</ul> 7571cb0ef41Sopenharmony_ci 7581cb0ef41Sopenharmony_ci<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>); 7591cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>(); 7601cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> challenge = <span class="hljs-title class_">Certificate</span>.<span class="hljs-title function_">exportChallenge</span>(spkac); 7611cb0ef41Sopenharmony_ci<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>)); 7621cb0ef41Sopenharmony_ci<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>); 7631cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>(); 7641cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> challenge = <span class="hljs-title class_">Certificate</span>.<span class="hljs-title function_">exportChallenge</span>(spkac); 7651cb0ef41Sopenharmony_ci<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>)); 7661cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: the challenge as a UTF8 string</span></code><button class="copy-button">copy</button></pre> 7671cb0ef41Sopenharmony_ci<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> 7681cb0ef41Sopenharmony_ci<div class="api_metadata"> 7691cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 7701cb0ef41Sopenharmony_ci<table> 7711cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 7721cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 7731cb0ef41Sopenharmony_ci<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> 7741cb0ef41Sopenharmony_ci<tr><td>v9.0.0</td> 7751cb0ef41Sopenharmony_ci<td><p><span>Added in: v9.0.0</span></p></td></tr> 7761cb0ef41Sopenharmony_ci</tbody></table> 7771cb0ef41Sopenharmony_ci</details> 7781cb0ef41Sopenharmony_ci</div> 7791cb0ef41Sopenharmony_ci<ul> 7801cb0ef41Sopenharmony_ci<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> 7811cb0ef41Sopenharmony_ci<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> 7821cb0ef41Sopenharmony_ci<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> The public key component of the <code>spkac</code> data structure, 7831cb0ef41Sopenharmony_ciwhich includes a public key and a challenge.</li> 7841cb0ef41Sopenharmony_ci</ul> 7851cb0ef41Sopenharmony_ci 7861cb0ef41Sopenharmony_ci<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>); 7871cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>(); 7881cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> publicKey = <span class="hljs-title class_">Certificate</span>.<span class="hljs-title function_">exportPublicKey</span>(spkac); 7891cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(publicKey); 7901cb0ef41Sopenharmony_ci<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>); 7911cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>(); 7921cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> publicKey = <span class="hljs-title class_">Certificate</span>.<span class="hljs-title function_">exportPublicKey</span>(spkac); 7931cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(publicKey); 7941cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: the public key as <Buffer ...></span></code><button class="copy-button">copy</button></pre> 7951cb0ef41Sopenharmony_ci<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> 7961cb0ef41Sopenharmony_ci<div class="api_metadata"> 7971cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 7981cb0ef41Sopenharmony_ci<table> 7991cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 8001cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 8011cb0ef41Sopenharmony_ci<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> 8021cb0ef41Sopenharmony_ci<tr><td>v9.0.0</td> 8031cb0ef41Sopenharmony_ci<td><p><span>Added in: v9.0.0</span></p></td></tr> 8041cb0ef41Sopenharmony_ci</tbody></table> 8051cb0ef41Sopenharmony_ci</details> 8061cb0ef41Sopenharmony_ci</div> 8071cb0ef41Sopenharmony_ci<ul> 8081cb0ef41Sopenharmony_ci<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> 8091cb0ef41Sopenharmony_ci<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> 8101cb0ef41Sopenharmony_ci<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, 8111cb0ef41Sopenharmony_ci<code>false</code> otherwise.</li> 8121cb0ef41Sopenharmony_ci</ul> 8131cb0ef41Sopenharmony_ci 8141cb0ef41Sopenharmony_ci<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>; 8151cb0ef41Sopenharmony_ci<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>); 8161cb0ef41Sopenharmony_ci 8171cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>(); 8181cb0ef41Sopenharmony_ci<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))); 8191cb0ef41Sopenharmony_ci<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>); 8201cb0ef41Sopenharmony_ci<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>); 8211cb0ef41Sopenharmony_ci 8221cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>(); 8231cb0ef41Sopenharmony_ci<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))); 8241cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: true or false</span></code><button class="copy-button">copy</button></pre> 8251cb0ef41Sopenharmony_ci<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> 8261cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated</div><p></p> 8271cb0ef41Sopenharmony_ci<p>As a legacy interface, it is possible to create new instances of 8281cb0ef41Sopenharmony_cithe <code>crypto.Certificate</code> class as illustrated in the examples below.</p> 8291cb0ef41Sopenharmony_ci<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> 8301cb0ef41Sopenharmony_ci<p>Instances of the <code>Certificate</code> class can be created using the <code>new</code> keyword 8311cb0ef41Sopenharmony_cior by calling <code>crypto.Certificate()</code> as a function:</p> 8321cb0ef41Sopenharmony_ci 8331cb0ef41Sopenharmony_ci<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>); 8341cb0ef41Sopenharmony_ci 8351cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> cert1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Certificate</span>(); 8361cb0ef41Sopenharmony_ci<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>); 8371cb0ef41Sopenharmony_ci 8381cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> cert1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Certificate</span>(); 8391cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> cert2 = <span class="hljs-title class_">Certificate</span>();</code><button class="copy-button">copy</button></pre> 8401cb0ef41Sopenharmony_ci<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> 8411cb0ef41Sopenharmony_ci<div class="api_metadata"> 8421cb0ef41Sopenharmony_ci<span>Added in: v0.11.8</span> 8431cb0ef41Sopenharmony_ci</div> 8441cb0ef41Sopenharmony_ci<ul> 8451cb0ef41Sopenharmony_ci<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> 8461cb0ef41Sopenharmony_ci<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> 8471cb0ef41Sopenharmony_ci<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> The challenge component of the <code>spkac</code> data structure, which 8481cb0ef41Sopenharmony_ciincludes a public key and a challenge.</li> 8491cb0ef41Sopenharmony_ci</ul> 8501cb0ef41Sopenharmony_ci 8511cb0ef41Sopenharmony_ci<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>); 8521cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> cert = <span class="hljs-title class_">Certificate</span>(); 8531cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>(); 8541cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> challenge = cert.<span class="hljs-title function_">exportChallenge</span>(spkac); 8551cb0ef41Sopenharmony_ci<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>)); 8561cb0ef41Sopenharmony_ci<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>); 8571cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> cert = <span class="hljs-title class_">Certificate</span>(); 8581cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>(); 8591cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> challenge = cert.<span class="hljs-title function_">exportChallenge</span>(spkac); 8601cb0ef41Sopenharmony_ci<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>)); 8611cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: the challenge as a UTF8 string</span></code><button class="copy-button">copy</button></pre> 8621cb0ef41Sopenharmony_ci<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> 8631cb0ef41Sopenharmony_ci<div class="api_metadata"> 8641cb0ef41Sopenharmony_ci<span>Added in: v0.11.8</span> 8651cb0ef41Sopenharmony_ci</div> 8661cb0ef41Sopenharmony_ci<ul> 8671cb0ef41Sopenharmony_ci<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> 8681cb0ef41Sopenharmony_ci<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> 8691cb0ef41Sopenharmony_ci<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> The public key component of the <code>spkac</code> data structure, 8701cb0ef41Sopenharmony_ciwhich includes a public key and a challenge.</li> 8711cb0ef41Sopenharmony_ci</ul> 8721cb0ef41Sopenharmony_ci 8731cb0ef41Sopenharmony_ci<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>); 8741cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> cert = <span class="hljs-title class_">Certificate</span>(); 8751cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>(); 8761cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> publicKey = cert.<span class="hljs-title function_">exportPublicKey</span>(spkac); 8771cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(publicKey); 8781cb0ef41Sopenharmony_ci<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>); 8791cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> cert = <span class="hljs-title class_">Certificate</span>(); 8801cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>(); 8811cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> publicKey = cert.<span class="hljs-title function_">exportPublicKey</span>(spkac); 8821cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(publicKey); 8831cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: the public key as <Buffer ...></span></code><button class="copy-button">copy</button></pre> 8841cb0ef41Sopenharmony_ci<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> 8851cb0ef41Sopenharmony_ci<div class="api_metadata"> 8861cb0ef41Sopenharmony_ci<span>Added in: v0.11.8</span> 8871cb0ef41Sopenharmony_ci</div> 8881cb0ef41Sopenharmony_ci<ul> 8891cb0ef41Sopenharmony_ci<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> 8901cb0ef41Sopenharmony_ci<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> 8911cb0ef41Sopenharmony_ci<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, 8921cb0ef41Sopenharmony_ci<code>false</code> otherwise.</li> 8931cb0ef41Sopenharmony_ci</ul> 8941cb0ef41Sopenharmony_ci 8951cb0ef41Sopenharmony_ci<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>; 8961cb0ef41Sopenharmony_ci<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>); 8971cb0ef41Sopenharmony_ci 8981cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> cert = <span class="hljs-title class_">Certificate</span>(); 8991cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>(); 9001cb0ef41Sopenharmony_ci<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))); 9011cb0ef41Sopenharmony_ci<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>); 9021cb0ef41Sopenharmony_ci<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>); 9031cb0ef41Sopenharmony_ci 9041cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> cert = <span class="hljs-title class_">Certificate</span>(); 9051cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow</span>(); 9061cb0ef41Sopenharmony_ci<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))); 9071cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: true or false</span></code><button class="copy-button">copy</button></pre> 9081cb0ef41Sopenharmony_ci</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> 9091cb0ef41Sopenharmony_ci<div class="api_metadata"> 9101cb0ef41Sopenharmony_ci<span>Added in: v0.1.94</span> 9111cb0ef41Sopenharmony_ci</div> 9121cb0ef41Sopenharmony_ci<ul> 9131cb0ef41Sopenharmony_ci<li>Extends: <a href="stream.html#class-streamtransform" class="type"><stream.Transform></a></li> 9141cb0ef41Sopenharmony_ci</ul> 9151cb0ef41Sopenharmony_ci<p>Instances of the <code>Cipher</code> class are used to encrypt data. The class can be 9161cb0ef41Sopenharmony_ciused in one of two ways:</p> 9171cb0ef41Sopenharmony_ci<ul> 9181cb0ef41Sopenharmony_ci<li>As a <a href="stream.html">stream</a> that is both readable and writable, where plain unencrypted 9191cb0ef41Sopenharmony_cidata is written to produce encrypted data on the readable side, or</li> 9201cb0ef41Sopenharmony_ci<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 9211cb0ef41Sopenharmony_cithe encrypted data.</li> 9221cb0ef41Sopenharmony_ci</ul> 9231cb0ef41Sopenharmony_ci<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 9241cb0ef41Sopenharmony_ciused to create <code>Cipher</code> instances. <code>Cipher</code> objects are not to be created 9251cb0ef41Sopenharmony_cidirectly using the <code>new</code> keyword.</p> 9261cb0ef41Sopenharmony_ci<p>Example: Using <code>Cipher</code> objects as streams:</p> 9271cb0ef41Sopenharmony_ci 9281cb0ef41Sopenharmony_ci<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> { 9291cb0ef41Sopenharmony_ci scrypt, 9301cb0ef41Sopenharmony_ci randomFill, 9311cb0ef41Sopenharmony_ci createCipheriv, 9321cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 9331cb0ef41Sopenharmony_ci 9341cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>; 9351cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>; 9361cb0ef41Sopenharmony_ci 9371cb0ef41Sopenharmony_ci<span class="hljs-comment">// First, we'll generate the key. The key length is dependent on the algorithm.</span> 9381cb0ef41Sopenharmony_ci<span class="hljs-comment">// In this case for aes192, it is 24 bytes (192 bits).</span> 9391cb0ef41Sopenharmony_ci<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> { 9401cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 9411cb0ef41Sopenharmony_ci <span class="hljs-comment">// Then, we'll generate a random initialization vector</span> 9421cb0ef41Sopenharmony_ci <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> { 9431cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 9441cb0ef41Sopenharmony_ci 9451cb0ef41Sopenharmony_ci <span class="hljs-comment">// Once we have the key and iv, we can create and use the cipher...</span> 9461cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> cipher = <span class="hljs-title function_">createCipheriv</span>(algorithm, key, iv); 9471cb0ef41Sopenharmony_ci 9481cb0ef41Sopenharmony_ci <span class="hljs-keyword">let</span> encrypted = <span class="hljs-string">''</span>; 9491cb0ef41Sopenharmony_ci cipher.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'hex'</span>); 9501cb0ef41Sopenharmony_ci 9511cb0ef41Sopenharmony_ci 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); 9521cb0ef41Sopenharmony_ci 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)); 9531cb0ef41Sopenharmony_ci 9541cb0ef41Sopenharmony_ci cipher.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some clear text data'</span>); 9551cb0ef41Sopenharmony_ci cipher.<span class="hljs-title function_">end</span>(); 9561cb0ef41Sopenharmony_ci }); 9571cb0ef41Sopenharmony_ci});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 9581cb0ef41Sopenharmony_ci scrypt, 9591cb0ef41Sopenharmony_ci randomFill, 9601cb0ef41Sopenharmony_ci createCipheriv, 9611cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 9621cb0ef41Sopenharmony_ci 9631cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>; 9641cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>; 9651cb0ef41Sopenharmony_ci 9661cb0ef41Sopenharmony_ci<span class="hljs-comment">// First, we'll generate the key. The key length is dependent on the algorithm.</span> 9671cb0ef41Sopenharmony_ci<span class="hljs-comment">// In this case for aes192, it is 24 bytes (192 bits).</span> 9681cb0ef41Sopenharmony_ci<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> { 9691cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 9701cb0ef41Sopenharmony_ci <span class="hljs-comment">// Then, we'll generate a random initialization vector</span> 9711cb0ef41Sopenharmony_ci <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> { 9721cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 9731cb0ef41Sopenharmony_ci 9741cb0ef41Sopenharmony_ci <span class="hljs-comment">// Once we have the key and iv, we can create and use the cipher...</span> 9751cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> cipher = <span class="hljs-title function_">createCipheriv</span>(algorithm, key, iv); 9761cb0ef41Sopenharmony_ci 9771cb0ef41Sopenharmony_ci <span class="hljs-keyword">let</span> encrypted = <span class="hljs-string">''</span>; 9781cb0ef41Sopenharmony_ci cipher.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'hex'</span>); 9791cb0ef41Sopenharmony_ci 9801cb0ef41Sopenharmony_ci 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); 9811cb0ef41Sopenharmony_ci 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)); 9821cb0ef41Sopenharmony_ci 9831cb0ef41Sopenharmony_ci cipher.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some clear text data'</span>); 9841cb0ef41Sopenharmony_ci cipher.<span class="hljs-title function_">end</span>(); 9851cb0ef41Sopenharmony_ci }); 9861cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 9871cb0ef41Sopenharmony_ci<p>Example: Using <code>Cipher</code> and piped streams:</p> 9881cb0ef41Sopenharmony_ci 9891cb0ef41Sopenharmony_ci<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> { 9901cb0ef41Sopenharmony_ci createReadStream, 9911cb0ef41Sopenharmony_ci createWriteStream, 9921cb0ef41Sopenharmony_ci} <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 9931cb0ef41Sopenharmony_ci 9941cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { 9951cb0ef41Sopenharmony_ci pipeline, 9961cb0ef41Sopenharmony_ci} <span class="hljs-keyword">from</span> <span class="hljs-string">'node:stream'</span>; 9971cb0ef41Sopenharmony_ci 9981cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 9991cb0ef41Sopenharmony_ci scrypt, 10001cb0ef41Sopenharmony_ci randomFill, 10011cb0ef41Sopenharmony_ci createCipheriv, 10021cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 10031cb0ef41Sopenharmony_ci 10041cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>; 10051cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>; 10061cb0ef41Sopenharmony_ci 10071cb0ef41Sopenharmony_ci<span class="hljs-comment">// First, we'll generate the key. The key length is dependent on the algorithm.</span> 10081cb0ef41Sopenharmony_ci<span class="hljs-comment">// In this case for aes192, it is 24 bytes (192 bits).</span> 10091cb0ef41Sopenharmony_ci<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> { 10101cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 10111cb0ef41Sopenharmony_ci <span class="hljs-comment">// Then, we'll generate a random initialization vector</span> 10121cb0ef41Sopenharmony_ci <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> { 10131cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 10141cb0ef41Sopenharmony_ci 10151cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> cipher = <span class="hljs-title function_">createCipheriv</span>(algorithm, key, iv); 10161cb0ef41Sopenharmony_ci 10171cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> input = <span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'test.js'</span>); 10181cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> output = <span class="hljs-title function_">createWriteStream</span>(<span class="hljs-string">'test.enc'</span>); 10191cb0ef41Sopenharmony_ci 10201cb0ef41Sopenharmony_ci <span class="hljs-title function_">pipeline</span>(input, cipher, output, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 10211cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 10221cb0ef41Sopenharmony_ci }); 10231cb0ef41Sopenharmony_ci }); 10241cb0ef41Sopenharmony_ci});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 10251cb0ef41Sopenharmony_ci createReadStream, 10261cb0ef41Sopenharmony_ci createWriteStream, 10271cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 10281cb0ef41Sopenharmony_ci 10291cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 10301cb0ef41Sopenharmony_ci pipeline, 10311cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:stream'</span>); 10321cb0ef41Sopenharmony_ci 10331cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 10341cb0ef41Sopenharmony_ci scrypt, 10351cb0ef41Sopenharmony_ci randomFill, 10361cb0ef41Sopenharmony_ci createCipheriv, 10371cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 10381cb0ef41Sopenharmony_ci 10391cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>; 10401cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>; 10411cb0ef41Sopenharmony_ci 10421cb0ef41Sopenharmony_ci<span class="hljs-comment">// First, we'll generate the key. The key length is dependent on the algorithm.</span> 10431cb0ef41Sopenharmony_ci<span class="hljs-comment">// In this case for aes192, it is 24 bytes (192 bits).</span> 10441cb0ef41Sopenharmony_ci<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> { 10451cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 10461cb0ef41Sopenharmony_ci <span class="hljs-comment">// Then, we'll generate a random initialization vector</span> 10471cb0ef41Sopenharmony_ci <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> { 10481cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 10491cb0ef41Sopenharmony_ci 10501cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> cipher = <span class="hljs-title function_">createCipheriv</span>(algorithm, key, iv); 10511cb0ef41Sopenharmony_ci 10521cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> input = <span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'test.js'</span>); 10531cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> output = <span class="hljs-title function_">createWriteStream</span>(<span class="hljs-string">'test.enc'</span>); 10541cb0ef41Sopenharmony_ci 10551cb0ef41Sopenharmony_ci <span class="hljs-title function_">pipeline</span>(input, cipher, output, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 10561cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 10571cb0ef41Sopenharmony_ci }); 10581cb0ef41Sopenharmony_ci }); 10591cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 10601cb0ef41Sopenharmony_ci<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> 10611cb0ef41Sopenharmony_ci 10621cb0ef41Sopenharmony_ci<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> { 10631cb0ef41Sopenharmony_ci scrypt, 10641cb0ef41Sopenharmony_ci randomFill, 10651cb0ef41Sopenharmony_ci createCipheriv, 10661cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 10671cb0ef41Sopenharmony_ci 10681cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>; 10691cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>; 10701cb0ef41Sopenharmony_ci 10711cb0ef41Sopenharmony_ci<span class="hljs-comment">// First, we'll generate the key. The key length is dependent on the algorithm.</span> 10721cb0ef41Sopenharmony_ci<span class="hljs-comment">// In this case for aes192, it is 24 bytes (192 bits).</span> 10731cb0ef41Sopenharmony_ci<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> { 10741cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 10751cb0ef41Sopenharmony_ci <span class="hljs-comment">// Then, we'll generate a random initialization vector</span> 10761cb0ef41Sopenharmony_ci <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> { 10771cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 10781cb0ef41Sopenharmony_ci 10791cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> cipher = <span class="hljs-title function_">createCipheriv</span>(algorithm, key, iv); 10801cb0ef41Sopenharmony_ci 10811cb0ef41Sopenharmony_ci <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>); 10821cb0ef41Sopenharmony_ci encrypted += cipher.<span class="hljs-title function_">final</span>(<span class="hljs-string">'hex'</span>); 10831cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(encrypted); 10841cb0ef41Sopenharmony_ci }); 10851cb0ef41Sopenharmony_ci});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 10861cb0ef41Sopenharmony_ci scrypt, 10871cb0ef41Sopenharmony_ci randomFill, 10881cb0ef41Sopenharmony_ci createCipheriv, 10891cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 10901cb0ef41Sopenharmony_ci 10911cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>; 10921cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>; 10931cb0ef41Sopenharmony_ci 10941cb0ef41Sopenharmony_ci<span class="hljs-comment">// First, we'll generate the key. The key length is dependent on the algorithm.</span> 10951cb0ef41Sopenharmony_ci<span class="hljs-comment">// In this case for aes192, it is 24 bytes (192 bits).</span> 10961cb0ef41Sopenharmony_ci<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> { 10971cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 10981cb0ef41Sopenharmony_ci <span class="hljs-comment">// Then, we'll generate a random initialization vector</span> 10991cb0ef41Sopenharmony_ci <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> { 11001cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 11011cb0ef41Sopenharmony_ci 11021cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> cipher = <span class="hljs-title function_">createCipheriv</span>(algorithm, key, iv); 11031cb0ef41Sopenharmony_ci 11041cb0ef41Sopenharmony_ci <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>); 11051cb0ef41Sopenharmony_ci encrypted += cipher.<span class="hljs-title function_">final</span>(<span class="hljs-string">'hex'</span>); 11061cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(encrypted); 11071cb0ef41Sopenharmony_ci }); 11081cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 11091cb0ef41Sopenharmony_ci<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> 11101cb0ef41Sopenharmony_ci<div class="api_metadata"> 11111cb0ef41Sopenharmony_ci<span>Added in: v0.1.94</span> 11121cb0ef41Sopenharmony_ci</div> 11131cb0ef41Sopenharmony_ci<ul> 11141cb0ef41Sopenharmony_ci<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> 11151cb0ef41Sopenharmony_ci<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. 11161cb0ef41Sopenharmony_ciIf <code>outputEncoding</code> is specified, a string is 11171cb0ef41Sopenharmony_cireturned. If an <code>outputEncoding</code> is not provided, a <a href="buffer.html"><code>Buffer</code></a> is returned.</li> 11181cb0ef41Sopenharmony_ci</ul> 11191cb0ef41Sopenharmony_ci<p>Once the <code>cipher.final()</code> method has been called, the <code>Cipher</code> object can no 11201cb0ef41Sopenharmony_cilonger be used to encrypt data. Attempts to call <code>cipher.final()</code> more than 11211cb0ef41Sopenharmony_cionce will result in an error being thrown.</p> 11221cb0ef41Sopenharmony_ci<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> 11231cb0ef41Sopenharmony_ci<div class="api_metadata"> 11241cb0ef41Sopenharmony_ci<span>Added in: v1.0.0</span> 11251cb0ef41Sopenharmony_ci</div> 11261cb0ef41Sopenharmony_ci<ul> 11271cb0ef41Sopenharmony_ci<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> When using an authenticated encryption mode (<code>GCM</code>, <code>CCM</code>, 11281cb0ef41Sopenharmony_ci<code>OCB</code>, and <code>chacha20-poly1305</code> are currently supported), the 11291cb0ef41Sopenharmony_ci<code>cipher.getAuthTag()</code> method returns a 11301cb0ef41Sopenharmony_ci<a href="buffer.html"><code>Buffer</code></a> containing the <em>authentication tag</em> that has been computed from 11311cb0ef41Sopenharmony_cithe given data.</li> 11321cb0ef41Sopenharmony_ci</ul> 11331cb0ef41Sopenharmony_ci<p>The <code>cipher.getAuthTag()</code> method should only be called after encryption has 11341cb0ef41Sopenharmony_cibeen completed using the <a href="#cipherfinaloutputencoding"><code>cipher.final()</code></a> method.</p> 11351cb0ef41Sopenharmony_ci<p>If the <code>authTagLength</code> option was set during the <code>cipher</code> instance's creation, 11361cb0ef41Sopenharmony_cithis function will return exactly <code>authTagLength</code> bytes.</p> 11371cb0ef41Sopenharmony_ci<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> 11381cb0ef41Sopenharmony_ci<div class="api_metadata"> 11391cb0ef41Sopenharmony_ci<span>Added in: v1.0.0</span> 11401cb0ef41Sopenharmony_ci</div> 11411cb0ef41Sopenharmony_ci<ul> 11421cb0ef41Sopenharmony_ci<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> 11431cb0ef41Sopenharmony_ci<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> 11441cb0ef41Sopenharmony_ci<ul> 11451cb0ef41Sopenharmony_ci<li><code>plaintextLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 11461cb0ef41Sopenharmony_ci<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> 11471cb0ef41Sopenharmony_ci</ul> 11481cb0ef41Sopenharmony_ci</li> 11491cb0ef41Sopenharmony_ci<li>Returns: <a href="crypto.html#class-cipher" class="type"><Cipher></a> for method chaining.</li> 11501cb0ef41Sopenharmony_ci</ul> 11511cb0ef41Sopenharmony_ci<p>When using an authenticated encryption mode (<code>GCM</code>, <code>CCM</code>, <code>OCB</code>, and 11521cb0ef41Sopenharmony_ci<code>chacha20-poly1305</code> are 11531cb0ef41Sopenharmony_cicurrently supported), the <code>cipher.setAAD()</code> method sets the value used for the 11541cb0ef41Sopenharmony_ci<em>additional authenticated data</em> (AAD) input parameter.</p> 11551cb0ef41Sopenharmony_ci<p>The <code>plaintextLength</code> option is optional for <code>GCM</code> and <code>OCB</code>. When using <code>CCM</code>, 11561cb0ef41Sopenharmony_cithe <code>plaintextLength</code> option must be specified and its value must match the 11571cb0ef41Sopenharmony_cilength of the plaintext in bytes. See <a href="#ccm-mode">CCM mode</a>.</p> 11581cb0ef41Sopenharmony_ci<p>The <code>cipher.setAAD()</code> method must be called before <a href="#cipherupdatedata-inputencoding-outputencoding"><code>cipher.update()</code></a>.</p> 11591cb0ef41Sopenharmony_ci<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> 11601cb0ef41Sopenharmony_ci<div class="api_metadata"> 11611cb0ef41Sopenharmony_ci<span>Added in: v0.7.1</span> 11621cb0ef41Sopenharmony_ci</div> 11631cb0ef41Sopenharmony_ci<ul> 11641cb0ef41Sopenharmony_ci<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> 11651cb0ef41Sopenharmony_ci<li>Returns: <a href="crypto.html#class-cipher" class="type"><Cipher></a> for method chaining.</li> 11661cb0ef41Sopenharmony_ci</ul> 11671cb0ef41Sopenharmony_ci<p>When using block encryption algorithms, the <code>Cipher</code> class will automatically 11681cb0ef41Sopenharmony_ciadd padding to the input data to the appropriate block size. To disable the 11691cb0ef41Sopenharmony_cidefault padding call <code>cipher.setAutoPadding(false)</code>.</p> 11701cb0ef41Sopenharmony_ci<p>When <code>autoPadding</code> is <code>false</code>, the length of the entire input data must be a 11711cb0ef41Sopenharmony_cimultiple of the cipher's block size or <a href="#cipherfinaloutputencoding"><code>cipher.final()</code></a> will throw an error. 11721cb0ef41Sopenharmony_ciDisabling automatic padding is useful for non-standard padding, for instance 11731cb0ef41Sopenharmony_ciusing <code>0x0</code> instead of PKCS padding.</p> 11741cb0ef41Sopenharmony_ci<p>The <code>cipher.setAutoPadding()</code> method must be called before 11751cb0ef41Sopenharmony_ci<a href="#cipherfinaloutputencoding"><code>cipher.final()</code></a>.</p> 11761cb0ef41Sopenharmony_ci<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> 11771cb0ef41Sopenharmony_ci<div class="api_metadata"> 11781cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 11791cb0ef41Sopenharmony_ci<table> 11801cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 11811cb0ef41Sopenharmony_ci<tr><td>v6.0.0</td> 11821cb0ef41Sopenharmony_ci<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p></td></tr> 11831cb0ef41Sopenharmony_ci<tr><td>v0.1.94</td> 11841cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.94</span></p></td></tr> 11851cb0ef41Sopenharmony_ci</tbody></table> 11861cb0ef41Sopenharmony_ci</details> 11871cb0ef41Sopenharmony_ci</div> 11881cb0ef41Sopenharmony_ci<ul> 11891cb0ef41Sopenharmony_ci<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> 11901cb0ef41Sopenharmony_ci<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> 11911cb0ef41Sopenharmony_ci<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> 11921cb0ef41Sopenharmony_ci<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> 11931cb0ef41Sopenharmony_ci</ul> 11941cb0ef41Sopenharmony_ci<p>Updates the cipher with <code>data</code>. If the <code>inputEncoding</code> argument is given, 11951cb0ef41Sopenharmony_cithe <code>data</code> 11961cb0ef41Sopenharmony_ciargument is a string using the specified encoding. If the <code>inputEncoding</code> 11971cb0ef41Sopenharmony_ciargument is not given, <code>data</code> must be a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or 11981cb0ef41Sopenharmony_ci<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 11991cb0ef41Sopenharmony_ci<code>inputEncoding</code> is ignored.</p> 12001cb0ef41Sopenharmony_ci<p>The <code>outputEncoding</code> specifies the output format of the enciphered 12011cb0ef41Sopenharmony_cidata. If the <code>outputEncoding</code> 12021cb0ef41Sopenharmony_ciis specified, a string using the specified encoding is returned. If no 12031cb0ef41Sopenharmony_ci<code>outputEncoding</code> is provided, a <a href="buffer.html"><code>Buffer</code></a> is returned.</p> 12041cb0ef41Sopenharmony_ci<p>The <code>cipher.update()</code> method can be called multiple times with new data until 12051cb0ef41Sopenharmony_ci<a href="#cipherfinaloutputencoding"><code>cipher.final()</code></a> is called. Calling <code>cipher.update()</code> after 12061cb0ef41Sopenharmony_ci<a href="#cipherfinaloutputencoding"><code>cipher.final()</code></a> will result in an error being thrown.</p> 12071cb0ef41Sopenharmony_ci</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> 12081cb0ef41Sopenharmony_ci<div class="api_metadata"> 12091cb0ef41Sopenharmony_ci<span>Added in: v0.1.94</span> 12101cb0ef41Sopenharmony_ci</div> 12111cb0ef41Sopenharmony_ci<ul> 12121cb0ef41Sopenharmony_ci<li>Extends: <a href="stream.html#class-streamtransform" class="type"><stream.Transform></a></li> 12131cb0ef41Sopenharmony_ci</ul> 12141cb0ef41Sopenharmony_ci<p>Instances of the <code>Decipher</code> class are used to decrypt data. The class can be 12151cb0ef41Sopenharmony_ciused in one of two ways:</p> 12161cb0ef41Sopenharmony_ci<ul> 12171cb0ef41Sopenharmony_ci<li>As a <a href="stream.html">stream</a> that is both readable and writable, where plain encrypted 12181cb0ef41Sopenharmony_cidata is written to produce unencrypted data on the readable side, or</li> 12191cb0ef41Sopenharmony_ci<li>Using the <a href="#decipherupdatedata-inputencoding-outputencoding"><code>decipher.update()</code></a> and <a href="#decipherfinaloutputencoding"><code>decipher.final()</code></a> methods to 12201cb0ef41Sopenharmony_ciproduce the unencrypted data.</li> 12211cb0ef41Sopenharmony_ci</ul> 12221cb0ef41Sopenharmony_ci<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 12231cb0ef41Sopenharmony_ciused to create <code>Decipher</code> instances. <code>Decipher</code> objects are not to be created 12241cb0ef41Sopenharmony_cidirectly using the <code>new</code> keyword.</p> 12251cb0ef41Sopenharmony_ci<p>Example: Using <code>Decipher</code> objects as streams:</p> 12261cb0ef41Sopenharmony_ci 12271cb0ef41Sopenharmony_ci<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>; 12281cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 12291cb0ef41Sopenharmony_ci scryptSync, 12301cb0ef41Sopenharmony_ci createDecipheriv, 12311cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 12321cb0ef41Sopenharmony_ci 12331cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>; 12341cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>; 12351cb0ef41Sopenharmony_ci<span class="hljs-comment">// Key length is dependent on the algorithm. In this case for aes192, it is</span> 12361cb0ef41Sopenharmony_ci<span class="hljs-comment">// 24 bytes (192 bits).</span> 12371cb0ef41Sopenharmony_ci<span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span> 12381cb0ef41Sopenharmony_ci<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>); 12391cb0ef41Sopenharmony_ci<span class="hljs-comment">// The IV is usually passed along with the ciphertext.</span> 12401cb0ef41Sopenharmony_ci<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> 12411cb0ef41Sopenharmony_ci 12421cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> decipher = <span class="hljs-title function_">createDecipheriv</span>(algorithm, key, iv); 12431cb0ef41Sopenharmony_ci 12441cb0ef41Sopenharmony_ci<span class="hljs-keyword">let</span> decrypted = <span class="hljs-string">''</span>; 12451cb0ef41Sopenharmony_cidecipher.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> { 12461cb0ef41Sopenharmony_ci <span class="hljs-keyword">let</span> chunk; 12471cb0ef41Sopenharmony_ci <span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (chunk = decipher.<span class="hljs-title function_">read</span>())) { 12481cb0ef41Sopenharmony_ci decrypted += chunk.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'utf8'</span>); 12491cb0ef41Sopenharmony_ci } 12501cb0ef41Sopenharmony_ci}); 12511cb0ef41Sopenharmony_cidecipher.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> { 12521cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(decrypted); 12531cb0ef41Sopenharmony_ci <span class="hljs-comment">// Prints: some clear text data</span> 12541cb0ef41Sopenharmony_ci}); 12551cb0ef41Sopenharmony_ci 12561cb0ef41Sopenharmony_ci<span class="hljs-comment">// Encrypted with same algorithm, key and iv.</span> 12571cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> encrypted = 12581cb0ef41Sopenharmony_ci <span class="hljs-string">'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa'</span>; 12591cb0ef41Sopenharmony_cidecipher.<span class="hljs-title function_">write</span>(encrypted, <span class="hljs-string">'hex'</span>); 12601cb0ef41Sopenharmony_cidecipher.<span class="hljs-title function_">end</span>();</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 12611cb0ef41Sopenharmony_ci scryptSync, 12621cb0ef41Sopenharmony_ci createDecipheriv, 12631cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 12641cb0ef41Sopenharmony_ci<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>); 12651cb0ef41Sopenharmony_ci 12661cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>; 12671cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>; 12681cb0ef41Sopenharmony_ci<span class="hljs-comment">// Key length is dependent on the algorithm. In this case for aes192, it is</span> 12691cb0ef41Sopenharmony_ci<span class="hljs-comment">// 24 bytes (192 bits).</span> 12701cb0ef41Sopenharmony_ci<span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span> 12711cb0ef41Sopenharmony_ci<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>); 12721cb0ef41Sopenharmony_ci<span class="hljs-comment">// The IV is usually passed along with the ciphertext.</span> 12731cb0ef41Sopenharmony_ci<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> 12741cb0ef41Sopenharmony_ci 12751cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> decipher = <span class="hljs-title function_">createDecipheriv</span>(algorithm, key, iv); 12761cb0ef41Sopenharmony_ci 12771cb0ef41Sopenharmony_ci<span class="hljs-keyword">let</span> decrypted = <span class="hljs-string">''</span>; 12781cb0ef41Sopenharmony_cidecipher.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> { 12791cb0ef41Sopenharmony_ci <span class="hljs-keyword">let</span> chunk; 12801cb0ef41Sopenharmony_ci <span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (chunk = decipher.<span class="hljs-title function_">read</span>())) { 12811cb0ef41Sopenharmony_ci decrypted += chunk.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'utf8'</span>); 12821cb0ef41Sopenharmony_ci } 12831cb0ef41Sopenharmony_ci}); 12841cb0ef41Sopenharmony_cidecipher.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> { 12851cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(decrypted); 12861cb0ef41Sopenharmony_ci <span class="hljs-comment">// Prints: some clear text data</span> 12871cb0ef41Sopenharmony_ci}); 12881cb0ef41Sopenharmony_ci 12891cb0ef41Sopenharmony_ci<span class="hljs-comment">// Encrypted with same algorithm, key and iv.</span> 12901cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> encrypted = 12911cb0ef41Sopenharmony_ci <span class="hljs-string">'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa'</span>; 12921cb0ef41Sopenharmony_cidecipher.<span class="hljs-title function_">write</span>(encrypted, <span class="hljs-string">'hex'</span>); 12931cb0ef41Sopenharmony_cidecipher.<span class="hljs-title function_">end</span>();</code><button class="copy-button">copy</button></pre> 12941cb0ef41Sopenharmony_ci<p>Example: Using <code>Decipher</code> and piped streams:</p> 12951cb0ef41Sopenharmony_ci 12961cb0ef41Sopenharmony_ci<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> { 12971cb0ef41Sopenharmony_ci createReadStream, 12981cb0ef41Sopenharmony_ci createWriteStream, 12991cb0ef41Sopenharmony_ci} <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 13001cb0ef41Sopenharmony_ci<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>; 13011cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 13021cb0ef41Sopenharmony_ci scryptSync, 13031cb0ef41Sopenharmony_ci createDecipheriv, 13041cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 13051cb0ef41Sopenharmony_ci 13061cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>; 13071cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>; 13081cb0ef41Sopenharmony_ci<span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span> 13091cb0ef41Sopenharmony_ci<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>); 13101cb0ef41Sopenharmony_ci<span class="hljs-comment">// The IV is usually passed along with the ciphertext.</span> 13111cb0ef41Sopenharmony_ci<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> 13121cb0ef41Sopenharmony_ci 13131cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> decipher = <span class="hljs-title function_">createDecipheriv</span>(algorithm, key, iv); 13141cb0ef41Sopenharmony_ci 13151cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> input = <span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'test.enc'</span>); 13161cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> output = <span class="hljs-title function_">createWriteStream</span>(<span class="hljs-string">'test.js'</span>); 13171cb0ef41Sopenharmony_ci 13181cb0ef41Sopenharmony_ciinput.<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> { 13191cb0ef41Sopenharmony_ci createReadStream, 13201cb0ef41Sopenharmony_ci createWriteStream, 13211cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 13221cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 13231cb0ef41Sopenharmony_ci scryptSync, 13241cb0ef41Sopenharmony_ci createDecipheriv, 13251cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 13261cb0ef41Sopenharmony_ci<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>); 13271cb0ef41Sopenharmony_ci 13281cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>; 13291cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>; 13301cb0ef41Sopenharmony_ci<span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span> 13311cb0ef41Sopenharmony_ci<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>); 13321cb0ef41Sopenharmony_ci<span class="hljs-comment">// The IV is usually passed along with the ciphertext.</span> 13331cb0ef41Sopenharmony_ci<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> 13341cb0ef41Sopenharmony_ci 13351cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> decipher = <span class="hljs-title function_">createDecipheriv</span>(algorithm, key, iv); 13361cb0ef41Sopenharmony_ci 13371cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> input = <span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'test.enc'</span>); 13381cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> output = <span class="hljs-title function_">createWriteStream</span>(<span class="hljs-string">'test.js'</span>); 13391cb0ef41Sopenharmony_ci 13401cb0ef41Sopenharmony_ciinput.<span class="hljs-title function_">pipe</span>(decipher).<span class="hljs-title function_">pipe</span>(output);</code><button class="copy-button">copy</button></pre> 13411cb0ef41Sopenharmony_ci<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> 13421cb0ef41Sopenharmony_ci 13431cb0ef41Sopenharmony_ci<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>; 13441cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 13451cb0ef41Sopenharmony_ci scryptSync, 13461cb0ef41Sopenharmony_ci createDecipheriv, 13471cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 13481cb0ef41Sopenharmony_ci 13491cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>; 13501cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>; 13511cb0ef41Sopenharmony_ci<span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span> 13521cb0ef41Sopenharmony_ci<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>); 13531cb0ef41Sopenharmony_ci<span class="hljs-comment">// The IV is usually passed along with the ciphertext.</span> 13541cb0ef41Sopenharmony_ci<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> 13551cb0ef41Sopenharmony_ci 13561cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> decipher = <span class="hljs-title function_">createDecipheriv</span>(algorithm, key, iv); 13571cb0ef41Sopenharmony_ci 13581cb0ef41Sopenharmony_ci<span class="hljs-comment">// Encrypted using same algorithm, key and iv.</span> 13591cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> encrypted = 13601cb0ef41Sopenharmony_ci <span class="hljs-string">'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa'</span>; 13611cb0ef41Sopenharmony_ci<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>); 13621cb0ef41Sopenharmony_cidecrypted += decipher.<span class="hljs-title function_">final</span>(<span class="hljs-string">'utf8'</span>); 13631cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(decrypted); 13641cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: some clear text data</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 13651cb0ef41Sopenharmony_ci scryptSync, 13661cb0ef41Sopenharmony_ci createDecipheriv, 13671cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 13681cb0ef41Sopenharmony_ci<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>); 13691cb0ef41Sopenharmony_ci 13701cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>; 13711cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to generate key'</span>; 13721cb0ef41Sopenharmony_ci<span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span> 13731cb0ef41Sopenharmony_ci<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>); 13741cb0ef41Sopenharmony_ci<span class="hljs-comment">// The IV is usually passed along with the ciphertext.</span> 13751cb0ef41Sopenharmony_ci<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> 13761cb0ef41Sopenharmony_ci 13771cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> decipher = <span class="hljs-title function_">createDecipheriv</span>(algorithm, key, iv); 13781cb0ef41Sopenharmony_ci 13791cb0ef41Sopenharmony_ci<span class="hljs-comment">// Encrypted using same algorithm, key and iv.</span> 13801cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> encrypted = 13811cb0ef41Sopenharmony_ci <span class="hljs-string">'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa'</span>; 13821cb0ef41Sopenharmony_ci<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>); 13831cb0ef41Sopenharmony_cidecrypted += decipher.<span class="hljs-title function_">final</span>(<span class="hljs-string">'utf8'</span>); 13841cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(decrypted); 13851cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: some clear text data</span></code><button class="copy-button">copy</button></pre> 13861cb0ef41Sopenharmony_ci<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> 13871cb0ef41Sopenharmony_ci<div class="api_metadata"> 13881cb0ef41Sopenharmony_ci<span>Added in: v0.1.94</span> 13891cb0ef41Sopenharmony_ci</div> 13901cb0ef41Sopenharmony_ci<ul> 13911cb0ef41Sopenharmony_ci<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> 13921cb0ef41Sopenharmony_ci<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. 13931cb0ef41Sopenharmony_ciIf <code>outputEncoding</code> is specified, a string is 13941cb0ef41Sopenharmony_cireturned. If an <code>outputEncoding</code> is not provided, a <a href="buffer.html"><code>Buffer</code></a> is returned.</li> 13951cb0ef41Sopenharmony_ci</ul> 13961cb0ef41Sopenharmony_ci<p>Once the <code>decipher.final()</code> method has been called, the <code>Decipher</code> object can 13971cb0ef41Sopenharmony_cino longer be used to decrypt data. Attempts to call <code>decipher.final()</code> more 13981cb0ef41Sopenharmony_cithan once will result in an error being thrown.</p> 13991cb0ef41Sopenharmony_ci<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> 14001cb0ef41Sopenharmony_ci<div class="api_metadata"> 14011cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 14021cb0ef41Sopenharmony_ci<table> 14031cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 14041cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 14051cb0ef41Sopenharmony_ci<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> 14061cb0ef41Sopenharmony_ci<tr><td>v7.2.0</td> 14071cb0ef41Sopenharmony_ci<td><p>This method now returns a reference to <code>decipher</code>.</p></td></tr> 14081cb0ef41Sopenharmony_ci<tr><td>v1.0.0</td> 14091cb0ef41Sopenharmony_ci<td><p><span>Added in: v1.0.0</span></p></td></tr> 14101cb0ef41Sopenharmony_ci</tbody></table> 14111cb0ef41Sopenharmony_ci</details> 14121cb0ef41Sopenharmony_ci</div> 14131cb0ef41Sopenharmony_ci<ul> 14141cb0ef41Sopenharmony_ci<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> 14151cb0ef41Sopenharmony_ci<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> 14161cb0ef41Sopenharmony_ci<ul> 14171cb0ef41Sopenharmony_ci<li><code>plaintextLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 14181cb0ef41Sopenharmony_ci<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> 14191cb0ef41Sopenharmony_ci</ul> 14201cb0ef41Sopenharmony_ci</li> 14211cb0ef41Sopenharmony_ci<li>Returns: <a href="crypto.html#class-decipher" class="type"><Decipher></a> for method chaining.</li> 14221cb0ef41Sopenharmony_ci</ul> 14231cb0ef41Sopenharmony_ci<p>When using an authenticated encryption mode (<code>GCM</code>, <code>CCM</code>, <code>OCB</code>, and 14241cb0ef41Sopenharmony_ci<code>chacha20-poly1305</code> are 14251cb0ef41Sopenharmony_cicurrently supported), the <code>decipher.setAAD()</code> method sets the value used for the 14261cb0ef41Sopenharmony_ci<em>additional authenticated data</em> (AAD) input parameter.</p> 14271cb0ef41Sopenharmony_ci<p>The <code>options</code> argument is optional for <code>GCM</code>. When using <code>CCM</code>, the 14281cb0ef41Sopenharmony_ci<code>plaintextLength</code> option must be specified and its value must match the length 14291cb0ef41Sopenharmony_ciof the ciphertext in bytes. See <a href="#ccm-mode">CCM mode</a>.</p> 14301cb0ef41Sopenharmony_ci<p>The <code>decipher.setAAD()</code> method must be called before <a href="#decipherupdatedata-inputencoding-outputencoding"><code>decipher.update()</code></a>.</p> 14311cb0ef41Sopenharmony_ci<p>When passing a string as the <code>buffer</code>, please consider 14321cb0ef41Sopenharmony_ci<a href="#using-strings-as-inputs-to-cryptographic-apis">caveats when using strings as inputs to cryptographic APIs</a>.</p> 14331cb0ef41Sopenharmony_ci<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> 14341cb0ef41Sopenharmony_ci<div class="api_metadata"> 14351cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 14361cb0ef41Sopenharmony_ci<table> 14371cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 14381cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 14391cb0ef41Sopenharmony_ci<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> 14401cb0ef41Sopenharmony_ci<tr><td>v11.0.0</td> 14411cb0ef41Sopenharmony_ci<td><p>This method now throws if the GCM tag length is invalid.</p></td></tr> 14421cb0ef41Sopenharmony_ci<tr><td>v7.2.0</td> 14431cb0ef41Sopenharmony_ci<td><p>This method now returns a reference to <code>decipher</code>.</p></td></tr> 14441cb0ef41Sopenharmony_ci<tr><td>v1.0.0</td> 14451cb0ef41Sopenharmony_ci<td><p><span>Added in: v1.0.0</span></p></td></tr> 14461cb0ef41Sopenharmony_ci</tbody></table> 14471cb0ef41Sopenharmony_ci</details> 14481cb0ef41Sopenharmony_ci</div> 14491cb0ef41Sopenharmony_ci<ul> 14501cb0ef41Sopenharmony_ci<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> 14511cb0ef41Sopenharmony_ci<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> 14521cb0ef41Sopenharmony_ci<li>Returns: <a href="crypto.html#class-decipher" class="type"><Decipher></a> for method chaining.</li> 14531cb0ef41Sopenharmony_ci</ul> 14541cb0ef41Sopenharmony_ci<p>When using an authenticated encryption mode (<code>GCM</code>, <code>CCM</code>, <code>OCB</code>, and 14551cb0ef41Sopenharmony_ci<code>chacha20-poly1305</code> are 14561cb0ef41Sopenharmony_cicurrently supported), the <code>decipher.setAuthTag()</code> method is used to pass in the 14571cb0ef41Sopenharmony_cireceived <em>authentication tag</em>. If no tag is provided, or if the cipher text 14581cb0ef41Sopenharmony_cihas been tampered with, <a href="#decipherfinaloutputencoding"><code>decipher.final()</code></a> will throw, indicating that the 14591cb0ef41Sopenharmony_cicipher text should be discarded due to failed authentication. If the tag length 14601cb0ef41Sopenharmony_ciis 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 14611cb0ef41Sopenharmony_ci<code>authTagLength</code> option, <code>decipher.setAuthTag()</code> will throw an error.</p> 14621cb0ef41Sopenharmony_ci<p>The <code>decipher.setAuthTag()</code> method must be called before <a href="#decipherupdatedata-inputencoding-outputencoding"><code>decipher.update()</code></a> 14631cb0ef41Sopenharmony_cifor <code>CCM</code> mode or before <a href="#decipherfinaloutputencoding"><code>decipher.final()</code></a> for <code>GCM</code> and <code>OCB</code> modes and 14641cb0ef41Sopenharmony_ci<code>chacha20-poly1305</code>. 14651cb0ef41Sopenharmony_ci<code>decipher.setAuthTag()</code> can only be called once.</p> 14661cb0ef41Sopenharmony_ci<p>When passing a string as the authentication tag, please consider 14671cb0ef41Sopenharmony_ci<a href="#using-strings-as-inputs-to-cryptographic-apis">caveats when using strings as inputs to cryptographic APIs</a>.</p> 14681cb0ef41Sopenharmony_ci<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> 14691cb0ef41Sopenharmony_ci<div class="api_metadata"> 14701cb0ef41Sopenharmony_ci<span>Added in: v0.7.1</span> 14711cb0ef41Sopenharmony_ci</div> 14721cb0ef41Sopenharmony_ci<ul> 14731cb0ef41Sopenharmony_ci<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> 14741cb0ef41Sopenharmony_ci<li>Returns: <a href="crypto.html#class-decipher" class="type"><Decipher></a> for method chaining.</li> 14751cb0ef41Sopenharmony_ci</ul> 14761cb0ef41Sopenharmony_ci<p>When data has been encrypted without standard block padding, calling 14771cb0ef41Sopenharmony_ci<code>decipher.setAutoPadding(false)</code> will disable automatic padding to prevent 14781cb0ef41Sopenharmony_ci<a href="#decipherfinaloutputencoding"><code>decipher.final()</code></a> from checking for and removing padding.</p> 14791cb0ef41Sopenharmony_ci<p>Turning auto padding off will only work if the input data's length is a 14801cb0ef41Sopenharmony_cimultiple of the ciphers block size.</p> 14811cb0ef41Sopenharmony_ci<p>The <code>decipher.setAutoPadding()</code> method must be called before 14821cb0ef41Sopenharmony_ci<a href="#decipherfinaloutputencoding"><code>decipher.final()</code></a>.</p> 14831cb0ef41Sopenharmony_ci<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> 14841cb0ef41Sopenharmony_ci<div class="api_metadata"> 14851cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 14861cb0ef41Sopenharmony_ci<table> 14871cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 14881cb0ef41Sopenharmony_ci<tr><td>v6.0.0</td> 14891cb0ef41Sopenharmony_ci<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p></td></tr> 14901cb0ef41Sopenharmony_ci<tr><td>v0.1.94</td> 14911cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.94</span></p></td></tr> 14921cb0ef41Sopenharmony_ci</tbody></table> 14931cb0ef41Sopenharmony_ci</details> 14941cb0ef41Sopenharmony_ci</div> 14951cb0ef41Sopenharmony_ci<ul> 14961cb0ef41Sopenharmony_ci<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> 14971cb0ef41Sopenharmony_ci<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> 14981cb0ef41Sopenharmony_ci<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> 14991cb0ef41Sopenharmony_ci<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> 15001cb0ef41Sopenharmony_ci</ul> 15011cb0ef41Sopenharmony_ci<p>Updates the decipher with <code>data</code>. If the <code>inputEncoding</code> argument is given, 15021cb0ef41Sopenharmony_cithe <code>data</code> 15031cb0ef41Sopenharmony_ciargument is a string using the specified encoding. If the <code>inputEncoding</code> 15041cb0ef41Sopenharmony_ciargument is not given, <code>data</code> must be a <a href="buffer.html"><code>Buffer</code></a>. If <code>data</code> is a 15051cb0ef41Sopenharmony_ci<a href="buffer.html"><code>Buffer</code></a> then <code>inputEncoding</code> is ignored.</p> 15061cb0ef41Sopenharmony_ci<p>The <code>outputEncoding</code> specifies the output format of the enciphered 15071cb0ef41Sopenharmony_cidata. If the <code>outputEncoding</code> 15081cb0ef41Sopenharmony_ciis specified, a string using the specified encoding is returned. If no 15091cb0ef41Sopenharmony_ci<code>outputEncoding</code> is provided, a <a href="buffer.html"><code>Buffer</code></a> is returned.</p> 15101cb0ef41Sopenharmony_ci<p>The <code>decipher.update()</code> method can be called multiple times with new data until 15111cb0ef41Sopenharmony_ci<a href="#decipherfinaloutputencoding"><code>decipher.final()</code></a> is called. Calling <code>decipher.update()</code> after 15121cb0ef41Sopenharmony_ci<a href="#decipherfinaloutputencoding"><code>decipher.final()</code></a> will result in an error being thrown.</p> 15131cb0ef41Sopenharmony_ci</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> 15141cb0ef41Sopenharmony_ci<div class="api_metadata"> 15151cb0ef41Sopenharmony_ci<span>Added in: v0.5.0</span> 15161cb0ef41Sopenharmony_ci</div> 15171cb0ef41Sopenharmony_ci<p>The <code>DiffieHellman</code> class is a utility for creating Diffie-Hellman key 15181cb0ef41Sopenharmony_ciexchanges.</p> 15191cb0ef41Sopenharmony_ci<p>Instances of the <code>DiffieHellman</code> class can be created using the 15201cb0ef41Sopenharmony_ci<a href="#cryptocreatediffiehellmanprime-primeencoding-generator-generatorencoding"><code>crypto.createDiffieHellman()</code></a> function.</p> 15211cb0ef41Sopenharmony_ci 15221cb0ef41Sopenharmony_ci<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>; 15231cb0ef41Sopenharmony_ci 15241cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 15251cb0ef41Sopenharmony_ci createDiffieHellman, 15261cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 15271cb0ef41Sopenharmony_ci 15281cb0ef41Sopenharmony_ci<span class="hljs-comment">// Generate Alice's keys...</span> 15291cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> alice = <span class="hljs-title function_">createDiffieHellman</span>(<span class="hljs-number">2048</span>); 15301cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> aliceKey = alice.<span class="hljs-title function_">generateKeys</span>(); 15311cb0ef41Sopenharmony_ci 15321cb0ef41Sopenharmony_ci<span class="hljs-comment">// Generate Bob's keys...</span> 15331cb0ef41Sopenharmony_ci<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>()); 15341cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> bobKey = bob.<span class="hljs-title function_">generateKeys</span>(); 15351cb0ef41Sopenharmony_ci 15361cb0ef41Sopenharmony_ci<span class="hljs-comment">// Exchange and generate the secret...</span> 15371cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> aliceSecret = alice.<span class="hljs-title function_">computeSecret</span>(bobKey); 15381cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> bobSecret = bob.<span class="hljs-title function_">computeSecret</span>(aliceKey); 15391cb0ef41Sopenharmony_ci 15401cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span> 15411cb0ef41Sopenharmony_ciassert.<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>); 15421cb0ef41Sopenharmony_ci 15431cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 15441cb0ef41Sopenharmony_ci createDiffieHellman, 15451cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 15461cb0ef41Sopenharmony_ci 15471cb0ef41Sopenharmony_ci<span class="hljs-comment">// Generate Alice's keys...</span> 15481cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> alice = <span class="hljs-title function_">createDiffieHellman</span>(<span class="hljs-number">2048</span>); 15491cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> aliceKey = alice.<span class="hljs-title function_">generateKeys</span>(); 15501cb0ef41Sopenharmony_ci 15511cb0ef41Sopenharmony_ci<span class="hljs-comment">// Generate Bob's keys...</span> 15521cb0ef41Sopenharmony_ci<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>()); 15531cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> bobKey = bob.<span class="hljs-title function_">generateKeys</span>(); 15541cb0ef41Sopenharmony_ci 15551cb0ef41Sopenharmony_ci<span class="hljs-comment">// Exchange and generate the secret...</span> 15561cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> aliceSecret = alice.<span class="hljs-title function_">computeSecret</span>(bobKey); 15571cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> bobSecret = bob.<span class="hljs-title function_">computeSecret</span>(aliceKey); 15581cb0ef41Sopenharmony_ci 15591cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span> 15601cb0ef41Sopenharmony_ciassert.<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> 15611cb0ef41Sopenharmony_ci<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> 15621cb0ef41Sopenharmony_ci<div class="api_metadata"> 15631cb0ef41Sopenharmony_ci<span>Added in: v0.5.0</span> 15641cb0ef41Sopenharmony_ci</div> 15651cb0ef41Sopenharmony_ci<ul> 15661cb0ef41Sopenharmony_ci<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> 15671cb0ef41Sopenharmony_ci<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> 15681cb0ef41Sopenharmony_ci<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> 15691cb0ef41Sopenharmony_ci<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> 15701cb0ef41Sopenharmony_ci</ul> 15711cb0ef41Sopenharmony_ci<p>Computes the shared secret using <code>otherPublicKey</code> as the other 15721cb0ef41Sopenharmony_ciparty's public key and returns the computed shared secret. The supplied 15731cb0ef41Sopenharmony_cikey is interpreted using the specified <code>inputEncoding</code>, and secret is 15741cb0ef41Sopenharmony_ciencoded using specified <code>outputEncoding</code>. 15751cb0ef41Sopenharmony_ciIf the <code>inputEncoding</code> is not 15761cb0ef41Sopenharmony_ciprovided, <code>otherPublicKey</code> is expected to be a <a href="buffer.html"><code>Buffer</code></a>, 15771cb0ef41Sopenharmony_ci<code>TypedArray</code>, or <code>DataView</code>.</p> 15781cb0ef41Sopenharmony_ci<p>If <code>outputEncoding</code> is given a string is returned; otherwise, a 15791cb0ef41Sopenharmony_ci<a href="buffer.html"><code>Buffer</code></a> is returned.</p> 15801cb0ef41Sopenharmony_ci<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> 15811cb0ef41Sopenharmony_ci<div class="api_metadata"> 15821cb0ef41Sopenharmony_ci<span>Added in: v0.5.0</span> 15831cb0ef41Sopenharmony_ci</div> 15841cb0ef41Sopenharmony_ci<ul> 15851cb0ef41Sopenharmony_ci<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> 15861cb0ef41Sopenharmony_ci<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> 15871cb0ef41Sopenharmony_ci</ul> 15881cb0ef41Sopenharmony_ci<p>Generates private and public Diffie-Hellman key values unless they have been 15891cb0ef41Sopenharmony_cigenerated or computed already, and returns 15901cb0ef41Sopenharmony_cithe public key in the specified <code>encoding</code>. This key should be 15911cb0ef41Sopenharmony_citransferred to the other party. 15921cb0ef41Sopenharmony_ciIf <code>encoding</code> is provided a string is returned; otherwise a 15931cb0ef41Sopenharmony_ci<a href="buffer.html"><code>Buffer</code></a> is returned.</p> 15941cb0ef41Sopenharmony_ci<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, 15951cb0ef41Sopenharmony_cionce a private key has been generated or set, calling this function only updates 15961cb0ef41Sopenharmony_cithe public key but does not generate a new private key.</p> 15971cb0ef41Sopenharmony_ci<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> 15981cb0ef41Sopenharmony_ci<div class="api_metadata"> 15991cb0ef41Sopenharmony_ci<span>Added in: v0.5.0</span> 16001cb0ef41Sopenharmony_ci</div> 16011cb0ef41Sopenharmony_ci<ul> 16021cb0ef41Sopenharmony_ci<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> 16031cb0ef41Sopenharmony_ci<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> 16041cb0ef41Sopenharmony_ci</ul> 16051cb0ef41Sopenharmony_ci<p>Returns the Diffie-Hellman generator in the specified <code>encoding</code>. 16061cb0ef41Sopenharmony_ciIf <code>encoding</code> is provided a string is 16071cb0ef41Sopenharmony_cireturned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is returned.</p> 16081cb0ef41Sopenharmony_ci<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> 16091cb0ef41Sopenharmony_ci<div class="api_metadata"> 16101cb0ef41Sopenharmony_ci<span>Added in: v0.5.0</span> 16111cb0ef41Sopenharmony_ci</div> 16121cb0ef41Sopenharmony_ci<ul> 16131cb0ef41Sopenharmony_ci<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> 16141cb0ef41Sopenharmony_ci<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> 16151cb0ef41Sopenharmony_ci</ul> 16161cb0ef41Sopenharmony_ci<p>Returns the Diffie-Hellman prime in the specified <code>encoding</code>. 16171cb0ef41Sopenharmony_ciIf <code>encoding</code> is provided a string is 16181cb0ef41Sopenharmony_cireturned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is returned.</p> 16191cb0ef41Sopenharmony_ci<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> 16201cb0ef41Sopenharmony_ci<div class="api_metadata"> 16211cb0ef41Sopenharmony_ci<span>Added in: v0.5.0</span> 16221cb0ef41Sopenharmony_ci</div> 16231cb0ef41Sopenharmony_ci<ul> 16241cb0ef41Sopenharmony_ci<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> 16251cb0ef41Sopenharmony_ci<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> 16261cb0ef41Sopenharmony_ci</ul> 16271cb0ef41Sopenharmony_ci<p>Returns the Diffie-Hellman private key in the specified <code>encoding</code>. 16281cb0ef41Sopenharmony_ciIf <code>encoding</code> is provided a 16291cb0ef41Sopenharmony_cistring is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is returned.</p> 16301cb0ef41Sopenharmony_ci<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> 16311cb0ef41Sopenharmony_ci<div class="api_metadata"> 16321cb0ef41Sopenharmony_ci<span>Added in: v0.5.0</span> 16331cb0ef41Sopenharmony_ci</div> 16341cb0ef41Sopenharmony_ci<ul> 16351cb0ef41Sopenharmony_ci<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> 16361cb0ef41Sopenharmony_ci<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> 16371cb0ef41Sopenharmony_ci</ul> 16381cb0ef41Sopenharmony_ci<p>Returns the Diffie-Hellman public key in the specified <code>encoding</code>. 16391cb0ef41Sopenharmony_ciIf <code>encoding</code> is provided a 16401cb0ef41Sopenharmony_cistring is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is returned.</p> 16411cb0ef41Sopenharmony_ci<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> 16421cb0ef41Sopenharmony_ci<div class="api_metadata"> 16431cb0ef41Sopenharmony_ci<span>Added in: v0.5.0</span> 16441cb0ef41Sopenharmony_ci</div> 16451cb0ef41Sopenharmony_ci<ul> 16461cb0ef41Sopenharmony_ci<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> 16471cb0ef41Sopenharmony_ci<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> 16481cb0ef41Sopenharmony_ci</ul> 16491cb0ef41Sopenharmony_ci<p>Sets the Diffie-Hellman private key. If the <code>encoding</code> argument is provided, 16501cb0ef41Sopenharmony_ci<code>privateKey</code> is expected 16511cb0ef41Sopenharmony_cito be a string. If no <code>encoding</code> is provided, <code>privateKey</code> is expected 16521cb0ef41Sopenharmony_cito be a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or <code>DataView</code>.</p> 16531cb0ef41Sopenharmony_ci<p>This function does not automatically compute the associated public key. Either 16541cb0ef41Sopenharmony_ci<a href="#diffiehellmansetpublickeypublickey-encoding"><code>diffieHellman.setPublicKey()</code></a> or <a href="#diffiehellmangeneratekeysencoding"><code>diffieHellman.generateKeys()</code></a> can be 16551cb0ef41Sopenharmony_ciused to manually provide the public key or to automatically derive it.</p> 16561cb0ef41Sopenharmony_ci<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> 16571cb0ef41Sopenharmony_ci<div class="api_metadata"> 16581cb0ef41Sopenharmony_ci<span>Added in: v0.5.0</span> 16591cb0ef41Sopenharmony_ci</div> 16601cb0ef41Sopenharmony_ci<ul> 16611cb0ef41Sopenharmony_ci<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> 16621cb0ef41Sopenharmony_ci<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> 16631cb0ef41Sopenharmony_ci</ul> 16641cb0ef41Sopenharmony_ci<p>Sets the Diffie-Hellman public key. If the <code>encoding</code> argument is provided, 16651cb0ef41Sopenharmony_ci<code>publicKey</code> is expected 16661cb0ef41Sopenharmony_cito be a string. If no <code>encoding</code> is provided, <code>publicKey</code> is expected 16671cb0ef41Sopenharmony_cito be a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or <code>DataView</code>.</p> 16681cb0ef41Sopenharmony_ci<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> 16691cb0ef41Sopenharmony_ci<div class="api_metadata"> 16701cb0ef41Sopenharmony_ci<span>Added in: v0.11.12</span> 16711cb0ef41Sopenharmony_ci</div> 16721cb0ef41Sopenharmony_ci<p>A bit field containing any warnings and/or errors resulting from a check 16731cb0ef41Sopenharmony_ciperformed during initialization of the <code>DiffieHellman</code> object.</p> 16741cb0ef41Sopenharmony_ci<p>The following values are valid for this property (as defined in <code>node:constants</code> module):</p> 16751cb0ef41Sopenharmony_ci<ul> 16761cb0ef41Sopenharmony_ci<li><code>DH_CHECK_P_NOT_SAFE_PRIME</code></li> 16771cb0ef41Sopenharmony_ci<li><code>DH_CHECK_P_NOT_PRIME</code></li> 16781cb0ef41Sopenharmony_ci<li><code>DH_UNABLE_TO_CHECK_GENERATOR</code></li> 16791cb0ef41Sopenharmony_ci<li><code>DH_NOT_SUITABLE_GENERATOR</code></li> 16801cb0ef41Sopenharmony_ci</ul> 16811cb0ef41Sopenharmony_ci</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> 16821cb0ef41Sopenharmony_ci<div class="api_metadata"> 16831cb0ef41Sopenharmony_ci<span>Added in: v0.7.5</span> 16841cb0ef41Sopenharmony_ci</div> 16851cb0ef41Sopenharmony_ci<p>The <code>DiffieHellmanGroup</code> class takes a well-known modp group as its argument. 16861cb0ef41Sopenharmony_ciIt works the same as <code>DiffieHellman</code>, except that it does not allow changing 16871cb0ef41Sopenharmony_ciits keys after creation. In other words, it does not implement <code>setPublicKey()</code> 16881cb0ef41Sopenharmony_cior <code>setPrivateKey()</code> methods.</p> 16891cb0ef41Sopenharmony_ci 16901cb0ef41Sopenharmony_ci<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>); 16911cb0ef41Sopenharmony_ci<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>); 16921cb0ef41Sopenharmony_ci<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> 16931cb0ef41Sopenharmony_ci<p>The following groups are supported:</p> 16941cb0ef41Sopenharmony_ci<ul> 16951cb0ef41Sopenharmony_ci<li><code>'modp14'</code> (2048 bits, <a href="https://www.rfc-editor.org/rfc/rfc3526.txt">RFC 3526</a> Section 3)</li> 16961cb0ef41Sopenharmony_ci<li><code>'modp15'</code> (3072 bits, <a href="https://www.rfc-editor.org/rfc/rfc3526.txt">RFC 3526</a> Section 4)</li> 16971cb0ef41Sopenharmony_ci<li><code>'modp16'</code> (4096 bits, <a href="https://www.rfc-editor.org/rfc/rfc3526.txt">RFC 3526</a> Section 5)</li> 16981cb0ef41Sopenharmony_ci<li><code>'modp17'</code> (6144 bits, <a href="https://www.rfc-editor.org/rfc/rfc3526.txt">RFC 3526</a> Section 6)</li> 16991cb0ef41Sopenharmony_ci<li><code>'modp18'</code> (8192 bits, <a href="https://www.rfc-editor.org/rfc/rfc3526.txt">RFC 3526</a> Section 7)</li> 17001cb0ef41Sopenharmony_ci</ul> 17011cb0ef41Sopenharmony_ci<p>The following groups are still supported but deprecated (see <a href="#support-for-weak-or-compromised-algorithms">Caveats</a>):</p> 17021cb0ef41Sopenharmony_ci<ul> 17031cb0ef41Sopenharmony_ci<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> 17041cb0ef41Sopenharmony_ci<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> 17051cb0ef41Sopenharmony_ci<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> 17061cb0ef41Sopenharmony_ci</ul> 17071cb0ef41Sopenharmony_ci<p>These deprecated groups might be removed in future versions of Node.js.</p> 17081cb0ef41Sopenharmony_ci</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> 17091cb0ef41Sopenharmony_ci<div class="api_metadata"> 17101cb0ef41Sopenharmony_ci<span>Added in: v0.11.14</span> 17111cb0ef41Sopenharmony_ci</div> 17121cb0ef41Sopenharmony_ci<p>The <code>ECDH</code> class is a utility for creating Elliptic Curve Diffie-Hellman (ECDH) 17131cb0ef41Sopenharmony_cikey exchanges.</p> 17141cb0ef41Sopenharmony_ci<p>Instances of the <code>ECDH</code> class can be created using the 17151cb0ef41Sopenharmony_ci<a href="#cryptocreateecdhcurvename"><code>crypto.createECDH()</code></a> function.</p> 17161cb0ef41Sopenharmony_ci 17171cb0ef41Sopenharmony_ci<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>; 17181cb0ef41Sopenharmony_ci 17191cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 17201cb0ef41Sopenharmony_ci createECDH, 17211cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 17221cb0ef41Sopenharmony_ci 17231cb0ef41Sopenharmony_ci<span class="hljs-comment">// Generate Alice's keys...</span> 17241cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> alice = <span class="hljs-title function_">createECDH</span>(<span class="hljs-string">'secp521r1'</span>); 17251cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> aliceKey = alice.<span class="hljs-title function_">generateKeys</span>(); 17261cb0ef41Sopenharmony_ci 17271cb0ef41Sopenharmony_ci<span class="hljs-comment">// Generate Bob's keys...</span> 17281cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> bob = <span class="hljs-title function_">createECDH</span>(<span class="hljs-string">'secp521r1'</span>); 17291cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> bobKey = bob.<span class="hljs-title function_">generateKeys</span>(); 17301cb0ef41Sopenharmony_ci 17311cb0ef41Sopenharmony_ci<span class="hljs-comment">// Exchange and generate the secret...</span> 17321cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> aliceSecret = alice.<span class="hljs-title function_">computeSecret</span>(bobKey); 17331cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> bobSecret = bob.<span class="hljs-title function_">computeSecret</span>(aliceKey); 17341cb0ef41Sopenharmony_ci 17351cb0ef41Sopenharmony_ciassert.<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>)); 17361cb0ef41Sopenharmony_ci<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>); 17371cb0ef41Sopenharmony_ci 17381cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 17391cb0ef41Sopenharmony_ci createECDH, 17401cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 17411cb0ef41Sopenharmony_ci 17421cb0ef41Sopenharmony_ci<span class="hljs-comment">// Generate Alice's keys...</span> 17431cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> alice = <span class="hljs-title function_">createECDH</span>(<span class="hljs-string">'secp521r1'</span>); 17441cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> aliceKey = alice.<span class="hljs-title function_">generateKeys</span>(); 17451cb0ef41Sopenharmony_ci 17461cb0ef41Sopenharmony_ci<span class="hljs-comment">// Generate Bob's keys...</span> 17471cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> bob = <span class="hljs-title function_">createECDH</span>(<span class="hljs-string">'secp521r1'</span>); 17481cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> bobKey = bob.<span class="hljs-title function_">generateKeys</span>(); 17491cb0ef41Sopenharmony_ci 17501cb0ef41Sopenharmony_ci<span class="hljs-comment">// Exchange and generate the secret...</span> 17511cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> aliceSecret = alice.<span class="hljs-title function_">computeSecret</span>(bobKey); 17521cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> bobSecret = bob.<span class="hljs-title function_">computeSecret</span>(aliceKey); 17531cb0ef41Sopenharmony_ci 17541cb0ef41Sopenharmony_ciassert.<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>)); 17551cb0ef41Sopenharmony_ci<span class="hljs-comment">// OK</span></code><button class="copy-button">copy</button></pre> 17561cb0ef41Sopenharmony_ci<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> 17571cb0ef41Sopenharmony_ci<div class="api_metadata"> 17581cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span> 17591cb0ef41Sopenharmony_ci</div> 17601cb0ef41Sopenharmony_ci<ul> 17611cb0ef41Sopenharmony_ci<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> 17621cb0ef41Sopenharmony_ci<li><code>curve</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 17631cb0ef41Sopenharmony_ci<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> 17641cb0ef41Sopenharmony_ci<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> 17651cb0ef41Sopenharmony_ci<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> 17661cb0ef41Sopenharmony_ci<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> 17671cb0ef41Sopenharmony_ci</ul> 17681cb0ef41Sopenharmony_ci<p>Converts the EC Diffie-Hellman public key specified by <code>key</code> and <code>curve</code> to the 17691cb0ef41Sopenharmony_ciformat specified by <code>format</code>. The <code>format</code> argument specifies point encoding 17701cb0ef41Sopenharmony_ciand can be <code>'compressed'</code>, <code>'uncompressed'</code> or <code>'hybrid'</code>. The supplied key is 17711cb0ef41Sopenharmony_ciinterpreted using the specified <code>inputEncoding</code>, and the returned key is encoded 17721cb0ef41Sopenharmony_ciusing the specified <code>outputEncoding</code>.</p> 17731cb0ef41Sopenharmony_ci<p>Use <a href="#cryptogetcurves"><code>crypto.getCurves()</code></a> to obtain a list of available curve names. 17741cb0ef41Sopenharmony_ciOn recent OpenSSL releases, <code>openssl ecparam -list_curves</code> will also display 17751cb0ef41Sopenharmony_cithe name and description of each available elliptic curve.</p> 17761cb0ef41Sopenharmony_ci<p>If <code>format</code> is not specified the point will be returned in <code>'uncompressed'</code> 17771cb0ef41Sopenharmony_ciformat.</p> 17781cb0ef41Sopenharmony_ci<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>, 17791cb0ef41Sopenharmony_ci<code>TypedArray</code>, or <code>DataView</code>.</p> 17801cb0ef41Sopenharmony_ci<p>Example (uncompressing a key):</p> 17811cb0ef41Sopenharmony_ci 17821cb0ef41Sopenharmony_ci<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> { 17831cb0ef41Sopenharmony_ci createECDH, 17841cb0ef41Sopenharmony_ci <span class="hljs-variable constant_">ECDH</span>, 17851cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 17861cb0ef41Sopenharmony_ci 17871cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> ecdh = <span class="hljs-title function_">createECDH</span>(<span class="hljs-string">'secp256k1'</span>); 17881cb0ef41Sopenharmony_ciecdh.<span class="hljs-title function_">generateKeys</span>(); 17891cb0ef41Sopenharmony_ci 17901cb0ef41Sopenharmony_ci<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>); 17911cb0ef41Sopenharmony_ci 17921cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> uncompressedKey = <span class="hljs-variable constant_">ECDH</span>.<span class="hljs-title function_">convertKey</span>(compressedKey, 17931cb0ef41Sopenharmony_ci <span class="hljs-string">'secp256k1'</span>, 17941cb0ef41Sopenharmony_ci <span class="hljs-string">'hex'</span>, 17951cb0ef41Sopenharmony_ci <span class="hljs-string">'hex'</span>, 17961cb0ef41Sopenharmony_ci <span class="hljs-string">'uncompressed'</span>); 17971cb0ef41Sopenharmony_ci 17981cb0ef41Sopenharmony_ci<span class="hljs-comment">// The converted key and the uncompressed public key should be the same</span> 17991cb0ef41Sopenharmony_ci<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> { 18001cb0ef41Sopenharmony_ci createECDH, 18011cb0ef41Sopenharmony_ci <span class="hljs-variable constant_">ECDH</span>, 18021cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 18031cb0ef41Sopenharmony_ci 18041cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> ecdh = <span class="hljs-title function_">createECDH</span>(<span class="hljs-string">'secp256k1'</span>); 18051cb0ef41Sopenharmony_ciecdh.<span class="hljs-title function_">generateKeys</span>(); 18061cb0ef41Sopenharmony_ci 18071cb0ef41Sopenharmony_ci<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>); 18081cb0ef41Sopenharmony_ci 18091cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> uncompressedKey = <span class="hljs-variable constant_">ECDH</span>.<span class="hljs-title function_">convertKey</span>(compressedKey, 18101cb0ef41Sopenharmony_ci <span class="hljs-string">'secp256k1'</span>, 18111cb0ef41Sopenharmony_ci <span class="hljs-string">'hex'</span>, 18121cb0ef41Sopenharmony_ci <span class="hljs-string">'hex'</span>, 18131cb0ef41Sopenharmony_ci <span class="hljs-string">'uncompressed'</span>); 18141cb0ef41Sopenharmony_ci 18151cb0ef41Sopenharmony_ci<span class="hljs-comment">// The converted key and the uncompressed public key should be the same</span> 18161cb0ef41Sopenharmony_ci<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> 18171cb0ef41Sopenharmony_ci<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> 18181cb0ef41Sopenharmony_ci<div class="api_metadata"> 18191cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 18201cb0ef41Sopenharmony_ci<table> 18211cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 18221cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 18231cb0ef41Sopenharmony_ci<td><p>Changed error format to better support invalid public key error.</p></td></tr> 18241cb0ef41Sopenharmony_ci<tr><td>v6.0.0</td> 18251cb0ef41Sopenharmony_ci<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p></td></tr> 18261cb0ef41Sopenharmony_ci<tr><td>v0.11.14</td> 18271cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.11.14</span></p></td></tr> 18281cb0ef41Sopenharmony_ci</tbody></table> 18291cb0ef41Sopenharmony_ci</details> 18301cb0ef41Sopenharmony_ci</div> 18311cb0ef41Sopenharmony_ci<ul> 18321cb0ef41Sopenharmony_ci<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> 18331cb0ef41Sopenharmony_ci<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> 18341cb0ef41Sopenharmony_ci<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> 18351cb0ef41Sopenharmony_ci<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> 18361cb0ef41Sopenharmony_ci</ul> 18371cb0ef41Sopenharmony_ci<p>Computes the shared secret using <code>otherPublicKey</code> as the other 18381cb0ef41Sopenharmony_ciparty's public key and returns the computed shared secret. The supplied 18391cb0ef41Sopenharmony_cikey is interpreted using specified <code>inputEncoding</code>, and the returned secret 18401cb0ef41Sopenharmony_ciis encoded using the specified <code>outputEncoding</code>. 18411cb0ef41Sopenharmony_ciIf the <code>inputEncoding</code> is not 18421cb0ef41Sopenharmony_ciprovided, <code>otherPublicKey</code> is expected to be a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or 18431cb0ef41Sopenharmony_ci<code>DataView</code>.</p> 18441cb0ef41Sopenharmony_ci<p>If <code>outputEncoding</code> is given a string will be returned; otherwise a 18451cb0ef41Sopenharmony_ci<a href="buffer.html"><code>Buffer</code></a> is returned.</p> 18461cb0ef41Sopenharmony_ci<p><code>ecdh.computeSecret</code> will throw an 18471cb0ef41Sopenharmony_ci<code>ERR_CRYPTO_ECDH_INVALID_PUBLIC_KEY</code> error when <code>otherPublicKey</code> 18481cb0ef41Sopenharmony_cilies outside of the elliptic curve. Since <code>otherPublicKey</code> is 18491cb0ef41Sopenharmony_ciusually supplied from a remote user over an insecure network, 18501cb0ef41Sopenharmony_cibe sure to handle this exception accordingly.</p> 18511cb0ef41Sopenharmony_ci<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> 18521cb0ef41Sopenharmony_ci<div class="api_metadata"> 18531cb0ef41Sopenharmony_ci<span>Added in: v0.11.14</span> 18541cb0ef41Sopenharmony_ci</div> 18551cb0ef41Sopenharmony_ci<ul> 18561cb0ef41Sopenharmony_ci<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> 18571cb0ef41Sopenharmony_ci<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> 18581cb0ef41Sopenharmony_ci<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> 18591cb0ef41Sopenharmony_ci</ul> 18601cb0ef41Sopenharmony_ci<p>Generates private and public EC Diffie-Hellman key values, and returns 18611cb0ef41Sopenharmony_cithe public key in the specified <code>format</code> and <code>encoding</code>. This key should be 18621cb0ef41Sopenharmony_citransferred to the other party.</p> 18631cb0ef41Sopenharmony_ci<p>The <code>format</code> argument specifies point encoding and can be <code>'compressed'</code> or 18641cb0ef41Sopenharmony_ci<code>'uncompressed'</code>. If <code>format</code> is not specified, the point will be returned in 18651cb0ef41Sopenharmony_ci<code>'uncompressed'</code> format.</p> 18661cb0ef41Sopenharmony_ci<p>If <code>encoding</code> is provided a string is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> 18671cb0ef41Sopenharmony_ciis returned.</p> 18681cb0ef41Sopenharmony_ci<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> 18691cb0ef41Sopenharmony_ci<div class="api_metadata"> 18701cb0ef41Sopenharmony_ci<span>Added in: v0.11.14</span> 18711cb0ef41Sopenharmony_ci</div> 18721cb0ef41Sopenharmony_ci<ul> 18731cb0ef41Sopenharmony_ci<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> 18741cb0ef41Sopenharmony_ci<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> 18751cb0ef41Sopenharmony_ci</ul> 18761cb0ef41Sopenharmony_ci<p>If <code>encoding</code> is specified, a string is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is 18771cb0ef41Sopenharmony_cireturned.</p> 18781cb0ef41Sopenharmony_ci<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> 18791cb0ef41Sopenharmony_ci<div class="api_metadata"> 18801cb0ef41Sopenharmony_ci<span>Added in: v0.11.14</span> 18811cb0ef41Sopenharmony_ci</div> 18821cb0ef41Sopenharmony_ci<ul> 18831cb0ef41Sopenharmony_ci<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> 18841cb0ef41Sopenharmony_ci<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> 18851cb0ef41Sopenharmony_ci<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 18861cb0ef41Sopenharmony_ci<code>encoding</code> and <code>format</code>.</li> 18871cb0ef41Sopenharmony_ci</ul> 18881cb0ef41Sopenharmony_ci<p>The <code>format</code> argument specifies point encoding and can be <code>'compressed'</code> or 18891cb0ef41Sopenharmony_ci<code>'uncompressed'</code>. If <code>format</code> is not specified the point will be returned in 18901cb0ef41Sopenharmony_ci<code>'uncompressed'</code> format.</p> 18911cb0ef41Sopenharmony_ci<p>If <code>encoding</code> is specified, a string is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is 18921cb0ef41Sopenharmony_cireturned.</p> 18931cb0ef41Sopenharmony_ci<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> 18941cb0ef41Sopenharmony_ci<div class="api_metadata"> 18951cb0ef41Sopenharmony_ci<span>Added in: v0.11.14</span> 18961cb0ef41Sopenharmony_ci</div> 18971cb0ef41Sopenharmony_ci<ul> 18981cb0ef41Sopenharmony_ci<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> 18991cb0ef41Sopenharmony_ci<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> 19001cb0ef41Sopenharmony_ci</ul> 19011cb0ef41Sopenharmony_ci<p>Sets the EC Diffie-Hellman private key. 19021cb0ef41Sopenharmony_ciIf <code>encoding</code> is provided, <code>privateKey</code> is expected 19031cb0ef41Sopenharmony_cito be a string; otherwise <code>privateKey</code> is expected to be a <a href="buffer.html"><code>Buffer</code></a>, 19041cb0ef41Sopenharmony_ci<code>TypedArray</code>, or <code>DataView</code>.</p> 19051cb0ef41Sopenharmony_ci<p>If <code>privateKey</code> is not valid for the curve specified when the <code>ECDH</code> object was 19061cb0ef41Sopenharmony_cicreated, an error is thrown. Upon setting the private key, the associated 19071cb0ef41Sopenharmony_cipublic point (key) is also generated and set in the <code>ECDH</code> object.</p> 19081cb0ef41Sopenharmony_ci<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> 19091cb0ef41Sopenharmony_ci<div class="api_metadata"> 19101cb0ef41Sopenharmony_ci<span>Added in: v0.11.14</span><span>Deprecated since: v5.2.0</span> 19111cb0ef41Sopenharmony_ci</div> 19121cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated</div><p></p> 19131cb0ef41Sopenharmony_ci<ul> 19141cb0ef41Sopenharmony_ci<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> 19151cb0ef41Sopenharmony_ci<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> 19161cb0ef41Sopenharmony_ci</ul> 19171cb0ef41Sopenharmony_ci<p>Sets the EC Diffie-Hellman public key. 19181cb0ef41Sopenharmony_ciIf <code>encoding</code> is provided <code>publicKey</code> is expected to 19191cb0ef41Sopenharmony_cibe a string; otherwise a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or <code>DataView</code> is expected.</p> 19201cb0ef41Sopenharmony_ci<p>There is not normally a reason to call this method because <code>ECDH</code> 19211cb0ef41Sopenharmony_cionly requires a private key and the other party's public key to compute the 19221cb0ef41Sopenharmony_cishared secret. Typically either <a href="#ecdhgeneratekeysencoding-format"><code>ecdh.generateKeys()</code></a> or 19231cb0ef41Sopenharmony_ci<a href="#ecdhsetprivatekeyprivatekey-encoding"><code>ecdh.setPrivateKey()</code></a> will be called. The <a href="#ecdhsetprivatekeyprivatekey-encoding"><code>ecdh.setPrivateKey()</code></a> method 19241cb0ef41Sopenharmony_ciattempts to generate the public point/key associated with the private key being 19251cb0ef41Sopenharmony_ciset.</p> 19261cb0ef41Sopenharmony_ci<p>Example (obtaining a shared secret):</p> 19271cb0ef41Sopenharmony_ci 19281cb0ef41Sopenharmony_ci<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> { 19291cb0ef41Sopenharmony_ci createECDH, 19301cb0ef41Sopenharmony_ci createHash, 19311cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 19321cb0ef41Sopenharmony_ci 19331cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> alice = <span class="hljs-title function_">createECDH</span>(<span class="hljs-string">'secp256k1'</span>); 19341cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> bob = <span class="hljs-title function_">createECDH</span>(<span class="hljs-string">'secp256k1'</span>); 19351cb0ef41Sopenharmony_ci 19361cb0ef41Sopenharmony_ci<span class="hljs-comment">// This is a shortcut way of specifying one of Alice's previous private</span> 19371cb0ef41Sopenharmony_ci<span class="hljs-comment">// keys. It would be unwise to use such a predictable private key in a real</span> 19381cb0ef41Sopenharmony_ci<span class="hljs-comment">// application.</span> 19391cb0ef41Sopenharmony_cialice.<span class="hljs-title function_">setPrivateKey</span>( 19401cb0ef41Sopenharmony_ci <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>(), 19411cb0ef41Sopenharmony_ci); 19421cb0ef41Sopenharmony_ci 19431cb0ef41Sopenharmony_ci<span class="hljs-comment">// Bob uses a newly generated cryptographically strong</span> 19441cb0ef41Sopenharmony_ci<span class="hljs-comment">// pseudorandom key pair</span> 19451cb0ef41Sopenharmony_cibob.<span class="hljs-title function_">generateKeys</span>(); 19461cb0ef41Sopenharmony_ci 19471cb0ef41Sopenharmony_ci<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>); 19481cb0ef41Sopenharmony_ci<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>); 19491cb0ef41Sopenharmony_ci 19501cb0ef41Sopenharmony_ci<span class="hljs-comment">// aliceSecret and bobSecret should be the same shared secret value</span> 19511cb0ef41Sopenharmony_ci<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> { 19521cb0ef41Sopenharmony_ci createECDH, 19531cb0ef41Sopenharmony_ci createHash, 19541cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 19551cb0ef41Sopenharmony_ci 19561cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> alice = <span class="hljs-title function_">createECDH</span>(<span class="hljs-string">'secp256k1'</span>); 19571cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> bob = <span class="hljs-title function_">createECDH</span>(<span class="hljs-string">'secp256k1'</span>); 19581cb0ef41Sopenharmony_ci 19591cb0ef41Sopenharmony_ci<span class="hljs-comment">// This is a shortcut way of specifying one of Alice's previous private</span> 19601cb0ef41Sopenharmony_ci<span class="hljs-comment">// keys. It would be unwise to use such a predictable private key in a real</span> 19611cb0ef41Sopenharmony_ci<span class="hljs-comment">// application.</span> 19621cb0ef41Sopenharmony_cialice.<span class="hljs-title function_">setPrivateKey</span>( 19631cb0ef41Sopenharmony_ci <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>(), 19641cb0ef41Sopenharmony_ci); 19651cb0ef41Sopenharmony_ci 19661cb0ef41Sopenharmony_ci<span class="hljs-comment">// Bob uses a newly generated cryptographically strong</span> 19671cb0ef41Sopenharmony_ci<span class="hljs-comment">// pseudorandom key pair</span> 19681cb0ef41Sopenharmony_cibob.<span class="hljs-title function_">generateKeys</span>(); 19691cb0ef41Sopenharmony_ci 19701cb0ef41Sopenharmony_ci<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>); 19711cb0ef41Sopenharmony_ci<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>); 19721cb0ef41Sopenharmony_ci 19731cb0ef41Sopenharmony_ci<span class="hljs-comment">// aliceSecret and bobSecret should be the same shared secret value</span> 19741cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(aliceSecret === bobSecret);</code><button class="copy-button">copy</button></pre> 19751cb0ef41Sopenharmony_ci</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> 19761cb0ef41Sopenharmony_ci<div class="api_metadata"> 19771cb0ef41Sopenharmony_ci<span>Added in: v0.1.92</span> 19781cb0ef41Sopenharmony_ci</div> 19791cb0ef41Sopenharmony_ci<ul> 19801cb0ef41Sopenharmony_ci<li>Extends: <a href="stream.html#class-streamtransform" class="type"><stream.Transform></a></li> 19811cb0ef41Sopenharmony_ci</ul> 19821cb0ef41Sopenharmony_ci<p>The <code>Hash</code> class is a utility for creating hash digests of data. It can be 19831cb0ef41Sopenharmony_ciused in one of two ways:</p> 19841cb0ef41Sopenharmony_ci<ul> 19851cb0ef41Sopenharmony_ci<li>As a <a href="stream.html">stream</a> that is both readable and writable, where data is written 19861cb0ef41Sopenharmony_cito produce a computed hash digest on the readable side, or</li> 19871cb0ef41Sopenharmony_ci<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 19881cb0ef41Sopenharmony_cicomputed hash.</li> 19891cb0ef41Sopenharmony_ci</ul> 19901cb0ef41Sopenharmony_ci<p>The <a href="#cryptocreatehashalgorithm-options"><code>crypto.createHash()</code></a> method is used to create <code>Hash</code> instances. <code>Hash</code> 19911cb0ef41Sopenharmony_ciobjects are not to be created directly using the <code>new</code> keyword.</p> 19921cb0ef41Sopenharmony_ci<p>Example: Using <code>Hash</code> objects as streams:</p> 19931cb0ef41Sopenharmony_ci 19941cb0ef41Sopenharmony_ci<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> { 19951cb0ef41Sopenharmony_ci createHash, 19961cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 19971cb0ef41Sopenharmony_ci 19981cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> hash = <span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>); 19991cb0ef41Sopenharmony_ci 20001cb0ef41Sopenharmony_cihash.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> { 20011cb0ef41Sopenharmony_ci <span class="hljs-comment">// Only one element is going to be produced by the</span> 20021cb0ef41Sopenharmony_ci <span class="hljs-comment">// hash stream.</span> 20031cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> data = hash.<span class="hljs-title function_">read</span>(); 20041cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (data) { 20051cb0ef41Sopenharmony_ci <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>)); 20061cb0ef41Sopenharmony_ci <span class="hljs-comment">// Prints:</span> 20071cb0ef41Sopenharmony_ci <span class="hljs-comment">// 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50</span> 20081cb0ef41Sopenharmony_ci } 20091cb0ef41Sopenharmony_ci}); 20101cb0ef41Sopenharmony_ci 20111cb0ef41Sopenharmony_cihash.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to hash'</span>); 20121cb0ef41Sopenharmony_cihash.<span class="hljs-title function_">end</span>();</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 20131cb0ef41Sopenharmony_ci createHash, 20141cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 20151cb0ef41Sopenharmony_ci 20161cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> hash = <span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>); 20171cb0ef41Sopenharmony_ci 20181cb0ef41Sopenharmony_cihash.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> { 20191cb0ef41Sopenharmony_ci <span class="hljs-comment">// Only one element is going to be produced by the</span> 20201cb0ef41Sopenharmony_ci <span class="hljs-comment">// hash stream.</span> 20211cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> data = hash.<span class="hljs-title function_">read</span>(); 20221cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (data) { 20231cb0ef41Sopenharmony_ci <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>)); 20241cb0ef41Sopenharmony_ci <span class="hljs-comment">// Prints:</span> 20251cb0ef41Sopenharmony_ci <span class="hljs-comment">// 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50</span> 20261cb0ef41Sopenharmony_ci } 20271cb0ef41Sopenharmony_ci}); 20281cb0ef41Sopenharmony_ci 20291cb0ef41Sopenharmony_cihash.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to hash'</span>); 20301cb0ef41Sopenharmony_cihash.<span class="hljs-title function_">end</span>();</code><button class="copy-button">copy</button></pre> 20311cb0ef41Sopenharmony_ci<p>Example: Using <code>Hash</code> and piped streams:</p> 20321cb0ef41Sopenharmony_ci 20331cb0ef41Sopenharmony_ci<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>; 20341cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { stdout } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>; 20351cb0ef41Sopenharmony_ci<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>); 20361cb0ef41Sopenharmony_ci 20371cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> hash = <span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>); 20381cb0ef41Sopenharmony_ci 20391cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> input = <span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'test.js'</span>); 20401cb0ef41Sopenharmony_ciinput.<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>); 20411cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { createHash } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 20421cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { stdout } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:process'</span>); 20431cb0ef41Sopenharmony_ci 20441cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> hash = <span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>); 20451cb0ef41Sopenharmony_ci 20461cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> input = <span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'test.js'</span>); 20471cb0ef41Sopenharmony_ciinput.<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> 20481cb0ef41Sopenharmony_ci<p>Example: Using the <a href="#hashupdatedata-inputencoding"><code>hash.update()</code></a> and <a href="#hashdigestencoding"><code>hash.digest()</code></a> methods:</p> 20491cb0ef41Sopenharmony_ci 20501cb0ef41Sopenharmony_ci<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> { 20511cb0ef41Sopenharmony_ci createHash, 20521cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 20531cb0ef41Sopenharmony_ci 20541cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> hash = <span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>); 20551cb0ef41Sopenharmony_ci 20561cb0ef41Sopenharmony_cihash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to hash'</span>); 20571cb0ef41Sopenharmony_ci<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>)); 20581cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 20591cb0ef41Sopenharmony_ci<span class="hljs-comment">// 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 20601cb0ef41Sopenharmony_ci createHash, 20611cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 20621cb0ef41Sopenharmony_ci 20631cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> hash = <span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>); 20641cb0ef41Sopenharmony_ci 20651cb0ef41Sopenharmony_cihash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to hash'</span>); 20661cb0ef41Sopenharmony_ci<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>)); 20671cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 20681cb0ef41Sopenharmony_ci<span class="hljs-comment">// 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50</span></code><button class="copy-button">copy</button></pre> 20691cb0ef41Sopenharmony_ci<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> 20701cb0ef41Sopenharmony_ci<div class="api_metadata"> 20711cb0ef41Sopenharmony_ci<span>Added in: v13.1.0</span> 20721cb0ef41Sopenharmony_ci</div> 20731cb0ef41Sopenharmony_ci<ul> 20741cb0ef41Sopenharmony_ci<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> 20751cb0ef41Sopenharmony_ci<li>Returns: <a href="crypto.html#class-hash" class="type"><Hash></a></li> 20761cb0ef41Sopenharmony_ci</ul> 20771cb0ef41Sopenharmony_ci<p>Creates a new <code>Hash</code> object that contains a deep copy of the internal state 20781cb0ef41Sopenharmony_ciof the current <code>Hash</code> object.</p> 20791cb0ef41Sopenharmony_ci<p>The optional <code>options</code> argument controls stream behavior. For XOF hash 20801cb0ef41Sopenharmony_cifunctions such as <code>'shake256'</code>, the <code>outputLength</code> option can be used to 20811cb0ef41Sopenharmony_cispecify the desired output length in bytes.</p> 20821cb0ef41Sopenharmony_ci<p>An error is thrown when an attempt is made to copy the <code>Hash</code> object after 20831cb0ef41Sopenharmony_ciits <a href="#hashdigestencoding"><code>hash.digest()</code></a> method has been called.</p> 20841cb0ef41Sopenharmony_ci 20851cb0ef41Sopenharmony_ci<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> 20861cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 20871cb0ef41Sopenharmony_ci createHash, 20881cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 20891cb0ef41Sopenharmony_ci 20901cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> hash = <span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>); 20911cb0ef41Sopenharmony_ci 20921cb0ef41Sopenharmony_cihash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'one'</span>); 20931cb0ef41Sopenharmony_ci<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>)); 20941cb0ef41Sopenharmony_ci 20951cb0ef41Sopenharmony_cihash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'two'</span>); 20961cb0ef41Sopenharmony_ci<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>)); 20971cb0ef41Sopenharmony_ci 20981cb0ef41Sopenharmony_cihash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'three'</span>); 20991cb0ef41Sopenharmony_ci<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>)); 21001cb0ef41Sopenharmony_ci 21011cb0ef41Sopenharmony_ci<span class="hljs-comment">// Etc.</span></code><code class="language-js cjs"><span class="hljs-comment">// Calculate a rolling hash.</span> 21021cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 21031cb0ef41Sopenharmony_ci createHash, 21041cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 21051cb0ef41Sopenharmony_ci 21061cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> hash = <span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>); 21071cb0ef41Sopenharmony_ci 21081cb0ef41Sopenharmony_cihash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'one'</span>); 21091cb0ef41Sopenharmony_ci<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>)); 21101cb0ef41Sopenharmony_ci 21111cb0ef41Sopenharmony_cihash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'two'</span>); 21121cb0ef41Sopenharmony_ci<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>)); 21131cb0ef41Sopenharmony_ci 21141cb0ef41Sopenharmony_cihash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'three'</span>); 21151cb0ef41Sopenharmony_ci<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>)); 21161cb0ef41Sopenharmony_ci 21171cb0ef41Sopenharmony_ci<span class="hljs-comment">// Etc.</span></code><button class="copy-button">copy</button></pre> 21181cb0ef41Sopenharmony_ci<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> 21191cb0ef41Sopenharmony_ci<div class="api_metadata"> 21201cb0ef41Sopenharmony_ci<span>Added in: v0.1.92</span> 21211cb0ef41Sopenharmony_ci</div> 21221cb0ef41Sopenharmony_ci<ul> 21231cb0ef41Sopenharmony_ci<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> 21241cb0ef41Sopenharmony_ci<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> 21251cb0ef41Sopenharmony_ci</ul> 21261cb0ef41Sopenharmony_ci<p>Calculates the digest of all of the data passed to be hashed (using the 21271cb0ef41Sopenharmony_ci<a href="#hashupdatedata-inputencoding"><code>hash.update()</code></a> method). 21281cb0ef41Sopenharmony_ciIf <code>encoding</code> is provided a string will be returned; otherwise 21291cb0ef41Sopenharmony_cia <a href="buffer.html"><code>Buffer</code></a> is returned.</p> 21301cb0ef41Sopenharmony_ci<p>The <code>Hash</code> object can not be used again after <code>hash.digest()</code> method has been 21311cb0ef41Sopenharmony_cicalled. Multiple calls will cause an error to be thrown.</p> 21321cb0ef41Sopenharmony_ci<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> 21331cb0ef41Sopenharmony_ci<div class="api_metadata"> 21341cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 21351cb0ef41Sopenharmony_ci<table> 21361cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 21371cb0ef41Sopenharmony_ci<tr><td>v6.0.0</td> 21381cb0ef41Sopenharmony_ci<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p></td></tr> 21391cb0ef41Sopenharmony_ci<tr><td>v0.1.92</td> 21401cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.92</span></p></td></tr> 21411cb0ef41Sopenharmony_ci</tbody></table> 21421cb0ef41Sopenharmony_ci</details> 21431cb0ef41Sopenharmony_ci</div> 21441cb0ef41Sopenharmony_ci<ul> 21451cb0ef41Sopenharmony_ci<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> 21461cb0ef41Sopenharmony_ci<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> 21471cb0ef41Sopenharmony_ci</ul> 21481cb0ef41Sopenharmony_ci<p>Updates the hash content with the given <code>data</code>, the encoding of which 21491cb0ef41Sopenharmony_ciis given in <code>inputEncoding</code>. 21501cb0ef41Sopenharmony_ciIf <code>encoding</code> is not provided, and the <code>data</code> is a string, an 21511cb0ef41Sopenharmony_ciencoding of <code>'utf8'</code> is enforced. If <code>data</code> is a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or 21521cb0ef41Sopenharmony_ci<code>DataView</code>, then <code>inputEncoding</code> is ignored.</p> 21531cb0ef41Sopenharmony_ci<p>This can be called many times with new data as it is streamed.</p> 21541cb0ef41Sopenharmony_ci</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> 21551cb0ef41Sopenharmony_ci<div class="api_metadata"> 21561cb0ef41Sopenharmony_ci<span>Added in: v0.1.94</span> 21571cb0ef41Sopenharmony_ci</div> 21581cb0ef41Sopenharmony_ci<ul> 21591cb0ef41Sopenharmony_ci<li>Extends: <a href="stream.html#class-streamtransform" class="type"><stream.Transform></a></li> 21601cb0ef41Sopenharmony_ci</ul> 21611cb0ef41Sopenharmony_ci<p>The <code>Hmac</code> class is a utility for creating cryptographic HMAC digests. It can 21621cb0ef41Sopenharmony_cibe used in one of two ways:</p> 21631cb0ef41Sopenharmony_ci<ul> 21641cb0ef41Sopenharmony_ci<li>As a <a href="stream.html">stream</a> that is both readable and writable, where data is written 21651cb0ef41Sopenharmony_cito produce a computed HMAC digest on the readable side, or</li> 21661cb0ef41Sopenharmony_ci<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 21671cb0ef41Sopenharmony_cicomputed HMAC digest.</li> 21681cb0ef41Sopenharmony_ci</ul> 21691cb0ef41Sopenharmony_ci<p>The <a href="#cryptocreatehmacalgorithm-key-options"><code>crypto.createHmac()</code></a> method is used to create <code>Hmac</code> instances. <code>Hmac</code> 21701cb0ef41Sopenharmony_ciobjects are not to be created directly using the <code>new</code> keyword.</p> 21711cb0ef41Sopenharmony_ci<p>Example: Using <code>Hmac</code> objects as streams:</p> 21721cb0ef41Sopenharmony_ci 21731cb0ef41Sopenharmony_ci<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> { 21741cb0ef41Sopenharmony_ci createHmac, 21751cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 21761cb0ef41Sopenharmony_ci 21771cb0ef41Sopenharmony_ci<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>); 21781cb0ef41Sopenharmony_ci 21791cb0ef41Sopenharmony_cihmac.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> { 21801cb0ef41Sopenharmony_ci <span class="hljs-comment">// Only one element is going to be produced by the</span> 21811cb0ef41Sopenharmony_ci <span class="hljs-comment">// hash stream.</span> 21821cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> data = hmac.<span class="hljs-title function_">read</span>(); 21831cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (data) { 21841cb0ef41Sopenharmony_ci <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>)); 21851cb0ef41Sopenharmony_ci <span class="hljs-comment">// Prints:</span> 21861cb0ef41Sopenharmony_ci <span class="hljs-comment">// 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e</span> 21871cb0ef41Sopenharmony_ci } 21881cb0ef41Sopenharmony_ci}); 21891cb0ef41Sopenharmony_ci 21901cb0ef41Sopenharmony_cihmac.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to hash'</span>); 21911cb0ef41Sopenharmony_cihmac.<span class="hljs-title function_">end</span>();</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 21921cb0ef41Sopenharmony_ci createHmac, 21931cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 21941cb0ef41Sopenharmony_ci 21951cb0ef41Sopenharmony_ci<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>); 21961cb0ef41Sopenharmony_ci 21971cb0ef41Sopenharmony_cihmac.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> { 21981cb0ef41Sopenharmony_ci <span class="hljs-comment">// Only one element is going to be produced by the</span> 21991cb0ef41Sopenharmony_ci <span class="hljs-comment">// hash stream.</span> 22001cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> data = hmac.<span class="hljs-title function_">read</span>(); 22011cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (data) { 22021cb0ef41Sopenharmony_ci <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>)); 22031cb0ef41Sopenharmony_ci <span class="hljs-comment">// Prints:</span> 22041cb0ef41Sopenharmony_ci <span class="hljs-comment">// 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e</span> 22051cb0ef41Sopenharmony_ci } 22061cb0ef41Sopenharmony_ci}); 22071cb0ef41Sopenharmony_ci 22081cb0ef41Sopenharmony_cihmac.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to hash'</span>); 22091cb0ef41Sopenharmony_cihmac.<span class="hljs-title function_">end</span>();</code><button class="copy-button">copy</button></pre> 22101cb0ef41Sopenharmony_ci<p>Example: Using <code>Hmac</code> and piped streams:</p> 22111cb0ef41Sopenharmony_ci 22121cb0ef41Sopenharmony_ci<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>; 22131cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { stdout } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>; 22141cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 22151cb0ef41Sopenharmony_ci createHmac, 22161cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 22171cb0ef41Sopenharmony_ci 22181cb0ef41Sopenharmony_ci<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>); 22191cb0ef41Sopenharmony_ci 22201cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> input = <span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'test.js'</span>); 22211cb0ef41Sopenharmony_ciinput.<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> { 22221cb0ef41Sopenharmony_ci createReadStream, 22231cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 22241cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 22251cb0ef41Sopenharmony_ci createHmac, 22261cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 22271cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { stdout } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:process'</span>); 22281cb0ef41Sopenharmony_ci 22291cb0ef41Sopenharmony_ci<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>); 22301cb0ef41Sopenharmony_ci 22311cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> input = <span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'test.js'</span>); 22321cb0ef41Sopenharmony_ciinput.<span class="hljs-title function_">pipe</span>(hmac).<span class="hljs-title function_">pipe</span>(stdout);</code><button class="copy-button">copy</button></pre> 22331cb0ef41Sopenharmony_ci<p>Example: Using the <a href="#hmacupdatedata-inputencoding"><code>hmac.update()</code></a> and <a href="#hmacdigestencoding"><code>hmac.digest()</code></a> methods:</p> 22341cb0ef41Sopenharmony_ci 22351cb0ef41Sopenharmony_ci<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> { 22361cb0ef41Sopenharmony_ci createHmac, 22371cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 22381cb0ef41Sopenharmony_ci 22391cb0ef41Sopenharmony_ci<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>); 22401cb0ef41Sopenharmony_ci 22411cb0ef41Sopenharmony_cihmac.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to hash'</span>); 22421cb0ef41Sopenharmony_ci<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>)); 22431cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 22441cb0ef41Sopenharmony_ci<span class="hljs-comment">// 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 22451cb0ef41Sopenharmony_ci createHmac, 22461cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 22471cb0ef41Sopenharmony_ci 22481cb0ef41Sopenharmony_ci<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>); 22491cb0ef41Sopenharmony_ci 22501cb0ef41Sopenharmony_cihmac.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to hash'</span>); 22511cb0ef41Sopenharmony_ci<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>)); 22521cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 22531cb0ef41Sopenharmony_ci<span class="hljs-comment">// 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e</span></code><button class="copy-button">copy</button></pre> 22541cb0ef41Sopenharmony_ci<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> 22551cb0ef41Sopenharmony_ci<div class="api_metadata"> 22561cb0ef41Sopenharmony_ci<span>Added in: v0.1.94</span> 22571cb0ef41Sopenharmony_ci</div> 22581cb0ef41Sopenharmony_ci<ul> 22591cb0ef41Sopenharmony_ci<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> 22601cb0ef41Sopenharmony_ci<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> 22611cb0ef41Sopenharmony_ci</ul> 22621cb0ef41Sopenharmony_ci<p>Calculates the HMAC digest of all of the data passed using <a href="#hmacupdatedata-inputencoding"><code>hmac.update()</code></a>. 22631cb0ef41Sopenharmony_ciIf <code>encoding</code> is 22641cb0ef41Sopenharmony_ciprovided a string is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> is returned;</p> 22651cb0ef41Sopenharmony_ci<p>The <code>Hmac</code> object can not be used again after <code>hmac.digest()</code> has been 22661cb0ef41Sopenharmony_cicalled. Multiple calls to <code>hmac.digest()</code> will result in an error being thrown.</p> 22671cb0ef41Sopenharmony_ci<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> 22681cb0ef41Sopenharmony_ci<div class="api_metadata"> 22691cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 22701cb0ef41Sopenharmony_ci<table> 22711cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 22721cb0ef41Sopenharmony_ci<tr><td>v6.0.0</td> 22731cb0ef41Sopenharmony_ci<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p></td></tr> 22741cb0ef41Sopenharmony_ci<tr><td>v0.1.94</td> 22751cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.94</span></p></td></tr> 22761cb0ef41Sopenharmony_ci</tbody></table> 22771cb0ef41Sopenharmony_ci</details> 22781cb0ef41Sopenharmony_ci</div> 22791cb0ef41Sopenharmony_ci<ul> 22801cb0ef41Sopenharmony_ci<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> 22811cb0ef41Sopenharmony_ci<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> 22821cb0ef41Sopenharmony_ci</ul> 22831cb0ef41Sopenharmony_ci<p>Updates the <code>Hmac</code> content with the given <code>data</code>, the encoding of which 22841cb0ef41Sopenharmony_ciis given in <code>inputEncoding</code>. 22851cb0ef41Sopenharmony_ciIf <code>encoding</code> is not provided, and the <code>data</code> is a string, an 22861cb0ef41Sopenharmony_ciencoding of <code>'utf8'</code> is enforced. If <code>data</code> is a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or 22871cb0ef41Sopenharmony_ci<code>DataView</code>, then <code>inputEncoding</code> is ignored.</p> 22881cb0ef41Sopenharmony_ci<p>This can be called many times with new data as it is streamed.</p> 22891cb0ef41Sopenharmony_ci</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> 22901cb0ef41Sopenharmony_ci<div class="api_metadata"> 22911cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 22921cb0ef41Sopenharmony_ci<table> 22931cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 22941cb0ef41Sopenharmony_ci<tr><td>v14.5.0, v12.19.0</td> 22951cb0ef41Sopenharmony_ci<td><p>Instances of this class can now be passed to worker threads using <code>postMessage</code>.</p></td></tr> 22961cb0ef41Sopenharmony_ci<tr><td>v11.13.0</td> 22971cb0ef41Sopenharmony_ci<td><p>This class is now exported.</p></td></tr> 22981cb0ef41Sopenharmony_ci<tr><td>v11.6.0</td> 22991cb0ef41Sopenharmony_ci<td><p><span>Added in: v11.6.0</span></p></td></tr> 23001cb0ef41Sopenharmony_ci</tbody></table> 23011cb0ef41Sopenharmony_ci</details> 23021cb0ef41Sopenharmony_ci</div> 23031cb0ef41Sopenharmony_ci<p>Node.js uses a <code>KeyObject</code> class to represent a symmetric or asymmetric key, 23041cb0ef41Sopenharmony_ciand each kind of key exposes different functions. The 23051cb0ef41Sopenharmony_ci<a href="#cryptocreatesecretkeykey-encoding"><code>crypto.createSecretKey()</code></a>, <a href="#cryptocreatepublickeykey"><code>crypto.createPublicKey()</code></a> and 23061cb0ef41Sopenharmony_ci<a href="#cryptocreateprivatekeykey"><code>crypto.createPrivateKey()</code></a> methods are used to create <code>KeyObject</code> 23071cb0ef41Sopenharmony_ciinstances. <code>KeyObject</code> objects are not to be created directly using the <code>new</code> 23081cb0ef41Sopenharmony_cikeyword.</p> 23091cb0ef41Sopenharmony_ci<p>Most applications should consider using the new <code>KeyObject</code> API instead of 23101cb0ef41Sopenharmony_cipassing keys as strings or <code>Buffer</code>s due to improved security features.</p> 23111cb0ef41Sopenharmony_ci<p><code>KeyObject</code> instances can be passed to other threads via <a href="worker_threads.html#portpostmessagevalue-transferlist"><code>postMessage()</code></a>. 23121cb0ef41Sopenharmony_ciThe receiver obtains a cloned <code>KeyObject</code>, and the <code>KeyObject</code> does not need to 23131cb0ef41Sopenharmony_cibe listed in the <code>transferList</code> argument.</p> 23141cb0ef41Sopenharmony_ci<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> 23151cb0ef41Sopenharmony_ci<div class="api_metadata"> 23161cb0ef41Sopenharmony_ci<span>Added in: v15.0.0</span> 23171cb0ef41Sopenharmony_ci</div> 23181cb0ef41Sopenharmony_ci<ul> 23191cb0ef41Sopenharmony_ci<li><code>key</code> <a href="webcrypto.html#class-cryptokey" class="type"><CryptoKey></a></li> 23201cb0ef41Sopenharmony_ci<li>Returns: <a href="crypto.html#class-keyobject" class="type"><KeyObject></a></li> 23211cb0ef41Sopenharmony_ci</ul> 23221cb0ef41Sopenharmony_ci<p>Example: Converting a <code>CryptoKey</code> instance to a <code>KeyObject</code>:</p> 23231cb0ef41Sopenharmony_ci 23241cb0ef41Sopenharmony_ci<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>); 23251cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { subtle } = webcrypto; 23261cb0ef41Sopenharmony_ci 23271cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> key = <span class="hljs-keyword">await</span> subtle.<span class="hljs-title function_">generateKey</span>({ 23281cb0ef41Sopenharmony_ci <span class="hljs-attr">name</span>: <span class="hljs-string">'HMAC'</span>, 23291cb0ef41Sopenharmony_ci <span class="hljs-attr">hash</span>: <span class="hljs-string">'SHA-256'</span>, 23301cb0ef41Sopenharmony_ci <span class="hljs-attr">length</span>: <span class="hljs-number">256</span>, 23311cb0ef41Sopenharmony_ci}, <span class="hljs-literal">true</span>, [<span class="hljs-string">'sign'</span>, <span class="hljs-string">'verify'</span>]); 23321cb0ef41Sopenharmony_ci 23331cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> keyObject = <span class="hljs-title class_">KeyObject</span>.<span class="hljs-title function_">from</span>(key); 23341cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(keyObject.<span class="hljs-property">symmetricKeySize</span>); 23351cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 32 (symmetric key size in bytes)</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 23361cb0ef41Sopenharmony_ci <span class="hljs-attr">webcrypto</span>: { 23371cb0ef41Sopenharmony_ci subtle, 23381cb0ef41Sopenharmony_ci }, 23391cb0ef41Sopenharmony_ci <span class="hljs-title class_">KeyObject</span>, 23401cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 23411cb0ef41Sopenharmony_ci 23421cb0ef41Sopenharmony_ci(<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { 23431cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> key = <span class="hljs-keyword">await</span> subtle.<span class="hljs-title function_">generateKey</span>({ 23441cb0ef41Sopenharmony_ci <span class="hljs-attr">name</span>: <span class="hljs-string">'HMAC'</span>, 23451cb0ef41Sopenharmony_ci <span class="hljs-attr">hash</span>: <span class="hljs-string">'SHA-256'</span>, 23461cb0ef41Sopenharmony_ci <span class="hljs-attr">length</span>: <span class="hljs-number">256</span>, 23471cb0ef41Sopenharmony_ci }, <span class="hljs-literal">true</span>, [<span class="hljs-string">'sign'</span>, <span class="hljs-string">'verify'</span>]); 23481cb0ef41Sopenharmony_ci 23491cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> keyObject = <span class="hljs-title class_">KeyObject</span>.<span class="hljs-title function_">from</span>(key); 23501cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(keyObject.<span class="hljs-property">symmetricKeySize</span>); 23511cb0ef41Sopenharmony_ci <span class="hljs-comment">// Prints: 32 (symmetric key size in bytes)</span> 23521cb0ef41Sopenharmony_ci})();</code><button class="copy-button">copy</button></pre> 23531cb0ef41Sopenharmony_ci<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> 23541cb0ef41Sopenharmony_ci<div class="api_metadata"> 23551cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 23561cb0ef41Sopenharmony_ci<table> 23571cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 23581cb0ef41Sopenharmony_ci<tr><td>v16.9.0</td> 23591cb0ef41Sopenharmony_ci<td><p>Expose <code>RSASSA-PSS-params</code> sequence parameters for RSA-PSS keys.</p></td></tr> 23601cb0ef41Sopenharmony_ci<tr><td>v15.7.0</td> 23611cb0ef41Sopenharmony_ci<td><p><span>Added in: v15.7.0</span></p></td></tr> 23621cb0ef41Sopenharmony_ci</tbody></table> 23631cb0ef41Sopenharmony_ci</details> 23641cb0ef41Sopenharmony_ci</div> 23651cb0ef41Sopenharmony_ci<ul> 23661cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 23671cb0ef41Sopenharmony_ci<ul> 23681cb0ef41Sopenharmony_ci<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> 23691cb0ef41Sopenharmony_ci<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> 23701cb0ef41Sopenharmony_ci<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> 23711cb0ef41Sopenharmony_ci<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 23721cb0ef41Sopenharmony_ciMGF1 (RSA-PSS).</li> 23731cb0ef41Sopenharmony_ci<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> 23741cb0ef41Sopenharmony_ci<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> 23751cb0ef41Sopenharmony_ci<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> 23761cb0ef41Sopenharmony_ci</ul> 23771cb0ef41Sopenharmony_ci</li> 23781cb0ef41Sopenharmony_ci</ul> 23791cb0ef41Sopenharmony_ci<p>This property exists only on asymmetric keys. Depending on the type of the key, 23801cb0ef41Sopenharmony_cithis object contains information about the key. None of the information obtained 23811cb0ef41Sopenharmony_cithrough this property can be used to uniquely identify a key or to compromise 23821cb0ef41Sopenharmony_cithe security of the key.</p> 23831cb0ef41Sopenharmony_ci<p>For RSA-PSS keys, if the key material contains a <code>RSASSA-PSS-params</code> sequence, 23841cb0ef41Sopenharmony_cithe <code>hashAlgorithm</code>, <code>mgf1HashAlgorithm</code>, and <code>saltLength</code> properties will be 23851cb0ef41Sopenharmony_ciset.</p> 23861cb0ef41Sopenharmony_ci<p>Other key details might be exposed via this API using additional attributes.</p> 23871cb0ef41Sopenharmony_ci<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> 23881cb0ef41Sopenharmony_ci<div class="api_metadata"> 23891cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 23901cb0ef41Sopenharmony_ci<table> 23911cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 23921cb0ef41Sopenharmony_ci<tr><td>v13.9.0, v12.17.0</td> 23931cb0ef41Sopenharmony_ci<td><p>Added support for <code>'dh'</code>.</p></td></tr> 23941cb0ef41Sopenharmony_ci<tr><td>v12.0.0</td> 23951cb0ef41Sopenharmony_ci<td><p>Added support for <code>'rsa-pss'</code>.</p></td></tr> 23961cb0ef41Sopenharmony_ci<tr><td>v12.0.0</td> 23971cb0ef41Sopenharmony_ci<td><p>This property now returns <code>undefined</code> for KeyObject instances of unrecognized type instead of aborting.</p></td></tr> 23981cb0ef41Sopenharmony_ci<tr><td>v12.0.0</td> 23991cb0ef41Sopenharmony_ci<td><p>Added support for <code>'x25519'</code> and <code>'x448'</code>.</p></td></tr> 24001cb0ef41Sopenharmony_ci<tr><td>v12.0.0</td> 24011cb0ef41Sopenharmony_ci<td><p>Added support for <code>'ed25519'</code> and <code>'ed448'</code>.</p></td></tr> 24021cb0ef41Sopenharmony_ci<tr><td>v11.6.0</td> 24031cb0ef41Sopenharmony_ci<td><p><span>Added in: v11.6.0</span></p></td></tr> 24041cb0ef41Sopenharmony_ci</tbody></table> 24051cb0ef41Sopenharmony_ci</details> 24061cb0ef41Sopenharmony_ci</div> 24071cb0ef41Sopenharmony_ci<ul> 24081cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 24091cb0ef41Sopenharmony_ci</ul> 24101cb0ef41Sopenharmony_ci<p>For asymmetric keys, this property represents the type of the key. Supported key 24111cb0ef41Sopenharmony_citypes are:</p> 24121cb0ef41Sopenharmony_ci<ul> 24131cb0ef41Sopenharmony_ci<li><code>'rsa'</code> (OID 1.2.840.113549.1.1.1)</li> 24141cb0ef41Sopenharmony_ci<li><code>'rsa-pss'</code> (OID 1.2.840.113549.1.1.10)</li> 24151cb0ef41Sopenharmony_ci<li><code>'dsa'</code> (OID 1.2.840.10040.4.1)</li> 24161cb0ef41Sopenharmony_ci<li><code>'ec'</code> (OID 1.2.840.10045.2.1)</li> 24171cb0ef41Sopenharmony_ci<li><code>'x25519'</code> (OID 1.3.101.110)</li> 24181cb0ef41Sopenharmony_ci<li><code>'x448'</code> (OID 1.3.101.111)</li> 24191cb0ef41Sopenharmony_ci<li><code>'ed25519'</code> (OID 1.3.101.112)</li> 24201cb0ef41Sopenharmony_ci<li><code>'ed448'</code> (OID 1.3.101.113)</li> 24211cb0ef41Sopenharmony_ci<li><code>'dh'</code> (OID 1.2.840.113549.1.3.1)</li> 24221cb0ef41Sopenharmony_ci</ul> 24231cb0ef41Sopenharmony_ci<p>This property is <code>undefined</code> for unrecognized <code>KeyObject</code> types and symmetric 24241cb0ef41Sopenharmony_cikeys.</p> 24251cb0ef41Sopenharmony_ci<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> 24261cb0ef41Sopenharmony_ci<div class="api_metadata"> 24271cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 24281cb0ef41Sopenharmony_ci<table> 24291cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 24301cb0ef41Sopenharmony_ci<tr><td>v15.9.0</td> 24311cb0ef41Sopenharmony_ci<td><p>Added support for <code>'jwk'</code> format.</p></td></tr> 24321cb0ef41Sopenharmony_ci<tr><td>v11.6.0</td> 24331cb0ef41Sopenharmony_ci<td><p><span>Added in: v11.6.0</span></p></td></tr> 24341cb0ef41Sopenharmony_ci</tbody></table> 24351cb0ef41Sopenharmony_ci</details> 24361cb0ef41Sopenharmony_ci</div> 24371cb0ef41Sopenharmony_ci<ul> 24381cb0ef41Sopenharmony_ci<li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 24391cb0ef41Sopenharmony_ci<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> 24401cb0ef41Sopenharmony_ci</ul> 24411cb0ef41Sopenharmony_ci<p>For symmetric keys, the following encoding options can be used:</p> 24421cb0ef41Sopenharmony_ci<ul> 24431cb0ef41Sopenharmony_ci<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> 24441cb0ef41Sopenharmony_ci</ul> 24451cb0ef41Sopenharmony_ci<p>For public keys, the following encoding options can be used:</p> 24461cb0ef41Sopenharmony_ci<ul> 24471cb0ef41Sopenharmony_ci<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> 24481cb0ef41Sopenharmony_ci<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> 24491cb0ef41Sopenharmony_ci</ul> 24501cb0ef41Sopenharmony_ci<p>For private keys, the following encoding options can be used:</p> 24511cb0ef41Sopenharmony_ci<ul> 24521cb0ef41Sopenharmony_ci<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 24531cb0ef41Sopenharmony_ci<code>'sec1'</code> (EC only).</li> 24541cb0ef41Sopenharmony_ci<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> 24551cb0ef41Sopenharmony_ci<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 24561cb0ef41Sopenharmony_cithe given <code>cipher</code> and <code>passphrase</code> using PKCS#5 v2.0 password based 24571cb0ef41Sopenharmony_ciencryption.</li> 24581cb0ef41Sopenharmony_ci<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 24591cb0ef41Sopenharmony_ci<code>cipher</code>.</li> 24601cb0ef41Sopenharmony_ci</ul> 24611cb0ef41Sopenharmony_ci<p>The result type depends on the selected encoding format, when PEM the 24621cb0ef41Sopenharmony_ciresult is a string, when DER it will be a buffer containing the data 24631cb0ef41Sopenharmony_ciencoded as DER, when <a href="https://tools.ietf.org/html/rfc7517">JWK</a> it will be an object.</p> 24641cb0ef41Sopenharmony_ci<p>When <a href="https://tools.ietf.org/html/rfc7517">JWK</a> encoding format was selected, all other encoding options are 24651cb0ef41Sopenharmony_ciignored.</p> 24661cb0ef41Sopenharmony_ci<p>PKCS#1, SEC1, and PKCS#8 type keys can be encrypted by using a combination of 24671cb0ef41Sopenharmony_cithe <code>cipher</code> and <code>format</code> options. The PKCS#8 <code>type</code> can be used with any 24681cb0ef41Sopenharmony_ci<code>format</code> to encrypt any key algorithm (RSA, EC, or DH) by specifying a 24691cb0ef41Sopenharmony_ci<code>cipher</code>. PKCS#1 and SEC1 can only be encrypted by specifying a <code>cipher</code> 24701cb0ef41Sopenharmony_ciwhen the PEM <code>format</code> is used. For maximum compatibility, use PKCS#8 for 24711cb0ef41Sopenharmony_ciencrypted private keys. Since PKCS#8 defines its own 24721cb0ef41Sopenharmony_ciencryption mechanism, PEM-level encryption is not supported when encrypting 24731cb0ef41Sopenharmony_cia 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 24741cb0ef41Sopenharmony_ciPKCS#1 and SEC1 encryption.</p> 24751cb0ef41Sopenharmony_ci<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> 24761cb0ef41Sopenharmony_ci<div class="api_metadata"> 24771cb0ef41Sopenharmony_ci<span>Added in: v17.7.0</span> 24781cb0ef41Sopenharmony_ci</div> 24791cb0ef41Sopenharmony_ci<ul> 24801cb0ef41Sopenharmony_ci<li><code>otherKeyObject</code>: <a href="crypto.html#class-keyobject" class="type"><KeyObject></a> A <code>KeyObject</code> with which to 24811cb0ef41Sopenharmony_cicompare <code>keyObject</code>.</li> 24821cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 24831cb0ef41Sopenharmony_ci</ul> 24841cb0ef41Sopenharmony_ci<p>Returns <code>true</code> or <code>false</code> depending on whether the keys have exactly the same 24851cb0ef41Sopenharmony_citype, value, and parameters. This method is not 24861cb0ef41Sopenharmony_ci<a href="https://en.wikipedia.org/wiki/Timing_attack">constant time</a>.</p> 24871cb0ef41Sopenharmony_ci<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> 24881cb0ef41Sopenharmony_ci<div class="api_metadata"> 24891cb0ef41Sopenharmony_ci<span>Added in: v11.6.0</span> 24901cb0ef41Sopenharmony_ci</div> 24911cb0ef41Sopenharmony_ci<ul> 24921cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 24931cb0ef41Sopenharmony_ci</ul> 24941cb0ef41Sopenharmony_ci<p>For secret keys, this property represents the size of the key in bytes. This 24951cb0ef41Sopenharmony_ciproperty is <code>undefined</code> for asymmetric keys.</p> 24961cb0ef41Sopenharmony_ci<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> 24971cb0ef41Sopenharmony_ci<div class="api_metadata"> 24981cb0ef41Sopenharmony_ci<span>Added in: v11.6.0</span> 24991cb0ef41Sopenharmony_ci</div> 25001cb0ef41Sopenharmony_ci<ul> 25011cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 25021cb0ef41Sopenharmony_ci</ul> 25031cb0ef41Sopenharmony_ci<p>Depending on the type of this <code>KeyObject</code>, this property is either 25041cb0ef41Sopenharmony_ci<code>'secret'</code> for secret (symmetric) keys, <code>'public'</code> for public (asymmetric) keys 25051cb0ef41Sopenharmony_cior <code>'private'</code> for private (asymmetric) keys.</p> 25061cb0ef41Sopenharmony_ci</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> 25071cb0ef41Sopenharmony_ci<div class="api_metadata"> 25081cb0ef41Sopenharmony_ci<span>Added in: v0.1.92</span> 25091cb0ef41Sopenharmony_ci</div> 25101cb0ef41Sopenharmony_ci<ul> 25111cb0ef41Sopenharmony_ci<li>Extends: <a href="stream.html#class-streamwritable" class="type"><stream.Writable></a></li> 25121cb0ef41Sopenharmony_ci</ul> 25131cb0ef41Sopenharmony_ci<p>The <code>Sign</code> class is a utility for generating signatures. It can be used in one 25141cb0ef41Sopenharmony_ciof two ways:</p> 25151cb0ef41Sopenharmony_ci<ul> 25161cb0ef41Sopenharmony_ci<li>As a writable <a href="stream.html">stream</a>, where data to be signed is written and the 25171cb0ef41Sopenharmony_ci<a href="#signsignprivatekey-outputencoding"><code>sign.sign()</code></a> method is used to generate and return the signature, or</li> 25181cb0ef41Sopenharmony_ci<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 25191cb0ef41Sopenharmony_cisignature.</li> 25201cb0ef41Sopenharmony_ci</ul> 25211cb0ef41Sopenharmony_ci<p>The <a href="#cryptocreatesignalgorithm-options"><code>crypto.createSign()</code></a> method is used to create <code>Sign</code> instances. The 25221cb0ef41Sopenharmony_ciargument is the string name of the hash function to use. <code>Sign</code> objects are not 25231cb0ef41Sopenharmony_cito be created directly using the <code>new</code> keyword.</p> 25241cb0ef41Sopenharmony_ci<p>Example: Using <code>Sign</code> and <a href="#class-verify"><code>Verify</code></a> objects as streams:</p> 25251cb0ef41Sopenharmony_ci 25261cb0ef41Sopenharmony_ci<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> { 25271cb0ef41Sopenharmony_ci generateKeyPairSync, 25281cb0ef41Sopenharmony_ci createSign, 25291cb0ef41Sopenharmony_ci createVerify, 25301cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 25311cb0ef41Sopenharmony_ci 25321cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { privateKey, publicKey } = <span class="hljs-title function_">generateKeyPairSync</span>(<span class="hljs-string">'ec'</span>, { 25331cb0ef41Sopenharmony_ci <span class="hljs-attr">namedCurve</span>: <span class="hljs-string">'sect239k1'</span>, 25341cb0ef41Sopenharmony_ci}); 25351cb0ef41Sopenharmony_ci 25361cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> sign = <span class="hljs-title function_">createSign</span>(<span class="hljs-string">'SHA256'</span>); 25371cb0ef41Sopenharmony_cisign.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to sign'</span>); 25381cb0ef41Sopenharmony_cisign.<span class="hljs-title function_">end</span>(); 25391cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> signature = sign.<span class="hljs-title function_">sign</span>(privateKey, <span class="hljs-string">'hex'</span>); 25401cb0ef41Sopenharmony_ci 25411cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> verify = <span class="hljs-title function_">createVerify</span>(<span class="hljs-string">'SHA256'</span>); 25421cb0ef41Sopenharmony_civerify.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to sign'</span>); 25431cb0ef41Sopenharmony_civerify.<span class="hljs-title function_">end</span>(); 25441cb0ef41Sopenharmony_ci<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>)); 25451cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: true</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 25461cb0ef41Sopenharmony_ci generateKeyPairSync, 25471cb0ef41Sopenharmony_ci createSign, 25481cb0ef41Sopenharmony_ci createVerify, 25491cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 25501cb0ef41Sopenharmony_ci 25511cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { privateKey, publicKey } = <span class="hljs-title function_">generateKeyPairSync</span>(<span class="hljs-string">'ec'</span>, { 25521cb0ef41Sopenharmony_ci <span class="hljs-attr">namedCurve</span>: <span class="hljs-string">'sect239k1'</span>, 25531cb0ef41Sopenharmony_ci}); 25541cb0ef41Sopenharmony_ci 25551cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> sign = <span class="hljs-title function_">createSign</span>(<span class="hljs-string">'SHA256'</span>); 25561cb0ef41Sopenharmony_cisign.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to sign'</span>); 25571cb0ef41Sopenharmony_cisign.<span class="hljs-title function_">end</span>(); 25581cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> signature = sign.<span class="hljs-title function_">sign</span>(privateKey, <span class="hljs-string">'hex'</span>); 25591cb0ef41Sopenharmony_ci 25601cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> verify = <span class="hljs-title function_">createVerify</span>(<span class="hljs-string">'SHA256'</span>); 25611cb0ef41Sopenharmony_civerify.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to sign'</span>); 25621cb0ef41Sopenharmony_civerify.<span class="hljs-title function_">end</span>(); 25631cb0ef41Sopenharmony_ci<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>)); 25641cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: true</span></code><button class="copy-button">copy</button></pre> 25651cb0ef41Sopenharmony_ci<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> 25661cb0ef41Sopenharmony_ci 25671cb0ef41Sopenharmony_ci<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> { 25681cb0ef41Sopenharmony_ci generateKeyPairSync, 25691cb0ef41Sopenharmony_ci createSign, 25701cb0ef41Sopenharmony_ci createVerify, 25711cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 25721cb0ef41Sopenharmony_ci 25731cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { privateKey, publicKey } = <span class="hljs-title function_">generateKeyPairSync</span>(<span class="hljs-string">'rsa'</span>, { 25741cb0ef41Sopenharmony_ci <span class="hljs-attr">modulusLength</span>: <span class="hljs-number">2048</span>, 25751cb0ef41Sopenharmony_ci}); 25761cb0ef41Sopenharmony_ci 25771cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> sign = <span class="hljs-title function_">createSign</span>(<span class="hljs-string">'SHA256'</span>); 25781cb0ef41Sopenharmony_cisign.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to sign'</span>); 25791cb0ef41Sopenharmony_cisign.<span class="hljs-title function_">end</span>(); 25801cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> signature = sign.<span class="hljs-title function_">sign</span>(privateKey); 25811cb0ef41Sopenharmony_ci 25821cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> verify = <span class="hljs-title function_">createVerify</span>(<span class="hljs-string">'SHA256'</span>); 25831cb0ef41Sopenharmony_civerify.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to sign'</span>); 25841cb0ef41Sopenharmony_civerify.<span class="hljs-title function_">end</span>(); 25851cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(verify.<span class="hljs-title function_">verify</span>(publicKey, signature)); 25861cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: true</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 25871cb0ef41Sopenharmony_ci generateKeyPairSync, 25881cb0ef41Sopenharmony_ci createSign, 25891cb0ef41Sopenharmony_ci createVerify, 25901cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 25911cb0ef41Sopenharmony_ci 25921cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { privateKey, publicKey } = <span class="hljs-title function_">generateKeyPairSync</span>(<span class="hljs-string">'rsa'</span>, { 25931cb0ef41Sopenharmony_ci <span class="hljs-attr">modulusLength</span>: <span class="hljs-number">2048</span>, 25941cb0ef41Sopenharmony_ci}); 25951cb0ef41Sopenharmony_ci 25961cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> sign = <span class="hljs-title function_">createSign</span>(<span class="hljs-string">'SHA256'</span>); 25971cb0ef41Sopenharmony_cisign.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to sign'</span>); 25981cb0ef41Sopenharmony_cisign.<span class="hljs-title function_">end</span>(); 25991cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> signature = sign.<span class="hljs-title function_">sign</span>(privateKey); 26001cb0ef41Sopenharmony_ci 26011cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> verify = <span class="hljs-title function_">createVerify</span>(<span class="hljs-string">'SHA256'</span>); 26021cb0ef41Sopenharmony_civerify.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to sign'</span>); 26031cb0ef41Sopenharmony_civerify.<span class="hljs-title function_">end</span>(); 26041cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(verify.<span class="hljs-title function_">verify</span>(publicKey, signature)); 26051cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: true</span></code><button class="copy-button">copy</button></pre> 26061cb0ef41Sopenharmony_ci<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> 26071cb0ef41Sopenharmony_ci<div class="api_metadata"> 26081cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 26091cb0ef41Sopenharmony_ci<table> 26101cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 26111cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 26121cb0ef41Sopenharmony_ci<td><p>The privateKey can also be an ArrayBuffer and CryptoKey.</p></td></tr> 26131cb0ef41Sopenharmony_ci<tr><td>v13.2.0, v12.16.0</td> 26141cb0ef41Sopenharmony_ci<td><p>This function now supports IEEE-P1363 DSA and ECDSA signatures.</p></td></tr> 26151cb0ef41Sopenharmony_ci<tr><td>v12.0.0</td> 26161cb0ef41Sopenharmony_ci<td><p>This function now supports RSA-PSS keys.</p></td></tr> 26171cb0ef41Sopenharmony_ci<tr><td>v11.6.0</td> 26181cb0ef41Sopenharmony_ci<td><p>This function now supports key objects.</p></td></tr> 26191cb0ef41Sopenharmony_ci<tr><td>v8.0.0</td> 26201cb0ef41Sopenharmony_ci<td><p>Support for RSASSA-PSS and additional options was added.</p></td></tr> 26211cb0ef41Sopenharmony_ci<tr><td>v0.1.92</td> 26221cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.92</span></p></td></tr> 26231cb0ef41Sopenharmony_ci</tbody></table> 26241cb0ef41Sopenharmony_ci</details> 26251cb0ef41Sopenharmony_ci</div> 26261cb0ef41Sopenharmony_ci<!--lint disable maximum-line-length remark-lint--> 26271cb0ef41Sopenharmony_ci<ul> 26281cb0ef41Sopenharmony_ci<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> 26291cb0ef41Sopenharmony_ci<ul> 26301cb0ef41Sopenharmony_ci<li><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 26311cb0ef41Sopenharmony_ci<li><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 26321cb0ef41Sopenharmony_ci<li><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 26331cb0ef41Sopenharmony_ci</ul> 26341cb0ef41Sopenharmony_ci</li> 26351cb0ef41Sopenharmony_ci<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> 26361cb0ef41Sopenharmony_ci<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> 26371cb0ef41Sopenharmony_ci</ul> 26381cb0ef41Sopenharmony_ci<!--lint enable maximum-line-length remark-lint--> 26391cb0ef41Sopenharmony_ci<p>Calculates the signature on all the data passed through using either 26401cb0ef41Sopenharmony_ci<a href="#signupdatedata-inputencoding"><code>sign.update()</code></a> or <a href="stream.html#writablewritechunk-encoding-callback"><code>sign.write()</code></a>.</p> 26411cb0ef41Sopenharmony_ci<p>If <code>privateKey</code> is not a <a href="#class-keyobject"><code>KeyObject</code></a>, this function behaves as if 26421cb0ef41Sopenharmony_ci<code>privateKey</code> had been passed to <a href="#cryptocreateprivatekeykey"><code>crypto.createPrivateKey()</code></a>. If it is an 26431cb0ef41Sopenharmony_ciobject, the following additional properties can be passed:</p> 26441cb0ef41Sopenharmony_ci<ul> 26451cb0ef41Sopenharmony_ci<li> 26461cb0ef41Sopenharmony_ci<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 26471cb0ef41Sopenharmony_ciformat of the generated signature. It can be one of the following:</p> 26481cb0ef41Sopenharmony_ci<ul> 26491cb0ef41Sopenharmony_ci<li><code>'der'</code> (default): DER-encoded ASN.1 signature structure encoding <code>(r, s)</code>.</li> 26501cb0ef41Sopenharmony_ci<li><code>'ieee-p1363'</code>: Signature format <code>r || s</code> as proposed in IEEE-P1363.</li> 26511cb0ef41Sopenharmony_ci</ul> 26521cb0ef41Sopenharmony_ci</li> 26531cb0ef41Sopenharmony_ci<li> 26541cb0ef41Sopenharmony_ci<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> 26551cb0ef41Sopenharmony_ci<ul> 26561cb0ef41Sopenharmony_ci<li><code>crypto.constants.RSA_PKCS1_PADDING</code> (default)</li> 26571cb0ef41Sopenharmony_ci<li><code>crypto.constants.RSA_PKCS1_PSS_PADDING</code></li> 26581cb0ef41Sopenharmony_ci</ul> 26591cb0ef41Sopenharmony_ci<p><code>RSA_PKCS1_PSS_PADDING</code> will use MGF1 with the same hash function 26601cb0ef41Sopenharmony_ciused 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 26611cb0ef41Sopenharmony_cian MGF1 hash function has been specified as part of the key in compliance with 26621cb0ef41Sopenharmony_cisection 3.3 of <a href="https://www.rfc-editor.org/rfc/rfc4055.txt">RFC 4055</a>.</p> 26631cb0ef41Sopenharmony_ci</li> 26641cb0ef41Sopenharmony_ci<li> 26651cb0ef41Sopenharmony_ci<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 26661cb0ef41Sopenharmony_ci<code>RSA_PKCS1_PSS_PADDING</code>. The special value 26671cb0ef41Sopenharmony_ci<code>crypto.constants.RSA_PSS_SALTLEN_DIGEST</code> sets the salt length to the digest 26681cb0ef41Sopenharmony_cisize, <code>crypto.constants.RSA_PSS_SALTLEN_MAX_SIGN</code> (default) sets it to the 26691cb0ef41Sopenharmony_cimaximum permissible value.</p> 26701cb0ef41Sopenharmony_ci</li> 26711cb0ef41Sopenharmony_ci</ul> 26721cb0ef41Sopenharmony_ci<p>If <code>outputEncoding</code> is provided a string is returned; otherwise a <a href="buffer.html"><code>Buffer</code></a> 26731cb0ef41Sopenharmony_ciis returned.</p> 26741cb0ef41Sopenharmony_ci<p>The <code>Sign</code> object can not be again used after <code>sign.sign()</code> method has been 26751cb0ef41Sopenharmony_cicalled. Multiple calls to <code>sign.sign()</code> will result in an error being thrown.</p> 26761cb0ef41Sopenharmony_ci<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> 26771cb0ef41Sopenharmony_ci<div class="api_metadata"> 26781cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 26791cb0ef41Sopenharmony_ci<table> 26801cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 26811cb0ef41Sopenharmony_ci<tr><td>v6.0.0</td> 26821cb0ef41Sopenharmony_ci<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p></td></tr> 26831cb0ef41Sopenharmony_ci<tr><td>v0.1.92</td> 26841cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.92</span></p></td></tr> 26851cb0ef41Sopenharmony_ci</tbody></table> 26861cb0ef41Sopenharmony_ci</details> 26871cb0ef41Sopenharmony_ci</div> 26881cb0ef41Sopenharmony_ci<ul> 26891cb0ef41Sopenharmony_ci<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> 26901cb0ef41Sopenharmony_ci<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> 26911cb0ef41Sopenharmony_ci</ul> 26921cb0ef41Sopenharmony_ci<p>Updates the <code>Sign</code> content with the given <code>data</code>, the encoding of which 26931cb0ef41Sopenharmony_ciis given in <code>inputEncoding</code>. 26941cb0ef41Sopenharmony_ciIf <code>encoding</code> is not provided, and the <code>data</code> is a string, an 26951cb0ef41Sopenharmony_ciencoding of <code>'utf8'</code> is enforced. If <code>data</code> is a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or 26961cb0ef41Sopenharmony_ci<code>DataView</code>, then <code>inputEncoding</code> is ignored.</p> 26971cb0ef41Sopenharmony_ci<p>This can be called many times with new data as it is streamed.</p> 26981cb0ef41Sopenharmony_ci</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> 26991cb0ef41Sopenharmony_ci<div class="api_metadata"> 27001cb0ef41Sopenharmony_ci<span>Added in: v0.1.92</span> 27011cb0ef41Sopenharmony_ci</div> 27021cb0ef41Sopenharmony_ci<ul> 27031cb0ef41Sopenharmony_ci<li>Extends: <a href="stream.html#class-streamwritable" class="type"><stream.Writable></a></li> 27041cb0ef41Sopenharmony_ci</ul> 27051cb0ef41Sopenharmony_ci<p>The <code>Verify</code> class is a utility for verifying signatures. It can be used in one 27061cb0ef41Sopenharmony_ciof two ways:</p> 27071cb0ef41Sopenharmony_ci<ul> 27081cb0ef41Sopenharmony_ci<li>As a writable <a href="stream.html">stream</a> where written data is used to validate against the 27091cb0ef41Sopenharmony_cisupplied signature, or</li> 27101cb0ef41Sopenharmony_ci<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 27111cb0ef41Sopenharmony_cithe signature.</li> 27121cb0ef41Sopenharmony_ci</ul> 27131cb0ef41Sopenharmony_ci<p>The <a href="#cryptocreateverifyalgorithm-options"><code>crypto.createVerify()</code></a> method is used to create <code>Verify</code> instances. 27141cb0ef41Sopenharmony_ci<code>Verify</code> objects are not to be created directly using the <code>new</code> keyword.</p> 27151cb0ef41Sopenharmony_ci<p>See <a href="#class-sign"><code>Sign</code></a> for examples.</p> 27161cb0ef41Sopenharmony_ci<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> 27171cb0ef41Sopenharmony_ci<div class="api_metadata"> 27181cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 27191cb0ef41Sopenharmony_ci<table> 27201cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 27211cb0ef41Sopenharmony_ci<tr><td>v6.0.0</td> 27221cb0ef41Sopenharmony_ci<td><p>The default <code>inputEncoding</code> changed from <code>binary</code> to <code>utf8</code>.</p></td></tr> 27231cb0ef41Sopenharmony_ci<tr><td>v0.1.92</td> 27241cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.92</span></p></td></tr> 27251cb0ef41Sopenharmony_ci</tbody></table> 27261cb0ef41Sopenharmony_ci</details> 27271cb0ef41Sopenharmony_ci</div> 27281cb0ef41Sopenharmony_ci<ul> 27291cb0ef41Sopenharmony_ci<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> 27301cb0ef41Sopenharmony_ci<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> 27311cb0ef41Sopenharmony_ci</ul> 27321cb0ef41Sopenharmony_ci<p>Updates the <code>Verify</code> content with the given <code>data</code>, the encoding of which 27331cb0ef41Sopenharmony_ciis given in <code>inputEncoding</code>. 27341cb0ef41Sopenharmony_ciIf <code>inputEncoding</code> is not provided, and the <code>data</code> is a string, an 27351cb0ef41Sopenharmony_ciencoding of <code>'utf8'</code> is enforced. If <code>data</code> is a <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or 27361cb0ef41Sopenharmony_ci<code>DataView</code>, then <code>inputEncoding</code> is ignored.</p> 27371cb0ef41Sopenharmony_ci<p>This can be called many times with new data as it is streamed.</p> 27381cb0ef41Sopenharmony_ci<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> 27391cb0ef41Sopenharmony_ci<div class="api_metadata"> 27401cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 27411cb0ef41Sopenharmony_ci<table> 27421cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 27431cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 27441cb0ef41Sopenharmony_ci<td><p>The object can also be an ArrayBuffer and CryptoKey.</p></td></tr> 27451cb0ef41Sopenharmony_ci<tr><td>v13.2.0, v12.16.0</td> 27461cb0ef41Sopenharmony_ci<td><p>This function now supports IEEE-P1363 DSA and ECDSA signatures.</p></td></tr> 27471cb0ef41Sopenharmony_ci<tr><td>v12.0.0</td> 27481cb0ef41Sopenharmony_ci<td><p>This function now supports RSA-PSS keys.</p></td></tr> 27491cb0ef41Sopenharmony_ci<tr><td>v11.7.0</td> 27501cb0ef41Sopenharmony_ci<td><p>The key can now be a private key.</p></td></tr> 27511cb0ef41Sopenharmony_ci<tr><td>v8.0.0</td> 27521cb0ef41Sopenharmony_ci<td><p>Support for RSASSA-PSS and additional options was added.</p></td></tr> 27531cb0ef41Sopenharmony_ci<tr><td>v0.1.92</td> 27541cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.92</span></p></td></tr> 27551cb0ef41Sopenharmony_ci</tbody></table> 27561cb0ef41Sopenharmony_ci</details> 27571cb0ef41Sopenharmony_ci</div> 27581cb0ef41Sopenharmony_ci<!--lint disable maximum-line-length remark-lint--> 27591cb0ef41Sopenharmony_ci<ul> 27601cb0ef41Sopenharmony_ci<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> 27611cb0ef41Sopenharmony_ci<ul> 27621cb0ef41Sopenharmony_ci<li><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 27631cb0ef41Sopenharmony_ci<li><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 27641cb0ef41Sopenharmony_ci<li><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 27651cb0ef41Sopenharmony_ci</ul> 27661cb0ef41Sopenharmony_ci</li> 27671cb0ef41Sopenharmony_ci<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> 27681cb0ef41Sopenharmony_ci<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> 27691cb0ef41Sopenharmony_ci<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 27701cb0ef41Sopenharmony_cisignature for the data and public key.</li> 27711cb0ef41Sopenharmony_ci</ul> 27721cb0ef41Sopenharmony_ci<!--lint enable maximum-line-length remark-lint--> 27731cb0ef41Sopenharmony_ci<p>Verifies the provided data using the given <code>object</code> and <code>signature</code>.</p> 27741cb0ef41Sopenharmony_ci<p>If <code>object</code> is not a <a href="#class-keyobject"><code>KeyObject</code></a>, this function behaves as if 27751cb0ef41Sopenharmony_ci<code>object</code> had been passed to <a href="#cryptocreatepublickeykey"><code>crypto.createPublicKey()</code></a>. If it is an 27761cb0ef41Sopenharmony_ciobject, the following additional properties can be passed:</p> 27771cb0ef41Sopenharmony_ci<ul> 27781cb0ef41Sopenharmony_ci<li> 27791cb0ef41Sopenharmony_ci<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 27801cb0ef41Sopenharmony_ciformat of the signature. It can be one of the following:</p> 27811cb0ef41Sopenharmony_ci<ul> 27821cb0ef41Sopenharmony_ci<li><code>'der'</code> (default): DER-encoded ASN.1 signature structure encoding <code>(r, s)</code>.</li> 27831cb0ef41Sopenharmony_ci<li><code>'ieee-p1363'</code>: Signature format <code>r || s</code> as proposed in IEEE-P1363.</li> 27841cb0ef41Sopenharmony_ci</ul> 27851cb0ef41Sopenharmony_ci</li> 27861cb0ef41Sopenharmony_ci<li> 27871cb0ef41Sopenharmony_ci<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> 27881cb0ef41Sopenharmony_ci<ul> 27891cb0ef41Sopenharmony_ci<li><code>crypto.constants.RSA_PKCS1_PADDING</code> (default)</li> 27901cb0ef41Sopenharmony_ci<li><code>crypto.constants.RSA_PKCS1_PSS_PADDING</code></li> 27911cb0ef41Sopenharmony_ci</ul> 27921cb0ef41Sopenharmony_ci<p><code>RSA_PKCS1_PSS_PADDING</code> will use MGF1 with the same hash function 27931cb0ef41Sopenharmony_ciused 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 27941cb0ef41Sopenharmony_cian MGF1 hash function has been specified as part of the key in compliance with 27951cb0ef41Sopenharmony_cisection 3.3 of <a href="https://www.rfc-editor.org/rfc/rfc4055.txt">RFC 4055</a>.</p> 27961cb0ef41Sopenharmony_ci</li> 27971cb0ef41Sopenharmony_ci<li> 27981cb0ef41Sopenharmony_ci<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 27991cb0ef41Sopenharmony_ci<code>RSA_PKCS1_PSS_PADDING</code>. The special value 28001cb0ef41Sopenharmony_ci<code>crypto.constants.RSA_PSS_SALTLEN_DIGEST</code> sets the salt length to the digest 28011cb0ef41Sopenharmony_cisize, <code>crypto.constants.RSA_PSS_SALTLEN_AUTO</code> (default) causes it to be 28021cb0ef41Sopenharmony_cidetermined automatically.</p> 28031cb0ef41Sopenharmony_ci</li> 28041cb0ef41Sopenharmony_ci</ul> 28051cb0ef41Sopenharmony_ci<p>The <code>signature</code> argument is the previously calculated signature for the data, in 28061cb0ef41Sopenharmony_cithe <code>signatureEncoding</code>. 28071cb0ef41Sopenharmony_ciIf a <code>signatureEncoding</code> is specified, the <code>signature</code> is expected to be a 28081cb0ef41Sopenharmony_cistring; otherwise <code>signature</code> is expected to be a <a href="buffer.html"><code>Buffer</code></a>, 28091cb0ef41Sopenharmony_ci<code>TypedArray</code>, or <code>DataView</code>.</p> 28101cb0ef41Sopenharmony_ci<p>The <code>verify</code> object can not be used again after <code>verify.verify()</code> has been 28111cb0ef41Sopenharmony_cicalled. Multiple calls to <code>verify.verify()</code> will result in an error being 28121cb0ef41Sopenharmony_cithrown.</p> 28131cb0ef41Sopenharmony_ci<p>Because public keys can be derived from private keys, a private key may 28141cb0ef41Sopenharmony_cibe passed instead of a public key.</p> 28151cb0ef41Sopenharmony_ci</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> 28161cb0ef41Sopenharmony_ci<div class="api_metadata"> 28171cb0ef41Sopenharmony_ci<span>Added in: v15.6.0</span> 28181cb0ef41Sopenharmony_ci</div> 28191cb0ef41Sopenharmony_ci<p>Encapsulates an X509 certificate and provides read-only access to 28201cb0ef41Sopenharmony_ciits information.</p> 28211cb0ef41Sopenharmony_ci 28221cb0ef41Sopenharmony_ci<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>); 28231cb0ef41Sopenharmony_ci 28241cb0ef41Sopenharmony_ci<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>); 28251cb0ef41Sopenharmony_ci 28261cb0ef41Sopenharmony_ci<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>); 28271cb0ef41Sopenharmony_ci 28281cb0ef41Sopenharmony_ci<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>); 28291cb0ef41Sopenharmony_ci 28301cb0ef41Sopenharmony_ci<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> 28311cb0ef41Sopenharmony_ci<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> 28321cb0ef41Sopenharmony_ci<div class="api_metadata"> 28331cb0ef41Sopenharmony_ci<span>Added in: v15.6.0</span> 28341cb0ef41Sopenharmony_ci</div> 28351cb0ef41Sopenharmony_ci<ul> 28361cb0ef41Sopenharmony_ci<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 28371cb0ef41Sopenharmony_ciX509 Certificate.</li> 28381cb0ef41Sopenharmony_ci</ul> 28391cb0ef41Sopenharmony_ci<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> 28401cb0ef41Sopenharmony_ci<div class="api_metadata"> 28411cb0ef41Sopenharmony_ci<span>Added in: v15.6.0</span> 28421cb0ef41Sopenharmony_ci</div> 28431cb0ef41Sopenharmony_ci<ul> 28441cb0ef41Sopenharmony_ci<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) 28451cb0ef41Sopenharmony_cicertificate.</li> 28461cb0ef41Sopenharmony_ci</ul> 28471cb0ef41Sopenharmony_ci<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> 28481cb0ef41Sopenharmony_ci<div class="api_metadata"> 28491cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 28501cb0ef41Sopenharmony_ci<table> 28511cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 28521cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td> 28531cb0ef41Sopenharmony_ci<td><p>The subject option now defaults to <code>'default'</code>.</p></td></tr> 28541cb0ef41Sopenharmony_ci<tr><td>v17.5.0</td> 28551cb0ef41Sopenharmony_ci<td><p>The subject option can now be set to <code>'default'</code>.</p></td></tr> 28561cb0ef41Sopenharmony_ci<tr><td>v17.5.0, v16.14.1</td> 28571cb0ef41Sopenharmony_ci<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> 28581cb0ef41Sopenharmony_ci<tr><td>v15.6.0</td> 28591cb0ef41Sopenharmony_ci<td><p><span>Added in: v15.6.0</span></p></td></tr> 28601cb0ef41Sopenharmony_ci</tbody></table> 28611cb0ef41Sopenharmony_ci</details> 28621cb0ef41Sopenharmony_ci</div> 28631cb0ef41Sopenharmony_ci<ul> 28641cb0ef41Sopenharmony_ci<li><code>email</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 28651cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 28661cb0ef41Sopenharmony_ci<ul> 28671cb0ef41Sopenharmony_ci<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>. 28681cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>'default'</code>.</li> 28691cb0ef41Sopenharmony_ci</ul> 28701cb0ef41Sopenharmony_ci</li> 28711cb0ef41Sopenharmony_ci<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, 28721cb0ef41Sopenharmony_ci<code>undefined</code> if it does not.</li> 28731cb0ef41Sopenharmony_ci</ul> 28741cb0ef41Sopenharmony_ci<p>Checks whether the certificate matches the given email address.</p> 28751cb0ef41Sopenharmony_ci<p>If the <code>'subject'</code> option is undefined or set to <code>'default'</code>, the certificate 28761cb0ef41Sopenharmony_cisubject is only considered if the subject alternative name extension either does 28771cb0ef41Sopenharmony_cinot exist or does not contain any email addresses.</p> 28781cb0ef41Sopenharmony_ci<p>If the <code>'subject'</code> option is set to <code>'always'</code> and if the subject alternative 28791cb0ef41Sopenharmony_ciname extension either does not exist or does not contain a matching email 28801cb0ef41Sopenharmony_ciaddress, the certificate subject is considered.</p> 28811cb0ef41Sopenharmony_ci<p>If the <code>'subject'</code> option is set to <code>'never'</code>, the certificate subject is never 28821cb0ef41Sopenharmony_ciconsidered, even if the certificate contains no subject alternative names.</p> 28831cb0ef41Sopenharmony_ci<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> 28841cb0ef41Sopenharmony_ci<div class="api_metadata"> 28851cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 28861cb0ef41Sopenharmony_ci<table> 28871cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 28881cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td> 28891cb0ef41Sopenharmony_ci<td><p>The subject option now defaults to <code>'default'</code>.</p></td></tr> 28901cb0ef41Sopenharmony_ci<tr><td>v17.5.0</td> 28911cb0ef41Sopenharmony_ci<td><p>The subject option can now be set to <code>'default'</code>.</p></td></tr> 28921cb0ef41Sopenharmony_ci<tr><td>v15.6.0</td> 28931cb0ef41Sopenharmony_ci<td><p><span>Added in: v15.6.0</span></p></td></tr> 28941cb0ef41Sopenharmony_ci</tbody></table> 28951cb0ef41Sopenharmony_ci</details> 28961cb0ef41Sopenharmony_ci</div> 28971cb0ef41Sopenharmony_ci<ul> 28981cb0ef41Sopenharmony_ci<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 28991cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 29001cb0ef41Sopenharmony_ci<ul> 29011cb0ef41Sopenharmony_ci<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>. 29021cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>'default'</code>.</li> 29031cb0ef41Sopenharmony_ci<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> 29041cb0ef41Sopenharmony_ci<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> 29051cb0ef41Sopenharmony_ci<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> 29061cb0ef41Sopenharmony_ci<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> 29071cb0ef41Sopenharmony_ci</ul> 29081cb0ef41Sopenharmony_ci</li> 29091cb0ef41Sopenharmony_ci<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>, 29101cb0ef41Sopenharmony_cior <code>undefined</code> if no subject name matches <code>name</code>.</li> 29111cb0ef41Sopenharmony_ci</ul> 29121cb0ef41Sopenharmony_ci<p>Checks whether the certificate matches the given host name.</p> 29131cb0ef41Sopenharmony_ci<p>If the certificate matches the given host name, the matching subject name is 29141cb0ef41Sopenharmony_cireturned. The returned name might be an exact match (e.g., <code>foo.example.com</code>) 29151cb0ef41Sopenharmony_cior it might contain wildcards (e.g., <code>*.example.com</code>). Because host name 29161cb0ef41Sopenharmony_cicomparisons are case-insensitive, the returned subject name might also differ 29171cb0ef41Sopenharmony_cifrom the given <code>name</code> in capitalization.</p> 29181cb0ef41Sopenharmony_ci<p>If the <code>'subject'</code> option is undefined or set to <code>'default'</code>, the certificate 29191cb0ef41Sopenharmony_cisubject is only considered if the subject alternative name extension either does 29201cb0ef41Sopenharmony_cinot exist or does not contain any DNS names. This behavior is consistent with 29211cb0ef41Sopenharmony_ci<a href="https://www.rfc-editor.org/rfc/rfc2818.txt">RFC 2818</a> ("HTTP Over TLS").</p> 29221cb0ef41Sopenharmony_ci<p>If the <code>'subject'</code> option is set to <code>'always'</code> and if the subject alternative 29231cb0ef41Sopenharmony_ciname extension either does not exist or does not contain a matching DNS name, 29241cb0ef41Sopenharmony_cithe certificate subject is considered.</p> 29251cb0ef41Sopenharmony_ci<p>If the <code>'subject'</code> option is set to <code>'never'</code>, the certificate subject is never 29261cb0ef41Sopenharmony_ciconsidered, even if the certificate contains no subject alternative names.</p> 29271cb0ef41Sopenharmony_ci<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> 29281cb0ef41Sopenharmony_ci<div class="api_metadata"> 29291cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 29301cb0ef41Sopenharmony_ci<table> 29311cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 29321cb0ef41Sopenharmony_ci<tr><td>v17.5.0, v16.14.1</td> 29331cb0ef41Sopenharmony_ci<td><p>The <code>options</code> argument has been removed since it had no effect.</p></td></tr> 29341cb0ef41Sopenharmony_ci<tr><td>v15.6.0</td> 29351cb0ef41Sopenharmony_ci<td><p><span>Added in: v15.6.0</span></p></td></tr> 29361cb0ef41Sopenharmony_ci</tbody></table> 29371cb0ef41Sopenharmony_ci</details> 29381cb0ef41Sopenharmony_ci</div> 29391cb0ef41Sopenharmony_ci<ul> 29401cb0ef41Sopenharmony_ci<li><code>ip</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 29411cb0ef41Sopenharmony_ci<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, 29421cb0ef41Sopenharmony_ci<code>undefined</code> if it does not.</li> 29431cb0ef41Sopenharmony_ci</ul> 29441cb0ef41Sopenharmony_ci<p>Checks whether the certificate matches the given IP address (IPv4 or IPv6).</p> 29451cb0ef41Sopenharmony_ci<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 29461cb0ef41Sopenharmony_cimust match the given <code>ip</code> address exactly. Other subject alternative names as 29471cb0ef41Sopenharmony_ciwell as the subject field of the certificate are ignored.</p> 29481cb0ef41Sopenharmony_ci<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> 29491cb0ef41Sopenharmony_ci<div class="api_metadata"> 29501cb0ef41Sopenharmony_ci<span>Added in: v15.6.0</span> 29511cb0ef41Sopenharmony_ci</div> 29521cb0ef41Sopenharmony_ci<ul> 29531cb0ef41Sopenharmony_ci<li><code>otherCert</code> <a href="crypto.html#class-x509certificate" class="type"><X509Certificate></a></li> 29541cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 29551cb0ef41Sopenharmony_ci</ul> 29561cb0ef41Sopenharmony_ci<p>Checks whether this certificate was issued by the given <code>otherCert</code>.</p> 29571cb0ef41Sopenharmony_ci<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> 29581cb0ef41Sopenharmony_ci<div class="api_metadata"> 29591cb0ef41Sopenharmony_ci<span>Added in: v15.6.0</span> 29601cb0ef41Sopenharmony_ci</div> 29611cb0ef41Sopenharmony_ci<ul> 29621cb0ef41Sopenharmony_ci<li><code>privateKey</code> <a href="crypto.html#class-keyobject" class="type"><KeyObject></a> A private key.</li> 29631cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 29641cb0ef41Sopenharmony_ci</ul> 29651cb0ef41Sopenharmony_ci<p>Checks whether the public key for this certificate is consistent with 29661cb0ef41Sopenharmony_cithe given private key.</p> 29671cb0ef41Sopenharmony_ci<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> 29681cb0ef41Sopenharmony_ci<div class="api_metadata"> 29691cb0ef41Sopenharmony_ci<span>Added in: v15.6.0</span> 29701cb0ef41Sopenharmony_ci</div> 29711cb0ef41Sopenharmony_ci<ul> 29721cb0ef41Sopenharmony_ci<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 29731cb0ef41Sopenharmony_ci</ul> 29741cb0ef41Sopenharmony_ci<p>The SHA-1 fingerprint of this certificate.</p> 29751cb0ef41Sopenharmony_ci<p>Because SHA-1 is cryptographically broken and because the security of SHA-1 is 29761cb0ef41Sopenharmony_cisignificantly worse than that of algorithms that are commonly used to sign 29771cb0ef41Sopenharmony_cicertificates, consider using <a href="#x509fingerprint256"><code>x509.fingerprint256</code></a> instead.</p> 29781cb0ef41Sopenharmony_ci<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> 29791cb0ef41Sopenharmony_ci<div class="api_metadata"> 29801cb0ef41Sopenharmony_ci<span>Added in: v15.6.0</span> 29811cb0ef41Sopenharmony_ci</div> 29821cb0ef41Sopenharmony_ci<ul> 29831cb0ef41Sopenharmony_ci<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 29841cb0ef41Sopenharmony_ci</ul> 29851cb0ef41Sopenharmony_ci<p>The SHA-256 fingerprint of this certificate.</p> 29861cb0ef41Sopenharmony_ci<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> 29871cb0ef41Sopenharmony_ci<div class="api_metadata"> 29881cb0ef41Sopenharmony_ci<span>Added in: v17.2.0, v16.14.0</span> 29891cb0ef41Sopenharmony_ci</div> 29901cb0ef41Sopenharmony_ci<ul> 29911cb0ef41Sopenharmony_ci<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 29921cb0ef41Sopenharmony_ci</ul> 29931cb0ef41Sopenharmony_ci<p>The SHA-512 fingerprint of this certificate.</p> 29941cb0ef41Sopenharmony_ci<p>Because computing the SHA-256 fingerprint is usually faster and because it is 29951cb0ef41Sopenharmony_cionly half the size of the SHA-512 fingerprint, <a href="#x509fingerprint256"><code>x509.fingerprint256</code></a> may be 29961cb0ef41Sopenharmony_cia better choice. While SHA-512 presumably provides a higher level of security in 29971cb0ef41Sopenharmony_cigeneral, the security of SHA-256 matches that of most algorithms that are 29981cb0ef41Sopenharmony_cicommonly used to sign certificates.</p> 29991cb0ef41Sopenharmony_ci<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> 30001cb0ef41Sopenharmony_ci<div class="api_metadata"> 30011cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 30021cb0ef41Sopenharmony_ci<table> 30031cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 30041cb0ef41Sopenharmony_ci<tr><td>v17.3.1, v16.13.2</td> 30051cb0ef41Sopenharmony_ci<td><p>Parts of this string may be encoded as JSON string literals in response to CVE-2021-44532.</p></td></tr> 30061cb0ef41Sopenharmony_ci<tr><td>v15.6.0</td> 30071cb0ef41Sopenharmony_ci<td><p><span>Added in: v15.6.0</span></p></td></tr> 30081cb0ef41Sopenharmony_ci</tbody></table> 30091cb0ef41Sopenharmony_ci</details> 30101cb0ef41Sopenharmony_ci</div> 30111cb0ef41Sopenharmony_ci<ul> 30121cb0ef41Sopenharmony_ci<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 30131cb0ef41Sopenharmony_ci</ul> 30141cb0ef41Sopenharmony_ci<p>A textual representation of the certificate's authority information access 30151cb0ef41Sopenharmony_ciextension.</p> 30161cb0ef41Sopenharmony_ci<p>This is a line feed separated list of access descriptions. Each line begins with 30171cb0ef41Sopenharmony_cithe access method and the kind of the access location, followed by a colon and 30181cb0ef41Sopenharmony_cithe value associated with the access location.</p> 30191cb0ef41Sopenharmony_ci<p>After the prefix denoting the access method and the kind of the access location, 30201cb0ef41Sopenharmony_cithe remainder of each line might be enclosed in quotes to indicate that the 30211cb0ef41Sopenharmony_civalue is a JSON string literal. For backward compatibility, Node.js only uses 30221cb0ef41Sopenharmony_ciJSON string literals within this property when necessary to avoid ambiguity. 30231cb0ef41Sopenharmony_ciThird-party code should be prepared to handle both possible entry formats.</p> 30241cb0ef41Sopenharmony_ci<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> 30251cb0ef41Sopenharmony_ci<div class="api_metadata"> 30261cb0ef41Sopenharmony_ci<span>Added in: v15.6.0</span> 30271cb0ef41Sopenharmony_ci</div> 30281cb0ef41Sopenharmony_ci<ul> 30291cb0ef41Sopenharmony_ci<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 30301cb0ef41Sopenharmony_ci</ul> 30311cb0ef41Sopenharmony_ci<p>The issuer identification included in this certificate.</p> 30321cb0ef41Sopenharmony_ci<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> 30331cb0ef41Sopenharmony_ci<div class="api_metadata"> 30341cb0ef41Sopenharmony_ci<span>Added in: v15.9.0</span> 30351cb0ef41Sopenharmony_ci</div> 30361cb0ef41Sopenharmony_ci<ul> 30371cb0ef41Sopenharmony_ci<li>Type: <a href="crypto.html#class-x509certificate" class="type"><X509Certificate></a></li> 30381cb0ef41Sopenharmony_ci</ul> 30391cb0ef41Sopenharmony_ci<p>The issuer certificate or <code>undefined</code> if the issuer certificate is not 30401cb0ef41Sopenharmony_ciavailable.</p> 30411cb0ef41Sopenharmony_ci<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> 30421cb0ef41Sopenharmony_ci<div class="api_metadata"> 30431cb0ef41Sopenharmony_ci<span>Added in: v15.6.0</span> 30441cb0ef41Sopenharmony_ci</div> 30451cb0ef41Sopenharmony_ci<ul> 30461cb0ef41Sopenharmony_ci<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> 30471cb0ef41Sopenharmony_ci</ul> 30481cb0ef41Sopenharmony_ci<p>An array detailing the key usages for this certificate.</p> 30491cb0ef41Sopenharmony_ci<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> 30501cb0ef41Sopenharmony_ci<div class="api_metadata"> 30511cb0ef41Sopenharmony_ci<span>Added in: v15.6.0</span> 30521cb0ef41Sopenharmony_ci</div> 30531cb0ef41Sopenharmony_ci<ul> 30541cb0ef41Sopenharmony_ci<li>Type: <a href="crypto.html#class-keyobject" class="type"><KeyObject></a></li> 30551cb0ef41Sopenharmony_ci</ul> 30561cb0ef41Sopenharmony_ci<p>The public key <a href="crypto.html#class-keyobject" class="type"><KeyObject></a> for this certificate.</p> 30571cb0ef41Sopenharmony_ci<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> 30581cb0ef41Sopenharmony_ci<div class="api_metadata"> 30591cb0ef41Sopenharmony_ci<span>Added in: v15.6.0</span> 30601cb0ef41Sopenharmony_ci</div> 30611cb0ef41Sopenharmony_ci<ul> 30621cb0ef41Sopenharmony_ci<li>Type: <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 30631cb0ef41Sopenharmony_ci</ul> 30641cb0ef41Sopenharmony_ci<p>A <code>Buffer</code> containing the DER encoding of this certificate.</p> 30651cb0ef41Sopenharmony_ci<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> 30661cb0ef41Sopenharmony_ci<div class="api_metadata"> 30671cb0ef41Sopenharmony_ci<span>Added in: v15.6.0</span> 30681cb0ef41Sopenharmony_ci</div> 30691cb0ef41Sopenharmony_ci<ul> 30701cb0ef41Sopenharmony_ci<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 30711cb0ef41Sopenharmony_ci</ul> 30721cb0ef41Sopenharmony_ci<p>The serial number of this certificate.</p> 30731cb0ef41Sopenharmony_ci<p>Serial numbers are assigned by certificate authorities and do not uniquely 30741cb0ef41Sopenharmony_ciidentify certificates. Consider using <a href="#x509fingerprint256"><code>x509.fingerprint256</code></a> as a unique 30751cb0ef41Sopenharmony_ciidentifier instead.</p> 30761cb0ef41Sopenharmony_ci<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> 30771cb0ef41Sopenharmony_ci<div class="api_metadata"> 30781cb0ef41Sopenharmony_ci<span>Added in: v15.6.0</span> 30791cb0ef41Sopenharmony_ci</div> 30801cb0ef41Sopenharmony_ci<ul> 30811cb0ef41Sopenharmony_ci<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 30821cb0ef41Sopenharmony_ci</ul> 30831cb0ef41Sopenharmony_ci<p>The complete subject of this certificate.</p> 30841cb0ef41Sopenharmony_ci<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> 30851cb0ef41Sopenharmony_ci<div class="api_metadata"> 30861cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 30871cb0ef41Sopenharmony_ci<table> 30881cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 30891cb0ef41Sopenharmony_ci<tr><td>v17.3.1, v16.13.2</td> 30901cb0ef41Sopenharmony_ci<td><p>Parts of this string may be encoded as JSON string literals in response to CVE-2021-44532.</p></td></tr> 30911cb0ef41Sopenharmony_ci<tr><td>v15.6.0</td> 30921cb0ef41Sopenharmony_ci<td><p><span>Added in: v15.6.0</span></p></td></tr> 30931cb0ef41Sopenharmony_ci</tbody></table> 30941cb0ef41Sopenharmony_ci</details> 30951cb0ef41Sopenharmony_ci</div> 30961cb0ef41Sopenharmony_ci<ul> 30971cb0ef41Sopenharmony_ci<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 30981cb0ef41Sopenharmony_ci</ul> 30991cb0ef41Sopenharmony_ci<p>The subject alternative name specified for this certificate.</p> 31001cb0ef41Sopenharmony_ci<p>This is a comma-separated list of subject alternative names. Each entry begins 31011cb0ef41Sopenharmony_ciwith a string identifying the kind of the subject alternative name followed by 31021cb0ef41Sopenharmony_cia colon and the value associated with the entry.</p> 31031cb0ef41Sopenharmony_ci<p>Earlier versions of Node.js incorrectly assumed that it is safe to split this 31041cb0ef41Sopenharmony_ciproperty 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, 31051cb0ef41Sopenharmony_ciboth malicious and legitimate certificates can contain subject alternative names 31061cb0ef41Sopenharmony_cithat include this sequence when represented as a string.</p> 31071cb0ef41Sopenharmony_ci<p>After the prefix denoting the type of the entry, the remainder of each entry 31081cb0ef41Sopenharmony_cimight be enclosed in quotes to indicate that the value is a JSON string literal. 31091cb0ef41Sopenharmony_ciFor backward compatibility, Node.js only uses JSON string literals within this 31101cb0ef41Sopenharmony_ciproperty when necessary to avoid ambiguity. Third-party code should be prepared 31111cb0ef41Sopenharmony_cito handle both possible entry formats.</p> 31121cb0ef41Sopenharmony_ci<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> 31131cb0ef41Sopenharmony_ci<div class="api_metadata"> 31141cb0ef41Sopenharmony_ci<span>Added in: v15.6.0</span> 31151cb0ef41Sopenharmony_ci</div> 31161cb0ef41Sopenharmony_ci<ul> 31171cb0ef41Sopenharmony_ci<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 31181cb0ef41Sopenharmony_ci</ul> 31191cb0ef41Sopenharmony_ci<p>There is no standard JSON encoding for X509 certificates. The 31201cb0ef41Sopenharmony_ci<code>toJSON()</code> method returns a string containing the PEM encoded 31211cb0ef41Sopenharmony_cicertificate.</p> 31221cb0ef41Sopenharmony_ci<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> 31231cb0ef41Sopenharmony_ci<div class="api_metadata"> 31241cb0ef41Sopenharmony_ci<span>Added in: v15.6.0</span> 31251cb0ef41Sopenharmony_ci</div> 31261cb0ef41Sopenharmony_ci<ul> 31271cb0ef41Sopenharmony_ci<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 31281cb0ef41Sopenharmony_ci</ul> 31291cb0ef41Sopenharmony_ci<p>Returns information about this certificate using the legacy 31301cb0ef41Sopenharmony_ci<a href="tls.html#certificate-object">certificate object</a> encoding.</p> 31311cb0ef41Sopenharmony_ci<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> 31321cb0ef41Sopenharmony_ci<div class="api_metadata"> 31331cb0ef41Sopenharmony_ci<span>Added in: v15.6.0</span> 31341cb0ef41Sopenharmony_ci</div> 31351cb0ef41Sopenharmony_ci<ul> 31361cb0ef41Sopenharmony_ci<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 31371cb0ef41Sopenharmony_ci</ul> 31381cb0ef41Sopenharmony_ci<p>Returns the PEM-encoded certificate.</p> 31391cb0ef41Sopenharmony_ci<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> 31401cb0ef41Sopenharmony_ci<div class="api_metadata"> 31411cb0ef41Sopenharmony_ci<span>Added in: v15.6.0</span> 31421cb0ef41Sopenharmony_ci</div> 31431cb0ef41Sopenharmony_ci<ul> 31441cb0ef41Sopenharmony_ci<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 31451cb0ef41Sopenharmony_ci</ul> 31461cb0ef41Sopenharmony_ci<p>The date/time from which this certificate is considered valid.</p> 31471cb0ef41Sopenharmony_ci<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> 31481cb0ef41Sopenharmony_ci<div class="api_metadata"> 31491cb0ef41Sopenharmony_ci<span>Added in: v15.6.0</span> 31501cb0ef41Sopenharmony_ci</div> 31511cb0ef41Sopenharmony_ci<ul> 31521cb0ef41Sopenharmony_ci<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 31531cb0ef41Sopenharmony_ci</ul> 31541cb0ef41Sopenharmony_ci<p>The date/time until which this certificate is considered valid.</p> 31551cb0ef41Sopenharmony_ci<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> 31561cb0ef41Sopenharmony_ci<div class="api_metadata"> 31571cb0ef41Sopenharmony_ci<span>Added in: v15.6.0</span> 31581cb0ef41Sopenharmony_ci</div> 31591cb0ef41Sopenharmony_ci<ul> 31601cb0ef41Sopenharmony_ci<li><code>publicKey</code> <a href="crypto.html#class-keyobject" class="type"><KeyObject></a> A public key.</li> 31611cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 31621cb0ef41Sopenharmony_ci</ul> 31631cb0ef41Sopenharmony_ci<p>Verifies that this certificate was signed by the given public key. 31641cb0ef41Sopenharmony_ciDoes not perform any other validation checks on the certificate.</p> 31651cb0ef41Sopenharmony_ci</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> 31661cb0ef41Sopenharmony_ci<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> 31671cb0ef41Sopenharmony_ci<div class="api_metadata"> 31681cb0ef41Sopenharmony_ci<span>Added in: v6.3.0</span> 31691cb0ef41Sopenharmony_ci</div> 31701cb0ef41Sopenharmony_ci<ul> 31711cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 31721cb0ef41Sopenharmony_ci</ul> 31731cb0ef41Sopenharmony_ci<p>An object containing commonly used constants for crypto and security related 31741cb0ef41Sopenharmony_cioperations. The specific constants currently defined are described in 31751cb0ef41Sopenharmony_ci<a href="#crypto-constants">Crypto constants</a>.</p> 31761cb0ef41Sopenharmony_ci<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> 31771cb0ef41Sopenharmony_ci<div class="api_metadata"> 31781cb0ef41Sopenharmony_ci<span>Added in: v0.9.3</span><span>Deprecated since: v10.0.0</span> 31791cb0ef41Sopenharmony_ci</div> 31801cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated</div><p></p> 31811cb0ef41Sopenharmony_ci<p>The default encoding to use for functions that can take either strings 31821cb0ef41Sopenharmony_cior <a href="buffer.html">buffers</a>. The default value is <code>'buffer'</code>, which makes methods 31831cb0ef41Sopenharmony_cidefault to <a href="buffer.html"><code>Buffer</code></a> objects.</p> 31841cb0ef41Sopenharmony_ci<p>The <code>crypto.DEFAULT_ENCODING</code> mechanism is provided for backward compatibility 31851cb0ef41Sopenharmony_ciwith legacy programs that expect <code>'latin1'</code> to be the default encoding.</p> 31861cb0ef41Sopenharmony_ci<p>New applications should expect the default to be <code>'buffer'</code>.</p> 31871cb0ef41Sopenharmony_ci<p>This property is deprecated.</p> 31881cb0ef41Sopenharmony_ci<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> 31891cb0ef41Sopenharmony_ci<div class="api_metadata"> 31901cb0ef41Sopenharmony_ci<span>Added in: v6.0.0</span><span>Deprecated since: v10.0.0</span> 31911cb0ef41Sopenharmony_ci</div> 31921cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated</div><p></p> 31931cb0ef41Sopenharmony_ci<p>Property for checking and controlling whether a FIPS compliant crypto provider 31941cb0ef41Sopenharmony_ciis currently in use. Setting to true requires a FIPS build of Node.js.</p> 31951cb0ef41Sopenharmony_ci<p>This property is deprecated. Please use <code>crypto.setFips()</code> and 31961cb0ef41Sopenharmony_ci<code>crypto.getFips()</code> instead.</p> 31971cb0ef41Sopenharmony_ci<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> 31981cb0ef41Sopenharmony_ci<div class="api_metadata"> 31991cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 32001cb0ef41Sopenharmony_ci<table> 32011cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 32021cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td> 32031cb0ef41Sopenharmony_ci<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> 32041cb0ef41Sopenharmony_ci<tr><td>v15.8.0</td> 32051cb0ef41Sopenharmony_ci<td><p><span>Added in: v15.8.0</span></p></td></tr> 32061cb0ef41Sopenharmony_ci</tbody></table> 32071cb0ef41Sopenharmony_ci</details> 32081cb0ef41Sopenharmony_ci</div> 32091cb0ef41Sopenharmony_ci<ul> 32101cb0ef41Sopenharmony_ci<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> 32111cb0ef41Sopenharmony_ciA possible prime encoded as a sequence of big endian octets of arbitrary 32121cb0ef41Sopenharmony_cilength.</li> 32131cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 32141cb0ef41Sopenharmony_ci<ul> 32151cb0ef41Sopenharmony_ci<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 32161cb0ef41Sopenharmony_ciiterations to perform. When the value is <code>0</code> (zero), a number of checks 32171cb0ef41Sopenharmony_ciis used that yields a false positive rate of at most 2<sup>-64</sup> for 32181cb0ef41Sopenharmony_cirandom input. Care must be used when selecting a number of checks. Refer 32191cb0ef41Sopenharmony_cito 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> 32201cb0ef41Sopenharmony_cioptions for more details. <strong>Default:</strong> <code>0</code></li> 32211cb0ef41Sopenharmony_ci</ul> 32221cb0ef41Sopenharmony_ci</li> 32231cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 32241cb0ef41Sopenharmony_ci<ul> 32251cb0ef41Sopenharmony_ci<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> 32261cb0ef41Sopenharmony_ci<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 32271cb0ef41Sopenharmony_ciprobability less than <code>0.25 ** options.checks</code>.</li> 32281cb0ef41Sopenharmony_ci</ul> 32291cb0ef41Sopenharmony_ci</li> 32301cb0ef41Sopenharmony_ci</ul> 32311cb0ef41Sopenharmony_ci<p>Checks the primality of the <code>candidate</code>.</p> 32321cb0ef41Sopenharmony_ci<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> 32331cb0ef41Sopenharmony_ci<div class="api_metadata"> 32341cb0ef41Sopenharmony_ci<span>Added in: v15.8.0</span> 32351cb0ef41Sopenharmony_ci</div> 32361cb0ef41Sopenharmony_ci<ul> 32371cb0ef41Sopenharmony_ci<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> 32381cb0ef41Sopenharmony_ciA possible prime encoded as a sequence of big endian octets of arbitrary 32391cb0ef41Sopenharmony_cilength.</li> 32401cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 32411cb0ef41Sopenharmony_ci<ul> 32421cb0ef41Sopenharmony_ci<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 32431cb0ef41Sopenharmony_ciiterations to perform. When the value is <code>0</code> (zero), a number of checks 32441cb0ef41Sopenharmony_ciis used that yields a false positive rate of at most 2<sup>-64</sup> for 32451cb0ef41Sopenharmony_cirandom input. Care must be used when selecting a number of checks. Refer 32461cb0ef41Sopenharmony_cito 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> 32471cb0ef41Sopenharmony_cioptions for more details. <strong>Default:</strong> <code>0</code></li> 32481cb0ef41Sopenharmony_ci</ul> 32491cb0ef41Sopenharmony_ci</li> 32501cb0ef41Sopenharmony_ci<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 32511cb0ef41Sopenharmony_ciprobability less than <code>0.25 ** options.checks</code>.</li> 32521cb0ef41Sopenharmony_ci</ul> 32531cb0ef41Sopenharmony_ci<p>Checks the primality of the <code>candidate</code>.</p> 32541cb0ef41Sopenharmony_ci<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> 32551cb0ef41Sopenharmony_ci<div class="api_metadata"> 32561cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 32571cb0ef41Sopenharmony_ci<table> 32581cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 32591cb0ef41Sopenharmony_ci<tr><td>v17.9.0</td> 32601cb0ef41Sopenharmony_ci<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> 32611cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 32621cb0ef41Sopenharmony_ci<td><p>The password argument can be an ArrayBuffer and is limited to a maximum of 2 ** 31 - 1 bytes.</p></td></tr> 32631cb0ef41Sopenharmony_ci<tr><td>v10.10.0</td> 32641cb0ef41Sopenharmony_ci<td><p>Ciphers in OCB mode are now supported.</p></td></tr> 32651cb0ef41Sopenharmony_ci<tr><td>v10.2.0</td> 32661cb0ef41Sopenharmony_ci<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> 32671cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 32681cb0ef41Sopenharmony_ci<td><p><span>Deprecated since: v10.0.0</span></p></td></tr> 32691cb0ef41Sopenharmony_ci<tr><td>v0.1.94</td> 32701cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.94</span></p></td></tr> 32711cb0ef41Sopenharmony_ci</tbody></table> 32721cb0ef41Sopenharmony_ci</details> 32731cb0ef41Sopenharmony_ci</div> 32741cb0ef41Sopenharmony_ci<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> 32751cb0ef41Sopenharmony_ci<ul> 32761cb0ef41Sopenharmony_ci<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 32771cb0ef41Sopenharmony_ci<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> 32781cb0ef41Sopenharmony_ci<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> 32791cb0ef41Sopenharmony_ci<li>Returns: <a href="crypto.html#class-cipher" class="type"><Cipher></a></li> 32801cb0ef41Sopenharmony_ci</ul> 32811cb0ef41Sopenharmony_ci<p>Creates and returns a <code>Cipher</code> object that uses the given <code>algorithm</code> and 32821cb0ef41Sopenharmony_ci<code>password</code>.</p> 32831cb0ef41Sopenharmony_ci<p>The <code>options</code> argument controls stream behavior and is optional except when a 32841cb0ef41Sopenharmony_cicipher in CCM or OCB mode (e.g. <code>'aes-128-ccm'</code>) is used. In that case, the 32851cb0ef41Sopenharmony_ci<code>authTagLength</code> option is required and specifies the length of the 32861cb0ef41Sopenharmony_ciauthentication tag in bytes, see <a href="#ccm-mode">CCM mode</a>. In GCM mode, the <code>authTagLength</code> 32871cb0ef41Sopenharmony_cioption is not required but can be used to set the length of the authentication 32881cb0ef41Sopenharmony_citag that will be returned by <code>getAuthTag()</code> and defaults to 16 bytes. 32891cb0ef41Sopenharmony_ciFor <code>chacha20-poly1305</code>, the <code>authTagLength</code> option defaults to 16 bytes.</p> 32901cb0ef41Sopenharmony_ci<p>The <code>algorithm</code> is dependent on OpenSSL, examples are <code>'aes192'</code>, etc. On 32911cb0ef41Sopenharmony_cirecent OpenSSL releases, <code>openssl list -cipher-algorithms</code> will 32921cb0ef41Sopenharmony_cidisplay the available cipher algorithms.</p> 32931cb0ef41Sopenharmony_ci<p>The <code>password</code> is used to derive the cipher key and initialization vector (IV). 32941cb0ef41Sopenharmony_ciThe value must be either a <code>'latin1'</code> encoded string, a <a href="buffer.html"><code>Buffer</code></a>, a 32951cb0ef41Sopenharmony_ci<code>TypedArray</code>, or a <code>DataView</code>.</p> 32961cb0ef41Sopenharmony_ci<p><strong class="critical">This function is semantically insecure for all 32971cb0ef41Sopenharmony_cisupported ciphers and fatally flawed for ciphers in counter mode (such as CTR, 32981cb0ef41Sopenharmony_ciGCM, or CCM).</strong></p> 32991cb0ef41Sopenharmony_ci<p>The implementation of <code>crypto.createCipher()</code> derives keys using the OpenSSL 33001cb0ef41Sopenharmony_cifunction <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 33011cb0ef41Sopenharmony_ciiteration, and no salt. The lack of salt allows dictionary attacks as the same 33021cb0ef41Sopenharmony_cipassword always creates the same key. The low iteration count and 33031cb0ef41Sopenharmony_cinon-cryptographically secure hash algorithm allow passwords to be tested very 33041cb0ef41Sopenharmony_cirapidly.</p> 33051cb0ef41Sopenharmony_ci<p>In line with OpenSSL's recommendation to use a more modern algorithm instead of 33061cb0ef41Sopenharmony_ci<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 33071cb0ef41Sopenharmony_citheir 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> 33081cb0ef41Sopenharmony_cito create the <code>Cipher</code> object. Users should not use ciphers with counter mode 33091cb0ef41Sopenharmony_ci(e.g. CTR, GCM, or CCM) in <code>crypto.createCipher()</code>. A warning is emitted when 33101cb0ef41Sopenharmony_cithey are used in order to avoid the risk of IV reuse that causes 33111cb0ef41Sopenharmony_civulnerabilities. For the case when IV is reused in GCM, see <a href="https://github.com/nonce-disrespect/nonce-disrespect">Nonce-Disrespecting 33121cb0ef41Sopenharmony_ciAdversaries</a> for details.</p> 33131cb0ef41Sopenharmony_ci<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> 33141cb0ef41Sopenharmony_ci<div class="api_metadata"> 33151cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 33161cb0ef41Sopenharmony_ci<table> 33171cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 33181cb0ef41Sopenharmony_ci<tr><td>v17.9.0</td> 33191cb0ef41Sopenharmony_ci<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> 33201cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 33211cb0ef41Sopenharmony_ci<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> 33221cb0ef41Sopenharmony_ci<tr><td>v11.6.0</td> 33231cb0ef41Sopenharmony_ci<td><p>The <code>key</code> argument can now be a <code>KeyObject</code>.</p></td></tr> 33241cb0ef41Sopenharmony_ci<tr><td>v11.2.0, v10.17.0</td> 33251cb0ef41Sopenharmony_ci<td><p>The cipher <code>chacha20-poly1305</code> (the IETF variant of ChaCha20-Poly1305) is now supported.</p></td></tr> 33261cb0ef41Sopenharmony_ci<tr><td>v10.10.0</td> 33271cb0ef41Sopenharmony_ci<td><p>Ciphers in OCB mode are now supported.</p></td></tr> 33281cb0ef41Sopenharmony_ci<tr><td>v10.2.0</td> 33291cb0ef41Sopenharmony_ci<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> 33301cb0ef41Sopenharmony_ci<tr><td>v9.9.0</td> 33311cb0ef41Sopenharmony_ci<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> 33321cb0ef41Sopenharmony_ci<tr><td>v0.1.94</td> 33331cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.94</span></p></td></tr> 33341cb0ef41Sopenharmony_ci</tbody></table> 33351cb0ef41Sopenharmony_ci</details> 33361cb0ef41Sopenharmony_ci</div> 33371cb0ef41Sopenharmony_ci<ul> 33381cb0ef41Sopenharmony_ci<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 33391cb0ef41Sopenharmony_ci<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> 33401cb0ef41Sopenharmony_ci<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> 33411cb0ef41Sopenharmony_ci<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> 33421cb0ef41Sopenharmony_ci<li>Returns: <a href="crypto.html#class-cipher" class="type"><Cipher></a></li> 33431cb0ef41Sopenharmony_ci</ul> 33441cb0ef41Sopenharmony_ci<p>Creates and returns a <code>Cipher</code> object, with the given <code>algorithm</code>, <code>key</code> and 33451cb0ef41Sopenharmony_ciinitialization vector (<code>iv</code>).</p> 33461cb0ef41Sopenharmony_ci<p>The <code>options</code> argument controls stream behavior and is optional except when a 33471cb0ef41Sopenharmony_cicipher in CCM or OCB mode (e.g. <code>'aes-128-ccm'</code>) is used. In that case, the 33481cb0ef41Sopenharmony_ci<code>authTagLength</code> option is required and specifies the length of the 33491cb0ef41Sopenharmony_ciauthentication tag in bytes, see <a href="#ccm-mode">CCM mode</a>. In GCM mode, the <code>authTagLength</code> 33501cb0ef41Sopenharmony_cioption is not required but can be used to set the length of the authentication 33511cb0ef41Sopenharmony_citag that will be returned by <code>getAuthTag()</code> and defaults to 16 bytes. 33521cb0ef41Sopenharmony_ciFor <code>chacha20-poly1305</code>, the <code>authTagLength</code> option defaults to 16 bytes.</p> 33531cb0ef41Sopenharmony_ci<p>The <code>algorithm</code> is dependent on OpenSSL, examples are <code>'aes192'</code>, etc. On 33541cb0ef41Sopenharmony_cirecent OpenSSL releases, <code>openssl list -cipher-algorithms</code> will 33551cb0ef41Sopenharmony_cidisplay the available cipher algorithms.</p> 33561cb0ef41Sopenharmony_ci<p>The <code>key</code> is the raw key used by the <code>algorithm</code> and <code>iv</code> is an 33571cb0ef41Sopenharmony_ci<a href="https://en.wikipedia.org/wiki/Initialization_vector">initialization vector</a>. Both arguments must be <code>'utf8'</code> encoded strings, 33581cb0ef41Sopenharmony_ci<a href="buffer.html">Buffers</a>, <code>TypedArray</code>, or <code>DataView</code>s. The <code>key</code> may optionally be 33591cb0ef41Sopenharmony_cia <a href="#class-keyobject"><code>KeyObject</code></a> of type <code>secret</code>. If the cipher does not need 33601cb0ef41Sopenharmony_cian initialization vector, <code>iv</code> may be <code>null</code>.</p> 33611cb0ef41Sopenharmony_ci<p>When passing strings for <code>key</code> or <code>iv</code>, please consider 33621cb0ef41Sopenharmony_ci<a href="#using-strings-as-inputs-to-cryptographic-apis">caveats when using strings as inputs to cryptographic APIs</a>.</p> 33631cb0ef41Sopenharmony_ci<p>Initialization vectors should be unpredictable and unique; ideally, they will be 33641cb0ef41Sopenharmony_cicryptographically random. They do not have to be secret: IVs are typically just 33651cb0ef41Sopenharmony_ciadded to ciphertext messages unencrypted. It may sound contradictory that 33661cb0ef41Sopenharmony_cisomething has to be unpredictable and unique, but does not have to be secret; 33671cb0ef41Sopenharmony_ciremember that an attacker must not be able to predict ahead of time what a 33681cb0ef41Sopenharmony_cigiven IV will be.</p> 33691cb0ef41Sopenharmony_ci<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> 33701cb0ef41Sopenharmony_ci<div class="api_metadata"> 33711cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 33721cb0ef41Sopenharmony_ci<table> 33731cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 33741cb0ef41Sopenharmony_ci<tr><td>v17.9.0</td> 33751cb0ef41Sopenharmony_ci<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> 33761cb0ef41Sopenharmony_ci<tr><td>v10.10.0</td> 33771cb0ef41Sopenharmony_ci<td><p>Ciphers in OCB mode are now supported.</p></td></tr> 33781cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td> 33791cb0ef41Sopenharmony_ci<td><p><span>Deprecated since: v10.0.0</span></p></td></tr> 33801cb0ef41Sopenharmony_ci<tr><td>v0.1.94</td> 33811cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.94</span></p></td></tr> 33821cb0ef41Sopenharmony_ci</tbody></table> 33831cb0ef41Sopenharmony_ci</details> 33841cb0ef41Sopenharmony_ci</div> 33851cb0ef41Sopenharmony_ci<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> 33861cb0ef41Sopenharmony_ci<ul> 33871cb0ef41Sopenharmony_ci<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 33881cb0ef41Sopenharmony_ci<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> 33891cb0ef41Sopenharmony_ci<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> 33901cb0ef41Sopenharmony_ci<li>Returns: <a href="crypto.html#class-decipher" class="type"><Decipher></a></li> 33911cb0ef41Sopenharmony_ci</ul> 33921cb0ef41Sopenharmony_ci<p>Creates and returns a <code>Decipher</code> object that uses the given <code>algorithm</code> and 33931cb0ef41Sopenharmony_ci<code>password</code> (key).</p> 33941cb0ef41Sopenharmony_ci<p>The <code>options</code> argument controls stream behavior and is optional except when a 33951cb0ef41Sopenharmony_cicipher in CCM or OCB mode (e.g. <code>'aes-128-ccm'</code>) is used. In that case, the 33961cb0ef41Sopenharmony_ci<code>authTagLength</code> option is required and specifies the length of the 33971cb0ef41Sopenharmony_ciauthentication tag in bytes, see <a href="#ccm-mode">CCM mode</a>. 33981cb0ef41Sopenharmony_ciFor <code>chacha20-poly1305</code>, the <code>authTagLength</code> option defaults to 16 bytes.</p> 33991cb0ef41Sopenharmony_ci<p><strong class="critical">This function is semantically insecure for all 34001cb0ef41Sopenharmony_cisupported ciphers and fatally flawed for ciphers in counter mode (such as CTR, 34011cb0ef41Sopenharmony_ciGCM, or CCM).</strong></p> 34021cb0ef41Sopenharmony_ci<p>The implementation of <code>crypto.createDecipher()</code> derives keys using the OpenSSL 34031cb0ef41Sopenharmony_cifunction <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 34041cb0ef41Sopenharmony_ciiteration, and no salt. The lack of salt allows dictionary attacks as the same 34051cb0ef41Sopenharmony_cipassword always creates the same key. The low iteration count and 34061cb0ef41Sopenharmony_cinon-cryptographically secure hash algorithm allow passwords to be tested very 34071cb0ef41Sopenharmony_cirapidly.</p> 34081cb0ef41Sopenharmony_ci<p>In line with OpenSSL's recommendation to use a more modern algorithm instead of 34091cb0ef41Sopenharmony_ci<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 34101cb0ef41Sopenharmony_citheir 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> 34111cb0ef41Sopenharmony_cito create the <code>Decipher</code> object.</p> 34121cb0ef41Sopenharmony_ci<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> 34131cb0ef41Sopenharmony_ci<div class="api_metadata"> 34141cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 34151cb0ef41Sopenharmony_ci<table> 34161cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 34171cb0ef41Sopenharmony_ci<tr><td>v17.9.0</td> 34181cb0ef41Sopenharmony_ci<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> 34191cb0ef41Sopenharmony_ci<tr><td>v11.6.0</td> 34201cb0ef41Sopenharmony_ci<td><p>The <code>key</code> argument can now be a <code>KeyObject</code>.</p></td></tr> 34211cb0ef41Sopenharmony_ci<tr><td>v11.2.0, v10.17.0</td> 34221cb0ef41Sopenharmony_ci<td><p>The cipher <code>chacha20-poly1305</code> (the IETF variant of ChaCha20-Poly1305) is now supported.</p></td></tr> 34231cb0ef41Sopenharmony_ci<tr><td>v10.10.0</td> 34241cb0ef41Sopenharmony_ci<td><p>Ciphers in OCB mode are now supported.</p></td></tr> 34251cb0ef41Sopenharmony_ci<tr><td>v10.2.0</td> 34261cb0ef41Sopenharmony_ci<td><p>The <code>authTagLength</code> option can now be used to restrict accepted GCM authentication tag lengths.</p></td></tr> 34271cb0ef41Sopenharmony_ci<tr><td>v9.9.0</td> 34281cb0ef41Sopenharmony_ci<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> 34291cb0ef41Sopenharmony_ci<tr><td>v0.1.94</td> 34301cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.94</span></p></td></tr> 34311cb0ef41Sopenharmony_ci</tbody></table> 34321cb0ef41Sopenharmony_ci</details> 34331cb0ef41Sopenharmony_ci</div> 34341cb0ef41Sopenharmony_ci<ul> 34351cb0ef41Sopenharmony_ci<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 34361cb0ef41Sopenharmony_ci<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> 34371cb0ef41Sopenharmony_ci<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> 34381cb0ef41Sopenharmony_ci<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> 34391cb0ef41Sopenharmony_ci<li>Returns: <a href="crypto.html#class-decipher" class="type"><Decipher></a></li> 34401cb0ef41Sopenharmony_ci</ul> 34411cb0ef41Sopenharmony_ci<p>Creates and returns a <code>Decipher</code> object that uses the given <code>algorithm</code>, <code>key</code> 34421cb0ef41Sopenharmony_ciand initialization vector (<code>iv</code>).</p> 34431cb0ef41Sopenharmony_ci<p>The <code>options</code> argument controls stream behavior and is optional except when a 34441cb0ef41Sopenharmony_cicipher in CCM or OCB mode (e.g. <code>'aes-128-ccm'</code>) is used. In that case, the 34451cb0ef41Sopenharmony_ci<code>authTagLength</code> option is required and specifies the length of the 34461cb0ef41Sopenharmony_ciauthentication tag in bytes, see <a href="#ccm-mode">CCM mode</a>. In GCM mode, the <code>authTagLength</code> 34471cb0ef41Sopenharmony_cioption is not required but can be used to restrict accepted authentication tags 34481cb0ef41Sopenharmony_cito those with the specified length. 34491cb0ef41Sopenharmony_ciFor <code>chacha20-poly1305</code>, the <code>authTagLength</code> option defaults to 16 bytes.</p> 34501cb0ef41Sopenharmony_ci<p>The <code>algorithm</code> is dependent on OpenSSL, examples are <code>'aes192'</code>, etc. On 34511cb0ef41Sopenharmony_cirecent OpenSSL releases, <code>openssl list -cipher-algorithms</code> will 34521cb0ef41Sopenharmony_cidisplay the available cipher algorithms.</p> 34531cb0ef41Sopenharmony_ci<p>The <code>key</code> is the raw key used by the <code>algorithm</code> and <code>iv</code> is an 34541cb0ef41Sopenharmony_ci<a href="https://en.wikipedia.org/wiki/Initialization_vector">initialization vector</a>. Both arguments must be <code>'utf8'</code> encoded strings, 34551cb0ef41Sopenharmony_ci<a href="buffer.html">Buffers</a>, <code>TypedArray</code>, or <code>DataView</code>s. The <code>key</code> may optionally be 34561cb0ef41Sopenharmony_cia <a href="#class-keyobject"><code>KeyObject</code></a> of type <code>secret</code>. If the cipher does not need 34571cb0ef41Sopenharmony_cian initialization vector, <code>iv</code> may be <code>null</code>.</p> 34581cb0ef41Sopenharmony_ci<p>When passing strings for <code>key</code> or <code>iv</code>, please consider 34591cb0ef41Sopenharmony_ci<a href="#using-strings-as-inputs-to-cryptographic-apis">caveats when using strings as inputs to cryptographic APIs</a>.</p> 34601cb0ef41Sopenharmony_ci<p>Initialization vectors should be unpredictable and unique; ideally, they will be 34611cb0ef41Sopenharmony_cicryptographically random. They do not have to be secret: IVs are typically just 34621cb0ef41Sopenharmony_ciadded to ciphertext messages unencrypted. It may sound contradictory that 34631cb0ef41Sopenharmony_cisomething has to be unpredictable and unique, but does not have to be secret; 34641cb0ef41Sopenharmony_ciremember that an attacker must not be able to predict ahead of time what a given 34651cb0ef41Sopenharmony_ciIV will be.</p> 34661cb0ef41Sopenharmony_ci<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> 34671cb0ef41Sopenharmony_ci<div class="api_metadata"> 34681cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 34691cb0ef41Sopenharmony_ci<table> 34701cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 34711cb0ef41Sopenharmony_ci<tr><td>v8.0.0</td> 34721cb0ef41Sopenharmony_ci<td><p>The <code>prime</code> argument can be any <code>TypedArray</code> or <code>DataView</code> now.</p></td></tr> 34731cb0ef41Sopenharmony_ci<tr><td>v8.0.0</td> 34741cb0ef41Sopenharmony_ci<td><p>The <code>prime</code> argument can be a <code>Uint8Array</code> now.</p></td></tr> 34751cb0ef41Sopenharmony_ci<tr><td>v6.0.0</td> 34761cb0ef41Sopenharmony_ci<td><p>The default for the encoding parameters changed from <code>binary</code> to <code>utf8</code>.</p></td></tr> 34771cb0ef41Sopenharmony_ci<tr><td>v0.11.12</td> 34781cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.11.12</span></p></td></tr> 34791cb0ef41Sopenharmony_ci</tbody></table> 34801cb0ef41Sopenharmony_ci</details> 34811cb0ef41Sopenharmony_ci</div> 34821cb0ef41Sopenharmony_ci<ul> 34831cb0ef41Sopenharmony_ci<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> 34841cb0ef41Sopenharmony_ci<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> 34851cb0ef41Sopenharmony_ci<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> 34861cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>2</code></li> 34871cb0ef41Sopenharmony_ci<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> 34881cb0ef41Sopenharmony_ci<li>Returns: <a href="crypto.html#class-diffiehellman" class="type"><DiffieHellman></a></li> 34891cb0ef41Sopenharmony_ci</ul> 34901cb0ef41Sopenharmony_ci<p>Creates a <code>DiffieHellman</code> key exchange object using the supplied <code>prime</code> and an 34911cb0ef41Sopenharmony_cioptional specific <code>generator</code>.</p> 34921cb0ef41Sopenharmony_ci<p>The <code>generator</code> argument can be a number, string, or <a href="buffer.html"><code>Buffer</code></a>. If 34931cb0ef41Sopenharmony_ci<code>generator</code> is not specified, the value <code>2</code> is used.</p> 34941cb0ef41Sopenharmony_ci<p>If <code>primeEncoding</code> is specified, <code>prime</code> is expected to be a string; otherwise 34951cb0ef41Sopenharmony_cia <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or <code>DataView</code> is expected.</p> 34961cb0ef41Sopenharmony_ci<p>If <code>generatorEncoding</code> is specified, <code>generator</code> is expected to be a string; 34971cb0ef41Sopenharmony_ciotherwise a number, <a href="buffer.html"><code>Buffer</code></a>, <code>TypedArray</code>, or <code>DataView</code> is expected.</p> 34981cb0ef41Sopenharmony_ci<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> 34991cb0ef41Sopenharmony_ci<div class="api_metadata"> 35001cb0ef41Sopenharmony_ci<span>Added in: v0.5.0</span> 35011cb0ef41Sopenharmony_ci</div> 35021cb0ef41Sopenharmony_ci<ul> 35031cb0ef41Sopenharmony_ci<li><code>primeLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 35041cb0ef41Sopenharmony_ci<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> 35051cb0ef41Sopenharmony_ci<li>Returns: <a href="crypto.html#class-diffiehellman" class="type"><DiffieHellman></a></li> 35061cb0ef41Sopenharmony_ci</ul> 35071cb0ef41Sopenharmony_ci<p>Creates a <code>DiffieHellman</code> key exchange object and generates a prime of 35081cb0ef41Sopenharmony_ci<code>primeLength</code> bits using an optional specific numeric <code>generator</code>. 35091cb0ef41Sopenharmony_ciIf <code>generator</code> is not specified, the value <code>2</code> is used.</p> 35101cb0ef41Sopenharmony_ci<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> 35111cb0ef41Sopenharmony_ci<div class="api_metadata"> 35121cb0ef41Sopenharmony_ci<span>Added in: v0.9.3</span> 35131cb0ef41Sopenharmony_ci</div> 35141cb0ef41Sopenharmony_ci<ul> 35151cb0ef41Sopenharmony_ci<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 35161cb0ef41Sopenharmony_ci<li>Returns: <a href="crypto.html#class-diffiehellmangroup" class="type"><DiffieHellmanGroup></a></li> 35171cb0ef41Sopenharmony_ci</ul> 35181cb0ef41Sopenharmony_ci<p>An alias for <a href="#cryptogetdiffiehellmangroupname"><code>crypto.getDiffieHellman()</code></a></p> 35191cb0ef41Sopenharmony_ci<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> 35201cb0ef41Sopenharmony_ci<div class="api_metadata"> 35211cb0ef41Sopenharmony_ci<span>Added in: v0.11.14</span> 35221cb0ef41Sopenharmony_ci</div> 35231cb0ef41Sopenharmony_ci<ul> 35241cb0ef41Sopenharmony_ci<li><code>curveName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 35251cb0ef41Sopenharmony_ci<li>Returns: <a href="crypto.html#class-ecdh" class="type"><ECDH></a></li> 35261cb0ef41Sopenharmony_ci</ul> 35271cb0ef41Sopenharmony_ci<p>Creates an Elliptic Curve Diffie-Hellman (<code>ECDH</code>) key exchange object using a 35281cb0ef41Sopenharmony_cipredefined curve specified by the <code>curveName</code> string. Use 35291cb0ef41Sopenharmony_ci<a href="#cryptogetcurves"><code>crypto.getCurves()</code></a> to obtain a list of available curve names. On recent 35301cb0ef41Sopenharmony_ciOpenSSL releases, <code>openssl ecparam -list_curves</code> will also display the name 35311cb0ef41Sopenharmony_ciand description of each available elliptic curve.</p> 35321cb0ef41Sopenharmony_ci<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> 35331cb0ef41Sopenharmony_ci<div class="api_metadata"> 35341cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 35351cb0ef41Sopenharmony_ci<table> 35361cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 35371cb0ef41Sopenharmony_ci<tr><td>v12.8.0</td> 35381cb0ef41Sopenharmony_ci<td><p>The <code>outputLength</code> option was added for XOF hash functions.</p></td></tr> 35391cb0ef41Sopenharmony_ci<tr><td>v0.1.92</td> 35401cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.92</span></p></td></tr> 35411cb0ef41Sopenharmony_ci</tbody></table> 35421cb0ef41Sopenharmony_ci</details> 35431cb0ef41Sopenharmony_ci</div> 35441cb0ef41Sopenharmony_ci<ul> 35451cb0ef41Sopenharmony_ci<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 35461cb0ef41Sopenharmony_ci<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> 35471cb0ef41Sopenharmony_ci<li>Returns: <a href="crypto.html#class-hash" class="type"><Hash></a></li> 35481cb0ef41Sopenharmony_ci</ul> 35491cb0ef41Sopenharmony_ci<p>Creates and returns a <code>Hash</code> object that can be used to generate hash digests 35501cb0ef41Sopenharmony_ciusing the given <code>algorithm</code>. Optional <code>options</code> argument controls stream 35511cb0ef41Sopenharmony_cibehavior. For XOF hash functions such as <code>'shake256'</code>, the <code>outputLength</code> option 35521cb0ef41Sopenharmony_cican be used to specify the desired output length in bytes.</p> 35531cb0ef41Sopenharmony_ci<p>The <code>algorithm</code> is dependent on the available algorithms supported by the 35541cb0ef41Sopenharmony_civersion of OpenSSL on the platform. Examples are <code>'sha256'</code>, <code>'sha512'</code>, etc. 35551cb0ef41Sopenharmony_ciOn recent releases of OpenSSL, <code>openssl list -digest-algorithms</code> will 35561cb0ef41Sopenharmony_cidisplay the available digest algorithms.</p> 35571cb0ef41Sopenharmony_ci<p>Example: generating the sha256 sum of a file</p> 35581cb0ef41Sopenharmony_ci 35591cb0ef41Sopenharmony_ci<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> { 35601cb0ef41Sopenharmony_ci createReadStream, 35611cb0ef41Sopenharmony_ci} <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 35621cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { argv } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>; 35631cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 35641cb0ef41Sopenharmony_ci createHash, 35651cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 35661cb0ef41Sopenharmony_ci 35671cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> filename = argv[<span class="hljs-number">2</span>]; 35681cb0ef41Sopenharmony_ci 35691cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> hash = <span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>); 35701cb0ef41Sopenharmony_ci 35711cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> input = <span class="hljs-title function_">createReadStream</span>(filename); 35721cb0ef41Sopenharmony_ciinput.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> { 35731cb0ef41Sopenharmony_ci <span class="hljs-comment">// Only one element is going to be produced by the</span> 35741cb0ef41Sopenharmony_ci <span class="hljs-comment">// hash stream.</span> 35751cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> data = input.<span class="hljs-title function_">read</span>(); 35761cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (data) 35771cb0ef41Sopenharmony_ci hash.<span class="hljs-title function_">update</span>(data); 35781cb0ef41Sopenharmony_ci <span class="hljs-keyword">else</span> { 35791cb0ef41Sopenharmony_ci <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>); 35801cb0ef41Sopenharmony_ci } 35811cb0ef41Sopenharmony_ci});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 35821cb0ef41Sopenharmony_ci createReadStream, 35831cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 35841cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 35851cb0ef41Sopenharmony_ci createHash, 35861cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 35871cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { argv } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:process'</span>); 35881cb0ef41Sopenharmony_ci 35891cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> filename = argv[<span class="hljs-number">2</span>]; 35901cb0ef41Sopenharmony_ci 35911cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> hash = <span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>); 35921cb0ef41Sopenharmony_ci 35931cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> input = <span class="hljs-title function_">createReadStream</span>(filename); 35941cb0ef41Sopenharmony_ciinput.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> { 35951cb0ef41Sopenharmony_ci <span class="hljs-comment">// Only one element is going to be produced by the</span> 35961cb0ef41Sopenharmony_ci <span class="hljs-comment">// hash stream.</span> 35971cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> data = input.<span class="hljs-title function_">read</span>(); 35981cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (data) 35991cb0ef41Sopenharmony_ci hash.<span class="hljs-title function_">update</span>(data); 36001cb0ef41Sopenharmony_ci <span class="hljs-keyword">else</span> { 36011cb0ef41Sopenharmony_ci <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>); 36021cb0ef41Sopenharmony_ci } 36031cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 36041cb0ef41Sopenharmony_ci<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> 36051cb0ef41Sopenharmony_ci<div class="api_metadata"> 36061cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 36071cb0ef41Sopenharmony_ci<table> 36081cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 36091cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 36101cb0ef41Sopenharmony_ci<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> 36111cb0ef41Sopenharmony_ci<tr><td>v11.6.0</td> 36121cb0ef41Sopenharmony_ci<td><p>The <code>key</code> argument can now be a <code>KeyObject</code>.</p></td></tr> 36131cb0ef41Sopenharmony_ci<tr><td>v0.1.94</td> 36141cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.94</span></p></td></tr> 36151cb0ef41Sopenharmony_ci</tbody></table> 36161cb0ef41Sopenharmony_ci</details> 36171cb0ef41Sopenharmony_ci</div> 36181cb0ef41Sopenharmony_ci<ul> 36191cb0ef41Sopenharmony_ci<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 36201cb0ef41Sopenharmony_ci<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> 36211cb0ef41Sopenharmony_ci<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> 36221cb0ef41Sopenharmony_ci<ul> 36231cb0ef41Sopenharmony_ci<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> 36241cb0ef41Sopenharmony_ci</ul> 36251cb0ef41Sopenharmony_ci</li> 36261cb0ef41Sopenharmony_ci<li>Returns: <a href="crypto.html#class-hmac" class="type"><Hmac></a></li> 36271cb0ef41Sopenharmony_ci</ul> 36281cb0ef41Sopenharmony_ci<p>Creates and returns an <code>Hmac</code> object that uses the given <code>algorithm</code> and <code>key</code>. 36291cb0ef41Sopenharmony_ciOptional <code>options</code> argument controls stream behavior.</p> 36301cb0ef41Sopenharmony_ci<p>The <code>algorithm</code> is dependent on the available algorithms supported by the 36311cb0ef41Sopenharmony_civersion of OpenSSL on the platform. Examples are <code>'sha256'</code>, <code>'sha512'</code>, etc. 36321cb0ef41Sopenharmony_ciOn recent releases of OpenSSL, <code>openssl list -digest-algorithms</code> will 36331cb0ef41Sopenharmony_cidisplay the available digest algorithms.</p> 36341cb0ef41Sopenharmony_ci<p>The <code>key</code> is the HMAC key used to generate the cryptographic HMAC hash. If it is 36351cb0ef41Sopenharmony_cia <a href="#class-keyobject"><code>KeyObject</code></a>, its type must be <code>secret</code>. If it is a string, please consider 36361cb0ef41Sopenharmony_ci<a href="#using-strings-as-inputs-to-cryptographic-apis">caveats when using strings as inputs to cryptographic APIs</a>. If it was 36371cb0ef41Sopenharmony_ciobtained from a cryptographically secure source of entropy, such as 36381cb0ef41Sopenharmony_ci<a href="#cryptorandombytessize-callback"><code>crypto.randomBytes()</code></a> or <a href="#cryptogeneratekeytype-options-callback"><code>crypto.generateKey()</code></a>, its length should not 36391cb0ef41Sopenharmony_ciexceed the block size of <code>algorithm</code> (e.g., 512 bits for SHA-256).</p> 36401cb0ef41Sopenharmony_ci<p>Example: generating the sha256 HMAC of a file</p> 36411cb0ef41Sopenharmony_ci 36421cb0ef41Sopenharmony_ci<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> { 36431cb0ef41Sopenharmony_ci createReadStream, 36441cb0ef41Sopenharmony_ci} <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>; 36451cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { argv } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>; 36461cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 36471cb0ef41Sopenharmony_ci createHmac, 36481cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 36491cb0ef41Sopenharmony_ci 36501cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> filename = argv[<span class="hljs-number">2</span>]; 36511cb0ef41Sopenharmony_ci 36521cb0ef41Sopenharmony_ci<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>); 36531cb0ef41Sopenharmony_ci 36541cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> input = <span class="hljs-title function_">createReadStream</span>(filename); 36551cb0ef41Sopenharmony_ciinput.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> { 36561cb0ef41Sopenharmony_ci <span class="hljs-comment">// Only one element is going to be produced by the</span> 36571cb0ef41Sopenharmony_ci <span class="hljs-comment">// hash stream.</span> 36581cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> data = input.<span class="hljs-title function_">read</span>(); 36591cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (data) 36601cb0ef41Sopenharmony_ci hmac.<span class="hljs-title function_">update</span>(data); 36611cb0ef41Sopenharmony_ci <span class="hljs-keyword">else</span> { 36621cb0ef41Sopenharmony_ci <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>); 36631cb0ef41Sopenharmony_ci } 36641cb0ef41Sopenharmony_ci});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 36651cb0ef41Sopenharmony_ci createReadStream, 36661cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 36671cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 36681cb0ef41Sopenharmony_ci createHmac, 36691cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 36701cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { argv } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:process'</span>); 36711cb0ef41Sopenharmony_ci 36721cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> filename = argv[<span class="hljs-number">2</span>]; 36731cb0ef41Sopenharmony_ci 36741cb0ef41Sopenharmony_ci<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>); 36751cb0ef41Sopenharmony_ci 36761cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> input = <span class="hljs-title function_">createReadStream</span>(filename); 36771cb0ef41Sopenharmony_ciinput.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> { 36781cb0ef41Sopenharmony_ci <span class="hljs-comment">// Only one element is going to be produced by the</span> 36791cb0ef41Sopenharmony_ci <span class="hljs-comment">// hash stream.</span> 36801cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> data = input.<span class="hljs-title function_">read</span>(); 36811cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (data) 36821cb0ef41Sopenharmony_ci hmac.<span class="hljs-title function_">update</span>(data); 36831cb0ef41Sopenharmony_ci <span class="hljs-keyword">else</span> { 36841cb0ef41Sopenharmony_ci <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>); 36851cb0ef41Sopenharmony_ci } 36861cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 36871cb0ef41Sopenharmony_ci<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> 36881cb0ef41Sopenharmony_ci<div class="api_metadata"> 36891cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 36901cb0ef41Sopenharmony_ci<table> 36911cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 36921cb0ef41Sopenharmony_ci<tr><td>v15.12.0</td> 36931cb0ef41Sopenharmony_ci<td><p>The key can also be a JWK object.</p></td></tr> 36941cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 36951cb0ef41Sopenharmony_ci<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> 36961cb0ef41Sopenharmony_ci<tr><td>v11.6.0</td> 36971cb0ef41Sopenharmony_ci<td><p><span>Added in: v11.6.0</span></p></td></tr> 36981cb0ef41Sopenharmony_ci</tbody></table> 36991cb0ef41Sopenharmony_ci</details> 37001cb0ef41Sopenharmony_ci</div> 37011cb0ef41Sopenharmony_ci<!--lint disable maximum-line-length remark-lint--> 37021cb0ef41Sopenharmony_ci<ul> 37031cb0ef41Sopenharmony_ci<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> 37041cb0ef41Sopenharmony_ci<ul> 37051cb0ef41Sopenharmony_ci<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 37061cb0ef41Sopenharmony_cimaterial, either in PEM, DER, or JWK format.</li> 37071cb0ef41Sopenharmony_ci<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>. 37081cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>'pem'</code>.</li> 37091cb0ef41Sopenharmony_ci<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 37101cb0ef41Sopenharmony_cirequired only if the <code>format</code> is <code>'der'</code> and ignored otherwise.</li> 37111cb0ef41Sopenharmony_ci<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> 37121cb0ef41Sopenharmony_ci<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> 37131cb0ef41Sopenharmony_ci</ul> 37141cb0ef41Sopenharmony_ci</li> 37151cb0ef41Sopenharmony_ci<li>Returns: <a href="crypto.html#class-keyobject" class="type"><KeyObject></a></li> 37161cb0ef41Sopenharmony_ci</ul> 37171cb0ef41Sopenharmony_ci<!--lint enable maximum-line-length remark-lint--> 37181cb0ef41Sopenharmony_ci<p>Creates and returns a new key object containing a private key. If <code>key</code> is a 37191cb0ef41Sopenharmony_cistring or <code>Buffer</code>, <code>format</code> is assumed to be <code>'pem'</code>; otherwise, <code>key</code> 37201cb0ef41Sopenharmony_cimust be an object with the properties described above.</p> 37211cb0ef41Sopenharmony_ci<p>If the private key is encrypted, a <code>passphrase</code> must be specified. The length 37221cb0ef41Sopenharmony_ciof the passphrase is limited to 1024 bytes.</p> 37231cb0ef41Sopenharmony_ci<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> 37241cb0ef41Sopenharmony_ci<div class="api_metadata"> 37251cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 37261cb0ef41Sopenharmony_ci<table> 37271cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 37281cb0ef41Sopenharmony_ci<tr><td>v15.12.0</td> 37291cb0ef41Sopenharmony_ci<td><p>The key can also be a JWK object.</p></td></tr> 37301cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 37311cb0ef41Sopenharmony_ci<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> 37321cb0ef41Sopenharmony_ci<tr><td>v11.13.0</td> 37331cb0ef41Sopenharmony_ci<td><p>The <code>key</code> argument can now be a <code>KeyObject</code> with type <code>private</code>.</p></td></tr> 37341cb0ef41Sopenharmony_ci<tr><td>v11.7.0</td> 37351cb0ef41Sopenharmony_ci<td><p>The <code>key</code> argument can now be a private key.</p></td></tr> 37361cb0ef41Sopenharmony_ci<tr><td>v11.6.0</td> 37371cb0ef41Sopenharmony_ci<td><p><span>Added in: v11.6.0</span></p></td></tr> 37381cb0ef41Sopenharmony_ci</tbody></table> 37391cb0ef41Sopenharmony_ci</details> 37401cb0ef41Sopenharmony_ci</div> 37411cb0ef41Sopenharmony_ci<!--lint disable maximum-line-length remark-lint--> 37421cb0ef41Sopenharmony_ci<ul> 37431cb0ef41Sopenharmony_ci<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> 37441cb0ef41Sopenharmony_ci<ul> 37451cb0ef41Sopenharmony_ci<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 37461cb0ef41Sopenharmony_cimaterial, either in PEM, DER, or JWK format.</li> 37471cb0ef41Sopenharmony_ci<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>. 37481cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>'pem'</code>.</li> 37491cb0ef41Sopenharmony_ci<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 37501cb0ef41Sopenharmony_cirequired only if the <code>format</code> is <code>'der'</code> and ignored otherwise.</li> 37511cb0ef41Sopenharmony_ci<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> 37521cb0ef41Sopenharmony_ci</ul> 37531cb0ef41Sopenharmony_ci</li> 37541cb0ef41Sopenharmony_ci<li>Returns: <a href="crypto.html#class-keyobject" class="type"><KeyObject></a></li> 37551cb0ef41Sopenharmony_ci</ul> 37561cb0ef41Sopenharmony_ci<!--lint enable maximum-line-length remark-lint--> 37571cb0ef41Sopenharmony_ci<p>Creates and returns a new key object containing a public key. If <code>key</code> is a 37581cb0ef41Sopenharmony_cistring or <code>Buffer</code>, <code>format</code> is assumed to be <code>'pem'</code>; if <code>key</code> is a <code>KeyObject</code> 37591cb0ef41Sopenharmony_ciwith type <code>'private'</code>, the public key is derived from the given private key; 37601cb0ef41Sopenharmony_ciotherwise, <code>key</code> must be an object with the properties described above.</p> 37611cb0ef41Sopenharmony_ci<p>If the format is <code>'pem'</code>, the <code>'key'</code> may also be an X.509 certificate.</p> 37621cb0ef41Sopenharmony_ci<p>Because public keys can be derived from private keys, a private key may be 37631cb0ef41Sopenharmony_cipassed instead of a public key. In that case, this function behaves as if 37641cb0ef41Sopenharmony_ci<a href="#cryptocreateprivatekeykey"><code>crypto.createPrivateKey()</code></a> had been called, except that the type of the 37651cb0ef41Sopenharmony_cireturned <code>KeyObject</code> will be <code>'public'</code> and that the private key cannot be 37661cb0ef41Sopenharmony_ciextracted from the returned <code>KeyObject</code>. Similarly, if a <code>KeyObject</code> with type 37671cb0ef41Sopenharmony_ci<code>'private'</code> is given, a new <code>KeyObject</code> with type <code>'public'</code> will be returned 37681cb0ef41Sopenharmony_ciand it will be impossible to extract the private key from the returned object.</p> 37691cb0ef41Sopenharmony_ci<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> 37701cb0ef41Sopenharmony_ci<div class="api_metadata"> 37711cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 37721cb0ef41Sopenharmony_ci<table> 37731cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 37741cb0ef41Sopenharmony_ci<tr><td>v18.8.0</td> 37751cb0ef41Sopenharmony_ci<td><p>The key can now be zero-length.</p></td></tr> 37761cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 37771cb0ef41Sopenharmony_ci<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> 37781cb0ef41Sopenharmony_ci<tr><td>v11.6.0</td> 37791cb0ef41Sopenharmony_ci<td><p><span>Added in: v11.6.0</span></p></td></tr> 37801cb0ef41Sopenharmony_ci</tbody></table> 37811cb0ef41Sopenharmony_ci</details> 37821cb0ef41Sopenharmony_ci</div> 37831cb0ef41Sopenharmony_ci<ul> 37841cb0ef41Sopenharmony_ci<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> 37851cb0ef41Sopenharmony_ci<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> 37861cb0ef41Sopenharmony_ci<li>Returns: <a href="crypto.html#class-keyobject" class="type"><KeyObject></a></li> 37871cb0ef41Sopenharmony_ci</ul> 37881cb0ef41Sopenharmony_ci<p>Creates and returns a new key object containing a secret key for symmetric 37891cb0ef41Sopenharmony_ciencryption or <code>Hmac</code>.</p> 37901cb0ef41Sopenharmony_ci<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> 37911cb0ef41Sopenharmony_ci<div class="api_metadata"> 37921cb0ef41Sopenharmony_ci<span>Added in: v0.1.92</span> 37931cb0ef41Sopenharmony_ci</div> 37941cb0ef41Sopenharmony_ci<ul> 37951cb0ef41Sopenharmony_ci<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 37961cb0ef41Sopenharmony_ci<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> 37971cb0ef41Sopenharmony_ci<li>Returns: <a href="crypto.html#class-sign" class="type"><Sign></a></li> 37981cb0ef41Sopenharmony_ci</ul> 37991cb0ef41Sopenharmony_ci<p>Creates and returns a <code>Sign</code> object that uses the given <code>algorithm</code>. Use 38001cb0ef41Sopenharmony_ci<a href="#cryptogethashes"><code>crypto.getHashes()</code></a> to obtain the names of the available digest algorithms. 38011cb0ef41Sopenharmony_ciOptional <code>options</code> argument controls the <code>stream.Writable</code> behavior.</p> 38021cb0ef41Sopenharmony_ci<p>In some cases, a <code>Sign</code> instance can be created using the name of a signature 38031cb0ef41Sopenharmony_cialgorithm, such as <code>'RSA-SHA256'</code>, instead of a digest algorithm. This will use 38041cb0ef41Sopenharmony_cithe corresponding digest algorithm. This does not work for all signature 38051cb0ef41Sopenharmony_cialgorithms, such as <code>'ecdsa-with-SHA256'</code>, so it is best to always use digest 38061cb0ef41Sopenharmony_cialgorithm names.</p> 38071cb0ef41Sopenharmony_ci<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> 38081cb0ef41Sopenharmony_ci<div class="api_metadata"> 38091cb0ef41Sopenharmony_ci<span>Added in: v0.1.92</span> 38101cb0ef41Sopenharmony_ci</div> 38111cb0ef41Sopenharmony_ci<ul> 38121cb0ef41Sopenharmony_ci<li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 38131cb0ef41Sopenharmony_ci<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> 38141cb0ef41Sopenharmony_ci<li>Returns: <a href="crypto.html#class-verify" class="type"><Verify></a></li> 38151cb0ef41Sopenharmony_ci</ul> 38161cb0ef41Sopenharmony_ci<p>Creates and returns a <code>Verify</code> object that uses the given algorithm. 38171cb0ef41Sopenharmony_ciUse <a href="#cryptogethashes"><code>crypto.getHashes()</code></a> to obtain an array of names of the available 38181cb0ef41Sopenharmony_cisigning algorithms. Optional <code>options</code> argument controls the 38191cb0ef41Sopenharmony_ci<code>stream.Writable</code> behavior.</p> 38201cb0ef41Sopenharmony_ci<p>In some cases, a <code>Verify</code> instance can be created using the name of a signature 38211cb0ef41Sopenharmony_cialgorithm, such as <code>'RSA-SHA256'</code>, instead of a digest algorithm. This will use 38221cb0ef41Sopenharmony_cithe corresponding digest algorithm. This does not work for all signature 38231cb0ef41Sopenharmony_cialgorithms, such as <code>'ecdsa-with-SHA256'</code>, so it is best to always use digest 38241cb0ef41Sopenharmony_cialgorithm names.</p> 38251cb0ef41Sopenharmony_ci<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> 38261cb0ef41Sopenharmony_ci<div class="api_metadata"> 38271cb0ef41Sopenharmony_ci<span>Added in: v13.9.0, v12.17.0</span> 38281cb0ef41Sopenharmony_ci</div> 38291cb0ef41Sopenharmony_ci<ul> 38301cb0ef41Sopenharmony_ci<li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 38311cb0ef41Sopenharmony_ci<ul> 38321cb0ef41Sopenharmony_ci<li><code>privateKey</code>: <a href="crypto.html#class-keyobject" class="type"><KeyObject></a></li> 38331cb0ef41Sopenharmony_ci<li><code>publicKey</code>: <a href="crypto.html#class-keyobject" class="type"><KeyObject></a></li> 38341cb0ef41Sopenharmony_ci</ul> 38351cb0ef41Sopenharmony_ci</li> 38361cb0ef41Sopenharmony_ci<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 38371cb0ef41Sopenharmony_ci</ul> 38381cb0ef41Sopenharmony_ci<p>Computes the Diffie-Hellman secret based on a <code>privateKey</code> and a <code>publicKey</code>. 38391cb0ef41Sopenharmony_ciBoth keys must have the same <code>asymmetricKeyType</code>, which must be one of <code>'dh'</code> 38401cb0ef41Sopenharmony_ci(for Diffie-Hellman), <code>'ec'</code> (for ECDH), <code>'x448'</code>, or <code>'x25519'</code> (for ECDH-ES).</p> 38411cb0ef41Sopenharmony_ci<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> 38421cb0ef41Sopenharmony_ci<div class="api_metadata"> 38431cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 38441cb0ef41Sopenharmony_ci<table> 38451cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 38461cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td> 38471cb0ef41Sopenharmony_ci<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> 38481cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 38491cb0ef41Sopenharmony_ci<td><p><span>Added in: v15.0.0</span></p></td></tr> 38501cb0ef41Sopenharmony_ci</tbody></table> 38511cb0ef41Sopenharmony_ci</details> 38521cb0ef41Sopenharmony_ci</div> 38531cb0ef41Sopenharmony_ci<ul> 38541cb0ef41Sopenharmony_ci<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 38551cb0ef41Sopenharmony_ciaccepted values are <code>'hmac'</code> and <code>'aes'</code>.</li> 38561cb0ef41Sopenharmony_ci<li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 38571cb0ef41Sopenharmony_ci<ul> 38581cb0ef41Sopenharmony_ci<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 38591cb0ef41Sopenharmony_civalue greater than 0. 38601cb0ef41Sopenharmony_ci<ul> 38611cb0ef41Sopenharmony_ci<li>If <code>type</code> is <code>'hmac'</code>, the minimum is 8, and the maximum length is 38621cb0ef41Sopenharmony_ci2<sup>31</sup>-1. If the value is not a multiple of 8, the generated 38631cb0ef41Sopenharmony_cikey will be truncated to <code>Math.floor(length / 8)</code>.</li> 38641cb0ef41Sopenharmony_ci<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> 38651cb0ef41Sopenharmony_ci</ul> 38661cb0ef41Sopenharmony_ci</li> 38671cb0ef41Sopenharmony_ci</ul> 38681cb0ef41Sopenharmony_ci</li> 38691cb0ef41Sopenharmony_ci<li><code>callback</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 38701cb0ef41Sopenharmony_ci<ul> 38711cb0ef41Sopenharmony_ci<li><code>err</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 38721cb0ef41Sopenharmony_ci<li><code>key</code>: <a href="crypto.html#class-keyobject" class="type"><KeyObject></a></li> 38731cb0ef41Sopenharmony_ci</ul> 38741cb0ef41Sopenharmony_ci</li> 38751cb0ef41Sopenharmony_ci</ul> 38761cb0ef41Sopenharmony_ci<p>Asynchronously generates a new random secret key of the given <code>length</code>. The 38771cb0ef41Sopenharmony_ci<code>type</code> will determine which validations will be performed on the <code>length</code>.</p> 38781cb0ef41Sopenharmony_ci 38791cb0ef41Sopenharmony_ci<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> { 38801cb0ef41Sopenharmony_ci generateKey, 38811cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 38821cb0ef41Sopenharmony_ci 38831cb0ef41Sopenharmony_ci<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> { 38841cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 38851cb0ef41Sopenharmony_ci <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> 38861cb0ef41Sopenharmony_ci});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 38871cb0ef41Sopenharmony_ci generateKey, 38881cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 38891cb0ef41Sopenharmony_ci 38901cb0ef41Sopenharmony_ci<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> { 38911cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 38921cb0ef41Sopenharmony_ci <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> 38931cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 38941cb0ef41Sopenharmony_ci<p>The size of a generated HMAC key should not exceed the block size of the 38951cb0ef41Sopenharmony_ciunderlying hash function. See <a href="#cryptocreatehmacalgorithm-key-options"><code>crypto.createHmac()</code></a> for more information.</p> 38961cb0ef41Sopenharmony_ci<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> 38971cb0ef41Sopenharmony_ci<div class="api_metadata"> 38981cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 38991cb0ef41Sopenharmony_ci<table> 39001cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 39011cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td> 39021cb0ef41Sopenharmony_ci<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> 39031cb0ef41Sopenharmony_ci<tr><td>v16.10.0</td> 39041cb0ef41Sopenharmony_ci<td><p>Add ability to define <code>RSASSA-PSS-params</code> sequence parameters for RSA-PSS keys pairs.</p></td></tr> 39051cb0ef41Sopenharmony_ci<tr><td>v13.9.0, v12.17.0</td> 39061cb0ef41Sopenharmony_ci<td><p>Add support for Diffie-Hellman.</p></td></tr> 39071cb0ef41Sopenharmony_ci<tr><td>v12.0.0</td> 39081cb0ef41Sopenharmony_ci<td><p>Add support for RSA-PSS key pairs.</p></td></tr> 39091cb0ef41Sopenharmony_ci<tr><td>v12.0.0</td> 39101cb0ef41Sopenharmony_ci<td><p>Add ability to generate X25519 and X448 key pairs.</p></td></tr> 39111cb0ef41Sopenharmony_ci<tr><td>v12.0.0</td> 39121cb0ef41Sopenharmony_ci<td><p>Add ability to generate Ed25519 and Ed448 key pairs.</p></td></tr> 39131cb0ef41Sopenharmony_ci<tr><td>v11.6.0</td> 39141cb0ef41Sopenharmony_ci<td><p>The <code>generateKeyPair</code> and <code>generateKeyPairSync</code> functions now produce key objects if no encoding was specified.</p></td></tr> 39151cb0ef41Sopenharmony_ci<tr><td>v10.12.0</td> 39161cb0ef41Sopenharmony_ci<td><p><span>Added in: v10.12.0</span></p></td></tr> 39171cb0ef41Sopenharmony_ci</tbody></table> 39181cb0ef41Sopenharmony_ci</details> 39191cb0ef41Sopenharmony_ci</div> 39201cb0ef41Sopenharmony_ci<ul> 39211cb0ef41Sopenharmony_ci<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>, 39221cb0ef41Sopenharmony_ci<code>'ed448'</code>, <code>'x25519'</code>, <code>'x448'</code>, or <code>'dh'</code>.</li> 39231cb0ef41Sopenharmony_ci<li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 39241cb0ef41Sopenharmony_ci<ul> 39251cb0ef41Sopenharmony_ci<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> 39261cb0ef41Sopenharmony_ci<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> 39271cb0ef41Sopenharmony_ci<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> 39281cb0ef41Sopenharmony_ci<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 39291cb0ef41Sopenharmony_ciMGF1 (RSA-PSS).</li> 39301cb0ef41Sopenharmony_ci<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> 39311cb0ef41Sopenharmony_ci<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> 39321cb0ef41Sopenharmony_ci<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> 39331cb0ef41Sopenharmony_ci<li><code>prime</code>: <a href="buffer.html#class-buffer" class="type"><Buffer></a> The prime parameter (DH).</li> 39341cb0ef41Sopenharmony_ci<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> 39351cb0ef41Sopenharmony_ci<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> 39361cb0ef41Sopenharmony_ci<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 39371cb0ef41Sopenharmony_ci<a href="#cryptogetdiffiehellmangroupname"><code>crypto.getDiffieHellman()</code></a>.</li> 39381cb0ef41Sopenharmony_ci<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). 39391cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>'named'</code>.</li> 39401cb0ef41Sopenharmony_ci<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> 39411cb0ef41Sopenharmony_ci<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> 39421cb0ef41Sopenharmony_ci</ul> 39431cb0ef41Sopenharmony_ci</li> 39441cb0ef41Sopenharmony_ci<li><code>callback</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 39451cb0ef41Sopenharmony_ci<ul> 39461cb0ef41Sopenharmony_ci<li><code>err</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 39471cb0ef41Sopenharmony_ci<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> 39481cb0ef41Sopenharmony_ci<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> 39491cb0ef41Sopenharmony_ci</ul> 39501cb0ef41Sopenharmony_ci</li> 39511cb0ef41Sopenharmony_ci</ul> 39521cb0ef41Sopenharmony_ci<p>Generates a new asymmetric key pair of the given <code>type</code>. RSA, RSA-PSS, DSA, EC, 39531cb0ef41Sopenharmony_ciEd25519, Ed448, X25519, X448, and DH are currently supported.</p> 39541cb0ef41Sopenharmony_ci<p>If a <code>publicKeyEncoding</code> or <code>privateKeyEncoding</code> was specified, this function 39551cb0ef41Sopenharmony_cibehaves as if <a href="#keyobjectexportoptions"><code>keyObject.export()</code></a> had been called on its result. Otherwise, 39561cb0ef41Sopenharmony_cithe respective part of the key is returned as a <a href="#class-keyobject"><code>KeyObject</code></a>.</p> 39571cb0ef41Sopenharmony_ci<p>It is recommended to encode public keys as <code>'spki'</code> and private keys as 39581cb0ef41Sopenharmony_ci<code>'pkcs8'</code> with encryption for long-term storage:</p> 39591cb0ef41Sopenharmony_ci 39601cb0ef41Sopenharmony_ci<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> { 39611cb0ef41Sopenharmony_ci generateKeyPair, 39621cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 39631cb0ef41Sopenharmony_ci 39641cb0ef41Sopenharmony_ci<span class="hljs-title function_">generateKeyPair</span>(<span class="hljs-string">'rsa'</span>, { 39651cb0ef41Sopenharmony_ci <span class="hljs-attr">modulusLength</span>: <span class="hljs-number">4096</span>, 39661cb0ef41Sopenharmony_ci <span class="hljs-attr">publicKeyEncoding</span>: { 39671cb0ef41Sopenharmony_ci <span class="hljs-attr">type</span>: <span class="hljs-string">'spki'</span>, 39681cb0ef41Sopenharmony_ci <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>, 39691cb0ef41Sopenharmony_ci }, 39701cb0ef41Sopenharmony_ci <span class="hljs-attr">privateKeyEncoding</span>: { 39711cb0ef41Sopenharmony_ci <span class="hljs-attr">type</span>: <span class="hljs-string">'pkcs8'</span>, 39721cb0ef41Sopenharmony_ci <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>, 39731cb0ef41Sopenharmony_ci <span class="hljs-attr">cipher</span>: <span class="hljs-string">'aes-256-cbc'</span>, 39741cb0ef41Sopenharmony_ci <span class="hljs-attr">passphrase</span>: <span class="hljs-string">'top secret'</span>, 39751cb0ef41Sopenharmony_ci }, 39761cb0ef41Sopenharmony_ci}, <span class="hljs-function">(<span class="hljs-params">err, publicKey, privateKey</span>) =></span> { 39771cb0ef41Sopenharmony_ci <span class="hljs-comment">// Handle errors and use the generated key pair.</span> 39781cb0ef41Sopenharmony_ci});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 39791cb0ef41Sopenharmony_ci generateKeyPair, 39801cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 39811cb0ef41Sopenharmony_ci 39821cb0ef41Sopenharmony_ci<span class="hljs-title function_">generateKeyPair</span>(<span class="hljs-string">'rsa'</span>, { 39831cb0ef41Sopenharmony_ci <span class="hljs-attr">modulusLength</span>: <span class="hljs-number">4096</span>, 39841cb0ef41Sopenharmony_ci <span class="hljs-attr">publicKeyEncoding</span>: { 39851cb0ef41Sopenharmony_ci <span class="hljs-attr">type</span>: <span class="hljs-string">'spki'</span>, 39861cb0ef41Sopenharmony_ci <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>, 39871cb0ef41Sopenharmony_ci }, 39881cb0ef41Sopenharmony_ci <span class="hljs-attr">privateKeyEncoding</span>: { 39891cb0ef41Sopenharmony_ci <span class="hljs-attr">type</span>: <span class="hljs-string">'pkcs8'</span>, 39901cb0ef41Sopenharmony_ci <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>, 39911cb0ef41Sopenharmony_ci <span class="hljs-attr">cipher</span>: <span class="hljs-string">'aes-256-cbc'</span>, 39921cb0ef41Sopenharmony_ci <span class="hljs-attr">passphrase</span>: <span class="hljs-string">'top secret'</span>, 39931cb0ef41Sopenharmony_ci }, 39941cb0ef41Sopenharmony_ci}, <span class="hljs-function">(<span class="hljs-params">err, publicKey, privateKey</span>) =></span> { 39951cb0ef41Sopenharmony_ci <span class="hljs-comment">// Handle errors and use the generated key pair.</span> 39961cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 39971cb0ef41Sopenharmony_ci<p>On completion, <code>callback</code> will be called with <code>err</code> set to <code>undefined</code> and 39981cb0ef41Sopenharmony_ci<code>publicKey</code> / <code>privateKey</code> representing the generated key pair.</p> 39991cb0ef41Sopenharmony_ci<p>If this method is invoked as its <a href="util.html#utilpromisifyoriginal"><code>util.promisify()</code></a>ed version, it returns 40001cb0ef41Sopenharmony_cia <code>Promise</code> for an <code>Object</code> with <code>publicKey</code> and <code>privateKey</code> properties.</p> 40011cb0ef41Sopenharmony_ci<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> 40021cb0ef41Sopenharmony_ci<div class="api_metadata"> 40031cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 40041cb0ef41Sopenharmony_ci<table> 40051cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 40061cb0ef41Sopenharmony_ci<tr><td>v16.10.0</td> 40071cb0ef41Sopenharmony_ci<td><p>Add ability to define <code>RSASSA-PSS-params</code> sequence parameters for RSA-PSS keys pairs.</p></td></tr> 40081cb0ef41Sopenharmony_ci<tr><td>v13.9.0, v12.17.0</td> 40091cb0ef41Sopenharmony_ci<td><p>Add support for Diffie-Hellman.</p></td></tr> 40101cb0ef41Sopenharmony_ci<tr><td>v12.0.0</td> 40111cb0ef41Sopenharmony_ci<td><p>Add support for RSA-PSS key pairs.</p></td></tr> 40121cb0ef41Sopenharmony_ci<tr><td>v12.0.0</td> 40131cb0ef41Sopenharmony_ci<td><p>Add ability to generate X25519 and X448 key pairs.</p></td></tr> 40141cb0ef41Sopenharmony_ci<tr><td>v12.0.0</td> 40151cb0ef41Sopenharmony_ci<td><p>Add ability to generate Ed25519 and Ed448 key pairs.</p></td></tr> 40161cb0ef41Sopenharmony_ci<tr><td>v11.6.0</td> 40171cb0ef41Sopenharmony_ci<td><p>The <code>generateKeyPair</code> and <code>generateKeyPairSync</code> functions now produce key objects if no encoding was specified.</p></td></tr> 40181cb0ef41Sopenharmony_ci<tr><td>v10.12.0</td> 40191cb0ef41Sopenharmony_ci<td><p><span>Added in: v10.12.0</span></p></td></tr> 40201cb0ef41Sopenharmony_ci</tbody></table> 40211cb0ef41Sopenharmony_ci</details> 40221cb0ef41Sopenharmony_ci</div> 40231cb0ef41Sopenharmony_ci<ul> 40241cb0ef41Sopenharmony_ci<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>, 40251cb0ef41Sopenharmony_ci<code>'ed448'</code>, <code>'x25519'</code>, <code>'x448'</code>, or <code>'dh'</code>.</li> 40261cb0ef41Sopenharmony_ci<li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 40271cb0ef41Sopenharmony_ci<ul> 40281cb0ef41Sopenharmony_ci<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> 40291cb0ef41Sopenharmony_ci<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> 40301cb0ef41Sopenharmony_ci<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> 40311cb0ef41Sopenharmony_ci<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 40321cb0ef41Sopenharmony_ciMGF1 (RSA-PSS).</li> 40331cb0ef41Sopenharmony_ci<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> 40341cb0ef41Sopenharmony_ci<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> 40351cb0ef41Sopenharmony_ci<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> 40361cb0ef41Sopenharmony_ci<li><code>prime</code>: <a href="buffer.html#class-buffer" class="type"><Buffer></a> The prime parameter (DH).</li> 40371cb0ef41Sopenharmony_ci<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> 40381cb0ef41Sopenharmony_ci<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> 40391cb0ef41Sopenharmony_ci<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 40401cb0ef41Sopenharmony_ci<a href="#cryptogetdiffiehellmangroupname"><code>crypto.getDiffieHellman()</code></a>.</li> 40411cb0ef41Sopenharmony_ci<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). 40421cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>'named'</code>.</li> 40431cb0ef41Sopenharmony_ci<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> 40441cb0ef41Sopenharmony_ci<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> 40451cb0ef41Sopenharmony_ci</ul> 40461cb0ef41Sopenharmony_ci</li> 40471cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 40481cb0ef41Sopenharmony_ci<ul> 40491cb0ef41Sopenharmony_ci<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> 40501cb0ef41Sopenharmony_ci<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> 40511cb0ef41Sopenharmony_ci</ul> 40521cb0ef41Sopenharmony_ci</li> 40531cb0ef41Sopenharmony_ci</ul> 40541cb0ef41Sopenharmony_ci<p>Generates a new asymmetric key pair of the given <code>type</code>. RSA, RSA-PSS, DSA, EC, 40551cb0ef41Sopenharmony_ciEd25519, Ed448, X25519, X448, and DH are currently supported.</p> 40561cb0ef41Sopenharmony_ci<p>If a <code>publicKeyEncoding</code> or <code>privateKeyEncoding</code> was specified, this function 40571cb0ef41Sopenharmony_cibehaves as if <a href="#keyobjectexportoptions"><code>keyObject.export()</code></a> had been called on its result. Otherwise, 40581cb0ef41Sopenharmony_cithe respective part of the key is returned as a <a href="#class-keyobject"><code>KeyObject</code></a>.</p> 40591cb0ef41Sopenharmony_ci<p>When encoding public keys, it is recommended to use <code>'spki'</code>. When encoding 40601cb0ef41Sopenharmony_ciprivate keys, it is recommended to use <code>'pkcs8'</code> with a strong passphrase, 40611cb0ef41Sopenharmony_ciand to keep the passphrase confidential.</p> 40621cb0ef41Sopenharmony_ci 40631cb0ef41Sopenharmony_ci<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> { 40641cb0ef41Sopenharmony_ci generateKeyPairSync, 40651cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 40661cb0ef41Sopenharmony_ci 40671cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 40681cb0ef41Sopenharmony_ci publicKey, 40691cb0ef41Sopenharmony_ci privateKey, 40701cb0ef41Sopenharmony_ci} = <span class="hljs-title function_">generateKeyPairSync</span>(<span class="hljs-string">'rsa'</span>, { 40711cb0ef41Sopenharmony_ci <span class="hljs-attr">modulusLength</span>: <span class="hljs-number">4096</span>, 40721cb0ef41Sopenharmony_ci <span class="hljs-attr">publicKeyEncoding</span>: { 40731cb0ef41Sopenharmony_ci <span class="hljs-attr">type</span>: <span class="hljs-string">'spki'</span>, 40741cb0ef41Sopenharmony_ci <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>, 40751cb0ef41Sopenharmony_ci }, 40761cb0ef41Sopenharmony_ci <span class="hljs-attr">privateKeyEncoding</span>: { 40771cb0ef41Sopenharmony_ci <span class="hljs-attr">type</span>: <span class="hljs-string">'pkcs8'</span>, 40781cb0ef41Sopenharmony_ci <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>, 40791cb0ef41Sopenharmony_ci <span class="hljs-attr">cipher</span>: <span class="hljs-string">'aes-256-cbc'</span>, 40801cb0ef41Sopenharmony_ci <span class="hljs-attr">passphrase</span>: <span class="hljs-string">'top secret'</span>, 40811cb0ef41Sopenharmony_ci }, 40821cb0ef41Sopenharmony_ci});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 40831cb0ef41Sopenharmony_ci generateKeyPairSync, 40841cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 40851cb0ef41Sopenharmony_ci 40861cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 40871cb0ef41Sopenharmony_ci publicKey, 40881cb0ef41Sopenharmony_ci privateKey, 40891cb0ef41Sopenharmony_ci} = <span class="hljs-title function_">generateKeyPairSync</span>(<span class="hljs-string">'rsa'</span>, { 40901cb0ef41Sopenharmony_ci <span class="hljs-attr">modulusLength</span>: <span class="hljs-number">4096</span>, 40911cb0ef41Sopenharmony_ci <span class="hljs-attr">publicKeyEncoding</span>: { 40921cb0ef41Sopenharmony_ci <span class="hljs-attr">type</span>: <span class="hljs-string">'spki'</span>, 40931cb0ef41Sopenharmony_ci <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>, 40941cb0ef41Sopenharmony_ci }, 40951cb0ef41Sopenharmony_ci <span class="hljs-attr">privateKeyEncoding</span>: { 40961cb0ef41Sopenharmony_ci <span class="hljs-attr">type</span>: <span class="hljs-string">'pkcs8'</span>, 40971cb0ef41Sopenharmony_ci <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>, 40981cb0ef41Sopenharmony_ci <span class="hljs-attr">cipher</span>: <span class="hljs-string">'aes-256-cbc'</span>, 40991cb0ef41Sopenharmony_ci <span class="hljs-attr">passphrase</span>: <span class="hljs-string">'top secret'</span>, 41001cb0ef41Sopenharmony_ci }, 41011cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 41021cb0ef41Sopenharmony_ci<p>The return value <code>{ publicKey, privateKey }</code> represents the generated key pair. 41031cb0ef41Sopenharmony_ciWhen PEM encoding was selected, the respective key will be a string, otherwise 41041cb0ef41Sopenharmony_ciit will be a buffer containing the data encoded as DER.</p> 41051cb0ef41Sopenharmony_ci<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> 41061cb0ef41Sopenharmony_ci<div class="api_metadata"> 41071cb0ef41Sopenharmony_ci<span>Added in: v15.0.0</span> 41081cb0ef41Sopenharmony_ci</div> 41091cb0ef41Sopenharmony_ci<ul> 41101cb0ef41Sopenharmony_ci<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 41111cb0ef41Sopenharmony_ciaccepted values are <code>'hmac'</code> and <code>'aes'</code>.</li> 41121cb0ef41Sopenharmony_ci<li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 41131cb0ef41Sopenharmony_ci<ul> 41141cb0ef41Sopenharmony_ci<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. 41151cb0ef41Sopenharmony_ci<ul> 41161cb0ef41Sopenharmony_ci<li>If <code>type</code> is <code>'hmac'</code>, the minimum is 8, and the maximum length is 41171cb0ef41Sopenharmony_ci2<sup>31</sup>-1. If the value is not a multiple of 8, the generated 41181cb0ef41Sopenharmony_cikey will be truncated to <code>Math.floor(length / 8)</code>.</li> 41191cb0ef41Sopenharmony_ci<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> 41201cb0ef41Sopenharmony_ci</ul> 41211cb0ef41Sopenharmony_ci</li> 41221cb0ef41Sopenharmony_ci</ul> 41231cb0ef41Sopenharmony_ci</li> 41241cb0ef41Sopenharmony_ci<li>Returns: <a href="crypto.html#class-keyobject" class="type"><KeyObject></a></li> 41251cb0ef41Sopenharmony_ci</ul> 41261cb0ef41Sopenharmony_ci<p>Synchronously generates a new random secret key of the given <code>length</code>. The 41271cb0ef41Sopenharmony_ci<code>type</code> will determine which validations will be performed on the <code>length</code>.</p> 41281cb0ef41Sopenharmony_ci 41291cb0ef41Sopenharmony_ci<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> { 41301cb0ef41Sopenharmony_ci generateKeySync, 41311cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 41321cb0ef41Sopenharmony_ci 41331cb0ef41Sopenharmony_ci<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> }); 41341cb0ef41Sopenharmony_ci<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> { 41351cb0ef41Sopenharmony_ci generateKeySync, 41361cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 41371cb0ef41Sopenharmony_ci 41381cb0ef41Sopenharmony_ci<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> }); 41391cb0ef41Sopenharmony_ci<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> 41401cb0ef41Sopenharmony_ci<p>The size of a generated HMAC key should not exceed the block size of the 41411cb0ef41Sopenharmony_ciunderlying hash function. See <a href="#cryptocreatehmacalgorithm-key-options"><code>crypto.createHmac()</code></a> for more information.</p> 41421cb0ef41Sopenharmony_ci<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> 41431cb0ef41Sopenharmony_ci<div class="api_metadata"> 41441cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 41451cb0ef41Sopenharmony_ci<table> 41461cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 41471cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td> 41481cb0ef41Sopenharmony_ci<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> 41491cb0ef41Sopenharmony_ci<tr><td>v15.8.0</td> 41501cb0ef41Sopenharmony_ci<td><p><span>Added in: v15.8.0</span></p></td></tr> 41511cb0ef41Sopenharmony_ci</tbody></table> 41521cb0ef41Sopenharmony_ci</details> 41531cb0ef41Sopenharmony_ci</div> 41541cb0ef41Sopenharmony_ci<ul> 41551cb0ef41Sopenharmony_ci<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> 41561cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 41571cb0ef41Sopenharmony_ci<ul> 41581cb0ef41Sopenharmony_ci<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> 41591cb0ef41Sopenharmony_ci<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> 41601cb0ef41Sopenharmony_ci<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> 41611cb0ef41Sopenharmony_ci<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 41621cb0ef41Sopenharmony_cias a <code>bigint</code>.</li> 41631cb0ef41Sopenharmony_ci</ul> 41641cb0ef41Sopenharmony_ci</li> 41651cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 41661cb0ef41Sopenharmony_ci<ul> 41671cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 41681cb0ef41Sopenharmony_ci<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> 41691cb0ef41Sopenharmony_ci</ul> 41701cb0ef41Sopenharmony_ci</li> 41711cb0ef41Sopenharmony_ci</ul> 41721cb0ef41Sopenharmony_ci<p>Generates a pseudorandom prime of <code>size</code> bits.</p> 41731cb0ef41Sopenharmony_ci<p>If <code>options.safe</code> is <code>true</code>, the prime will be a safe prime -- that is, 41741cb0ef41Sopenharmony_ci<code>(prime - 1) / 2</code> will also be a prime.</p> 41751cb0ef41Sopenharmony_ci<p>The <code>options.add</code> and <code>options.rem</code> parameters can be used to enforce additional 41761cb0ef41Sopenharmony_cirequirements, e.g., for Diffie-Hellman:</p> 41771cb0ef41Sopenharmony_ci<ul> 41781cb0ef41Sopenharmony_ci<li>If <code>options.add</code> and <code>options.rem</code> are both set, the prime will satisfy the 41791cb0ef41Sopenharmony_cicondition that <code>prime % add = rem</code>.</li> 41801cb0ef41Sopenharmony_ci<li>If only <code>options.add</code> is set and <code>options.safe</code> is not <code>true</code>, the prime will 41811cb0ef41Sopenharmony_cisatisfy the condition that <code>prime % add = 1</code>.</li> 41821cb0ef41Sopenharmony_ci<li>If only <code>options.add</code> is set and <code>options.safe</code> is set to <code>true</code>, the prime 41831cb0ef41Sopenharmony_ciwill instead satisfy the condition that <code>prime % add = 3</code>. This is necessary 41841cb0ef41Sopenharmony_cibecause <code>prime % add = 1</code> for <code>options.add > 2</code> would contradict the condition 41851cb0ef41Sopenharmony_cienforced by <code>options.safe</code>.</li> 41861cb0ef41Sopenharmony_ci<li><code>options.rem</code> is ignored if <code>options.add</code> is not given.</li> 41871cb0ef41Sopenharmony_ci</ul> 41881cb0ef41Sopenharmony_ci<p>Both <code>options.add</code> and <code>options.rem</code> must be encoded as big-endian sequences 41891cb0ef41Sopenharmony_ciif given as an <code>ArrayBuffer</code>, <code>SharedArrayBuffer</code>, <code>TypedArray</code>, <code>Buffer</code>, or 41901cb0ef41Sopenharmony_ci<code>DataView</code>.</p> 41911cb0ef41Sopenharmony_ci<p>By default, the prime is encoded as a big-endian sequence of octets 41921cb0ef41Sopenharmony_ciin 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> 41931cb0ef41Sopenharmony_ciis provided.</p> 41941cb0ef41Sopenharmony_ci<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> 41951cb0ef41Sopenharmony_ci<div class="api_metadata"> 41961cb0ef41Sopenharmony_ci<span>Added in: v15.8.0</span> 41971cb0ef41Sopenharmony_ci</div> 41981cb0ef41Sopenharmony_ci<ul> 41991cb0ef41Sopenharmony_ci<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> 42001cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 42011cb0ef41Sopenharmony_ci<ul> 42021cb0ef41Sopenharmony_ci<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> 42031cb0ef41Sopenharmony_ci<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> 42041cb0ef41Sopenharmony_ci<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> 42051cb0ef41Sopenharmony_ci<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 42061cb0ef41Sopenharmony_cias a <code>bigint</code>.</li> 42071cb0ef41Sopenharmony_ci</ul> 42081cb0ef41Sopenharmony_ci</li> 42091cb0ef41Sopenharmony_ci<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> 42101cb0ef41Sopenharmony_ci</ul> 42111cb0ef41Sopenharmony_ci<p>Generates a pseudorandom prime of <code>size</code> bits.</p> 42121cb0ef41Sopenharmony_ci<p>If <code>options.safe</code> is <code>true</code>, the prime will be a safe prime -- that is, 42131cb0ef41Sopenharmony_ci<code>(prime - 1) / 2</code> will also be a prime.</p> 42141cb0ef41Sopenharmony_ci<p>The <code>options.add</code> and <code>options.rem</code> parameters can be used to enforce additional 42151cb0ef41Sopenharmony_cirequirements, e.g., for Diffie-Hellman:</p> 42161cb0ef41Sopenharmony_ci<ul> 42171cb0ef41Sopenharmony_ci<li>If <code>options.add</code> and <code>options.rem</code> are both set, the prime will satisfy the 42181cb0ef41Sopenharmony_cicondition that <code>prime % add = rem</code>.</li> 42191cb0ef41Sopenharmony_ci<li>If only <code>options.add</code> is set and <code>options.safe</code> is not <code>true</code>, the prime will 42201cb0ef41Sopenharmony_cisatisfy the condition that <code>prime % add = 1</code>.</li> 42211cb0ef41Sopenharmony_ci<li>If only <code>options.add</code> is set and <code>options.safe</code> is set to <code>true</code>, the prime 42221cb0ef41Sopenharmony_ciwill instead satisfy the condition that <code>prime % add = 3</code>. This is necessary 42231cb0ef41Sopenharmony_cibecause <code>prime % add = 1</code> for <code>options.add > 2</code> would contradict the condition 42241cb0ef41Sopenharmony_cienforced by <code>options.safe</code>.</li> 42251cb0ef41Sopenharmony_ci<li><code>options.rem</code> is ignored if <code>options.add</code> is not given.</li> 42261cb0ef41Sopenharmony_ci</ul> 42271cb0ef41Sopenharmony_ci<p>Both <code>options.add</code> and <code>options.rem</code> must be encoded as big-endian sequences 42281cb0ef41Sopenharmony_ciif given as an <code>ArrayBuffer</code>, <code>SharedArrayBuffer</code>, <code>TypedArray</code>, <code>Buffer</code>, or 42291cb0ef41Sopenharmony_ci<code>DataView</code>.</p> 42301cb0ef41Sopenharmony_ci<p>By default, the prime is encoded as a big-endian sequence of octets 42311cb0ef41Sopenharmony_ciin 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> 42321cb0ef41Sopenharmony_ciis provided.</p> 42331cb0ef41Sopenharmony_ci<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> 42341cb0ef41Sopenharmony_ci<div class="api_metadata"> 42351cb0ef41Sopenharmony_ci<span>Added in: v15.0.0</span> 42361cb0ef41Sopenharmony_ci</div> 42371cb0ef41Sopenharmony_ci<ul> 42381cb0ef41Sopenharmony_ci<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> 42391cb0ef41Sopenharmony_ci<li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 42401cb0ef41Sopenharmony_ci<ul> 42411cb0ef41Sopenharmony_ci<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> 42421cb0ef41Sopenharmony_ci<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> 42431cb0ef41Sopenharmony_ci</ul> 42441cb0ef41Sopenharmony_ci</li> 42451cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 42461cb0ef41Sopenharmony_ci<ul> 42471cb0ef41Sopenharmony_ci<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> 42481cb0ef41Sopenharmony_ci<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> 42491cb0ef41Sopenharmony_ci<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 42501cb0ef41Sopenharmony_ciis omitted when <code>mode</code> is <code>'stream'</code>.</li> 42511cb0ef41Sopenharmony_ci<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 42521cb0ef41Sopenharmony_cibytes. This property is omitted if the cipher does not use an initialization 42531cb0ef41Sopenharmony_civector.</li> 42541cb0ef41Sopenharmony_ci<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> 42551cb0ef41Sopenharmony_ci<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>, 42561cb0ef41Sopenharmony_ci<code>'ecb'</code>, <code>'gcm'</code>, <code>'ocb'</code>, <code>'ofb'</code>, <code>'stream'</code>, <code>'wrap'</code>, <code>'xts'</code>.</li> 42571cb0ef41Sopenharmony_ci</ul> 42581cb0ef41Sopenharmony_ci</li> 42591cb0ef41Sopenharmony_ci</ul> 42601cb0ef41Sopenharmony_ci<p>Returns information about a given cipher.</p> 42611cb0ef41Sopenharmony_ci<p>Some ciphers accept variable length keys and initialization vectors. By default, 42621cb0ef41Sopenharmony_cithe <code>crypto.getCipherInfo()</code> method will return the default values for these 42631cb0ef41Sopenharmony_ciciphers. To test if a given key length or iv length is acceptable for given 42641cb0ef41Sopenharmony_cicipher, use the <code>keyLength</code> and <code>ivLength</code> options. If the given values are 42651cb0ef41Sopenharmony_ciunacceptable, <code>undefined</code> will be returned.</p> 42661cb0ef41Sopenharmony_ci<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> 42671cb0ef41Sopenharmony_ci<div class="api_metadata"> 42681cb0ef41Sopenharmony_ci<span>Added in: v0.9.3</span> 42691cb0ef41Sopenharmony_ci</div> 42701cb0ef41Sopenharmony_ci<ul> 42711cb0ef41Sopenharmony_ci<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 42721cb0ef41Sopenharmony_cialgorithms.</li> 42731cb0ef41Sopenharmony_ci</ul> 42741cb0ef41Sopenharmony_ci 42751cb0ef41Sopenharmony_ci<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> { 42761cb0ef41Sopenharmony_ci getCiphers, 42771cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 42781cb0ef41Sopenharmony_ci 42791cb0ef41Sopenharmony_ci<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> { 42801cb0ef41Sopenharmony_ci getCiphers, 42811cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 42821cb0ef41Sopenharmony_ci 42831cb0ef41Sopenharmony_ci<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> 42841cb0ef41Sopenharmony_ci<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> 42851cb0ef41Sopenharmony_ci<div class="api_metadata"> 42861cb0ef41Sopenharmony_ci<span>Added in: v2.3.0</span> 42871cb0ef41Sopenharmony_ci</div> 42881cb0ef41Sopenharmony_ci<ul> 42891cb0ef41Sopenharmony_ci<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> 42901cb0ef41Sopenharmony_ci</ul> 42911cb0ef41Sopenharmony_ci 42921cb0ef41Sopenharmony_ci<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> { 42931cb0ef41Sopenharmony_ci getCurves, 42941cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 42951cb0ef41Sopenharmony_ci 42961cb0ef41Sopenharmony_ci<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> { 42971cb0ef41Sopenharmony_ci getCurves, 42981cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 42991cb0ef41Sopenharmony_ci 43001cb0ef41Sopenharmony_ci<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> 43011cb0ef41Sopenharmony_ci<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> 43021cb0ef41Sopenharmony_ci<div class="api_metadata"> 43031cb0ef41Sopenharmony_ci<span>Added in: v0.7.5</span> 43041cb0ef41Sopenharmony_ci</div> 43051cb0ef41Sopenharmony_ci<ul> 43061cb0ef41Sopenharmony_ci<li><code>groupName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 43071cb0ef41Sopenharmony_ci<li>Returns: <a href="crypto.html#class-diffiehellmangroup" class="type"><DiffieHellmanGroup></a></li> 43081cb0ef41Sopenharmony_ci</ul> 43091cb0ef41Sopenharmony_ci<p>Creates a predefined <code>DiffieHellmanGroup</code> key exchange object. The 43101cb0ef41Sopenharmony_cisupported groups are listed in the documentation for <a href="#class-diffiehellmangroup"><code>DiffieHellmanGroup</code></a>.</p> 43111cb0ef41Sopenharmony_ci<p>The returned object mimics the interface of objects created by 43121cb0ef41Sopenharmony_ci<a href="#cryptocreatediffiehellmanprime-primeencoding-generator-generatorencoding"><code>crypto.createDiffieHellman()</code></a>, but will not allow changing 43131cb0ef41Sopenharmony_cithe keys (with <a href="#diffiehellmansetpublickeypublickey-encoding"><code>diffieHellman.setPublicKey()</code></a>, for example). The 43141cb0ef41Sopenharmony_ciadvantage of using this method is that the parties do not have to 43151cb0ef41Sopenharmony_cigenerate nor exchange a group modulus beforehand, saving both processor 43161cb0ef41Sopenharmony_ciand communication time.</p> 43171cb0ef41Sopenharmony_ci<p>Example (obtaining a shared secret):</p> 43181cb0ef41Sopenharmony_ci 43191cb0ef41Sopenharmony_ci<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> { 43201cb0ef41Sopenharmony_ci getDiffieHellman, 43211cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 43221cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> alice = <span class="hljs-title function_">getDiffieHellman</span>(<span class="hljs-string">'modp14'</span>); 43231cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> bob = <span class="hljs-title function_">getDiffieHellman</span>(<span class="hljs-string">'modp14'</span>); 43241cb0ef41Sopenharmony_ci 43251cb0ef41Sopenharmony_cialice.<span class="hljs-title function_">generateKeys</span>(); 43261cb0ef41Sopenharmony_cibob.<span class="hljs-title function_">generateKeys</span>(); 43271cb0ef41Sopenharmony_ci 43281cb0ef41Sopenharmony_ci<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>); 43291cb0ef41Sopenharmony_ci<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>); 43301cb0ef41Sopenharmony_ci 43311cb0ef41Sopenharmony_ci<span class="hljs-comment">/* aliceSecret and bobSecret should be the same */</span> 43321cb0ef41Sopenharmony_ci<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> { 43331cb0ef41Sopenharmony_ci getDiffieHellman, 43341cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 43351cb0ef41Sopenharmony_ci 43361cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> alice = <span class="hljs-title function_">getDiffieHellman</span>(<span class="hljs-string">'modp14'</span>); 43371cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> bob = <span class="hljs-title function_">getDiffieHellman</span>(<span class="hljs-string">'modp14'</span>); 43381cb0ef41Sopenharmony_ci 43391cb0ef41Sopenharmony_cialice.<span class="hljs-title function_">generateKeys</span>(); 43401cb0ef41Sopenharmony_cibob.<span class="hljs-title function_">generateKeys</span>(); 43411cb0ef41Sopenharmony_ci 43421cb0ef41Sopenharmony_ci<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>); 43431cb0ef41Sopenharmony_ci<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>); 43441cb0ef41Sopenharmony_ci 43451cb0ef41Sopenharmony_ci<span class="hljs-comment">/* aliceSecret and bobSecret should be the same */</span> 43461cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(aliceSecret === bobSecret);</code><button class="copy-button">copy</button></pre> 43471cb0ef41Sopenharmony_ci<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> 43481cb0ef41Sopenharmony_ci<div class="api_metadata"> 43491cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span> 43501cb0ef41Sopenharmony_ci</div> 43511cb0ef41Sopenharmony_ci<ul> 43521cb0ef41Sopenharmony_ci<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 43531cb0ef41Sopenharmony_cicurrently in use, <code>0</code> otherwise. A future semver-major release may change 43541cb0ef41Sopenharmony_cithe 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> 43551cb0ef41Sopenharmony_ci</ul> 43561cb0ef41Sopenharmony_ci<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> 43571cb0ef41Sopenharmony_ci<div class="api_metadata"> 43581cb0ef41Sopenharmony_ci<span>Added in: v0.9.3</span> 43591cb0ef41Sopenharmony_ci</div> 43601cb0ef41Sopenharmony_ci<ul> 43611cb0ef41Sopenharmony_ci<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, 43621cb0ef41Sopenharmony_cisuch as <code>'RSA-SHA256'</code>. Hash algorithms are also called "digest" algorithms.</li> 43631cb0ef41Sopenharmony_ci</ul> 43641cb0ef41Sopenharmony_ci 43651cb0ef41Sopenharmony_ci<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> { 43661cb0ef41Sopenharmony_ci getHashes, 43671cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 43681cb0ef41Sopenharmony_ci 43691cb0ef41Sopenharmony_ci<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> { 43701cb0ef41Sopenharmony_ci getHashes, 43711cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 43721cb0ef41Sopenharmony_ci 43731cb0ef41Sopenharmony_ci<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> 43741cb0ef41Sopenharmony_ci<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> 43751cb0ef41Sopenharmony_ci<div class="api_metadata"> 43761cb0ef41Sopenharmony_ci<span>Added in: v17.4.0</span> 43771cb0ef41Sopenharmony_ci</div> 43781cb0ef41Sopenharmony_ci<ul> 43791cb0ef41Sopenharmony_ci<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> 43801cb0ef41Sopenharmony_ci<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> 43811cb0ef41Sopenharmony_ci</ul> 43821cb0ef41Sopenharmony_ci<p>A convenient alias for <a href="webcrypto.html#cryptogetrandomvaluestypedarray"><code>crypto.webcrypto.getRandomValues()</code></a>. This 43831cb0ef41Sopenharmony_ciimplementation is not compliant with the Web Crypto spec, to write 43841cb0ef41Sopenharmony_ciweb-compatible code use <a href="webcrypto.html#cryptogetrandomvaluestypedarray"><code>crypto.webcrypto.getRandomValues()</code></a> instead.</p> 43851cb0ef41Sopenharmony_ci<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> 43861cb0ef41Sopenharmony_ci<div class="api_metadata"> 43871cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 43881cb0ef41Sopenharmony_ci<table> 43891cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 43901cb0ef41Sopenharmony_ci<tr><td>v18.8.0</td> 43911cb0ef41Sopenharmony_ci<td><p>The input keying material can now be zero-length.</p></td></tr> 43921cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td> 43931cb0ef41Sopenharmony_ci<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> 43941cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 43951cb0ef41Sopenharmony_ci<td><p><span>Added in: v15.0.0</span></p></td></tr> 43961cb0ef41Sopenharmony_ci</tbody></table> 43971cb0ef41Sopenharmony_ci</details> 43981cb0ef41Sopenharmony_ci</div> 43991cb0ef41Sopenharmony_ci<ul> 44001cb0ef41Sopenharmony_ci<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> 44011cb0ef41Sopenharmony_ci<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 44021cb0ef41Sopenharmony_cikeying material. Must be provided but can be zero-length.</li> 44031cb0ef41Sopenharmony_ci<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 44041cb0ef41Sopenharmony_cibe provided but can be zero-length.</li> 44051cb0ef41Sopenharmony_ci<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. 44061cb0ef41Sopenharmony_ciMust be provided but can be zero-length, and cannot be more than 1024 bytes.</li> 44071cb0ef41Sopenharmony_ci<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. 44081cb0ef41Sopenharmony_ciThe maximum allowable value is <code>255</code> times the number of bytes produced by 44091cb0ef41Sopenharmony_cithe selected digest function (e.g. <code>sha512</code> generates 64-byte hashes, making 44101cb0ef41Sopenharmony_cithe maximum HKDF output 16320 bytes).</li> 44111cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 44121cb0ef41Sopenharmony_ci<ul> 44131cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 44141cb0ef41Sopenharmony_ci<li><code>derivedKey</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a></li> 44151cb0ef41Sopenharmony_ci</ul> 44161cb0ef41Sopenharmony_ci</li> 44171cb0ef41Sopenharmony_ci</ul> 44181cb0ef41Sopenharmony_ci<p>HKDF is a simple key derivation function defined in RFC 5869. The given <code>ikm</code>, 44191cb0ef41Sopenharmony_ci<code>salt</code> and <code>info</code> are used with the <code>digest</code> to derive a key of <code>keylen</code> bytes.</p> 44201cb0ef41Sopenharmony_ci<p>The supplied <code>callback</code> function is called with two arguments: <code>err</code> and 44211cb0ef41Sopenharmony_ci<code>derivedKey</code>. If an errors occurs while deriving the key, <code>err</code> will be set; 44221cb0ef41Sopenharmony_ciotherwise <code>err</code> will be <code>null</code>. The successfully generated <code>derivedKey</code> will 44231cb0ef41Sopenharmony_cibe 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 44241cb0ef41Sopenharmony_ciof the input arguments specify invalid values or types.</p> 44251cb0ef41Sopenharmony_ci 44261cb0ef41Sopenharmony_ci<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>; 44271cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 44281cb0ef41Sopenharmony_ci hkdf, 44291cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 44301cb0ef41Sopenharmony_ci 44311cb0ef41Sopenharmony_ci<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> { 44321cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 44331cb0ef41Sopenharmony_ci <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> 44341cb0ef41Sopenharmony_ci});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 44351cb0ef41Sopenharmony_ci hkdf, 44361cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 44371cb0ef41Sopenharmony_ci<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>); 44381cb0ef41Sopenharmony_ci 44391cb0ef41Sopenharmony_ci<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> { 44401cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 44411cb0ef41Sopenharmony_ci <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> 44421cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 44431cb0ef41Sopenharmony_ci<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> 44441cb0ef41Sopenharmony_ci<div class="api_metadata"> 44451cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 44461cb0ef41Sopenharmony_ci<table> 44471cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 44481cb0ef41Sopenharmony_ci<tr><td>v18.8.0</td> 44491cb0ef41Sopenharmony_ci<td><p>The input keying material can now be zero-length.</p></td></tr> 44501cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 44511cb0ef41Sopenharmony_ci<td><p><span>Added in: v15.0.0</span></p></td></tr> 44521cb0ef41Sopenharmony_ci</tbody></table> 44531cb0ef41Sopenharmony_ci</details> 44541cb0ef41Sopenharmony_ci</div> 44551cb0ef41Sopenharmony_ci<ul> 44561cb0ef41Sopenharmony_ci<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> 44571cb0ef41Sopenharmony_ci<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 44581cb0ef41Sopenharmony_cikeying material. Must be provided but can be zero-length.</li> 44591cb0ef41Sopenharmony_ci<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 44601cb0ef41Sopenharmony_cibe provided but can be zero-length.</li> 44611cb0ef41Sopenharmony_ci<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. 44621cb0ef41Sopenharmony_ciMust be provided but can be zero-length, and cannot be more than 1024 bytes.</li> 44631cb0ef41Sopenharmony_ci<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. 44641cb0ef41Sopenharmony_ciThe maximum allowable value is <code>255</code> times the number of bytes produced by 44651cb0ef41Sopenharmony_cithe selected digest function (e.g. <code>sha512</code> generates 64-byte hashes, making 44661cb0ef41Sopenharmony_cithe maximum HKDF output 16320 bytes).</li> 44671cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a></li> 44681cb0ef41Sopenharmony_ci</ul> 44691cb0ef41Sopenharmony_ci<p>Provides a synchronous HKDF key derivation function as defined in RFC 5869. The 44701cb0ef41Sopenharmony_cigiven <code>ikm</code>, <code>salt</code> and <code>info</code> are used with the <code>digest</code> to derive a key of 44711cb0ef41Sopenharmony_ci<code>keylen</code> bytes.</p> 44721cb0ef41Sopenharmony_ci<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> 44731cb0ef41Sopenharmony_ci<p>An error will be thrown if any of the input arguments specify invalid values or 44741cb0ef41Sopenharmony_citypes, or if the derived key cannot be generated.</p> 44751cb0ef41Sopenharmony_ci 44761cb0ef41Sopenharmony_ci<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>; 44771cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 44781cb0ef41Sopenharmony_ci hkdfSync, 44791cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 44801cb0ef41Sopenharmony_ci 44811cb0ef41Sopenharmony_ci<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>); 44821cb0ef41Sopenharmony_ci<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> { 44831cb0ef41Sopenharmony_ci hkdfSync, 44841cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 44851cb0ef41Sopenharmony_ci<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>); 44861cb0ef41Sopenharmony_ci 44871cb0ef41Sopenharmony_ci<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>); 44881cb0ef41Sopenharmony_ci<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> 44891cb0ef41Sopenharmony_ci<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> 44901cb0ef41Sopenharmony_ci<div class="api_metadata"> 44911cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 44921cb0ef41Sopenharmony_ci<table> 44931cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 44941cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td> 44951cb0ef41Sopenharmony_ci<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> 44961cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 44971cb0ef41Sopenharmony_ci<td><p>The password and salt arguments can also be ArrayBuffer instances.</p></td></tr> 44981cb0ef41Sopenharmony_ci<tr><td>v14.0.0</td> 44991cb0ef41Sopenharmony_ci<td><p>The <code>iterations</code> parameter is now restricted to positive values. Earlier releases treated other values as one.</p></td></tr> 45001cb0ef41Sopenharmony_ci<tr><td>v8.0.0</td> 45011cb0ef41Sopenharmony_ci<td><p>The <code>digest</code> parameter is always required now.</p></td></tr> 45021cb0ef41Sopenharmony_ci<tr><td>v6.0.0</td> 45031cb0ef41Sopenharmony_ci<td><p>Calling this function without passing the <code>digest</code> parameter is deprecated now and will emit a warning.</p></td></tr> 45041cb0ef41Sopenharmony_ci<tr><td>v6.0.0</td> 45051cb0ef41Sopenharmony_ci<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> 45061cb0ef41Sopenharmony_ci<tr><td>v0.5.5</td> 45071cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.5.5</span></p></td></tr> 45081cb0ef41Sopenharmony_ci</tbody></table> 45091cb0ef41Sopenharmony_ci</details> 45101cb0ef41Sopenharmony_ci</div> 45111cb0ef41Sopenharmony_ci<ul> 45121cb0ef41Sopenharmony_ci<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> 45131cb0ef41Sopenharmony_ci<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> 45141cb0ef41Sopenharmony_ci<li><code>iterations</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 45151cb0ef41Sopenharmony_ci<li><code>keylen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 45161cb0ef41Sopenharmony_ci<li><code>digest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 45171cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 45181cb0ef41Sopenharmony_ci<ul> 45191cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 45201cb0ef41Sopenharmony_ci<li><code>derivedKey</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 45211cb0ef41Sopenharmony_ci</ul> 45221cb0ef41Sopenharmony_ci</li> 45231cb0ef41Sopenharmony_ci</ul> 45241cb0ef41Sopenharmony_ci<p>Provides an asynchronous Password-Based Key Derivation Function 2 (PBKDF2) 45251cb0ef41Sopenharmony_ciimplementation. A selected HMAC digest algorithm specified by <code>digest</code> is 45261cb0ef41Sopenharmony_ciapplied to derive a key of the requested byte length (<code>keylen</code>) from the 45271cb0ef41Sopenharmony_ci<code>password</code>, <code>salt</code> and <code>iterations</code>.</p> 45281cb0ef41Sopenharmony_ci<p>The supplied <code>callback</code> function is called with two arguments: <code>err</code> and 45291cb0ef41Sopenharmony_ci<code>derivedKey</code>. If an error occurs while deriving the key, <code>err</code> will be set; 45301cb0ef41Sopenharmony_ciotherwise <code>err</code> will be <code>null</code>. By default, the successfully generated 45311cb0ef41Sopenharmony_ci<code>derivedKey</code> will be passed to the callback as a <a href="buffer.html"><code>Buffer</code></a>. An error will be 45321cb0ef41Sopenharmony_cithrown if any of the input arguments specify invalid values or types.</p> 45331cb0ef41Sopenharmony_ci<p>The <code>iterations</code> argument must be a number set as high as possible. The 45341cb0ef41Sopenharmony_cihigher the number of iterations, the more secure the derived key will be, 45351cb0ef41Sopenharmony_cibut will take a longer amount of time to complete.</p> 45361cb0ef41Sopenharmony_ci<p>The <code>salt</code> should be as unique as possible. It is recommended that a salt is 45371cb0ef41Sopenharmony_cirandom 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> 45381cb0ef41Sopenharmony_ci<p>When passing strings for <code>password</code> or <code>salt</code>, please consider 45391cb0ef41Sopenharmony_ci<a href="#using-strings-as-inputs-to-cryptographic-apis">caveats when using strings as inputs to cryptographic APIs</a>.</p> 45401cb0ef41Sopenharmony_ci 45411cb0ef41Sopenharmony_ci<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> { 45421cb0ef41Sopenharmony_ci pbkdf2, 45431cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 45441cb0ef41Sopenharmony_ci 45451cb0ef41Sopenharmony_ci<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> { 45461cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 45471cb0ef41Sopenharmony_ci <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> 45481cb0ef41Sopenharmony_ci});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 45491cb0ef41Sopenharmony_ci pbkdf2, 45501cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 45511cb0ef41Sopenharmony_ci 45521cb0ef41Sopenharmony_ci<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> { 45531cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 45541cb0ef41Sopenharmony_ci <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> 45551cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 45561cb0ef41Sopenharmony_ci<p>An array of supported digest functions can be retrieved using 45571cb0ef41Sopenharmony_ci<a href="#cryptogethashes"><code>crypto.getHashes()</code></a>.</p> 45581cb0ef41Sopenharmony_ci<p>This API uses libuv's threadpool, which can have surprising and 45591cb0ef41Sopenharmony_cinegative performance implications for some applications; see the 45601cb0ef41Sopenharmony_ci<a href="cli.html#uv_threadpool_sizesize"><code>UV_THREADPOOL_SIZE</code></a> documentation for more information.</p> 45611cb0ef41Sopenharmony_ci<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> 45621cb0ef41Sopenharmony_ci<div class="api_metadata"> 45631cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 45641cb0ef41Sopenharmony_ci<table> 45651cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 45661cb0ef41Sopenharmony_ci<tr><td>v14.0.0</td> 45671cb0ef41Sopenharmony_ci<td><p>The <code>iterations</code> parameter is now restricted to positive values. Earlier releases treated other values as one.</p></td></tr> 45681cb0ef41Sopenharmony_ci<tr><td>v6.0.0</td> 45691cb0ef41Sopenharmony_ci<td><p>Calling this function without passing the <code>digest</code> parameter is deprecated now and will emit a warning.</p></td></tr> 45701cb0ef41Sopenharmony_ci<tr><td>v6.0.0</td> 45711cb0ef41Sopenharmony_ci<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> 45721cb0ef41Sopenharmony_ci<tr><td>v0.9.3</td> 45731cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.9.3</span></p></td></tr> 45741cb0ef41Sopenharmony_ci</tbody></table> 45751cb0ef41Sopenharmony_ci</details> 45761cb0ef41Sopenharmony_ci</div> 45771cb0ef41Sopenharmony_ci<ul> 45781cb0ef41Sopenharmony_ci<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> 45791cb0ef41Sopenharmony_ci<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> 45801cb0ef41Sopenharmony_ci<li><code>iterations</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 45811cb0ef41Sopenharmony_ci<li><code>keylen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 45821cb0ef41Sopenharmony_ci<li><code>digest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 45831cb0ef41Sopenharmony_ci<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 45841cb0ef41Sopenharmony_ci</ul> 45851cb0ef41Sopenharmony_ci<p>Provides a synchronous Password-Based Key Derivation Function 2 (PBKDF2) 45861cb0ef41Sopenharmony_ciimplementation. A selected HMAC digest algorithm specified by <code>digest</code> is 45871cb0ef41Sopenharmony_ciapplied to derive a key of the requested byte length (<code>keylen</code>) from the 45881cb0ef41Sopenharmony_ci<code>password</code>, <code>salt</code> and <code>iterations</code>.</p> 45891cb0ef41Sopenharmony_ci<p>If an error occurs an <code>Error</code> will be thrown, otherwise the derived key will be 45901cb0ef41Sopenharmony_cireturned as a <a href="buffer.html"><code>Buffer</code></a>.</p> 45911cb0ef41Sopenharmony_ci<p>The <code>iterations</code> argument must be a number set as high as possible. The 45921cb0ef41Sopenharmony_cihigher the number of iterations, the more secure the derived key will be, 45931cb0ef41Sopenharmony_cibut will take a longer amount of time to complete.</p> 45941cb0ef41Sopenharmony_ci<p>The <code>salt</code> should be as unique as possible. It is recommended that a salt is 45951cb0ef41Sopenharmony_cirandom 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> 45961cb0ef41Sopenharmony_ci<p>When passing strings for <code>password</code> or <code>salt</code>, please consider 45971cb0ef41Sopenharmony_ci<a href="#using-strings-as-inputs-to-cryptographic-apis">caveats when using strings as inputs to cryptographic APIs</a>.</p> 45981cb0ef41Sopenharmony_ci 45991cb0ef41Sopenharmony_ci<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> { 46001cb0ef41Sopenharmony_ci pbkdf2Sync, 46011cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 46021cb0ef41Sopenharmony_ci 46031cb0ef41Sopenharmony_ci<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>); 46041cb0ef41Sopenharmony_ci<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> { 46051cb0ef41Sopenharmony_ci pbkdf2Sync, 46061cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 46071cb0ef41Sopenharmony_ci 46081cb0ef41Sopenharmony_ci<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>); 46091cb0ef41Sopenharmony_ci<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> 46101cb0ef41Sopenharmony_ci<p>An array of supported digest functions can be retrieved using 46111cb0ef41Sopenharmony_ci<a href="#cryptogethashes"><code>crypto.getHashes()</code></a>.</p> 46121cb0ef41Sopenharmony_ci<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> 46131cb0ef41Sopenharmony_ci<div class="api_metadata"> 46141cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 46151cb0ef41Sopenharmony_ci<table> 46161cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 46171cb0ef41Sopenharmony_ci<tr><td>v21.6.2, v20.11.1, v18.19.1</td> 46181cb0ef41Sopenharmony_ci<td><p>The <code>RSA_PKCS1_PADDING</code> padding was disabled unless the OpenSSL build supports implicit rejection.</p></td></tr> 46191cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 46201cb0ef41Sopenharmony_ci<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> 46211cb0ef41Sopenharmony_ci<tr><td>v12.11.0</td> 46221cb0ef41Sopenharmony_ci<td><p>The <code>oaepLabel</code> option was added.</p></td></tr> 46231cb0ef41Sopenharmony_ci<tr><td>v12.9.0</td> 46241cb0ef41Sopenharmony_ci<td><p>The <code>oaepHash</code> option was added.</p></td></tr> 46251cb0ef41Sopenharmony_ci<tr><td>v11.6.0</td> 46261cb0ef41Sopenharmony_ci<td><p>This function now supports key objects.</p></td></tr> 46271cb0ef41Sopenharmony_ci<tr><td>v0.11.14</td> 46281cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.11.14</span></p></td></tr> 46291cb0ef41Sopenharmony_ci</tbody></table> 46301cb0ef41Sopenharmony_ci</details> 46311cb0ef41Sopenharmony_ci</div> 46321cb0ef41Sopenharmony_ci<!--lint disable maximum-line-length remark-lint--> 46331cb0ef41Sopenharmony_ci<ul> 46341cb0ef41Sopenharmony_ci<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> 46351cb0ef41Sopenharmony_ci<ul> 46361cb0ef41Sopenharmony_ci<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. 46371cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>'sha1'</code></li> 46381cb0ef41Sopenharmony_ci<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 46391cb0ef41Sopenharmony_ciuse for OAEP padding. If not specified, no label is used.</li> 46401cb0ef41Sopenharmony_ci<li><code>padding</code> <a href="crypto.html#cryptoconstants" class="type"><crypto.constants></a> An optional padding value defined in 46411cb0ef41Sopenharmony_ci<code>crypto.constants</code>, which may be: <code>crypto.constants.RSA_NO_PADDING</code>, 46421cb0ef41Sopenharmony_ci<code>crypto.constants.RSA_PKCS1_PADDING</code>, or 46431cb0ef41Sopenharmony_ci<code>crypto.constants.RSA_PKCS1_OAEP_PADDING</code>.</li> 46441cb0ef41Sopenharmony_ci</ul> 46451cb0ef41Sopenharmony_ci</li> 46461cb0ef41Sopenharmony_ci<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> 46471cb0ef41Sopenharmony_ci<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> A new <code>Buffer</code> with the decrypted content.</li> 46481cb0ef41Sopenharmony_ci</ul> 46491cb0ef41Sopenharmony_ci<!--lint enable maximum-line-length remark-lint--> 46501cb0ef41Sopenharmony_ci<p>Decrypts <code>buffer</code> with <code>privateKey</code>. <code>buffer</code> was previously encrypted using 46511cb0ef41Sopenharmony_cithe corresponding public key, for example using <a href="#cryptopublicencryptkey-buffer"><code>crypto.publicEncrypt()</code></a>.</p> 46521cb0ef41Sopenharmony_ci<p>If <code>privateKey</code> is not a <a href="#class-keyobject"><code>KeyObject</code></a>, this function behaves as if 46531cb0ef41Sopenharmony_ci<code>privateKey</code> had been passed to <a href="#cryptocreateprivatekeykey"><code>crypto.createPrivateKey()</code></a>. If it is an 46541cb0ef41Sopenharmony_ciobject, the <code>padding</code> property can be passed. Otherwise, this function uses 46551cb0ef41Sopenharmony_ci<code>RSA_PKCS1_OAEP_PADDING</code>.</p> 46561cb0ef41Sopenharmony_ci<p>Using <code>crypto.constants.RSA_PKCS1_PADDING</code> in <a href="#cryptoprivatedecryptprivatekey-buffer"><code>crypto.privateDecrypt()</code></a> 46571cb0ef41Sopenharmony_cirequires OpenSSL to support implicit rejection (<code>rsa_pkcs1_implicit_rejection</code>). 46581cb0ef41Sopenharmony_ciIf the version of OpenSSL used by Node.js does not support this feature, 46591cb0ef41Sopenharmony_ciattempting to use <code>RSA_PKCS1_PADDING</code> will fail.</p> 46601cb0ef41Sopenharmony_ci<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> 46611cb0ef41Sopenharmony_ci<div class="api_metadata"> 46621cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 46631cb0ef41Sopenharmony_ci<table> 46641cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 46651cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 46661cb0ef41Sopenharmony_ci<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> 46671cb0ef41Sopenharmony_ci<tr><td>v11.6.0</td> 46681cb0ef41Sopenharmony_ci<td><p>This function now supports key objects.</p></td></tr> 46691cb0ef41Sopenharmony_ci<tr><td>v1.1.0</td> 46701cb0ef41Sopenharmony_ci<td><p><span>Added in: v1.1.0</span></p></td></tr> 46711cb0ef41Sopenharmony_ci</tbody></table> 46721cb0ef41Sopenharmony_ci</details> 46731cb0ef41Sopenharmony_ci</div> 46741cb0ef41Sopenharmony_ci<!--lint disable maximum-line-length remark-lint--> 46751cb0ef41Sopenharmony_ci<ul> 46761cb0ef41Sopenharmony_ci<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> 46771cb0ef41Sopenharmony_ci<ul> 46781cb0ef41Sopenharmony_ci<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> 46791cb0ef41Sopenharmony_ciA PEM encoded private key.</li> 46801cb0ef41Sopenharmony_ci<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 46811cb0ef41Sopenharmony_cipassphrase for the private key.</li> 46821cb0ef41Sopenharmony_ci<li><code>padding</code> <a href="crypto.html#cryptoconstants" class="type"><crypto.constants></a> An optional padding value defined in 46831cb0ef41Sopenharmony_ci<code>crypto.constants</code>, which may be: <code>crypto.constants.RSA_NO_PADDING</code> or 46841cb0ef41Sopenharmony_ci<code>crypto.constants.RSA_PKCS1_PADDING</code>.</li> 46851cb0ef41Sopenharmony_ci<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>, 46861cb0ef41Sopenharmony_cior <code>passphrase</code> are strings.</li> 46871cb0ef41Sopenharmony_ci</ul> 46881cb0ef41Sopenharmony_ci</li> 46891cb0ef41Sopenharmony_ci<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> 46901cb0ef41Sopenharmony_ci<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> A new <code>Buffer</code> with the encrypted content.</li> 46911cb0ef41Sopenharmony_ci</ul> 46921cb0ef41Sopenharmony_ci<!--lint enable maximum-line-length remark-lint--> 46931cb0ef41Sopenharmony_ci<p>Encrypts <code>buffer</code> with <code>privateKey</code>. The returned data can be decrypted using 46941cb0ef41Sopenharmony_cithe corresponding public key, for example using <a href="#cryptopublicdecryptkey-buffer"><code>crypto.publicDecrypt()</code></a>.</p> 46951cb0ef41Sopenharmony_ci<p>If <code>privateKey</code> is not a <a href="#class-keyobject"><code>KeyObject</code></a>, this function behaves as if 46961cb0ef41Sopenharmony_ci<code>privateKey</code> had been passed to <a href="#cryptocreateprivatekeykey"><code>crypto.createPrivateKey()</code></a>. If it is an 46971cb0ef41Sopenharmony_ciobject, the <code>padding</code> property can be passed. Otherwise, this function uses 46981cb0ef41Sopenharmony_ci<code>RSA_PKCS1_PADDING</code>.</p> 46991cb0ef41Sopenharmony_ci<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> 47001cb0ef41Sopenharmony_ci<div class="api_metadata"> 47011cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 47021cb0ef41Sopenharmony_ci<table> 47031cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 47041cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 47051cb0ef41Sopenharmony_ci<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> 47061cb0ef41Sopenharmony_ci<tr><td>v11.6.0</td> 47071cb0ef41Sopenharmony_ci<td><p>This function now supports key objects.</p></td></tr> 47081cb0ef41Sopenharmony_ci<tr><td>v1.1.0</td> 47091cb0ef41Sopenharmony_ci<td><p><span>Added in: v1.1.0</span></p></td></tr> 47101cb0ef41Sopenharmony_ci</tbody></table> 47111cb0ef41Sopenharmony_ci</details> 47121cb0ef41Sopenharmony_ci</div> 47131cb0ef41Sopenharmony_ci<!--lint disable maximum-line-length remark-lint--> 47141cb0ef41Sopenharmony_ci<ul> 47151cb0ef41Sopenharmony_ci<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> 47161cb0ef41Sopenharmony_ci<ul> 47171cb0ef41Sopenharmony_ci<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 47181cb0ef41Sopenharmony_cipassphrase for the private key.</li> 47191cb0ef41Sopenharmony_ci<li><code>padding</code> <a href="crypto.html#cryptoconstants" class="type"><crypto.constants></a> An optional padding value defined in 47201cb0ef41Sopenharmony_ci<code>crypto.constants</code>, which may be: <code>crypto.constants.RSA_NO_PADDING</code> or 47211cb0ef41Sopenharmony_ci<code>crypto.constants.RSA_PKCS1_PADDING</code>.</li> 47221cb0ef41Sopenharmony_ci<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>, 47231cb0ef41Sopenharmony_cior <code>passphrase</code> are strings.</li> 47241cb0ef41Sopenharmony_ci</ul> 47251cb0ef41Sopenharmony_ci</li> 47261cb0ef41Sopenharmony_ci<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> 47271cb0ef41Sopenharmony_ci<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> A new <code>Buffer</code> with the decrypted content.</li> 47281cb0ef41Sopenharmony_ci</ul> 47291cb0ef41Sopenharmony_ci<!--lint enable maximum-line-length remark-lint--> 47301cb0ef41Sopenharmony_ci<p>Decrypts <code>buffer</code> with <code>key</code>.<code>buffer</code> was previously encrypted using 47311cb0ef41Sopenharmony_cithe corresponding private key, for example using <a href="#cryptoprivateencryptprivatekey-buffer"><code>crypto.privateEncrypt()</code></a>.</p> 47321cb0ef41Sopenharmony_ci<p>If <code>key</code> is not a <a href="#class-keyobject"><code>KeyObject</code></a>, this function behaves as if 47331cb0ef41Sopenharmony_ci<code>key</code> had been passed to <a href="#cryptocreatepublickeykey"><code>crypto.createPublicKey()</code></a>. If it is an 47341cb0ef41Sopenharmony_ciobject, the <code>padding</code> property can be passed. Otherwise, this function uses 47351cb0ef41Sopenharmony_ci<code>RSA_PKCS1_PADDING</code>.</p> 47361cb0ef41Sopenharmony_ci<p>Because RSA public keys can be derived from private keys, a private key may 47371cb0ef41Sopenharmony_cibe passed instead of a public key.</p> 47381cb0ef41Sopenharmony_ci<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> 47391cb0ef41Sopenharmony_ci<div class="api_metadata"> 47401cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 47411cb0ef41Sopenharmony_ci<table> 47421cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 47431cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 47441cb0ef41Sopenharmony_ci<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> 47451cb0ef41Sopenharmony_ci<tr><td>v12.11.0</td> 47461cb0ef41Sopenharmony_ci<td><p>The <code>oaepLabel</code> option was added.</p></td></tr> 47471cb0ef41Sopenharmony_ci<tr><td>v12.9.0</td> 47481cb0ef41Sopenharmony_ci<td><p>The <code>oaepHash</code> option was added.</p></td></tr> 47491cb0ef41Sopenharmony_ci<tr><td>v11.6.0</td> 47501cb0ef41Sopenharmony_ci<td><p>This function now supports key objects.</p></td></tr> 47511cb0ef41Sopenharmony_ci<tr><td>v0.11.14</td> 47521cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.11.14</span></p></td></tr> 47531cb0ef41Sopenharmony_ci</tbody></table> 47541cb0ef41Sopenharmony_ci</details> 47551cb0ef41Sopenharmony_ci</div> 47561cb0ef41Sopenharmony_ci<!--lint disable maximum-line-length remark-lint--> 47571cb0ef41Sopenharmony_ci<ul> 47581cb0ef41Sopenharmony_ci<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> 47591cb0ef41Sopenharmony_ci<ul> 47601cb0ef41Sopenharmony_ci<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> 47611cb0ef41Sopenharmony_ciA 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> 47621cb0ef41Sopenharmony_ci<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. 47631cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>'sha1'</code></li> 47641cb0ef41Sopenharmony_ci<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 47651cb0ef41Sopenharmony_ciuse for OAEP padding. If not specified, no label is used.</li> 47661cb0ef41Sopenharmony_ci<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 47671cb0ef41Sopenharmony_cipassphrase for the private key.</li> 47681cb0ef41Sopenharmony_ci<li><code>padding</code> <a href="crypto.html#cryptoconstants" class="type"><crypto.constants></a> An optional padding value defined in 47691cb0ef41Sopenharmony_ci<code>crypto.constants</code>, which may be: <code>crypto.constants.RSA_NO_PADDING</code>, 47701cb0ef41Sopenharmony_ci<code>crypto.constants.RSA_PKCS1_PADDING</code>, or 47711cb0ef41Sopenharmony_ci<code>crypto.constants.RSA_PKCS1_OAEP_PADDING</code>.</li> 47721cb0ef41Sopenharmony_ci<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>, 47731cb0ef41Sopenharmony_ci<code>oaepLabel</code>, or <code>passphrase</code> are strings.</li> 47741cb0ef41Sopenharmony_ci</ul> 47751cb0ef41Sopenharmony_ci</li> 47761cb0ef41Sopenharmony_ci<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> 47771cb0ef41Sopenharmony_ci<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> A new <code>Buffer</code> with the encrypted content.</li> 47781cb0ef41Sopenharmony_ci</ul> 47791cb0ef41Sopenharmony_ci<!--lint enable maximum-line-length remark-lint--> 47801cb0ef41Sopenharmony_ci<p>Encrypts the content of <code>buffer</code> with <code>key</code> and returns a new 47811cb0ef41Sopenharmony_ci<a href="buffer.html"><code>Buffer</code></a> with encrypted content. The returned data can be decrypted using 47821cb0ef41Sopenharmony_cithe corresponding private key, for example using <a href="#cryptoprivatedecryptprivatekey-buffer"><code>crypto.privateDecrypt()</code></a>.</p> 47831cb0ef41Sopenharmony_ci<p>If <code>key</code> is not a <a href="#class-keyobject"><code>KeyObject</code></a>, this function behaves as if 47841cb0ef41Sopenharmony_ci<code>key</code> had been passed to <a href="#cryptocreatepublickeykey"><code>crypto.createPublicKey()</code></a>. If it is an 47851cb0ef41Sopenharmony_ciobject, the <code>padding</code> property can be passed. Otherwise, this function uses 47861cb0ef41Sopenharmony_ci<code>RSA_PKCS1_OAEP_PADDING</code>.</p> 47871cb0ef41Sopenharmony_ci<p>Because RSA public keys can be derived from private keys, a private key may 47881cb0ef41Sopenharmony_cibe passed instead of a public key.</p> 47891cb0ef41Sopenharmony_ci<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> 47901cb0ef41Sopenharmony_ci<div class="api_metadata"> 47911cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 47921cb0ef41Sopenharmony_ci<table> 47931cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 47941cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td> 47951cb0ef41Sopenharmony_ci<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> 47961cb0ef41Sopenharmony_ci<tr><td>v9.0.0</td> 47971cb0ef41Sopenharmony_ci<td><p>Passing <code>null</code> as the <code>callback</code> argument now throws <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 47981cb0ef41Sopenharmony_ci<tr><td>v0.5.8</td> 47991cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.5.8</span></p></td></tr> 48001cb0ef41Sopenharmony_ci</tbody></table> 48011cb0ef41Sopenharmony_ci</details> 48021cb0ef41Sopenharmony_ci</div> 48031cb0ef41Sopenharmony_ci<ul> 48041cb0ef41Sopenharmony_ci<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 48051cb0ef41Sopenharmony_cinot be larger than <code>2**31 - 1</code>.</li> 48061cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 48071cb0ef41Sopenharmony_ci<ul> 48081cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 48091cb0ef41Sopenharmony_ci<li><code>buf</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 48101cb0ef41Sopenharmony_ci</ul> 48111cb0ef41Sopenharmony_ci</li> 48121cb0ef41Sopenharmony_ci<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> if the <code>callback</code> function is not provided.</li> 48131cb0ef41Sopenharmony_ci</ul> 48141cb0ef41Sopenharmony_ci<p>Generates cryptographically strong pseudorandom data. The <code>size</code> argument 48151cb0ef41Sopenharmony_ciis a number indicating the number of bytes to generate.</p> 48161cb0ef41Sopenharmony_ci<p>If a <code>callback</code> function is provided, the bytes are generated asynchronously 48171cb0ef41Sopenharmony_ciand the <code>callback</code> function is invoked with two arguments: <code>err</code> and <code>buf</code>. 48181cb0ef41Sopenharmony_ciIf an error occurs, <code>err</code> will be an <code>Error</code> object; otherwise it is <code>null</code>. The 48191cb0ef41Sopenharmony_ci<code>buf</code> argument is a <a href="buffer.html"><code>Buffer</code></a> containing the generated bytes.</p> 48201cb0ef41Sopenharmony_ci 48211cb0ef41Sopenharmony_ci<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> 48221cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 48231cb0ef41Sopenharmony_ci randomBytes, 48241cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 48251cb0ef41Sopenharmony_ci 48261cb0ef41Sopenharmony_ci<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> { 48271cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 48281cb0ef41Sopenharmony_ci <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>); 48291cb0ef41Sopenharmony_ci});</code><code class="language-js cjs"><span class="hljs-comment">// Asynchronous</span> 48301cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 48311cb0ef41Sopenharmony_ci randomBytes, 48321cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 48331cb0ef41Sopenharmony_ci 48341cb0ef41Sopenharmony_ci<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> { 48351cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 48361cb0ef41Sopenharmony_ci <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>); 48371cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 48381cb0ef41Sopenharmony_ci<p>If the <code>callback</code> function is not provided, the random bytes are generated 48391cb0ef41Sopenharmony_cisynchronously and returned as a <a href="buffer.html"><code>Buffer</code></a>. An error will be thrown if 48401cb0ef41Sopenharmony_cithere is a problem generating the bytes.</p> 48411cb0ef41Sopenharmony_ci 48421cb0ef41Sopenharmony_ci<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> 48431cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 48441cb0ef41Sopenharmony_ci randomBytes, 48451cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 48461cb0ef41Sopenharmony_ci 48471cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> buf = <span class="hljs-title function_">randomBytes</span>(<span class="hljs-number">256</span>); 48481cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>( 48491cb0ef41Sopenharmony_ci <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> 48501cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 48511cb0ef41Sopenharmony_ci randomBytes, 48521cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 48531cb0ef41Sopenharmony_ci 48541cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> buf = <span class="hljs-title function_">randomBytes</span>(<span class="hljs-number">256</span>); 48551cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>( 48561cb0ef41Sopenharmony_ci <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> 48571cb0ef41Sopenharmony_ci<p>The <code>crypto.randomBytes()</code> method will not complete until there is 48581cb0ef41Sopenharmony_cisufficient entropy available. 48591cb0ef41Sopenharmony_ciThis should normally never take longer than a few milliseconds. The only time 48601cb0ef41Sopenharmony_ciwhen generating the random bytes may conceivably block for a longer period of 48611cb0ef41Sopenharmony_citime is right after boot, when the whole system is still low on entropy.</p> 48621cb0ef41Sopenharmony_ci<p>This API uses libuv's threadpool, which can have surprising and 48631cb0ef41Sopenharmony_cinegative performance implications for some applications; see the 48641cb0ef41Sopenharmony_ci<a href="cli.html#uv_threadpool_sizesize"><code>UV_THREADPOOL_SIZE</code></a> documentation for more information.</p> 48651cb0ef41Sopenharmony_ci<p>The asynchronous version of <code>crypto.randomBytes()</code> is carried out in a single 48661cb0ef41Sopenharmony_cithreadpool request. To minimize threadpool task length variation, partition 48671cb0ef41Sopenharmony_cilarge <code>randomBytes</code> requests when doing so as part of fulfilling a client 48681cb0ef41Sopenharmony_cirequest.</p> 48691cb0ef41Sopenharmony_ci<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> 48701cb0ef41Sopenharmony_ci<div class="api_metadata"> 48711cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 48721cb0ef41Sopenharmony_ci<table> 48731cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 48741cb0ef41Sopenharmony_ci<tr><td>v9.0.0</td> 48751cb0ef41Sopenharmony_ci<td><p>The <code>buffer</code> argument may be any <code>TypedArray</code> or <code>DataView</code>.</p></td></tr> 48761cb0ef41Sopenharmony_ci<tr><td>v7.10.0, v6.13.0</td> 48771cb0ef41Sopenharmony_ci<td><p><span>Added in: v7.10.0, v6.13.0</span></p></td></tr> 48781cb0ef41Sopenharmony_ci</tbody></table> 48791cb0ef41Sopenharmony_ci</details> 48801cb0ef41Sopenharmony_ci</div> 48811cb0ef41Sopenharmony_ci<ul> 48821cb0ef41Sopenharmony_ci<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 48831cb0ef41Sopenharmony_cisize of the provided <code>buffer</code> must not be larger than <code>2**31 - 1</code>.</li> 48841cb0ef41Sopenharmony_ci<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> 48851cb0ef41Sopenharmony_ci<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 48861cb0ef41Sopenharmony_cinot be larger than <code>2**31 - 1</code>.</li> 48871cb0ef41Sopenharmony_ci<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 48881cb0ef41Sopenharmony_ci<code>buffer</code> argument.</li> 48891cb0ef41Sopenharmony_ci</ul> 48901cb0ef41Sopenharmony_ci<p>Synchronous version of <a href="#cryptorandomfillbuffer-offset-size-callback"><code>crypto.randomFill()</code></a>.</p> 48911cb0ef41Sopenharmony_ci 48921cb0ef41Sopenharmony_ci<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>; 48931cb0ef41Sopenharmony_ci<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>); 48941cb0ef41Sopenharmony_ci 48951cb0ef41Sopenharmony_ci<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>); 48961cb0ef41Sopenharmony_ci<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>)); 48971cb0ef41Sopenharmony_ci 48981cb0ef41Sopenharmony_ci<span class="hljs-title function_">randomFillSync</span>(buf, <span class="hljs-number">5</span>); 48991cb0ef41Sopenharmony_ci<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>)); 49001cb0ef41Sopenharmony_ci 49011cb0ef41Sopenharmony_ci<span class="hljs-comment">// The above is equivalent to the following:</span> 49021cb0ef41Sopenharmony_ci<span class="hljs-title function_">randomFillSync</span>(buf, <span class="hljs-number">5</span>, <span class="hljs-number">5</span>); 49031cb0ef41Sopenharmony_ci<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>); 49041cb0ef41Sopenharmony_ci<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>); 49051cb0ef41Sopenharmony_ci 49061cb0ef41Sopenharmony_ci<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>); 49071cb0ef41Sopenharmony_ci<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>)); 49081cb0ef41Sopenharmony_ci 49091cb0ef41Sopenharmony_ci<span class="hljs-title function_">randomFillSync</span>(buf, <span class="hljs-number">5</span>); 49101cb0ef41Sopenharmony_ci<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>)); 49111cb0ef41Sopenharmony_ci 49121cb0ef41Sopenharmony_ci<span class="hljs-comment">// The above is equivalent to the following:</span> 49131cb0ef41Sopenharmony_ci<span class="hljs-title function_">randomFillSync</span>(buf, <span class="hljs-number">5</span>, <span class="hljs-number">5</span>); 49141cb0ef41Sopenharmony_ci<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> 49151cb0ef41Sopenharmony_ci<p>Any <code>ArrayBuffer</code>, <code>TypedArray</code> or <code>DataView</code> instance may be passed as 49161cb0ef41Sopenharmony_ci<code>buffer</code>.</p> 49171cb0ef41Sopenharmony_ci 49181cb0ef41Sopenharmony_ci<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>; 49191cb0ef41Sopenharmony_ci<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>); 49201cb0ef41Sopenharmony_ci 49211cb0ef41Sopenharmony_ci<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>); 49221cb0ef41Sopenharmony_ci<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>, 49231cb0ef41Sopenharmony_ci 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>)); 49241cb0ef41Sopenharmony_ci 49251cb0ef41Sopenharmony_ci<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>)); 49261cb0ef41Sopenharmony_ci<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>, 49271cb0ef41Sopenharmony_ci 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>)); 49281cb0ef41Sopenharmony_ci 49291cb0ef41Sopenharmony_ci<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>); 49301cb0ef41Sopenharmony_ci<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>); 49311cb0ef41Sopenharmony_ci<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>); 49321cb0ef41Sopenharmony_ci 49331cb0ef41Sopenharmony_ci<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>); 49341cb0ef41Sopenharmony_ci<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>, 49351cb0ef41Sopenharmony_ci 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>)); 49361cb0ef41Sopenharmony_ci 49371cb0ef41Sopenharmony_ci<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>)); 49381cb0ef41Sopenharmony_ci<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>, 49391cb0ef41Sopenharmony_ci 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>)); 49401cb0ef41Sopenharmony_ci 49411cb0ef41Sopenharmony_ci<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>); 49421cb0ef41Sopenharmony_ci<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> 49431cb0ef41Sopenharmony_ci<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> 49441cb0ef41Sopenharmony_ci<div class="api_metadata"> 49451cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 49461cb0ef41Sopenharmony_ci<table> 49471cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 49481cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td> 49491cb0ef41Sopenharmony_ci<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> 49501cb0ef41Sopenharmony_ci<tr><td>v9.0.0</td> 49511cb0ef41Sopenharmony_ci<td><p>The <code>buffer</code> argument may be any <code>TypedArray</code> or <code>DataView</code>.</p></td></tr> 49521cb0ef41Sopenharmony_ci<tr><td>v7.10.0, v6.13.0</td> 49531cb0ef41Sopenharmony_ci<td><p><span>Added in: v7.10.0, v6.13.0</span></p></td></tr> 49541cb0ef41Sopenharmony_ci</tbody></table> 49551cb0ef41Sopenharmony_ci</details> 49561cb0ef41Sopenharmony_ci</div> 49571cb0ef41Sopenharmony_ci<ul> 49581cb0ef41Sopenharmony_ci<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 49591cb0ef41Sopenharmony_cisize of the provided <code>buffer</code> must not be larger than <code>2**31 - 1</code>.</li> 49601cb0ef41Sopenharmony_ci<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> 49611cb0ef41Sopenharmony_ci<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 49621cb0ef41Sopenharmony_cinot be larger than <code>2**31 - 1</code>.</li> 49631cb0ef41Sopenharmony_ci<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> 49641cb0ef41Sopenharmony_ci</ul> 49651cb0ef41Sopenharmony_ci<p>This function is similar to <a href="#cryptorandombytessize-callback"><code>crypto.randomBytes()</code></a> but requires the first 49661cb0ef41Sopenharmony_ciargument to be a <a href="buffer.html"><code>Buffer</code></a> that will be filled. It also 49671cb0ef41Sopenharmony_cirequires that a callback is passed in.</p> 49681cb0ef41Sopenharmony_ci<p>If the <code>callback</code> function is not provided, an error will be thrown.</p> 49691cb0ef41Sopenharmony_ci 49701cb0ef41Sopenharmony_ci<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>; 49711cb0ef41Sopenharmony_ci<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>); 49721cb0ef41Sopenharmony_ci 49731cb0ef41Sopenharmony_ci<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>); 49741cb0ef41Sopenharmony_ci<span class="hljs-title function_">randomFill</span>(buf, <span class="hljs-function">(<span class="hljs-params">err, buf</span>) =></span> { 49751cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 49761cb0ef41Sopenharmony_ci <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>)); 49771cb0ef41Sopenharmony_ci}); 49781cb0ef41Sopenharmony_ci 49791cb0ef41Sopenharmony_ci<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> { 49801cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 49811cb0ef41Sopenharmony_ci <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>)); 49821cb0ef41Sopenharmony_ci}); 49831cb0ef41Sopenharmony_ci 49841cb0ef41Sopenharmony_ci<span class="hljs-comment">// The above is equivalent to the following:</span> 49851cb0ef41Sopenharmony_ci<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> { 49861cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 49871cb0ef41Sopenharmony_ci <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>)); 49881cb0ef41Sopenharmony_ci});</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>); 49891cb0ef41Sopenharmony_ci<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>); 49901cb0ef41Sopenharmony_ci 49911cb0ef41Sopenharmony_ci<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>); 49921cb0ef41Sopenharmony_ci<span class="hljs-title function_">randomFill</span>(buf, <span class="hljs-function">(<span class="hljs-params">err, buf</span>) =></span> { 49931cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 49941cb0ef41Sopenharmony_ci <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>)); 49951cb0ef41Sopenharmony_ci}); 49961cb0ef41Sopenharmony_ci 49971cb0ef41Sopenharmony_ci<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> { 49981cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 49991cb0ef41Sopenharmony_ci <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>)); 50001cb0ef41Sopenharmony_ci}); 50011cb0ef41Sopenharmony_ci 50021cb0ef41Sopenharmony_ci<span class="hljs-comment">// The above is equivalent to the following:</span> 50031cb0ef41Sopenharmony_ci<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> { 50041cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 50051cb0ef41Sopenharmony_ci <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>)); 50061cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 50071cb0ef41Sopenharmony_ci<p>Any <code>ArrayBuffer</code>, <code>TypedArray</code>, or <code>DataView</code> instance may be passed as 50081cb0ef41Sopenharmony_ci<code>buffer</code>.</p> 50091cb0ef41Sopenharmony_ci<p>While this includes instances of <code>Float32Array</code> and <code>Float64Array</code>, this 50101cb0ef41Sopenharmony_cifunction should not be used to generate random floating-point numbers. The 50111cb0ef41Sopenharmony_ciresult may contain <code>+Infinity</code>, <code>-Infinity</code>, and <code>NaN</code>, and even if the array 50121cb0ef41Sopenharmony_cicontains finite numbers only, they are not drawn from a uniform random 50131cb0ef41Sopenharmony_cidistribution and have no meaningful lower or upper bounds.</p> 50141cb0ef41Sopenharmony_ci 50151cb0ef41Sopenharmony_ci<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>; 50161cb0ef41Sopenharmony_ci<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>); 50171cb0ef41Sopenharmony_ci 50181cb0ef41Sopenharmony_ci<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>); 50191cb0ef41Sopenharmony_ci<span class="hljs-title function_">randomFill</span>(a, <span class="hljs-function">(<span class="hljs-params">err, buf</span>) =></span> { 50201cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 50211cb0ef41Sopenharmony_ci <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>) 50221cb0ef41Sopenharmony_ci .<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>)); 50231cb0ef41Sopenharmony_ci}); 50241cb0ef41Sopenharmony_ci 50251cb0ef41Sopenharmony_ci<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>)); 50261cb0ef41Sopenharmony_ci<span class="hljs-title function_">randomFill</span>(b, <span class="hljs-function">(<span class="hljs-params">err, buf</span>) =></span> { 50271cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 50281cb0ef41Sopenharmony_ci <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>) 50291cb0ef41Sopenharmony_ci .<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>)); 50301cb0ef41Sopenharmony_ci}); 50311cb0ef41Sopenharmony_ci 50321cb0ef41Sopenharmony_ci<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>); 50331cb0ef41Sopenharmony_ci<span class="hljs-title function_">randomFill</span>(c, <span class="hljs-function">(<span class="hljs-params">err, buf</span>) =></span> { 50341cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 50351cb0ef41Sopenharmony_ci <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>)); 50361cb0ef41Sopenharmony_ci});</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>); 50371cb0ef41Sopenharmony_ci<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>); 50381cb0ef41Sopenharmony_ci 50391cb0ef41Sopenharmony_ci<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>); 50401cb0ef41Sopenharmony_ci<span class="hljs-title function_">randomFill</span>(a, <span class="hljs-function">(<span class="hljs-params">err, buf</span>) =></span> { 50411cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 50421cb0ef41Sopenharmony_ci <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>) 50431cb0ef41Sopenharmony_ci .<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>)); 50441cb0ef41Sopenharmony_ci}); 50451cb0ef41Sopenharmony_ci 50461cb0ef41Sopenharmony_ci<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>)); 50471cb0ef41Sopenharmony_ci<span class="hljs-title function_">randomFill</span>(b, <span class="hljs-function">(<span class="hljs-params">err, buf</span>) =></span> { 50481cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 50491cb0ef41Sopenharmony_ci <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>) 50501cb0ef41Sopenharmony_ci .<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>)); 50511cb0ef41Sopenharmony_ci}); 50521cb0ef41Sopenharmony_ci 50531cb0ef41Sopenharmony_ci<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>); 50541cb0ef41Sopenharmony_ci<span class="hljs-title function_">randomFill</span>(c, <span class="hljs-function">(<span class="hljs-params">err, buf</span>) =></span> { 50551cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 50561cb0ef41Sopenharmony_ci <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>)); 50571cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 50581cb0ef41Sopenharmony_ci<p>This API uses libuv's threadpool, which can have surprising and 50591cb0ef41Sopenharmony_cinegative performance implications for some applications; see the 50601cb0ef41Sopenharmony_ci<a href="cli.html#uv_threadpool_sizesize"><code>UV_THREADPOOL_SIZE</code></a> documentation for more information.</p> 50611cb0ef41Sopenharmony_ci<p>The asynchronous version of <code>crypto.randomFill()</code> is carried out in a single 50621cb0ef41Sopenharmony_cithreadpool request. To minimize threadpool task length variation, partition 50631cb0ef41Sopenharmony_cilarge <code>randomFill</code> requests when doing so as part of fulfilling a client 50641cb0ef41Sopenharmony_cirequest.</p> 50651cb0ef41Sopenharmony_ci<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> 50661cb0ef41Sopenharmony_ci<div class="api_metadata"> 50671cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 50681cb0ef41Sopenharmony_ci<table> 50691cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 50701cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td> 50711cb0ef41Sopenharmony_ci<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> 50721cb0ef41Sopenharmony_ci<tr><td>v14.10.0, v12.19.0</td> 50731cb0ef41Sopenharmony_ci<td><p><span>Added in: v14.10.0, v12.19.0</span></p></td></tr> 50741cb0ef41Sopenharmony_ci</tbody></table> 50751cb0ef41Sopenharmony_ci</details> 50761cb0ef41Sopenharmony_ci</div> 50771cb0ef41Sopenharmony_ci<ul> 50781cb0ef41Sopenharmony_ci<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> 50791cb0ef41Sopenharmony_ci<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> 50801cb0ef41Sopenharmony_ci<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> 50811cb0ef41Sopenharmony_ci</ul> 50821cb0ef41Sopenharmony_ci<p>Return a random integer <code>n</code> such that <code>min <= n < max</code>. This 50831cb0ef41Sopenharmony_ciimplementation avoids <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#Modulo_bias">modulo bias</a>.</p> 50841cb0ef41Sopenharmony_ci<p>The range (<code>max - min</code>) must be less than 2<sup>48</sup>. <code>min</code> and <code>max</code> must 50851cb0ef41Sopenharmony_cibe <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isSafeInteger">safe integers</a>.</p> 50861cb0ef41Sopenharmony_ci<p>If the <code>callback</code> function is not provided, the random integer is 50871cb0ef41Sopenharmony_cigenerated synchronously.</p> 50881cb0ef41Sopenharmony_ci 50891cb0ef41Sopenharmony_ci<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> 50901cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 50911cb0ef41Sopenharmony_ci randomInt, 50921cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 50931cb0ef41Sopenharmony_ci 50941cb0ef41Sopenharmony_ci<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> { 50951cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 50961cb0ef41Sopenharmony_ci <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>); 50971cb0ef41Sopenharmony_ci});</code><code class="language-js cjs"><span class="hljs-comment">// Asynchronous</span> 50981cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 50991cb0ef41Sopenharmony_ci randomInt, 51001cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 51011cb0ef41Sopenharmony_ci 51021cb0ef41Sopenharmony_ci<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> { 51031cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 51041cb0ef41Sopenharmony_ci <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>); 51051cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 51061cb0ef41Sopenharmony_ci 51071cb0ef41Sopenharmony_ci<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> 51081cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 51091cb0ef41Sopenharmony_ci randomInt, 51101cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 51111cb0ef41Sopenharmony_ci 51121cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> n = <span class="hljs-title function_">randomInt</span>(<span class="hljs-number">3</span>); 51131cb0ef41Sopenharmony_ci<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> 51141cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 51151cb0ef41Sopenharmony_ci randomInt, 51161cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 51171cb0ef41Sopenharmony_ci 51181cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> n = <span class="hljs-title function_">randomInt</span>(<span class="hljs-number">3</span>); 51191cb0ef41Sopenharmony_ci<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> 51201cb0ef41Sopenharmony_ci 51211cb0ef41Sopenharmony_ci<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> 51221cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 51231cb0ef41Sopenharmony_ci randomInt, 51241cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 51251cb0ef41Sopenharmony_ci 51261cb0ef41Sopenharmony_ci<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>); 51271cb0ef41Sopenharmony_ci<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> 51281cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 51291cb0ef41Sopenharmony_ci randomInt, 51301cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 51311cb0ef41Sopenharmony_ci 51321cb0ef41Sopenharmony_ci<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>); 51331cb0ef41Sopenharmony_ci<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> 51341cb0ef41Sopenharmony_ci<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> 51351cb0ef41Sopenharmony_ci<div class="api_metadata"> 51361cb0ef41Sopenharmony_ci<span>Added in: v15.6.0, v14.17.0</span> 51371cb0ef41Sopenharmony_ci</div> 51381cb0ef41Sopenharmony_ci<ul> 51391cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 51401cb0ef41Sopenharmony_ci<ul> 51411cb0ef41Sopenharmony_ci<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, 51421cb0ef41Sopenharmony_ciNode.js generates and caches enough 51431cb0ef41Sopenharmony_cirandom data to generate up to 128 random UUIDs. To generate a UUID 51441cb0ef41Sopenharmony_ciwithout using the cache, set <code>disableEntropyCache</code> to <code>true</code>. 51451cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>false</code>.</li> 51461cb0ef41Sopenharmony_ci</ul> 51471cb0ef41Sopenharmony_ci</li> 51481cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 51491cb0ef41Sopenharmony_ci</ul> 51501cb0ef41Sopenharmony_ci<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 51511cb0ef41Sopenharmony_cicryptographic pseudorandom number generator.</p> 51521cb0ef41Sopenharmony_ci<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> 51531cb0ef41Sopenharmony_ci<div class="api_metadata"> 51541cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 51551cb0ef41Sopenharmony_ci<table> 51561cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 51571cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td> 51581cb0ef41Sopenharmony_ci<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> 51591cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 51601cb0ef41Sopenharmony_ci<td><p>The password and salt arguments can also be ArrayBuffer instances.</p></td></tr> 51611cb0ef41Sopenharmony_ci<tr><td>v12.8.0, v10.17.0</td> 51621cb0ef41Sopenharmony_ci<td><p>The <code>maxmem</code> value can now be any safe integer.</p></td></tr> 51631cb0ef41Sopenharmony_ci<tr><td>v10.9.0</td> 51641cb0ef41Sopenharmony_ci<td><p>The <code>cost</code>, <code>blockSize</code> and <code>parallelization</code> option names have been added.</p></td></tr> 51651cb0ef41Sopenharmony_ci<tr><td>v10.5.0</td> 51661cb0ef41Sopenharmony_ci<td><p><span>Added in: v10.5.0</span></p></td></tr> 51671cb0ef41Sopenharmony_ci</tbody></table> 51681cb0ef41Sopenharmony_ci</details> 51691cb0ef41Sopenharmony_ci</div> 51701cb0ef41Sopenharmony_ci<ul> 51711cb0ef41Sopenharmony_ci<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> 51721cb0ef41Sopenharmony_ci<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> 51731cb0ef41Sopenharmony_ci<li><code>keylen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 51741cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 51751cb0ef41Sopenharmony_ci<ul> 51761cb0ef41Sopenharmony_ci<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 51771cb0ef41Sopenharmony_cithan one. <strong>Default:</strong> <code>16384</code>.</li> 51781cb0ef41Sopenharmony_ci<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> 51791cb0ef41Sopenharmony_ci<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> 51801cb0ef41Sopenharmony_ci<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> 51811cb0ef41Sopenharmony_ci<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> 51821cb0ef41Sopenharmony_ci<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> 51831cb0ef41Sopenharmony_ci<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) 51841cb0ef41Sopenharmony_ci<code>128 * N * r > maxmem</code>. <strong>Default:</strong> <code>32 * 1024 * 1024</code>.</li> 51851cb0ef41Sopenharmony_ci</ul> 51861cb0ef41Sopenharmony_ci</li> 51871cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 51881cb0ef41Sopenharmony_ci<ul> 51891cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 51901cb0ef41Sopenharmony_ci<li><code>derivedKey</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 51911cb0ef41Sopenharmony_ci</ul> 51921cb0ef41Sopenharmony_ci</li> 51931cb0ef41Sopenharmony_ci</ul> 51941cb0ef41Sopenharmony_ci<p>Provides an asynchronous <a href="https://en.wikipedia.org/wiki/Scrypt">scrypt</a> implementation. Scrypt is a password-based 51951cb0ef41Sopenharmony_cikey derivation function that is designed to be expensive computationally and 51961cb0ef41Sopenharmony_cimemory-wise in order to make brute-force attacks unrewarding.</p> 51971cb0ef41Sopenharmony_ci<p>The <code>salt</code> should be as unique as possible. It is recommended that a salt is 51981cb0ef41Sopenharmony_cirandom 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> 51991cb0ef41Sopenharmony_ci<p>When passing strings for <code>password</code> or <code>salt</code>, please consider 52001cb0ef41Sopenharmony_ci<a href="#using-strings-as-inputs-to-cryptographic-apis">caveats when using strings as inputs to cryptographic APIs</a>.</p> 52011cb0ef41Sopenharmony_ci<p>The <code>callback</code> function is called with two arguments: <code>err</code> and <code>derivedKey</code>. 52021cb0ef41Sopenharmony_ci<code>err</code> is an exception object when key derivation fails, otherwise <code>err</code> is 52031cb0ef41Sopenharmony_ci<code>null</code>. <code>derivedKey</code> is passed to the callback as a <a href="buffer.html"><code>Buffer</code></a>.</p> 52041cb0ef41Sopenharmony_ci<p>An exception is thrown when any of the input arguments specify invalid values 52051cb0ef41Sopenharmony_cior types.</p> 52061cb0ef41Sopenharmony_ci 52071cb0ef41Sopenharmony_ci<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> { 52081cb0ef41Sopenharmony_ci scrypt, 52091cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 52101cb0ef41Sopenharmony_ci 52111cb0ef41Sopenharmony_ci<span class="hljs-comment">// Using the factory defaults.</span> 52121cb0ef41Sopenharmony_ci<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> { 52131cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 52141cb0ef41Sopenharmony_ci <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> 52151cb0ef41Sopenharmony_ci}); 52161cb0ef41Sopenharmony_ci<span class="hljs-comment">// Using a custom N parameter. Must be a power of two.</span> 52171cb0ef41Sopenharmony_ci<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> { 52181cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 52191cb0ef41Sopenharmony_ci <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> 52201cb0ef41Sopenharmony_ci});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 52211cb0ef41Sopenharmony_ci scrypt, 52221cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 52231cb0ef41Sopenharmony_ci 52241cb0ef41Sopenharmony_ci<span class="hljs-comment">// Using the factory defaults.</span> 52251cb0ef41Sopenharmony_ci<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> { 52261cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 52271cb0ef41Sopenharmony_ci <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> 52281cb0ef41Sopenharmony_ci}); 52291cb0ef41Sopenharmony_ci<span class="hljs-comment">// Using a custom N parameter. Must be a power of two.</span> 52301cb0ef41Sopenharmony_ci<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> { 52311cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 52321cb0ef41Sopenharmony_ci <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> 52331cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 52341cb0ef41Sopenharmony_ci<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> 52351cb0ef41Sopenharmony_ci<div class="api_metadata"> 52361cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 52371cb0ef41Sopenharmony_ci<table> 52381cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 52391cb0ef41Sopenharmony_ci<tr><td>v12.8.0, v10.17.0</td> 52401cb0ef41Sopenharmony_ci<td><p>The <code>maxmem</code> value can now be any safe integer.</p></td></tr> 52411cb0ef41Sopenharmony_ci<tr><td>v10.9.0</td> 52421cb0ef41Sopenharmony_ci<td><p>The <code>cost</code>, <code>blockSize</code> and <code>parallelization</code> option names have been added.</p></td></tr> 52431cb0ef41Sopenharmony_ci<tr><td>v10.5.0</td> 52441cb0ef41Sopenharmony_ci<td><p><span>Added in: v10.5.0</span></p></td></tr> 52451cb0ef41Sopenharmony_ci</tbody></table> 52461cb0ef41Sopenharmony_ci</details> 52471cb0ef41Sopenharmony_ci</div> 52481cb0ef41Sopenharmony_ci<ul> 52491cb0ef41Sopenharmony_ci<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> 52501cb0ef41Sopenharmony_ci<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> 52511cb0ef41Sopenharmony_ci<li><code>keylen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 52521cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 52531cb0ef41Sopenharmony_ci<ul> 52541cb0ef41Sopenharmony_ci<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 52551cb0ef41Sopenharmony_cithan one. <strong>Default:</strong> <code>16384</code>.</li> 52561cb0ef41Sopenharmony_ci<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> 52571cb0ef41Sopenharmony_ci<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> 52581cb0ef41Sopenharmony_ci<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> 52591cb0ef41Sopenharmony_ci<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> 52601cb0ef41Sopenharmony_ci<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> 52611cb0ef41Sopenharmony_ci<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) 52621cb0ef41Sopenharmony_ci<code>128 * N * r > maxmem</code>. <strong>Default:</strong> <code>32 * 1024 * 1024</code>.</li> 52631cb0ef41Sopenharmony_ci</ul> 52641cb0ef41Sopenharmony_ci</li> 52651cb0ef41Sopenharmony_ci<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 52661cb0ef41Sopenharmony_ci</ul> 52671cb0ef41Sopenharmony_ci<p>Provides a synchronous <a href="https://en.wikipedia.org/wiki/Scrypt">scrypt</a> implementation. Scrypt is a password-based 52681cb0ef41Sopenharmony_cikey derivation function that is designed to be expensive computationally and 52691cb0ef41Sopenharmony_cimemory-wise in order to make brute-force attacks unrewarding.</p> 52701cb0ef41Sopenharmony_ci<p>The <code>salt</code> should be as unique as possible. It is recommended that a salt is 52711cb0ef41Sopenharmony_cirandom 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> 52721cb0ef41Sopenharmony_ci<p>When passing strings for <code>password</code> or <code>salt</code>, please consider 52731cb0ef41Sopenharmony_ci<a href="#using-strings-as-inputs-to-cryptographic-apis">caveats when using strings as inputs to cryptographic APIs</a>.</p> 52741cb0ef41Sopenharmony_ci<p>An exception is thrown when key derivation fails, otherwise the derived key is 52751cb0ef41Sopenharmony_cireturned as a <a href="buffer.html"><code>Buffer</code></a>.</p> 52761cb0ef41Sopenharmony_ci<p>An exception is thrown when any of the input arguments specify invalid values 52771cb0ef41Sopenharmony_cior types.</p> 52781cb0ef41Sopenharmony_ci 52791cb0ef41Sopenharmony_ci<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> { 52801cb0ef41Sopenharmony_ci scryptSync, 52811cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 52821cb0ef41Sopenharmony_ci<span class="hljs-comment">// Using the factory defaults.</span> 52831cb0ef41Sopenharmony_ci 52841cb0ef41Sopenharmony_ci<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>); 52851cb0ef41Sopenharmony_ci<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> 52861cb0ef41Sopenharmony_ci<span class="hljs-comment">// Using a custom N parameter. Must be a power of two.</span> 52871cb0ef41Sopenharmony_ci<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> }); 52881cb0ef41Sopenharmony_ci<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> { 52891cb0ef41Sopenharmony_ci scryptSync, 52901cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 52911cb0ef41Sopenharmony_ci<span class="hljs-comment">// Using the factory defaults.</span> 52921cb0ef41Sopenharmony_ci 52931cb0ef41Sopenharmony_ci<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>); 52941cb0ef41Sopenharmony_ci<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> 52951cb0ef41Sopenharmony_ci<span class="hljs-comment">// Using a custom N parameter. Must be a power of two.</span> 52961cb0ef41Sopenharmony_ci<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> }); 52971cb0ef41Sopenharmony_ci<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> 52981cb0ef41Sopenharmony_ci<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> 52991cb0ef41Sopenharmony_ci<div class="api_metadata"> 53001cb0ef41Sopenharmony_ci<span>Added in: v15.6.0</span> 53011cb0ef41Sopenharmony_ci</div> 53021cb0ef41Sopenharmony_ci<ul> 53031cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 53041cb0ef41Sopenharmony_ci<ul> 53051cb0ef41Sopenharmony_ci<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 53061cb0ef41Sopenharmony_ciusing the <code>--secure-heap=n</code> command-line flag.</li> 53071cb0ef41Sopenharmony_ci<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 53081cb0ef41Sopenharmony_cispecified using the <code>--secure-heap-min</code> command-line flag.</li> 53091cb0ef41Sopenharmony_ci<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 53101cb0ef41Sopenharmony_cithe secure heap.</li> 53111cb0ef41Sopenharmony_ci<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> 53121cb0ef41Sopenharmony_ciallocated bytes.</li> 53131cb0ef41Sopenharmony_ci</ul> 53141cb0ef41Sopenharmony_ci</li> 53151cb0ef41Sopenharmony_ci</ul> 53161cb0ef41Sopenharmony_ci<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> 53171cb0ef41Sopenharmony_ci<div class="api_metadata"> 53181cb0ef41Sopenharmony_ci<span>Added in: v0.11.11</span> 53191cb0ef41Sopenharmony_ci</div> 53201cb0ef41Sopenharmony_ci<ul> 53211cb0ef41Sopenharmony_ci<li><code>engine</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 53221cb0ef41Sopenharmony_ci<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> 53231cb0ef41Sopenharmony_ci</ul> 53241cb0ef41Sopenharmony_ci<p>Load and set the <code>engine</code> for some or all OpenSSL functions (selected by flags).</p> 53251cb0ef41Sopenharmony_ci<p><code>engine</code> could be either an id or a path to the engine's shared library.</p> 53261cb0ef41Sopenharmony_ci<p>The optional <code>flags</code> argument uses <code>ENGINE_METHOD_ALL</code> by default. The <code>flags</code> 53271cb0ef41Sopenharmony_ciis a bit field taking one of or a mix of the following flags (defined in 53281cb0ef41Sopenharmony_ci<code>crypto.constants</code>):</p> 53291cb0ef41Sopenharmony_ci<ul> 53301cb0ef41Sopenharmony_ci<li><code>crypto.constants.ENGINE_METHOD_RSA</code></li> 53311cb0ef41Sopenharmony_ci<li><code>crypto.constants.ENGINE_METHOD_DSA</code></li> 53321cb0ef41Sopenharmony_ci<li><code>crypto.constants.ENGINE_METHOD_DH</code></li> 53331cb0ef41Sopenharmony_ci<li><code>crypto.constants.ENGINE_METHOD_RAND</code></li> 53341cb0ef41Sopenharmony_ci<li><code>crypto.constants.ENGINE_METHOD_EC</code></li> 53351cb0ef41Sopenharmony_ci<li><code>crypto.constants.ENGINE_METHOD_CIPHERS</code></li> 53361cb0ef41Sopenharmony_ci<li><code>crypto.constants.ENGINE_METHOD_DIGESTS</code></li> 53371cb0ef41Sopenharmony_ci<li><code>crypto.constants.ENGINE_METHOD_PKEY_METHS</code></li> 53381cb0ef41Sopenharmony_ci<li><code>crypto.constants.ENGINE_METHOD_PKEY_ASN1_METHS</code></li> 53391cb0ef41Sopenharmony_ci<li><code>crypto.constants.ENGINE_METHOD_ALL</code></li> 53401cb0ef41Sopenharmony_ci<li><code>crypto.constants.ENGINE_METHOD_NONE</code></li> 53411cb0ef41Sopenharmony_ci</ul> 53421cb0ef41Sopenharmony_ci<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> 53431cb0ef41Sopenharmony_ci<div class="api_metadata"> 53441cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span> 53451cb0ef41Sopenharmony_ci</div> 53461cb0ef41Sopenharmony_ci<ul> 53471cb0ef41Sopenharmony_ci<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> 53481cb0ef41Sopenharmony_ci</ul> 53491cb0ef41Sopenharmony_ci<p>Enables the FIPS compliant crypto provider in a FIPS-enabled Node.js build. 53501cb0ef41Sopenharmony_ciThrows an error if FIPS mode is not available.</p> 53511cb0ef41Sopenharmony_ci<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> 53521cb0ef41Sopenharmony_ci<div class="api_metadata"> 53531cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 53541cb0ef41Sopenharmony_ci<table> 53551cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 53561cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td> 53571cb0ef41Sopenharmony_ci<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> 53581cb0ef41Sopenharmony_ci<tr><td>v15.12.0</td> 53591cb0ef41Sopenharmony_ci<td><p>Optional callback argument added.</p></td></tr> 53601cb0ef41Sopenharmony_ci<tr><td>v13.2.0, v12.16.0</td> 53611cb0ef41Sopenharmony_ci<td><p>This function now supports IEEE-P1363 DSA and ECDSA signatures.</p></td></tr> 53621cb0ef41Sopenharmony_ci<tr><td>v12.0.0</td> 53631cb0ef41Sopenharmony_ci<td><p><span>Added in: v12.0.0</span></p></td></tr> 53641cb0ef41Sopenharmony_ci</tbody></table> 53651cb0ef41Sopenharmony_ci</details> 53661cb0ef41Sopenharmony_ci</div> 53671cb0ef41Sopenharmony_ci<!--lint disable maximum-line-length remark-lint--> 53681cb0ef41Sopenharmony_ci<ul> 53691cb0ef41Sopenharmony_ci<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> 53701cb0ef41Sopenharmony_ci<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> 53711cb0ef41Sopenharmony_ci<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> 53721cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 53731cb0ef41Sopenharmony_ci<ul> 53741cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 53751cb0ef41Sopenharmony_ci<li><code>signature</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a></li> 53761cb0ef41Sopenharmony_ci</ul> 53771cb0ef41Sopenharmony_ci</li> 53781cb0ef41Sopenharmony_ci<li>Returns: <a href="buffer.html#class-buffer" class="type"><Buffer></a> if the <code>callback</code> function is not provided.</li> 53791cb0ef41Sopenharmony_ci</ul> 53801cb0ef41Sopenharmony_ci<!--lint enable maximum-line-length remark-lint--> 53811cb0ef41Sopenharmony_ci<p>Calculates and returns the signature for <code>data</code> using the given private key and 53821cb0ef41Sopenharmony_cialgorithm. If <code>algorithm</code> is <code>null</code> or <code>undefined</code>, then the algorithm is 53831cb0ef41Sopenharmony_cidependent upon the key type (especially Ed25519 and Ed448).</p> 53841cb0ef41Sopenharmony_ci<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 53851cb0ef41Sopenharmony_cipassed to <a href="#cryptocreateprivatekeykey"><code>crypto.createPrivateKey()</code></a>. If it is an object, the following 53861cb0ef41Sopenharmony_ciadditional properties can be passed:</p> 53871cb0ef41Sopenharmony_ci<ul> 53881cb0ef41Sopenharmony_ci<li> 53891cb0ef41Sopenharmony_ci<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 53901cb0ef41Sopenharmony_ciformat of the generated signature. It can be one of the following:</p> 53911cb0ef41Sopenharmony_ci<ul> 53921cb0ef41Sopenharmony_ci<li><code>'der'</code> (default): DER-encoded ASN.1 signature structure encoding <code>(r, s)</code>.</li> 53931cb0ef41Sopenharmony_ci<li><code>'ieee-p1363'</code>: Signature format <code>r || s</code> as proposed in IEEE-P1363.</li> 53941cb0ef41Sopenharmony_ci</ul> 53951cb0ef41Sopenharmony_ci</li> 53961cb0ef41Sopenharmony_ci<li> 53971cb0ef41Sopenharmony_ci<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> 53981cb0ef41Sopenharmony_ci<ul> 53991cb0ef41Sopenharmony_ci<li><code>crypto.constants.RSA_PKCS1_PADDING</code> (default)</li> 54001cb0ef41Sopenharmony_ci<li><code>crypto.constants.RSA_PKCS1_PSS_PADDING</code></li> 54011cb0ef41Sopenharmony_ci</ul> 54021cb0ef41Sopenharmony_ci<p><code>RSA_PKCS1_PSS_PADDING</code> will use MGF1 with the same hash function 54031cb0ef41Sopenharmony_ciused 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> 54041cb0ef41Sopenharmony_ci</li> 54051cb0ef41Sopenharmony_ci<li> 54061cb0ef41Sopenharmony_ci<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 54071cb0ef41Sopenharmony_ci<code>RSA_PKCS1_PSS_PADDING</code>. The special value 54081cb0ef41Sopenharmony_ci<code>crypto.constants.RSA_PSS_SALTLEN_DIGEST</code> sets the salt length to the digest 54091cb0ef41Sopenharmony_cisize, <code>crypto.constants.RSA_PSS_SALTLEN_MAX_SIGN</code> (default) sets it to the 54101cb0ef41Sopenharmony_cimaximum permissible value.</p> 54111cb0ef41Sopenharmony_ci</li> 54121cb0ef41Sopenharmony_ci</ul> 54131cb0ef41Sopenharmony_ci<p>If the <code>callback</code> function is provided this function uses libuv's threadpool.</p> 54141cb0ef41Sopenharmony_ci<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> 54151cb0ef41Sopenharmony_ci<div class="api_metadata"> 54161cb0ef41Sopenharmony_ci<span>Added in: v17.4.0</span> 54171cb0ef41Sopenharmony_ci</div> 54181cb0ef41Sopenharmony_ci<ul> 54191cb0ef41Sopenharmony_ci<li>Type: <a href="webcrypto.html#class-subtlecrypto" class="type"><SubtleCrypto></a></li> 54201cb0ef41Sopenharmony_ci</ul> 54211cb0ef41Sopenharmony_ci<p>A convenient alias for <a href="webcrypto.html#class-subtlecrypto"><code>crypto.webcrypto.subtle</code></a>.</p> 54221cb0ef41Sopenharmony_ci<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> 54231cb0ef41Sopenharmony_ci<div class="api_metadata"> 54241cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 54251cb0ef41Sopenharmony_ci<table> 54261cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 54271cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 54281cb0ef41Sopenharmony_ci<td><p>The a and b arguments can also be ArrayBuffer.</p></td></tr> 54291cb0ef41Sopenharmony_ci<tr><td>v6.6.0</td> 54301cb0ef41Sopenharmony_ci<td><p><span>Added in: v6.6.0</span></p></td></tr> 54311cb0ef41Sopenharmony_ci</tbody></table> 54321cb0ef41Sopenharmony_ci</details> 54331cb0ef41Sopenharmony_ci</div> 54341cb0ef41Sopenharmony_ci<ul> 54351cb0ef41Sopenharmony_ci<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> 54361cb0ef41Sopenharmony_ci<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> 54371cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 54381cb0ef41Sopenharmony_ci</ul> 54391cb0ef41Sopenharmony_ci<p>This function compares the underlying bytes that represent the given 54401cb0ef41Sopenharmony_ci<code>ArrayBuffer</code>, <code>TypedArray</code>, or <code>DataView</code> instances using a constant-time 54411cb0ef41Sopenharmony_cialgorithm.</p> 54421cb0ef41Sopenharmony_ci<p>This function does not leak timing information that 54431cb0ef41Sopenharmony_ciwould allow an attacker to guess one of the values. This is suitable for 54441cb0ef41Sopenharmony_cicomparing HMAC digests or secret values like authentication cookies or 54451cb0ef41Sopenharmony_ci<a href="https://www.w3.org/TR/capability-urls/">capability urls</a>.</p> 54461cb0ef41Sopenharmony_ci<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 54471cb0ef41Sopenharmony_cimust have the same byte length. An error is thrown if <code>a</code> and <code>b</code> have 54481cb0ef41Sopenharmony_cidifferent byte lengths.</p> 54491cb0ef41Sopenharmony_ci<p>If at least one of <code>a</code> and <code>b</code> is a <code>TypedArray</code> with more than one byte per 54501cb0ef41Sopenharmony_cientry, such as <code>Uint16Array</code>, the result will be computed using the platform 54511cb0ef41Sopenharmony_cibyte order.</p> 54521cb0ef41Sopenharmony_ci<p><strong class="critical">When both of the inputs are <code>Float32Array</code>s or 54531cb0ef41Sopenharmony_ci<code>Float64Array</code>s, this function might return unexpected results due to IEEE 754 54541cb0ef41Sopenharmony_ciencoding of floating-point numbers. In particular, neither <code>x === y</code> nor 54551cb0ef41Sopenharmony_ci<code>Object.is(x, y)</code> implies that the byte representations of two floating-point 54561cb0ef41Sopenharmony_cinumbers <code>x</code> and <code>y</code> are equal.</strong></p> 54571cb0ef41Sopenharmony_ci<p>Use of <code>crypto.timingSafeEqual</code> does not guarantee that the <em>surrounding</em> code 54581cb0ef41Sopenharmony_ciis timing-safe. Care should be taken to ensure that the surrounding code does 54591cb0ef41Sopenharmony_cinot introduce timing vulnerabilities.</p> 54601cb0ef41Sopenharmony_ci<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> 54611cb0ef41Sopenharmony_ci<div class="api_metadata"> 54621cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 54631cb0ef41Sopenharmony_ci<table> 54641cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 54651cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td> 54661cb0ef41Sopenharmony_ci<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> 54671cb0ef41Sopenharmony_ci<tr><td>v15.12.0</td> 54681cb0ef41Sopenharmony_ci<td><p>Optional callback argument added.</p></td></tr> 54691cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 54701cb0ef41Sopenharmony_ci<td><p>The data, key, and signature arguments can also be ArrayBuffer.</p></td></tr> 54711cb0ef41Sopenharmony_ci<tr><td>v13.2.0, v12.16.0</td> 54721cb0ef41Sopenharmony_ci<td><p>This function now supports IEEE-P1363 DSA and ECDSA signatures.</p></td></tr> 54731cb0ef41Sopenharmony_ci<tr><td>v12.0.0</td> 54741cb0ef41Sopenharmony_ci<td><p><span>Added in: v12.0.0</span></p></td></tr> 54751cb0ef41Sopenharmony_ci</tbody></table> 54761cb0ef41Sopenharmony_ci</details> 54771cb0ef41Sopenharmony_ci</div> 54781cb0ef41Sopenharmony_ci<!--lint disable maximum-line-length remark-lint--> 54791cb0ef41Sopenharmony_ci<ul> 54801cb0ef41Sopenharmony_ci<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> 54811cb0ef41Sopenharmony_ci<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> 54821cb0ef41Sopenharmony_ci<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> 54831cb0ef41Sopenharmony_ci<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> 54841cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> 54851cb0ef41Sopenharmony_ci<ul> 54861cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> 54871cb0ef41Sopenharmony_ci<li><code>result</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 54881cb0ef41Sopenharmony_ci</ul> 54891cb0ef41Sopenharmony_ci</li> 54901cb0ef41Sopenharmony_ci<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 54911cb0ef41Sopenharmony_cisignature for the data and public key if the <code>callback</code> function is not 54921cb0ef41Sopenharmony_ciprovided.</li> 54931cb0ef41Sopenharmony_ci</ul> 54941cb0ef41Sopenharmony_ci<!--lint enable maximum-line-length remark-lint--> 54951cb0ef41Sopenharmony_ci<p>Verifies the given signature for <code>data</code> using the given key and algorithm. If 54961cb0ef41Sopenharmony_ci<code>algorithm</code> is <code>null</code> or <code>undefined</code>, then the algorithm is dependent upon the 54971cb0ef41Sopenharmony_cikey type (especially Ed25519 and Ed448).</p> 54981cb0ef41Sopenharmony_ci<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 54991cb0ef41Sopenharmony_cipassed to <a href="#cryptocreatepublickeykey"><code>crypto.createPublicKey()</code></a>. If it is an object, the following 55001cb0ef41Sopenharmony_ciadditional properties can be passed:</p> 55011cb0ef41Sopenharmony_ci<ul> 55021cb0ef41Sopenharmony_ci<li> 55031cb0ef41Sopenharmony_ci<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 55041cb0ef41Sopenharmony_ciformat of the signature. It can be one of the following:</p> 55051cb0ef41Sopenharmony_ci<ul> 55061cb0ef41Sopenharmony_ci<li><code>'der'</code> (default): DER-encoded ASN.1 signature structure encoding <code>(r, s)</code>.</li> 55071cb0ef41Sopenharmony_ci<li><code>'ieee-p1363'</code>: Signature format <code>r || s</code> as proposed in IEEE-P1363.</li> 55081cb0ef41Sopenharmony_ci</ul> 55091cb0ef41Sopenharmony_ci</li> 55101cb0ef41Sopenharmony_ci<li> 55111cb0ef41Sopenharmony_ci<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> 55121cb0ef41Sopenharmony_ci<ul> 55131cb0ef41Sopenharmony_ci<li><code>crypto.constants.RSA_PKCS1_PADDING</code> (default)</li> 55141cb0ef41Sopenharmony_ci<li><code>crypto.constants.RSA_PKCS1_PSS_PADDING</code></li> 55151cb0ef41Sopenharmony_ci</ul> 55161cb0ef41Sopenharmony_ci<p><code>RSA_PKCS1_PSS_PADDING</code> will use MGF1 with the same hash function 55171cb0ef41Sopenharmony_ciused 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> 55181cb0ef41Sopenharmony_ci</li> 55191cb0ef41Sopenharmony_ci<li> 55201cb0ef41Sopenharmony_ci<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 55211cb0ef41Sopenharmony_ci<code>RSA_PKCS1_PSS_PADDING</code>. The special value 55221cb0ef41Sopenharmony_ci<code>crypto.constants.RSA_PSS_SALTLEN_DIGEST</code> sets the salt length to the digest 55231cb0ef41Sopenharmony_cisize, <code>crypto.constants.RSA_PSS_SALTLEN_MAX_SIGN</code> (default) sets it to the 55241cb0ef41Sopenharmony_cimaximum permissible value.</p> 55251cb0ef41Sopenharmony_ci</li> 55261cb0ef41Sopenharmony_ci</ul> 55271cb0ef41Sopenharmony_ci<p>The <code>signature</code> argument is the previously calculated signature for the <code>data</code>.</p> 55281cb0ef41Sopenharmony_ci<p>Because public keys can be derived from private keys, a private key or a public 55291cb0ef41Sopenharmony_cikey may be passed for <code>key</code>.</p> 55301cb0ef41Sopenharmony_ci<p>If the <code>callback</code> function is provided this function uses libuv's threadpool.</p> 55311cb0ef41Sopenharmony_ci<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> 55321cb0ef41Sopenharmony_ci<div class="api_metadata"> 55331cb0ef41Sopenharmony_ci<span>Added in: v15.0.0</span> 55341cb0ef41Sopenharmony_ci</div> 55351cb0ef41Sopenharmony_ci<p>Type: <a href="webcrypto.html#class-crypto" class="type"><Crypto></a> An implementation of the Web Crypto API standard.</p> 55361cb0ef41Sopenharmony_ci<p>See the <a href="webcrypto.html">Web Crypto API documentation</a> for details.</p> 55371cb0ef41Sopenharmony_ci</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> 55381cb0ef41Sopenharmony_ci<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> 55391cb0ef41Sopenharmony_ci<p>For historical reasons, many cryptographic APIs provided by Node.js accept 55401cb0ef41Sopenharmony_cistrings as inputs where the underlying cryptographic algorithm works on byte 55411cb0ef41Sopenharmony_cisequences. These instances include plaintexts, ciphertexts, symmetric keys, 55421cb0ef41Sopenharmony_ciinitialization vectors, passphrases, salts, authentication tags, 55431cb0ef41Sopenharmony_ciand additional authenticated data.</p> 55441cb0ef41Sopenharmony_ci<p>When passing strings to cryptographic APIs, consider the following factors.</p> 55451cb0ef41Sopenharmony_ci<ul> 55461cb0ef41Sopenharmony_ci<li> 55471cb0ef41Sopenharmony_ci<p>Not all byte sequences are valid UTF-8 strings. Therefore, when a byte 55481cb0ef41Sopenharmony_cisequence of length <code>n</code> is derived from a string, its entropy is generally 55491cb0ef41Sopenharmony_cilower than the entropy of a random or pseudorandom <code>n</code> byte sequence. 55501cb0ef41Sopenharmony_ciFor example, no UTF-8 string will result in the byte sequence <code>c0 af</code>. Secret 55511cb0ef41Sopenharmony_cikeys should almost exclusively be random or pseudorandom byte sequences.</p> 55521cb0ef41Sopenharmony_ci</li> 55531cb0ef41Sopenharmony_ci<li> 55541cb0ef41Sopenharmony_ci<p>Similarly, when converting random or pseudorandom byte sequences to UTF-8 55551cb0ef41Sopenharmony_cistrings, subsequences that do not represent valid code points may be replaced 55561cb0ef41Sopenharmony_ciby the Unicode replacement character (<code>U+FFFD</code>). The byte representation of 55571cb0ef41Sopenharmony_cithe resulting Unicode string may, therefore, not be equal to the byte sequence 55581cb0ef41Sopenharmony_cithat the string was created from.</p> 55591cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> original = [<span class="hljs-number">0xc0</span>, <span class="hljs-number">0xaf</span>]; 55601cb0ef41Sopenharmony_ci<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>); 55611cb0ef41Sopenharmony_ci<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>); 55621cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(stringAsBytes); 55631cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints '<Buffer ef bf bd ef bf bd>'.</span></code> <button class="copy-button">copy</button></pre> 55641cb0ef41Sopenharmony_ci<p>The outputs of ciphers, hash functions, signature algorithms, and key 55651cb0ef41Sopenharmony_ciderivation functions are pseudorandom byte sequences and should not be 55661cb0ef41Sopenharmony_ciused as Unicode strings.</p> 55671cb0ef41Sopenharmony_ci</li> 55681cb0ef41Sopenharmony_ci<li> 55691cb0ef41Sopenharmony_ci<p>When strings are obtained from user input, some Unicode characters can be 55701cb0ef41Sopenharmony_cirepresented in multiple equivalent ways that result in different byte 55711cb0ef41Sopenharmony_cisequences. For example, when passing a user passphrase to a key derivation 55721cb0ef41Sopenharmony_cifunction, such as PBKDF2 or scrypt, the result of the key derivation function 55731cb0ef41Sopenharmony_cidepends on whether the string uses composed or decomposed characters. Node.js 55741cb0ef41Sopenharmony_cidoes not normalize character representations. Developers should consider using 55751cb0ef41Sopenharmony_ci<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 55761cb0ef41Sopenharmony_cicryptographic APIs.</p> 55771cb0ef41Sopenharmony_ci</li> 55781cb0ef41Sopenharmony_ci</ul> 55791cb0ef41Sopenharmony_ci<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> 55801cb0ef41Sopenharmony_ci<p>The Crypto module was added to Node.js before there was the concept of a 55811cb0ef41Sopenharmony_ciunified Stream API, and before there were <a href="buffer.html"><code>Buffer</code></a> objects for handling 55821cb0ef41Sopenharmony_cibinary data. As such, many <code>crypto</code> classes have methods not 55831cb0ef41Sopenharmony_citypically found on other Node.js classes that implement the <a href="stream.html">streams</a> 55841cb0ef41Sopenharmony_ciAPI (e.g. <code>update()</code>, <code>final()</code>, or <code>digest()</code>). Also, many methods accepted 55851cb0ef41Sopenharmony_ciand returned <code>'latin1'</code> encoded strings by default rather than <code>Buffer</code>s. This 55861cb0ef41Sopenharmony_cidefault was changed after Node.js v0.8 to use <a href="buffer.html"><code>Buffer</code></a> objects by default 55871cb0ef41Sopenharmony_ciinstead.</p> 55881cb0ef41Sopenharmony_ci<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> 55891cb0ef41Sopenharmony_ci<p>The <code>node:crypto</code> module still supports some algorithms which are already 55901cb0ef41Sopenharmony_cicompromised and are not recommended for use. The API also allows 55911cb0ef41Sopenharmony_cithe use of ciphers and hashes with a small key size that are too weak for safe 55921cb0ef41Sopenharmony_ciuse.</p> 55931cb0ef41Sopenharmony_ci<p>Users should take full responsibility for selecting the crypto 55941cb0ef41Sopenharmony_cialgorithm and key size according to their security requirements.</p> 55951cb0ef41Sopenharmony_ci<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> 55961cb0ef41Sopenharmony_ci<ul> 55971cb0ef41Sopenharmony_ci<li>MD5 and SHA-1 are no longer acceptable where collision resistance is 55981cb0ef41Sopenharmony_cirequired such as digital signatures.</li> 55991cb0ef41Sopenharmony_ci<li>The key used with RSA, DSA, and DH algorithms is recommended to have 56001cb0ef41Sopenharmony_ciat least 2048 bits and that of the curve of ECDSA and ECDH at least 56011cb0ef41Sopenharmony_ci224 bits, to be safe to use for several years.</li> 56021cb0ef41Sopenharmony_ci<li>The DH groups of <code>modp1</code>, <code>modp2</code> and <code>modp5</code> have a key size 56031cb0ef41Sopenharmony_cismaller than 2048 bits and are not recommended.</li> 56041cb0ef41Sopenharmony_ci</ul> 56051cb0ef41Sopenharmony_ci<p>See the reference for other recommendations and details.</p> 56061cb0ef41Sopenharmony_ci<p>Some algorithms that have known weaknesses and are of little relevance in 56071cb0ef41Sopenharmony_cipractice are only available through the <a href="cli.html#--openssl-legacy-provider">legacy provider</a>, which is not 56081cb0ef41Sopenharmony_cienabled by default.</p> 56091cb0ef41Sopenharmony_ci<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> 56101cb0ef41Sopenharmony_ci<p>CCM is one of the supported <a href="https://en.wikipedia.org/wiki/Authenticated_encryption">AEAD algorithms</a>. Applications which use this 56111cb0ef41Sopenharmony_cimode must adhere to certain restrictions when using the cipher API:</p> 56121cb0ef41Sopenharmony_ci<ul> 56131cb0ef41Sopenharmony_ci<li>The authentication tag length must be specified during cipher creation by 56141cb0ef41Sopenharmony_cisetting the <code>authTagLength</code> option and must be one of 4, 6, 8, 10, 12, 14 or 56151cb0ef41Sopenharmony_ci16 bytes.</li> 56161cb0ef41Sopenharmony_ci<li>The length of the initialization vector (nonce) <code>N</code> must be between 7 and 13 56171cb0ef41Sopenharmony_cibytes (<code>7 ≤ N ≤ 13</code>).</li> 56181cb0ef41Sopenharmony_ci<li>The length of the plaintext is limited to <code>2 ** (8 * (15 - N))</code> bytes.</li> 56191cb0ef41Sopenharmony_ci<li>When decrypting, the authentication tag must be set via <code>setAuthTag()</code> before 56201cb0ef41Sopenharmony_cicalling <code>update()</code>. 56211cb0ef41Sopenharmony_ciOtherwise, decryption will fail and <code>final()</code> will throw an error in 56221cb0ef41Sopenharmony_cicompliance with section 2.6 of <a href="https://www.rfc-editor.org/rfc/rfc3610.txt">RFC 3610</a>.</li> 56231cb0ef41Sopenharmony_ci<li>Using stream methods such as <code>write(data)</code>, <code>end(data)</code> or <code>pipe()</code> in CCM 56241cb0ef41Sopenharmony_cimode might fail as CCM cannot handle more than one chunk of data per instance.</li> 56251cb0ef41Sopenharmony_ci<li>When passing additional authenticated data (AAD), the length of the actual 56261cb0ef41Sopenharmony_cimessage in bytes must be passed to <code>setAAD()</code> via the <code>plaintextLength</code> 56271cb0ef41Sopenharmony_cioption. 56281cb0ef41Sopenharmony_ciMany crypto libraries include the authentication tag in the ciphertext, 56291cb0ef41Sopenharmony_ciwhich means that they produce ciphertexts of the length 56301cb0ef41Sopenharmony_ci<code>plaintextLength + authTagLength</code>. Node.js does not include the authentication 56311cb0ef41Sopenharmony_citag, so the ciphertext length is always <code>plaintextLength</code>. 56321cb0ef41Sopenharmony_ciThis is not necessary if no AAD is used.</li> 56331cb0ef41Sopenharmony_ci<li>As CCM processes the whole message at once, <code>update()</code> must be called exactly 56341cb0ef41Sopenharmony_cionce.</li> 56351cb0ef41Sopenharmony_ci<li>Even though calling <code>update()</code> is sufficient to encrypt/decrypt the message, 56361cb0ef41Sopenharmony_ciapplications <em>must</em> call <code>final()</code> to compute or verify the 56371cb0ef41Sopenharmony_ciauthentication tag.</li> 56381cb0ef41Sopenharmony_ci</ul> 56391cb0ef41Sopenharmony_ci 56401cb0ef41Sopenharmony_ci<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>; 56411cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 56421cb0ef41Sopenharmony_ci createCipheriv, 56431cb0ef41Sopenharmony_ci createDecipheriv, 56441cb0ef41Sopenharmony_ci randomBytes, 56451cb0ef41Sopenharmony_ci} = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:crypto'</span>); 56461cb0ef41Sopenharmony_ci 56471cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> key = <span class="hljs-string">'keykeykeykeykeykeykeykey'</span>; 56481cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> nonce = <span class="hljs-title function_">randomBytes</span>(<span class="hljs-number">12</span>); 56491cb0ef41Sopenharmony_ci 56501cb0ef41Sopenharmony_ci<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>); 56511cb0ef41Sopenharmony_ci 56521cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> cipher = <span class="hljs-title function_">createCipheriv</span>(<span class="hljs-string">'aes-192-ccm'</span>, key, nonce, { 56531cb0ef41Sopenharmony_ci <span class="hljs-attr">authTagLength</span>: <span class="hljs-number">16</span>, 56541cb0ef41Sopenharmony_ci}); 56551cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> plaintext = <span class="hljs-string">'Hello world'</span>; 56561cb0ef41Sopenharmony_cicipher.<span class="hljs-title function_">setAAD</span>(aad, { 56571cb0ef41Sopenharmony_ci <span class="hljs-attr">plaintextLength</span>: <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">byteLength</span>(plaintext), 56581cb0ef41Sopenharmony_ci}); 56591cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> ciphertext = cipher.<span class="hljs-title function_">update</span>(plaintext, <span class="hljs-string">'utf8'</span>); 56601cb0ef41Sopenharmony_cicipher.<span class="hljs-title function_">final</span>(); 56611cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> tag = cipher.<span class="hljs-title function_">getAuthTag</span>(); 56621cb0ef41Sopenharmony_ci 56631cb0ef41Sopenharmony_ci<span class="hljs-comment">// Now transmit { ciphertext, nonce, tag }.</span> 56641cb0ef41Sopenharmony_ci 56651cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> decipher = <span class="hljs-title function_">createDecipheriv</span>(<span class="hljs-string">'aes-192-ccm'</span>, key, nonce, { 56661cb0ef41Sopenharmony_ci <span class="hljs-attr">authTagLength</span>: <span class="hljs-number">16</span>, 56671cb0ef41Sopenharmony_ci}); 56681cb0ef41Sopenharmony_cidecipher.<span class="hljs-title function_">setAuthTag</span>(tag); 56691cb0ef41Sopenharmony_cidecipher.<span class="hljs-title function_">setAAD</span>(aad, { 56701cb0ef41Sopenharmony_ci <span class="hljs-attr">plaintextLength</span>: ciphertext.<span class="hljs-property">length</span>, 56711cb0ef41Sopenharmony_ci}); 56721cb0ef41Sopenharmony_ci<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>); 56731cb0ef41Sopenharmony_ci 56741cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> { 56751cb0ef41Sopenharmony_ci decipher.<span class="hljs-title function_">final</span>(); 56761cb0ef41Sopenharmony_ci} <span class="hljs-keyword">catch</span> (err) { 56771cb0ef41Sopenharmony_ci <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 }); 56781cb0ef41Sopenharmony_ci} 56791cb0ef41Sopenharmony_ci 56801cb0ef41Sopenharmony_ci<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>); 56811cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { 56821cb0ef41Sopenharmony_ci createCipheriv, 56831cb0ef41Sopenharmony_ci createDecipheriv, 56841cb0ef41Sopenharmony_ci randomBytes, 56851cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:crypto'</span>); 56861cb0ef41Sopenharmony_ci 56871cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> key = <span class="hljs-string">'keykeykeykeykeykeykeykey'</span>; 56881cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> nonce = <span class="hljs-title function_">randomBytes</span>(<span class="hljs-number">12</span>); 56891cb0ef41Sopenharmony_ci 56901cb0ef41Sopenharmony_ci<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>); 56911cb0ef41Sopenharmony_ci 56921cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> cipher = <span class="hljs-title function_">createCipheriv</span>(<span class="hljs-string">'aes-192-ccm'</span>, key, nonce, { 56931cb0ef41Sopenharmony_ci <span class="hljs-attr">authTagLength</span>: <span class="hljs-number">16</span>, 56941cb0ef41Sopenharmony_ci}); 56951cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> plaintext = <span class="hljs-string">'Hello world'</span>; 56961cb0ef41Sopenharmony_cicipher.<span class="hljs-title function_">setAAD</span>(aad, { 56971cb0ef41Sopenharmony_ci <span class="hljs-attr">plaintextLength</span>: <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">byteLength</span>(plaintext), 56981cb0ef41Sopenharmony_ci}); 56991cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> ciphertext = cipher.<span class="hljs-title function_">update</span>(plaintext, <span class="hljs-string">'utf8'</span>); 57001cb0ef41Sopenharmony_cicipher.<span class="hljs-title function_">final</span>(); 57011cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> tag = cipher.<span class="hljs-title function_">getAuthTag</span>(); 57021cb0ef41Sopenharmony_ci 57031cb0ef41Sopenharmony_ci<span class="hljs-comment">// Now transmit { ciphertext, nonce, tag }.</span> 57041cb0ef41Sopenharmony_ci 57051cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> decipher = <span class="hljs-title function_">createDecipheriv</span>(<span class="hljs-string">'aes-192-ccm'</span>, key, nonce, { 57061cb0ef41Sopenharmony_ci <span class="hljs-attr">authTagLength</span>: <span class="hljs-number">16</span>, 57071cb0ef41Sopenharmony_ci}); 57081cb0ef41Sopenharmony_cidecipher.<span class="hljs-title function_">setAuthTag</span>(tag); 57091cb0ef41Sopenharmony_cidecipher.<span class="hljs-title function_">setAAD</span>(aad, { 57101cb0ef41Sopenharmony_ci <span class="hljs-attr">plaintextLength</span>: ciphertext.<span class="hljs-property">length</span>, 57111cb0ef41Sopenharmony_ci}); 57121cb0ef41Sopenharmony_ci<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>); 57131cb0ef41Sopenharmony_ci 57141cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> { 57151cb0ef41Sopenharmony_ci decipher.<span class="hljs-title function_">final</span>(); 57161cb0ef41Sopenharmony_ci} <span class="hljs-keyword">catch</span> (err) { 57171cb0ef41Sopenharmony_ci <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 }); 57181cb0ef41Sopenharmony_ci} 57191cb0ef41Sopenharmony_ci 57201cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(receivedPlaintext);</code><button class="copy-button">copy</button></pre> 57211cb0ef41Sopenharmony_ci<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> 57221cb0ef41Sopenharmony_ci<p>When using OpenSSL 3, Node.js supports FIPS 140-2 when used with an appropriate 57231cb0ef41Sopenharmony_ciOpenSSL 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 57241cb0ef41Sopenharmony_ciinstalled 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> 57251cb0ef41Sopenharmony_ci<p>For FIPS support in Node.js you will need:</p> 57261cb0ef41Sopenharmony_ci<ul> 57271cb0ef41Sopenharmony_ci<li>A correctly installed OpenSSL 3 FIPS provider.</li> 57281cb0ef41Sopenharmony_ci<li>An OpenSSL 3 <a href="https://www.openssl.org/docs/man3.0/man5/fips_config.html">FIPS module configuration file</a>.</li> 57291cb0ef41Sopenharmony_ci<li>An OpenSSL 3 configuration file that references the FIPS module 57301cb0ef41Sopenharmony_ciconfiguration file.</li> 57311cb0ef41Sopenharmony_ci</ul> 57321cb0ef41Sopenharmony_ci<p>Node.js will need to be configured with an OpenSSL configuration file that 57331cb0ef41Sopenharmony_cipoints to the FIPS provider. An example configuration file looks like this:</p> 57341cb0ef41Sopenharmony_ci<pre><code class="language-text">nodejs_conf = nodejs_init 57351cb0ef41Sopenharmony_ci 57361cb0ef41Sopenharmony_ci.include /<absolute path>/fipsmodule.cnf 57371cb0ef41Sopenharmony_ci 57381cb0ef41Sopenharmony_ci[nodejs_init] 57391cb0ef41Sopenharmony_ciproviders = provider_sect 57401cb0ef41Sopenharmony_ci 57411cb0ef41Sopenharmony_ci[provider_sect] 57421cb0ef41Sopenharmony_cidefault = default_sect 57431cb0ef41Sopenharmony_ci# The fips section name should match the section name inside the 57441cb0ef41Sopenharmony_ci# included fipsmodule.cnf. 57451cb0ef41Sopenharmony_cifips = fips_sect 57461cb0ef41Sopenharmony_ci 57471cb0ef41Sopenharmony_ci[default_sect] 57481cb0ef41Sopenharmony_ciactivate = 1</code> <button class="copy-button">copy</button></pre> 57491cb0ef41Sopenharmony_ci<p>where <code>fipsmodule.cnf</code> is the FIPS module configuration file generated from the 57501cb0ef41Sopenharmony_ciFIPS provider installation step:</p> 57511cb0ef41Sopenharmony_ci<pre><code class="language-bash">openssl fipsinstall</code> <button class="copy-button">copy</button></pre> 57521cb0ef41Sopenharmony_ci<p>Set the <code>OPENSSL_CONF</code> environment variable to point to 57531cb0ef41Sopenharmony_ciyour configuration file and <code>OPENSSL_MODULES</code> to the location of the FIPS 57541cb0ef41Sopenharmony_ciprovider dynamic library. e.g.</p> 57551cb0ef41Sopenharmony_ci<pre><code class="language-bash"><span class="hljs-built_in">export</span> OPENSSL_CONF=/<path to configuration file>/nodejs.cnf 57561cb0ef41Sopenharmony_ci<span class="hljs-built_in">export</span> OPENSSL_MODULES=/<path to openssl lib>/ossl-modules</code> <button class="copy-button">copy</button></pre> 57571cb0ef41Sopenharmony_ci<p>FIPS mode can then be enabled in Node.js either by:</p> 57581cb0ef41Sopenharmony_ci<ul> 57591cb0ef41Sopenharmony_ci<li>Starting Node.js with <code>--enable-fips</code> or <code>--force-fips</code> command line flags.</li> 57601cb0ef41Sopenharmony_ci<li>Programmatically calling <code>crypto.setFips(true)</code>.</li> 57611cb0ef41Sopenharmony_ci</ul> 57621cb0ef41Sopenharmony_ci<p>Optionally FIPS mode can be enabled in Node.js via the OpenSSL configuration 57631cb0ef41Sopenharmony_cifile. e.g.</p> 57641cb0ef41Sopenharmony_ci<pre><code class="language-text">nodejs_conf = nodejs_init 57651cb0ef41Sopenharmony_ci 57661cb0ef41Sopenharmony_ci.include /<absolute path>/fipsmodule.cnf 57671cb0ef41Sopenharmony_ci 57681cb0ef41Sopenharmony_ci[nodejs_init] 57691cb0ef41Sopenharmony_ciproviders = provider_sect 57701cb0ef41Sopenharmony_cialg_section = algorithm_sect 57711cb0ef41Sopenharmony_ci 57721cb0ef41Sopenharmony_ci[provider_sect] 57731cb0ef41Sopenharmony_cidefault = default_sect 57741cb0ef41Sopenharmony_ci# The fips section name should match the section name inside the 57751cb0ef41Sopenharmony_ci# included fipsmodule.cnf. 57761cb0ef41Sopenharmony_cifips = fips_sect 57771cb0ef41Sopenharmony_ci 57781cb0ef41Sopenharmony_ci[default_sect] 57791cb0ef41Sopenharmony_ciactivate = 1 57801cb0ef41Sopenharmony_ci 57811cb0ef41Sopenharmony_ci[algorithm_sect] 57821cb0ef41Sopenharmony_cidefault_properties = fips=yes</code> <button class="copy-button">copy</button></pre> 57831cb0ef41Sopenharmony_ci</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> 57841cb0ef41Sopenharmony_ci<p>The following constants exported by <code>crypto.constants</code> apply to various uses of 57851cb0ef41Sopenharmony_cithe <code>node:crypto</code>, <code>node:tls</code>, and <code>node:https</code> modules and are generally 57861cb0ef41Sopenharmony_cispecific to OpenSSL.</p> 57871cb0ef41Sopenharmony_ci<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> 57881cb0ef41Sopenharmony_ci<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> 57891cb0ef41Sopenharmony_ci<table> 57901cb0ef41Sopenharmony_ci <tbody><tr> 57911cb0ef41Sopenharmony_ci <th>Constant</th> 57921cb0ef41Sopenharmony_ci <th>Description</th> 57931cb0ef41Sopenharmony_ci </tr> 57941cb0ef41Sopenharmony_ci <tr> 57951cb0ef41Sopenharmony_ci <td><code>SSL_OP_ALL</code></td> 57961cb0ef41Sopenharmony_ci <td>Applies multiple bug workarounds within OpenSSL. See 57971cb0ef41Sopenharmony_ci <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> 57981cb0ef41Sopenharmony_ci for detail.</td> 57991cb0ef41Sopenharmony_ci </tr> 58001cb0ef41Sopenharmony_ci <tr> 58011cb0ef41Sopenharmony_ci <td><code>SSL_OP_ALLOW_NO_DHE_KEX</code></td> 58021cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to allow a non-[EC]DHE-based key exchange mode 58031cb0ef41Sopenharmony_ci for TLS v1.3</td> 58041cb0ef41Sopenharmony_ci </tr> 58051cb0ef41Sopenharmony_ci <tr> 58061cb0ef41Sopenharmony_ci <td><code>SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION</code></td> 58071cb0ef41Sopenharmony_ci <td>Allows legacy insecure renegotiation between OpenSSL and unpatched 58081cb0ef41Sopenharmony_ci clients or servers. See 58091cb0ef41Sopenharmony_ci <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> 58101cb0ef41Sopenharmony_ci </tr> 58111cb0ef41Sopenharmony_ci <tr> 58121cb0ef41Sopenharmony_ci <td><code>SSL_OP_CIPHER_SERVER_PREFERENCE</code></td> 58131cb0ef41Sopenharmony_ci <td>Attempts to use the server's preferences instead of the client's when 58141cb0ef41Sopenharmony_ci selecting a cipher. Behavior depends on protocol version. See 58151cb0ef41Sopenharmony_ci <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> 58161cb0ef41Sopenharmony_ci </tr> 58171cb0ef41Sopenharmony_ci <tr> 58181cb0ef41Sopenharmony_ci <td><code>SSL_OP_CISCO_ANYCONNECT</code></td> 58191cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to use Cisco's "speshul" version of DTLS_BAD_VER.</td> 58201cb0ef41Sopenharmony_ci </tr> 58211cb0ef41Sopenharmony_ci <tr> 58221cb0ef41Sopenharmony_ci <td><code>SSL_OP_COOKIE_EXCHANGE</code></td> 58231cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to turn on cookie exchange.</td> 58241cb0ef41Sopenharmony_ci </tr> 58251cb0ef41Sopenharmony_ci <tr> 58261cb0ef41Sopenharmony_ci <td><code>SSL_OP_CRYPTOPRO_TLSEXT_BUG</code></td> 58271cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to add server-hello extension from an early version 58281cb0ef41Sopenharmony_ci of the cryptopro draft.</td> 58291cb0ef41Sopenharmony_ci </tr> 58301cb0ef41Sopenharmony_ci <tr> 58311cb0ef41Sopenharmony_ci <td><code>SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS</code></td> 58321cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to disable a SSL 3.0/TLS 1.0 vulnerability 58331cb0ef41Sopenharmony_ci workaround added in OpenSSL 0.9.6d.</td> 58341cb0ef41Sopenharmony_ci </tr> 58351cb0ef41Sopenharmony_ci <tr> 58361cb0ef41Sopenharmony_ci <td><code>SSL_OP_LEGACY_SERVER_CONNECT</code></td> 58371cb0ef41Sopenharmony_ci <td>Allows initial connection to servers that do not support RI.</td> 58381cb0ef41Sopenharmony_ci </tr> 58391cb0ef41Sopenharmony_ci <tr> 58401cb0ef41Sopenharmony_ci <td><code>SSL_OP_NO_COMPRESSION</code></td> 58411cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to disable support for SSL/TLS compression.</td> 58421cb0ef41Sopenharmony_ci </tr> 58431cb0ef41Sopenharmony_ci <tr> 58441cb0ef41Sopenharmony_ci <td><code>SSL_OP_NO_ENCRYPT_THEN_MAC</code></td> 58451cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to disable encrypt-then-MAC.</td> 58461cb0ef41Sopenharmony_ci </tr> 58471cb0ef41Sopenharmony_ci <tr> 58481cb0ef41Sopenharmony_ci <td><code>SSL_OP_NO_QUERY_MTU</code></td> 58491cb0ef41Sopenharmony_ci <td></td> 58501cb0ef41Sopenharmony_ci </tr> 58511cb0ef41Sopenharmony_ci <tr> 58521cb0ef41Sopenharmony_ci <td><code>SSL_OP_NO_RENEGOTIATION</code></td> 58531cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to disable renegotiation.</td> 58541cb0ef41Sopenharmony_ci </tr> 58551cb0ef41Sopenharmony_ci <tr> 58561cb0ef41Sopenharmony_ci <td><code>SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION</code></td> 58571cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to always start a new session when performing 58581cb0ef41Sopenharmony_ci renegotiation.</td> 58591cb0ef41Sopenharmony_ci </tr> 58601cb0ef41Sopenharmony_ci <tr> 58611cb0ef41Sopenharmony_ci <td><code>SSL_OP_NO_SSLv2</code></td> 58621cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to turn off SSL v2</td> 58631cb0ef41Sopenharmony_ci </tr> 58641cb0ef41Sopenharmony_ci <tr> 58651cb0ef41Sopenharmony_ci <td><code>SSL_OP_NO_SSLv3</code></td> 58661cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to turn off SSL v3</td> 58671cb0ef41Sopenharmony_ci </tr> 58681cb0ef41Sopenharmony_ci <tr> 58691cb0ef41Sopenharmony_ci <td><code>SSL_OP_NO_TICKET</code></td> 58701cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to disable use of RFC4507bis tickets.</td> 58711cb0ef41Sopenharmony_ci </tr> 58721cb0ef41Sopenharmony_ci <tr> 58731cb0ef41Sopenharmony_ci <td><code>SSL_OP_NO_TLSv1</code></td> 58741cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to turn off TLS v1</td> 58751cb0ef41Sopenharmony_ci </tr> 58761cb0ef41Sopenharmony_ci <tr> 58771cb0ef41Sopenharmony_ci <td><code>SSL_OP_NO_TLSv1_1</code></td> 58781cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to turn off TLS v1.1</td> 58791cb0ef41Sopenharmony_ci </tr> 58801cb0ef41Sopenharmony_ci <tr> 58811cb0ef41Sopenharmony_ci <td><code>SSL_OP_NO_TLSv1_2</code></td> 58821cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to turn off TLS v1.2</td> 58831cb0ef41Sopenharmony_ci </tr> 58841cb0ef41Sopenharmony_ci <tr> 58851cb0ef41Sopenharmony_ci <td><code>SSL_OP_NO_TLSv1_3</code></td> 58861cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to turn off TLS v1.3</td> 58871cb0ef41Sopenharmony_ci </tr> 58881cb0ef41Sopenharmony_ci <tr> 58891cb0ef41Sopenharmony_ci <td><code>SSL_OP_PRIORITIZE_CHACHA</code></td> 58901cb0ef41Sopenharmony_ci <td>Instructs OpenSSL server to prioritize ChaCha20-Poly1305 58911cb0ef41Sopenharmony_ci when the client does. 58921cb0ef41Sopenharmony_ci This option has no effect if 58931cb0ef41Sopenharmony_ci <code>SSL_OP_CIPHER_SERVER_PREFERENCE</code> 58941cb0ef41Sopenharmony_ci is not enabled.</td> 58951cb0ef41Sopenharmony_ci </tr> 58961cb0ef41Sopenharmony_ci <tr> 58971cb0ef41Sopenharmony_ci <td><code>SSL_OP_TLS_ROLLBACK_BUG</code></td> 58981cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to disable version rollback attack detection.</td> 58991cb0ef41Sopenharmony_ci </tr> 59001cb0ef41Sopenharmony_ci</tbody></table> 59011cb0ef41Sopenharmony_ci<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> 59021cb0ef41Sopenharmony_ci<table> 59031cb0ef41Sopenharmony_ci <tbody><tr> 59041cb0ef41Sopenharmony_ci <th>Constant</th> 59051cb0ef41Sopenharmony_ci <th>Description</th> 59061cb0ef41Sopenharmony_ci </tr> 59071cb0ef41Sopenharmony_ci <tr> 59081cb0ef41Sopenharmony_ci <td><code>ENGINE_METHOD_RSA</code></td> 59091cb0ef41Sopenharmony_ci <td>Limit engine usage to RSA</td> 59101cb0ef41Sopenharmony_ci </tr> 59111cb0ef41Sopenharmony_ci <tr> 59121cb0ef41Sopenharmony_ci <td><code>ENGINE_METHOD_DSA</code></td> 59131cb0ef41Sopenharmony_ci <td>Limit engine usage to DSA</td> 59141cb0ef41Sopenharmony_ci </tr> 59151cb0ef41Sopenharmony_ci <tr> 59161cb0ef41Sopenharmony_ci <td><code>ENGINE_METHOD_DH</code></td> 59171cb0ef41Sopenharmony_ci <td>Limit engine usage to DH</td> 59181cb0ef41Sopenharmony_ci </tr> 59191cb0ef41Sopenharmony_ci <tr> 59201cb0ef41Sopenharmony_ci <td><code>ENGINE_METHOD_RAND</code></td> 59211cb0ef41Sopenharmony_ci <td>Limit engine usage to RAND</td> 59221cb0ef41Sopenharmony_ci </tr> 59231cb0ef41Sopenharmony_ci <tr> 59241cb0ef41Sopenharmony_ci <td><code>ENGINE_METHOD_EC</code></td> 59251cb0ef41Sopenharmony_ci <td>Limit engine usage to EC</td> 59261cb0ef41Sopenharmony_ci </tr> 59271cb0ef41Sopenharmony_ci <tr> 59281cb0ef41Sopenharmony_ci <td><code>ENGINE_METHOD_CIPHERS</code></td> 59291cb0ef41Sopenharmony_ci <td>Limit engine usage to CIPHERS</td> 59301cb0ef41Sopenharmony_ci </tr> 59311cb0ef41Sopenharmony_ci <tr> 59321cb0ef41Sopenharmony_ci <td><code>ENGINE_METHOD_DIGESTS</code></td> 59331cb0ef41Sopenharmony_ci <td>Limit engine usage to DIGESTS</td> 59341cb0ef41Sopenharmony_ci </tr> 59351cb0ef41Sopenharmony_ci <tr> 59361cb0ef41Sopenharmony_ci <td><code>ENGINE_METHOD_PKEY_METHS</code></td> 59371cb0ef41Sopenharmony_ci <td>Limit engine usage to PKEY_METHDS</td> 59381cb0ef41Sopenharmony_ci </tr> 59391cb0ef41Sopenharmony_ci <tr> 59401cb0ef41Sopenharmony_ci <td><code>ENGINE_METHOD_PKEY_ASN1_METHS</code></td> 59411cb0ef41Sopenharmony_ci <td>Limit engine usage to PKEY_ASN1_METHS</td> 59421cb0ef41Sopenharmony_ci </tr> 59431cb0ef41Sopenharmony_ci <tr> 59441cb0ef41Sopenharmony_ci <td><code>ENGINE_METHOD_ALL</code></td> 59451cb0ef41Sopenharmony_ci <td></td> 59461cb0ef41Sopenharmony_ci </tr> 59471cb0ef41Sopenharmony_ci <tr> 59481cb0ef41Sopenharmony_ci <td><code>ENGINE_METHOD_NONE</code></td> 59491cb0ef41Sopenharmony_ci <td></td> 59501cb0ef41Sopenharmony_ci </tr> 59511cb0ef41Sopenharmony_ci</tbody></table> 59521cb0ef41Sopenharmony_ci<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> 59531cb0ef41Sopenharmony_ci<table> 59541cb0ef41Sopenharmony_ci <tbody><tr> 59551cb0ef41Sopenharmony_ci <th>Constant</th> 59561cb0ef41Sopenharmony_ci <th>Description</th> 59571cb0ef41Sopenharmony_ci </tr> 59581cb0ef41Sopenharmony_ci <tr> 59591cb0ef41Sopenharmony_ci <td><code>DH_CHECK_P_NOT_SAFE_PRIME</code></td> 59601cb0ef41Sopenharmony_ci <td></td> 59611cb0ef41Sopenharmony_ci </tr> 59621cb0ef41Sopenharmony_ci <tr> 59631cb0ef41Sopenharmony_ci <td><code>DH_CHECK_P_NOT_PRIME</code></td> 59641cb0ef41Sopenharmony_ci <td></td> 59651cb0ef41Sopenharmony_ci </tr> 59661cb0ef41Sopenharmony_ci <tr> 59671cb0ef41Sopenharmony_ci <td><code>DH_UNABLE_TO_CHECK_GENERATOR</code></td> 59681cb0ef41Sopenharmony_ci <td></td> 59691cb0ef41Sopenharmony_ci </tr> 59701cb0ef41Sopenharmony_ci <tr> 59711cb0ef41Sopenharmony_ci <td><code>DH_NOT_SUITABLE_GENERATOR</code></td> 59721cb0ef41Sopenharmony_ci <td></td> 59731cb0ef41Sopenharmony_ci </tr> 59741cb0ef41Sopenharmony_ci <tr> 59751cb0ef41Sopenharmony_ci <td><code>ALPN_ENABLED</code></td> 59761cb0ef41Sopenharmony_ci <td></td> 59771cb0ef41Sopenharmony_ci </tr> 59781cb0ef41Sopenharmony_ci <tr> 59791cb0ef41Sopenharmony_ci <td><code>RSA_PKCS1_PADDING</code></td> 59801cb0ef41Sopenharmony_ci <td></td> 59811cb0ef41Sopenharmony_ci </tr> 59821cb0ef41Sopenharmony_ci <tr> 59831cb0ef41Sopenharmony_ci <td><code>RSA_SSLV23_PADDING</code></td> 59841cb0ef41Sopenharmony_ci <td></td> 59851cb0ef41Sopenharmony_ci </tr> 59861cb0ef41Sopenharmony_ci <tr> 59871cb0ef41Sopenharmony_ci <td><code>RSA_NO_PADDING</code></td> 59881cb0ef41Sopenharmony_ci <td></td> 59891cb0ef41Sopenharmony_ci </tr> 59901cb0ef41Sopenharmony_ci <tr> 59911cb0ef41Sopenharmony_ci <td><code>RSA_PKCS1_OAEP_PADDING</code></td> 59921cb0ef41Sopenharmony_ci <td></td> 59931cb0ef41Sopenharmony_ci </tr> 59941cb0ef41Sopenharmony_ci <tr> 59951cb0ef41Sopenharmony_ci <td><code>RSA_X931_PADDING</code></td> 59961cb0ef41Sopenharmony_ci <td></td> 59971cb0ef41Sopenharmony_ci </tr> 59981cb0ef41Sopenharmony_ci <tr> 59991cb0ef41Sopenharmony_ci <td><code>RSA_PKCS1_PSS_PADDING</code></td> 60001cb0ef41Sopenharmony_ci <td></td> 60011cb0ef41Sopenharmony_ci </tr> 60021cb0ef41Sopenharmony_ci <tr> 60031cb0ef41Sopenharmony_ci <td><code>RSA_PSS_SALTLEN_DIGEST</code></td> 60041cb0ef41Sopenharmony_ci <td>Sets the salt length for <code>RSA_PKCS1_PSS_PADDING</code> to the 60051cb0ef41Sopenharmony_ci digest size when signing or verifying.</td> 60061cb0ef41Sopenharmony_ci </tr> 60071cb0ef41Sopenharmony_ci <tr> 60081cb0ef41Sopenharmony_ci <td><code>RSA_PSS_SALTLEN_MAX_SIGN</code></td> 60091cb0ef41Sopenharmony_ci <td>Sets the salt length for <code>RSA_PKCS1_PSS_PADDING</code> to the 60101cb0ef41Sopenharmony_ci maximum permissible value when signing data.</td> 60111cb0ef41Sopenharmony_ci </tr> 60121cb0ef41Sopenharmony_ci <tr> 60131cb0ef41Sopenharmony_ci <td><code>RSA_PSS_SALTLEN_AUTO</code></td> 60141cb0ef41Sopenharmony_ci <td>Causes the salt length for <code>RSA_PKCS1_PSS_PADDING</code> to be 60151cb0ef41Sopenharmony_ci determined automatically when verifying a signature.</td> 60161cb0ef41Sopenharmony_ci </tr> 60171cb0ef41Sopenharmony_ci <tr> 60181cb0ef41Sopenharmony_ci <td><code>POINT_CONVERSION_COMPRESSED</code></td> 60191cb0ef41Sopenharmony_ci <td></td> 60201cb0ef41Sopenharmony_ci </tr> 60211cb0ef41Sopenharmony_ci <tr> 60221cb0ef41Sopenharmony_ci <td><code>POINT_CONVERSION_UNCOMPRESSED</code></td> 60231cb0ef41Sopenharmony_ci <td></td> 60241cb0ef41Sopenharmony_ci </tr> 60251cb0ef41Sopenharmony_ci <tr> 60261cb0ef41Sopenharmony_ci <td><code>POINT_CONVERSION_HYBRID</code></td> 60271cb0ef41Sopenharmony_ci <td></td> 60281cb0ef41Sopenharmony_ci </tr> 60291cb0ef41Sopenharmony_ci</tbody></table> 60301cb0ef41Sopenharmony_ci<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> 60311cb0ef41Sopenharmony_ci<table> 60321cb0ef41Sopenharmony_ci <tbody><tr> 60331cb0ef41Sopenharmony_ci <th>Constant</th> 60341cb0ef41Sopenharmony_ci <th>Description</th> 60351cb0ef41Sopenharmony_ci </tr> 60361cb0ef41Sopenharmony_ci <tr> 60371cb0ef41Sopenharmony_ci <td><code>defaultCoreCipherList</code></td> 60381cb0ef41Sopenharmony_ci <td>Specifies the built-in default cipher list used by Node.js.</td> 60391cb0ef41Sopenharmony_ci </tr> 60401cb0ef41Sopenharmony_ci <tr> 60411cb0ef41Sopenharmony_ci <td><code>defaultCipherList</code></td> 60421cb0ef41Sopenharmony_ci <td>Specifies the active default cipher list used by the current Node.js 60431cb0ef41Sopenharmony_ci process.</td> 60441cb0ef41Sopenharmony_ci </tr> 60451cb0ef41Sopenharmony_ci</tbody></table></section> 60461cb0ef41Sopenharmony_ci <!-- API END --> 60471cb0ef41Sopenharmony_ci </div> 60481cb0ef41Sopenharmony_ci </div> 60491cb0ef41Sopenharmony_ci </div> 60501cb0ef41Sopenharmony_ci</body> 60511cb0ef41Sopenharmony_ci</html> 6052