11cb0ef41Sopenharmony_ci# Crypto 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci<!--introduced_in=v0.3.6--> 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci> Stability: 2 - Stable 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci<!-- source_link=lib/crypto.js --> 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciThe `node:crypto` module provides cryptographic functionality that includes a 101cb0ef41Sopenharmony_ciset of wrappers for OpenSSL's hash, HMAC, cipher, decipher, sign, and verify 111cb0ef41Sopenharmony_cifunctions. 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ci```mjs 141cb0ef41Sopenharmony_ciconst { createHmac } = await import('node:crypto'); 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ciconst secret = 'abcdefg'; 171cb0ef41Sopenharmony_ciconst hash = createHmac('sha256', secret) 181cb0ef41Sopenharmony_ci .update('I love cupcakes') 191cb0ef41Sopenharmony_ci .digest('hex'); 201cb0ef41Sopenharmony_ciconsole.log(hash); 211cb0ef41Sopenharmony_ci// Prints: 221cb0ef41Sopenharmony_ci// c0fa1bc00531bd78ef38c628449c5102aeabd49b5dc3a2a516ea6ea959d6658e 231cb0ef41Sopenharmony_ci``` 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ci```cjs 261cb0ef41Sopenharmony_ciconst { createHmac } = require('node:crypto'); 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ciconst secret = 'abcdefg'; 291cb0ef41Sopenharmony_ciconst hash = createHmac('sha256', secret) 301cb0ef41Sopenharmony_ci .update('I love cupcakes') 311cb0ef41Sopenharmony_ci .digest('hex'); 321cb0ef41Sopenharmony_ciconsole.log(hash); 331cb0ef41Sopenharmony_ci// Prints: 341cb0ef41Sopenharmony_ci// c0fa1bc00531bd78ef38c628449c5102aeabd49b5dc3a2a516ea6ea959d6658e 351cb0ef41Sopenharmony_ci``` 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci## Determining if crypto support is unavailable 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ciIt is possible for Node.js to be built without including support for the 401cb0ef41Sopenharmony_ci`node:crypto` module. In such cases, attempting to `import` from `crypto` or 411cb0ef41Sopenharmony_cicalling `require('node:crypto')` will result in an error being thrown. 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ciWhen using CommonJS, the error thrown can be caught using try/catch: 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci<!-- eslint-skip --> 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ci```cjs 481cb0ef41Sopenharmony_cilet crypto; 491cb0ef41Sopenharmony_citry { 501cb0ef41Sopenharmony_ci crypto = require('node:crypto'); 511cb0ef41Sopenharmony_ci} catch (err) { 521cb0ef41Sopenharmony_ci console.error('crypto support is disabled!'); 531cb0ef41Sopenharmony_ci} 541cb0ef41Sopenharmony_ci``` 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ciWhen using the lexical ESM `import` keyword, the error can only be 571cb0ef41Sopenharmony_cicaught if a handler for `process.on('uncaughtException')` is registered 581cb0ef41Sopenharmony_ci_before_ any attempt to load the module is made (using, for instance, 591cb0ef41Sopenharmony_cia preload module). 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ciWhen using ESM, if there is a chance that the code may be run on a build 621cb0ef41Sopenharmony_ciof Node.js where crypto support is not enabled, consider using the 631cb0ef41Sopenharmony_ci[`import()`][] function instead of the lexical `import` keyword: 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_ci```mjs 661cb0ef41Sopenharmony_cilet crypto; 671cb0ef41Sopenharmony_citry { 681cb0ef41Sopenharmony_ci crypto = await import('node:crypto'); 691cb0ef41Sopenharmony_ci} catch (err) { 701cb0ef41Sopenharmony_ci console.error('crypto support is disabled!'); 711cb0ef41Sopenharmony_ci} 721cb0ef41Sopenharmony_ci``` 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ci## Class: `Certificate` 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci<!-- YAML 771cb0ef41Sopenharmony_ciadded: v0.11.8 781cb0ef41Sopenharmony_ci--> 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ciSPKAC is a Certificate Signing Request mechanism originally implemented by 811cb0ef41Sopenharmony_ciNetscape and was specified formally as part of HTML5's `keygen` element. 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ci`<keygen>` is deprecated since [HTML 5.2][] and new projects 841cb0ef41Sopenharmony_cishould not use this element anymore. 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_ciThe `node:crypto` module provides the `Certificate` class for working with SPKAC 871cb0ef41Sopenharmony_cidata. The most common usage is handling output generated by the HTML5 881cb0ef41Sopenharmony_ci`<keygen>` element. Node.js uses [OpenSSL's SPKAC implementation][] internally. 891cb0ef41Sopenharmony_ci 901cb0ef41Sopenharmony_ci### Static method: `Certificate.exportChallenge(spkac[, encoding])` 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_ci<!-- YAML 931cb0ef41Sopenharmony_ciadded: v9.0.0 941cb0ef41Sopenharmony_cichanges: 951cb0ef41Sopenharmony_ci - version: v15.0.0 961cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/35093 971cb0ef41Sopenharmony_ci description: The spkac argument can be an ArrayBuffer. Limited the size of 981cb0ef41Sopenharmony_ci the spkac argument to a maximum of 2**31 - 1 bytes. 991cb0ef41Sopenharmony_ci--> 1001cb0ef41Sopenharmony_ci 1011cb0ef41Sopenharmony_ci* `spkac` {string|ArrayBuffer|Buffer|TypedArray|DataView} 1021cb0ef41Sopenharmony_ci* `encoding` {string} The [encoding][] of the `spkac` string. 1031cb0ef41Sopenharmony_ci* Returns: {Buffer} The challenge component of the `spkac` data structure, which 1041cb0ef41Sopenharmony_ci includes a public key and a challenge. 1051cb0ef41Sopenharmony_ci 1061cb0ef41Sopenharmony_ci```mjs 1071cb0ef41Sopenharmony_ciconst { Certificate } = await import('node:crypto'); 1081cb0ef41Sopenharmony_ciconst spkac = getSpkacSomehow(); 1091cb0ef41Sopenharmony_ciconst challenge = Certificate.exportChallenge(spkac); 1101cb0ef41Sopenharmony_ciconsole.log(challenge.toString('utf8')); 1111cb0ef41Sopenharmony_ci// Prints: the challenge as a UTF8 string 1121cb0ef41Sopenharmony_ci``` 1131cb0ef41Sopenharmony_ci 1141cb0ef41Sopenharmony_ci```cjs 1151cb0ef41Sopenharmony_ciconst { Certificate } = require('node:crypto'); 1161cb0ef41Sopenharmony_ciconst spkac = getSpkacSomehow(); 1171cb0ef41Sopenharmony_ciconst challenge = Certificate.exportChallenge(spkac); 1181cb0ef41Sopenharmony_ciconsole.log(challenge.toString('utf8')); 1191cb0ef41Sopenharmony_ci// Prints: the challenge as a UTF8 string 1201cb0ef41Sopenharmony_ci``` 1211cb0ef41Sopenharmony_ci 1221cb0ef41Sopenharmony_ci### Static method: `Certificate.exportPublicKey(spkac[, encoding])` 1231cb0ef41Sopenharmony_ci 1241cb0ef41Sopenharmony_ci<!-- YAML 1251cb0ef41Sopenharmony_ciadded: v9.0.0 1261cb0ef41Sopenharmony_cichanges: 1271cb0ef41Sopenharmony_ci - version: v15.0.0 1281cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/35093 1291cb0ef41Sopenharmony_ci description: The spkac argument can be an ArrayBuffer. Limited the size of 1301cb0ef41Sopenharmony_ci the spkac argument to a maximum of 2**31 - 1 bytes. 1311cb0ef41Sopenharmony_ci--> 1321cb0ef41Sopenharmony_ci 1331cb0ef41Sopenharmony_ci* `spkac` {string|ArrayBuffer|Buffer|TypedArray|DataView} 1341cb0ef41Sopenharmony_ci* `encoding` {string} The [encoding][] of the `spkac` string. 1351cb0ef41Sopenharmony_ci* Returns: {Buffer} The public key component of the `spkac` data structure, 1361cb0ef41Sopenharmony_ci which includes a public key and a challenge. 1371cb0ef41Sopenharmony_ci 1381cb0ef41Sopenharmony_ci```mjs 1391cb0ef41Sopenharmony_ciconst { Certificate } = await import('node:crypto'); 1401cb0ef41Sopenharmony_ciconst spkac = getSpkacSomehow(); 1411cb0ef41Sopenharmony_ciconst publicKey = Certificate.exportPublicKey(spkac); 1421cb0ef41Sopenharmony_ciconsole.log(publicKey); 1431cb0ef41Sopenharmony_ci// Prints: the public key as <Buffer ...> 1441cb0ef41Sopenharmony_ci``` 1451cb0ef41Sopenharmony_ci 1461cb0ef41Sopenharmony_ci```cjs 1471cb0ef41Sopenharmony_ciconst { Certificate } = require('node:crypto'); 1481cb0ef41Sopenharmony_ciconst spkac = getSpkacSomehow(); 1491cb0ef41Sopenharmony_ciconst publicKey = Certificate.exportPublicKey(spkac); 1501cb0ef41Sopenharmony_ciconsole.log(publicKey); 1511cb0ef41Sopenharmony_ci// Prints: the public key as <Buffer ...> 1521cb0ef41Sopenharmony_ci``` 1531cb0ef41Sopenharmony_ci 1541cb0ef41Sopenharmony_ci### Static method: `Certificate.verifySpkac(spkac[, encoding])` 1551cb0ef41Sopenharmony_ci 1561cb0ef41Sopenharmony_ci<!-- YAML 1571cb0ef41Sopenharmony_ciadded: v9.0.0 1581cb0ef41Sopenharmony_cichanges: 1591cb0ef41Sopenharmony_ci - version: v15.0.0 1601cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/35093 1611cb0ef41Sopenharmony_ci description: The spkac argument can be an ArrayBuffer. Added encoding. 1621cb0ef41Sopenharmony_ci Limited the size of the spkac argument to a maximum of 1631cb0ef41Sopenharmony_ci 2**31 - 1 bytes. 1641cb0ef41Sopenharmony_ci--> 1651cb0ef41Sopenharmony_ci 1661cb0ef41Sopenharmony_ci* `spkac` {string|ArrayBuffer|Buffer|TypedArray|DataView} 1671cb0ef41Sopenharmony_ci* `encoding` {string} The [encoding][] of the `spkac` string. 1681cb0ef41Sopenharmony_ci* Returns: {boolean} `true` if the given `spkac` data structure is valid, 1691cb0ef41Sopenharmony_ci `false` otherwise. 1701cb0ef41Sopenharmony_ci 1711cb0ef41Sopenharmony_ci```mjs 1721cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer'; 1731cb0ef41Sopenharmony_ciconst { Certificate } = await import('node:crypto'); 1741cb0ef41Sopenharmony_ci 1751cb0ef41Sopenharmony_ciconst spkac = getSpkacSomehow(); 1761cb0ef41Sopenharmony_ciconsole.log(Certificate.verifySpkac(Buffer.from(spkac))); 1771cb0ef41Sopenharmony_ci// Prints: true or false 1781cb0ef41Sopenharmony_ci``` 1791cb0ef41Sopenharmony_ci 1801cb0ef41Sopenharmony_ci```cjs 1811cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer'); 1821cb0ef41Sopenharmony_ciconst { Certificate } = require('node:crypto'); 1831cb0ef41Sopenharmony_ci 1841cb0ef41Sopenharmony_ciconst spkac = getSpkacSomehow(); 1851cb0ef41Sopenharmony_ciconsole.log(Certificate.verifySpkac(Buffer.from(spkac))); 1861cb0ef41Sopenharmony_ci// Prints: true or false 1871cb0ef41Sopenharmony_ci``` 1881cb0ef41Sopenharmony_ci 1891cb0ef41Sopenharmony_ci### Legacy API 1901cb0ef41Sopenharmony_ci 1911cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated 1921cb0ef41Sopenharmony_ci 1931cb0ef41Sopenharmony_ciAs a legacy interface, it is possible to create new instances of 1941cb0ef41Sopenharmony_cithe `crypto.Certificate` class as illustrated in the examples below. 1951cb0ef41Sopenharmony_ci 1961cb0ef41Sopenharmony_ci#### `new crypto.Certificate()` 1971cb0ef41Sopenharmony_ci 1981cb0ef41Sopenharmony_ciInstances of the `Certificate` class can be created using the `new` keyword 1991cb0ef41Sopenharmony_cior by calling `crypto.Certificate()` as a function: 2001cb0ef41Sopenharmony_ci 2011cb0ef41Sopenharmony_ci```mjs 2021cb0ef41Sopenharmony_ciconst { Certificate } = await import('node:crypto'); 2031cb0ef41Sopenharmony_ci 2041cb0ef41Sopenharmony_ciconst cert1 = new Certificate(); 2051cb0ef41Sopenharmony_ciconst cert2 = Certificate(); 2061cb0ef41Sopenharmony_ci``` 2071cb0ef41Sopenharmony_ci 2081cb0ef41Sopenharmony_ci```cjs 2091cb0ef41Sopenharmony_ciconst { Certificate } = require('node:crypto'); 2101cb0ef41Sopenharmony_ci 2111cb0ef41Sopenharmony_ciconst cert1 = new Certificate(); 2121cb0ef41Sopenharmony_ciconst cert2 = Certificate(); 2131cb0ef41Sopenharmony_ci``` 2141cb0ef41Sopenharmony_ci 2151cb0ef41Sopenharmony_ci#### `certificate.exportChallenge(spkac[, encoding])` 2161cb0ef41Sopenharmony_ci 2171cb0ef41Sopenharmony_ci<!-- YAML 2181cb0ef41Sopenharmony_ciadded: v0.11.8 2191cb0ef41Sopenharmony_ci--> 2201cb0ef41Sopenharmony_ci 2211cb0ef41Sopenharmony_ci* `spkac` {string|ArrayBuffer|Buffer|TypedArray|DataView} 2221cb0ef41Sopenharmony_ci* `encoding` {string} The [encoding][] of the `spkac` string. 2231cb0ef41Sopenharmony_ci* Returns: {Buffer} The challenge component of the `spkac` data structure, which 2241cb0ef41Sopenharmony_ci includes a public key and a challenge. 2251cb0ef41Sopenharmony_ci 2261cb0ef41Sopenharmony_ci```mjs 2271cb0ef41Sopenharmony_ciconst { Certificate } = await import('node:crypto'); 2281cb0ef41Sopenharmony_ciconst cert = Certificate(); 2291cb0ef41Sopenharmony_ciconst spkac = getSpkacSomehow(); 2301cb0ef41Sopenharmony_ciconst challenge = cert.exportChallenge(spkac); 2311cb0ef41Sopenharmony_ciconsole.log(challenge.toString('utf8')); 2321cb0ef41Sopenharmony_ci// Prints: the challenge as a UTF8 string 2331cb0ef41Sopenharmony_ci``` 2341cb0ef41Sopenharmony_ci 2351cb0ef41Sopenharmony_ci```cjs 2361cb0ef41Sopenharmony_ciconst { Certificate } = require('node:crypto'); 2371cb0ef41Sopenharmony_ciconst cert = Certificate(); 2381cb0ef41Sopenharmony_ciconst spkac = getSpkacSomehow(); 2391cb0ef41Sopenharmony_ciconst challenge = cert.exportChallenge(spkac); 2401cb0ef41Sopenharmony_ciconsole.log(challenge.toString('utf8')); 2411cb0ef41Sopenharmony_ci// Prints: the challenge as a UTF8 string 2421cb0ef41Sopenharmony_ci``` 2431cb0ef41Sopenharmony_ci 2441cb0ef41Sopenharmony_ci#### `certificate.exportPublicKey(spkac[, encoding])` 2451cb0ef41Sopenharmony_ci 2461cb0ef41Sopenharmony_ci<!-- YAML 2471cb0ef41Sopenharmony_ciadded: v0.11.8 2481cb0ef41Sopenharmony_ci--> 2491cb0ef41Sopenharmony_ci 2501cb0ef41Sopenharmony_ci* `spkac` {string|ArrayBuffer|Buffer|TypedArray|DataView} 2511cb0ef41Sopenharmony_ci* `encoding` {string} The [encoding][] of the `spkac` string. 2521cb0ef41Sopenharmony_ci* Returns: {Buffer} The public key component of the `spkac` data structure, 2531cb0ef41Sopenharmony_ci which includes a public key and a challenge. 2541cb0ef41Sopenharmony_ci 2551cb0ef41Sopenharmony_ci```mjs 2561cb0ef41Sopenharmony_ciconst { Certificate } = await import('node:crypto'); 2571cb0ef41Sopenharmony_ciconst cert = Certificate(); 2581cb0ef41Sopenharmony_ciconst spkac = getSpkacSomehow(); 2591cb0ef41Sopenharmony_ciconst publicKey = cert.exportPublicKey(spkac); 2601cb0ef41Sopenharmony_ciconsole.log(publicKey); 2611cb0ef41Sopenharmony_ci// Prints: the public key as <Buffer ...> 2621cb0ef41Sopenharmony_ci``` 2631cb0ef41Sopenharmony_ci 2641cb0ef41Sopenharmony_ci```cjs 2651cb0ef41Sopenharmony_ciconst { Certificate } = require('node:crypto'); 2661cb0ef41Sopenharmony_ciconst cert = Certificate(); 2671cb0ef41Sopenharmony_ciconst spkac = getSpkacSomehow(); 2681cb0ef41Sopenharmony_ciconst publicKey = cert.exportPublicKey(spkac); 2691cb0ef41Sopenharmony_ciconsole.log(publicKey); 2701cb0ef41Sopenharmony_ci// Prints: the public key as <Buffer ...> 2711cb0ef41Sopenharmony_ci``` 2721cb0ef41Sopenharmony_ci 2731cb0ef41Sopenharmony_ci#### `certificate.verifySpkac(spkac[, encoding])` 2741cb0ef41Sopenharmony_ci 2751cb0ef41Sopenharmony_ci<!-- YAML 2761cb0ef41Sopenharmony_ciadded: v0.11.8 2771cb0ef41Sopenharmony_ci--> 2781cb0ef41Sopenharmony_ci 2791cb0ef41Sopenharmony_ci* `spkac` {string|ArrayBuffer|Buffer|TypedArray|DataView} 2801cb0ef41Sopenharmony_ci* `encoding` {string} The [encoding][] of the `spkac` string. 2811cb0ef41Sopenharmony_ci* Returns: {boolean} `true` if the given `spkac` data structure is valid, 2821cb0ef41Sopenharmony_ci `false` otherwise. 2831cb0ef41Sopenharmony_ci 2841cb0ef41Sopenharmony_ci```mjs 2851cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer'; 2861cb0ef41Sopenharmony_ciconst { Certificate } = await import('node:crypto'); 2871cb0ef41Sopenharmony_ci 2881cb0ef41Sopenharmony_ciconst cert = Certificate(); 2891cb0ef41Sopenharmony_ciconst spkac = getSpkacSomehow(); 2901cb0ef41Sopenharmony_ciconsole.log(cert.verifySpkac(Buffer.from(spkac))); 2911cb0ef41Sopenharmony_ci// Prints: true or false 2921cb0ef41Sopenharmony_ci``` 2931cb0ef41Sopenharmony_ci 2941cb0ef41Sopenharmony_ci```cjs 2951cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer'); 2961cb0ef41Sopenharmony_ciconst { Certificate } = require('node:crypto'); 2971cb0ef41Sopenharmony_ci 2981cb0ef41Sopenharmony_ciconst cert = Certificate(); 2991cb0ef41Sopenharmony_ciconst spkac = getSpkacSomehow(); 3001cb0ef41Sopenharmony_ciconsole.log(cert.verifySpkac(Buffer.from(spkac))); 3011cb0ef41Sopenharmony_ci// Prints: true or false 3021cb0ef41Sopenharmony_ci``` 3031cb0ef41Sopenharmony_ci 3041cb0ef41Sopenharmony_ci## Class: `Cipher` 3051cb0ef41Sopenharmony_ci 3061cb0ef41Sopenharmony_ci<!-- YAML 3071cb0ef41Sopenharmony_ciadded: v0.1.94 3081cb0ef41Sopenharmony_ci--> 3091cb0ef41Sopenharmony_ci 3101cb0ef41Sopenharmony_ci* Extends: {stream.Transform} 3111cb0ef41Sopenharmony_ci 3121cb0ef41Sopenharmony_ciInstances of the `Cipher` class are used to encrypt data. The class can be 3131cb0ef41Sopenharmony_ciused in one of two ways: 3141cb0ef41Sopenharmony_ci 3151cb0ef41Sopenharmony_ci* As a [stream][] that is both readable and writable, where plain unencrypted 3161cb0ef41Sopenharmony_ci data is written to produce encrypted data on the readable side, or 3171cb0ef41Sopenharmony_ci* Using the [`cipher.update()`][] and [`cipher.final()`][] methods to produce 3181cb0ef41Sopenharmony_ci the encrypted data. 3191cb0ef41Sopenharmony_ci 3201cb0ef41Sopenharmony_ciThe [`crypto.createCipher()`][] or [`crypto.createCipheriv()`][] methods are 3211cb0ef41Sopenharmony_ciused to create `Cipher` instances. `Cipher` objects are not to be created 3221cb0ef41Sopenharmony_cidirectly using the `new` keyword. 3231cb0ef41Sopenharmony_ci 3241cb0ef41Sopenharmony_ciExample: Using `Cipher` objects as streams: 3251cb0ef41Sopenharmony_ci 3261cb0ef41Sopenharmony_ci```mjs 3271cb0ef41Sopenharmony_ciconst { 3281cb0ef41Sopenharmony_ci scrypt, 3291cb0ef41Sopenharmony_ci randomFill, 3301cb0ef41Sopenharmony_ci createCipheriv, 3311cb0ef41Sopenharmony_ci} = await import('node:crypto'); 3321cb0ef41Sopenharmony_ci 3331cb0ef41Sopenharmony_ciconst algorithm = 'aes-192-cbc'; 3341cb0ef41Sopenharmony_ciconst password = 'Password used to generate key'; 3351cb0ef41Sopenharmony_ci 3361cb0ef41Sopenharmony_ci// First, we'll generate the key. The key length is dependent on the algorithm. 3371cb0ef41Sopenharmony_ci// In this case for aes192, it is 24 bytes (192 bits). 3381cb0ef41Sopenharmony_ciscrypt(password, 'salt', 24, (err, key) => { 3391cb0ef41Sopenharmony_ci if (err) throw err; 3401cb0ef41Sopenharmony_ci // Then, we'll generate a random initialization vector 3411cb0ef41Sopenharmony_ci randomFill(new Uint8Array(16), (err, iv) => { 3421cb0ef41Sopenharmony_ci if (err) throw err; 3431cb0ef41Sopenharmony_ci 3441cb0ef41Sopenharmony_ci // Once we have the key and iv, we can create and use the cipher... 3451cb0ef41Sopenharmony_ci const cipher = createCipheriv(algorithm, key, iv); 3461cb0ef41Sopenharmony_ci 3471cb0ef41Sopenharmony_ci let encrypted = ''; 3481cb0ef41Sopenharmony_ci cipher.setEncoding('hex'); 3491cb0ef41Sopenharmony_ci 3501cb0ef41Sopenharmony_ci cipher.on('data', (chunk) => encrypted += chunk); 3511cb0ef41Sopenharmony_ci cipher.on('end', () => console.log(encrypted)); 3521cb0ef41Sopenharmony_ci 3531cb0ef41Sopenharmony_ci cipher.write('some clear text data'); 3541cb0ef41Sopenharmony_ci cipher.end(); 3551cb0ef41Sopenharmony_ci }); 3561cb0ef41Sopenharmony_ci}); 3571cb0ef41Sopenharmony_ci``` 3581cb0ef41Sopenharmony_ci 3591cb0ef41Sopenharmony_ci```cjs 3601cb0ef41Sopenharmony_ciconst { 3611cb0ef41Sopenharmony_ci scrypt, 3621cb0ef41Sopenharmony_ci randomFill, 3631cb0ef41Sopenharmony_ci createCipheriv, 3641cb0ef41Sopenharmony_ci} = require('node:crypto'); 3651cb0ef41Sopenharmony_ci 3661cb0ef41Sopenharmony_ciconst algorithm = 'aes-192-cbc'; 3671cb0ef41Sopenharmony_ciconst password = 'Password used to generate key'; 3681cb0ef41Sopenharmony_ci 3691cb0ef41Sopenharmony_ci// First, we'll generate the key. The key length is dependent on the algorithm. 3701cb0ef41Sopenharmony_ci// In this case for aes192, it is 24 bytes (192 bits). 3711cb0ef41Sopenharmony_ciscrypt(password, 'salt', 24, (err, key) => { 3721cb0ef41Sopenharmony_ci if (err) throw err; 3731cb0ef41Sopenharmony_ci // Then, we'll generate a random initialization vector 3741cb0ef41Sopenharmony_ci randomFill(new Uint8Array(16), (err, iv) => { 3751cb0ef41Sopenharmony_ci if (err) throw err; 3761cb0ef41Sopenharmony_ci 3771cb0ef41Sopenharmony_ci // Once we have the key and iv, we can create and use the cipher... 3781cb0ef41Sopenharmony_ci const cipher = createCipheriv(algorithm, key, iv); 3791cb0ef41Sopenharmony_ci 3801cb0ef41Sopenharmony_ci let encrypted = ''; 3811cb0ef41Sopenharmony_ci cipher.setEncoding('hex'); 3821cb0ef41Sopenharmony_ci 3831cb0ef41Sopenharmony_ci cipher.on('data', (chunk) => encrypted += chunk); 3841cb0ef41Sopenharmony_ci cipher.on('end', () => console.log(encrypted)); 3851cb0ef41Sopenharmony_ci 3861cb0ef41Sopenharmony_ci cipher.write('some clear text data'); 3871cb0ef41Sopenharmony_ci cipher.end(); 3881cb0ef41Sopenharmony_ci }); 3891cb0ef41Sopenharmony_ci}); 3901cb0ef41Sopenharmony_ci``` 3911cb0ef41Sopenharmony_ci 3921cb0ef41Sopenharmony_ciExample: Using `Cipher` and piped streams: 3931cb0ef41Sopenharmony_ci 3941cb0ef41Sopenharmony_ci```mjs 3951cb0ef41Sopenharmony_ciimport { 3961cb0ef41Sopenharmony_ci createReadStream, 3971cb0ef41Sopenharmony_ci createWriteStream, 3981cb0ef41Sopenharmony_ci} from 'node:fs'; 3991cb0ef41Sopenharmony_ci 4001cb0ef41Sopenharmony_ciimport { 4011cb0ef41Sopenharmony_ci pipeline, 4021cb0ef41Sopenharmony_ci} from 'node:stream'; 4031cb0ef41Sopenharmony_ci 4041cb0ef41Sopenharmony_ciconst { 4051cb0ef41Sopenharmony_ci scrypt, 4061cb0ef41Sopenharmony_ci randomFill, 4071cb0ef41Sopenharmony_ci createCipheriv, 4081cb0ef41Sopenharmony_ci} = await import('node:crypto'); 4091cb0ef41Sopenharmony_ci 4101cb0ef41Sopenharmony_ciconst algorithm = 'aes-192-cbc'; 4111cb0ef41Sopenharmony_ciconst password = 'Password used to generate key'; 4121cb0ef41Sopenharmony_ci 4131cb0ef41Sopenharmony_ci// First, we'll generate the key. The key length is dependent on the algorithm. 4141cb0ef41Sopenharmony_ci// In this case for aes192, it is 24 bytes (192 bits). 4151cb0ef41Sopenharmony_ciscrypt(password, 'salt', 24, (err, key) => { 4161cb0ef41Sopenharmony_ci if (err) throw err; 4171cb0ef41Sopenharmony_ci // Then, we'll generate a random initialization vector 4181cb0ef41Sopenharmony_ci randomFill(new Uint8Array(16), (err, iv) => { 4191cb0ef41Sopenharmony_ci if (err) throw err; 4201cb0ef41Sopenharmony_ci 4211cb0ef41Sopenharmony_ci const cipher = createCipheriv(algorithm, key, iv); 4221cb0ef41Sopenharmony_ci 4231cb0ef41Sopenharmony_ci const input = createReadStream('test.js'); 4241cb0ef41Sopenharmony_ci const output = createWriteStream('test.enc'); 4251cb0ef41Sopenharmony_ci 4261cb0ef41Sopenharmony_ci pipeline(input, cipher, output, (err) => { 4271cb0ef41Sopenharmony_ci if (err) throw err; 4281cb0ef41Sopenharmony_ci }); 4291cb0ef41Sopenharmony_ci }); 4301cb0ef41Sopenharmony_ci}); 4311cb0ef41Sopenharmony_ci``` 4321cb0ef41Sopenharmony_ci 4331cb0ef41Sopenharmony_ci```cjs 4341cb0ef41Sopenharmony_ciconst { 4351cb0ef41Sopenharmony_ci createReadStream, 4361cb0ef41Sopenharmony_ci createWriteStream, 4371cb0ef41Sopenharmony_ci} = require('node:fs'); 4381cb0ef41Sopenharmony_ci 4391cb0ef41Sopenharmony_ciconst { 4401cb0ef41Sopenharmony_ci pipeline, 4411cb0ef41Sopenharmony_ci} = require('node:stream'); 4421cb0ef41Sopenharmony_ci 4431cb0ef41Sopenharmony_ciconst { 4441cb0ef41Sopenharmony_ci scrypt, 4451cb0ef41Sopenharmony_ci randomFill, 4461cb0ef41Sopenharmony_ci createCipheriv, 4471cb0ef41Sopenharmony_ci} = require('node:crypto'); 4481cb0ef41Sopenharmony_ci 4491cb0ef41Sopenharmony_ciconst algorithm = 'aes-192-cbc'; 4501cb0ef41Sopenharmony_ciconst password = 'Password used to generate key'; 4511cb0ef41Sopenharmony_ci 4521cb0ef41Sopenharmony_ci// First, we'll generate the key. The key length is dependent on the algorithm. 4531cb0ef41Sopenharmony_ci// In this case for aes192, it is 24 bytes (192 bits). 4541cb0ef41Sopenharmony_ciscrypt(password, 'salt', 24, (err, key) => { 4551cb0ef41Sopenharmony_ci if (err) throw err; 4561cb0ef41Sopenharmony_ci // Then, we'll generate a random initialization vector 4571cb0ef41Sopenharmony_ci randomFill(new Uint8Array(16), (err, iv) => { 4581cb0ef41Sopenharmony_ci if (err) throw err; 4591cb0ef41Sopenharmony_ci 4601cb0ef41Sopenharmony_ci const cipher = createCipheriv(algorithm, key, iv); 4611cb0ef41Sopenharmony_ci 4621cb0ef41Sopenharmony_ci const input = createReadStream('test.js'); 4631cb0ef41Sopenharmony_ci const output = createWriteStream('test.enc'); 4641cb0ef41Sopenharmony_ci 4651cb0ef41Sopenharmony_ci pipeline(input, cipher, output, (err) => { 4661cb0ef41Sopenharmony_ci if (err) throw err; 4671cb0ef41Sopenharmony_ci }); 4681cb0ef41Sopenharmony_ci }); 4691cb0ef41Sopenharmony_ci}); 4701cb0ef41Sopenharmony_ci``` 4711cb0ef41Sopenharmony_ci 4721cb0ef41Sopenharmony_ciExample: Using the [`cipher.update()`][] and [`cipher.final()`][] methods: 4731cb0ef41Sopenharmony_ci 4741cb0ef41Sopenharmony_ci```mjs 4751cb0ef41Sopenharmony_ciconst { 4761cb0ef41Sopenharmony_ci scrypt, 4771cb0ef41Sopenharmony_ci randomFill, 4781cb0ef41Sopenharmony_ci createCipheriv, 4791cb0ef41Sopenharmony_ci} = await import('node:crypto'); 4801cb0ef41Sopenharmony_ci 4811cb0ef41Sopenharmony_ciconst algorithm = 'aes-192-cbc'; 4821cb0ef41Sopenharmony_ciconst password = 'Password used to generate key'; 4831cb0ef41Sopenharmony_ci 4841cb0ef41Sopenharmony_ci// First, we'll generate the key. The key length is dependent on the algorithm. 4851cb0ef41Sopenharmony_ci// In this case for aes192, it is 24 bytes (192 bits). 4861cb0ef41Sopenharmony_ciscrypt(password, 'salt', 24, (err, key) => { 4871cb0ef41Sopenharmony_ci if (err) throw err; 4881cb0ef41Sopenharmony_ci // Then, we'll generate a random initialization vector 4891cb0ef41Sopenharmony_ci randomFill(new Uint8Array(16), (err, iv) => { 4901cb0ef41Sopenharmony_ci if (err) throw err; 4911cb0ef41Sopenharmony_ci 4921cb0ef41Sopenharmony_ci const cipher = createCipheriv(algorithm, key, iv); 4931cb0ef41Sopenharmony_ci 4941cb0ef41Sopenharmony_ci let encrypted = cipher.update('some clear text data', 'utf8', 'hex'); 4951cb0ef41Sopenharmony_ci encrypted += cipher.final('hex'); 4961cb0ef41Sopenharmony_ci console.log(encrypted); 4971cb0ef41Sopenharmony_ci }); 4981cb0ef41Sopenharmony_ci}); 4991cb0ef41Sopenharmony_ci``` 5001cb0ef41Sopenharmony_ci 5011cb0ef41Sopenharmony_ci```cjs 5021cb0ef41Sopenharmony_ciconst { 5031cb0ef41Sopenharmony_ci scrypt, 5041cb0ef41Sopenharmony_ci randomFill, 5051cb0ef41Sopenharmony_ci createCipheriv, 5061cb0ef41Sopenharmony_ci} = require('node:crypto'); 5071cb0ef41Sopenharmony_ci 5081cb0ef41Sopenharmony_ciconst algorithm = 'aes-192-cbc'; 5091cb0ef41Sopenharmony_ciconst password = 'Password used to generate key'; 5101cb0ef41Sopenharmony_ci 5111cb0ef41Sopenharmony_ci// First, we'll generate the key. The key length is dependent on the algorithm. 5121cb0ef41Sopenharmony_ci// In this case for aes192, it is 24 bytes (192 bits). 5131cb0ef41Sopenharmony_ciscrypt(password, 'salt', 24, (err, key) => { 5141cb0ef41Sopenharmony_ci if (err) throw err; 5151cb0ef41Sopenharmony_ci // Then, we'll generate a random initialization vector 5161cb0ef41Sopenharmony_ci randomFill(new Uint8Array(16), (err, iv) => { 5171cb0ef41Sopenharmony_ci if (err) throw err; 5181cb0ef41Sopenharmony_ci 5191cb0ef41Sopenharmony_ci const cipher = createCipheriv(algorithm, key, iv); 5201cb0ef41Sopenharmony_ci 5211cb0ef41Sopenharmony_ci let encrypted = cipher.update('some clear text data', 'utf8', 'hex'); 5221cb0ef41Sopenharmony_ci encrypted += cipher.final('hex'); 5231cb0ef41Sopenharmony_ci console.log(encrypted); 5241cb0ef41Sopenharmony_ci }); 5251cb0ef41Sopenharmony_ci}); 5261cb0ef41Sopenharmony_ci``` 5271cb0ef41Sopenharmony_ci 5281cb0ef41Sopenharmony_ci### `cipher.final([outputEncoding])` 5291cb0ef41Sopenharmony_ci 5301cb0ef41Sopenharmony_ci<!-- YAML 5311cb0ef41Sopenharmony_ciadded: v0.1.94 5321cb0ef41Sopenharmony_ci--> 5331cb0ef41Sopenharmony_ci 5341cb0ef41Sopenharmony_ci* `outputEncoding` {string} The [encoding][] of the return value. 5351cb0ef41Sopenharmony_ci* Returns: {Buffer | string} Any remaining enciphered contents. 5361cb0ef41Sopenharmony_ci If `outputEncoding` is specified, a string is 5371cb0ef41Sopenharmony_ci returned. If an `outputEncoding` is not provided, a [`Buffer`][] is returned. 5381cb0ef41Sopenharmony_ci 5391cb0ef41Sopenharmony_ciOnce the `cipher.final()` method has been called, the `Cipher` object can no 5401cb0ef41Sopenharmony_cilonger be used to encrypt data. Attempts to call `cipher.final()` more than 5411cb0ef41Sopenharmony_cionce will result in an error being thrown. 5421cb0ef41Sopenharmony_ci 5431cb0ef41Sopenharmony_ci### `cipher.getAuthTag()` 5441cb0ef41Sopenharmony_ci 5451cb0ef41Sopenharmony_ci<!-- YAML 5461cb0ef41Sopenharmony_ciadded: v1.0.0 5471cb0ef41Sopenharmony_ci--> 5481cb0ef41Sopenharmony_ci 5491cb0ef41Sopenharmony_ci* Returns: {Buffer} When using an authenticated encryption mode (`GCM`, `CCM`, 5501cb0ef41Sopenharmony_ci `OCB`, and `chacha20-poly1305` are currently supported), the 5511cb0ef41Sopenharmony_ci `cipher.getAuthTag()` method returns a 5521cb0ef41Sopenharmony_ci [`Buffer`][] containing the _authentication tag_ that has been computed from 5531cb0ef41Sopenharmony_ci the given data. 5541cb0ef41Sopenharmony_ci 5551cb0ef41Sopenharmony_ciThe `cipher.getAuthTag()` method should only be called after encryption has 5561cb0ef41Sopenharmony_cibeen completed using the [`cipher.final()`][] method. 5571cb0ef41Sopenharmony_ci 5581cb0ef41Sopenharmony_ciIf the `authTagLength` option was set during the `cipher` instance's creation, 5591cb0ef41Sopenharmony_cithis function will return exactly `authTagLength` bytes. 5601cb0ef41Sopenharmony_ci 5611cb0ef41Sopenharmony_ci### `cipher.setAAD(buffer[, options])` 5621cb0ef41Sopenharmony_ci 5631cb0ef41Sopenharmony_ci<!-- YAML 5641cb0ef41Sopenharmony_ciadded: v1.0.0 5651cb0ef41Sopenharmony_ci--> 5661cb0ef41Sopenharmony_ci 5671cb0ef41Sopenharmony_ci* `buffer` {string|ArrayBuffer|Buffer|TypedArray|DataView} 5681cb0ef41Sopenharmony_ci* `options` {Object} [`stream.transform` options][] 5691cb0ef41Sopenharmony_ci * `plaintextLength` {number} 5701cb0ef41Sopenharmony_ci * `encoding` {string} The string encoding to use when `buffer` is a string. 5711cb0ef41Sopenharmony_ci* Returns: {Cipher} for method chaining. 5721cb0ef41Sopenharmony_ci 5731cb0ef41Sopenharmony_ciWhen using an authenticated encryption mode (`GCM`, `CCM`, `OCB`, and 5741cb0ef41Sopenharmony_ci`chacha20-poly1305` are 5751cb0ef41Sopenharmony_cicurrently supported), the `cipher.setAAD()` method sets the value used for the 5761cb0ef41Sopenharmony_ci_additional authenticated data_ (AAD) input parameter. 5771cb0ef41Sopenharmony_ci 5781cb0ef41Sopenharmony_ciThe `plaintextLength` option is optional for `GCM` and `OCB`. When using `CCM`, 5791cb0ef41Sopenharmony_cithe `plaintextLength` option must be specified and its value must match the 5801cb0ef41Sopenharmony_cilength of the plaintext in bytes. See [CCM mode][]. 5811cb0ef41Sopenharmony_ci 5821cb0ef41Sopenharmony_ciThe `cipher.setAAD()` method must be called before [`cipher.update()`][]. 5831cb0ef41Sopenharmony_ci 5841cb0ef41Sopenharmony_ci### `cipher.setAutoPadding([autoPadding])` 5851cb0ef41Sopenharmony_ci 5861cb0ef41Sopenharmony_ci<!-- YAML 5871cb0ef41Sopenharmony_ciadded: v0.7.1 5881cb0ef41Sopenharmony_ci--> 5891cb0ef41Sopenharmony_ci 5901cb0ef41Sopenharmony_ci* `autoPadding` {boolean} **Default:** `true` 5911cb0ef41Sopenharmony_ci* Returns: {Cipher} for method chaining. 5921cb0ef41Sopenharmony_ci 5931cb0ef41Sopenharmony_ciWhen using block encryption algorithms, the `Cipher` class will automatically 5941cb0ef41Sopenharmony_ciadd padding to the input data to the appropriate block size. To disable the 5951cb0ef41Sopenharmony_cidefault padding call `cipher.setAutoPadding(false)`. 5961cb0ef41Sopenharmony_ci 5971cb0ef41Sopenharmony_ciWhen `autoPadding` is `false`, the length of the entire input data must be a 5981cb0ef41Sopenharmony_cimultiple of the cipher's block size or [`cipher.final()`][] will throw an error. 5991cb0ef41Sopenharmony_ciDisabling automatic padding is useful for non-standard padding, for instance 6001cb0ef41Sopenharmony_ciusing `0x0` instead of PKCS padding. 6011cb0ef41Sopenharmony_ci 6021cb0ef41Sopenharmony_ciThe `cipher.setAutoPadding()` method must be called before 6031cb0ef41Sopenharmony_ci[`cipher.final()`][]. 6041cb0ef41Sopenharmony_ci 6051cb0ef41Sopenharmony_ci### `cipher.update(data[, inputEncoding][, outputEncoding])` 6061cb0ef41Sopenharmony_ci 6071cb0ef41Sopenharmony_ci<!-- YAML 6081cb0ef41Sopenharmony_ciadded: v0.1.94 6091cb0ef41Sopenharmony_cichanges: 6101cb0ef41Sopenharmony_ci - version: v6.0.0 6111cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/5522 6121cb0ef41Sopenharmony_ci description: The default `inputEncoding` changed from `binary` to `utf8`. 6131cb0ef41Sopenharmony_ci--> 6141cb0ef41Sopenharmony_ci 6151cb0ef41Sopenharmony_ci* `data` {string|Buffer|TypedArray|DataView} 6161cb0ef41Sopenharmony_ci* `inputEncoding` {string} The [encoding][] of the data. 6171cb0ef41Sopenharmony_ci* `outputEncoding` {string} The [encoding][] of the return value. 6181cb0ef41Sopenharmony_ci* Returns: {Buffer | string} 6191cb0ef41Sopenharmony_ci 6201cb0ef41Sopenharmony_ciUpdates the cipher with `data`. If the `inputEncoding` argument is given, 6211cb0ef41Sopenharmony_cithe `data` 6221cb0ef41Sopenharmony_ciargument is a string using the specified encoding. If the `inputEncoding` 6231cb0ef41Sopenharmony_ciargument is not given, `data` must be a [`Buffer`][], `TypedArray`, or 6241cb0ef41Sopenharmony_ci`DataView`. If `data` is a [`Buffer`][], `TypedArray`, or `DataView`, then 6251cb0ef41Sopenharmony_ci`inputEncoding` is ignored. 6261cb0ef41Sopenharmony_ci 6271cb0ef41Sopenharmony_ciThe `outputEncoding` specifies the output format of the enciphered 6281cb0ef41Sopenharmony_cidata. If the `outputEncoding` 6291cb0ef41Sopenharmony_ciis specified, a string using the specified encoding is returned. If no 6301cb0ef41Sopenharmony_ci`outputEncoding` is provided, a [`Buffer`][] is returned. 6311cb0ef41Sopenharmony_ci 6321cb0ef41Sopenharmony_ciThe `cipher.update()` method can be called multiple times with new data until 6331cb0ef41Sopenharmony_ci[`cipher.final()`][] is called. Calling `cipher.update()` after 6341cb0ef41Sopenharmony_ci[`cipher.final()`][] will result in an error being thrown. 6351cb0ef41Sopenharmony_ci 6361cb0ef41Sopenharmony_ci## Class: `Decipher` 6371cb0ef41Sopenharmony_ci 6381cb0ef41Sopenharmony_ci<!-- YAML 6391cb0ef41Sopenharmony_ciadded: v0.1.94 6401cb0ef41Sopenharmony_ci--> 6411cb0ef41Sopenharmony_ci 6421cb0ef41Sopenharmony_ci* Extends: {stream.Transform} 6431cb0ef41Sopenharmony_ci 6441cb0ef41Sopenharmony_ciInstances of the `Decipher` class are used to decrypt data. The class can be 6451cb0ef41Sopenharmony_ciused in one of two ways: 6461cb0ef41Sopenharmony_ci 6471cb0ef41Sopenharmony_ci* As a [stream][] that is both readable and writable, where plain encrypted 6481cb0ef41Sopenharmony_ci data is written to produce unencrypted data on the readable side, or 6491cb0ef41Sopenharmony_ci* Using the [`decipher.update()`][] and [`decipher.final()`][] methods to 6501cb0ef41Sopenharmony_ci produce the unencrypted data. 6511cb0ef41Sopenharmony_ci 6521cb0ef41Sopenharmony_ciThe [`crypto.createDecipher()`][] or [`crypto.createDecipheriv()`][] methods are 6531cb0ef41Sopenharmony_ciused to create `Decipher` instances. `Decipher` objects are not to be created 6541cb0ef41Sopenharmony_cidirectly using the `new` keyword. 6551cb0ef41Sopenharmony_ci 6561cb0ef41Sopenharmony_ciExample: Using `Decipher` objects as streams: 6571cb0ef41Sopenharmony_ci 6581cb0ef41Sopenharmony_ci```mjs 6591cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer'; 6601cb0ef41Sopenharmony_ciconst { 6611cb0ef41Sopenharmony_ci scryptSync, 6621cb0ef41Sopenharmony_ci createDecipheriv, 6631cb0ef41Sopenharmony_ci} = await import('node:crypto'); 6641cb0ef41Sopenharmony_ci 6651cb0ef41Sopenharmony_ciconst algorithm = 'aes-192-cbc'; 6661cb0ef41Sopenharmony_ciconst password = 'Password used to generate key'; 6671cb0ef41Sopenharmony_ci// Key length is dependent on the algorithm. In this case for aes192, it is 6681cb0ef41Sopenharmony_ci// 24 bytes (192 bits). 6691cb0ef41Sopenharmony_ci// Use the async `crypto.scrypt()` instead. 6701cb0ef41Sopenharmony_ciconst key = scryptSync(password, 'salt', 24); 6711cb0ef41Sopenharmony_ci// The IV is usually passed along with the ciphertext. 6721cb0ef41Sopenharmony_ciconst iv = Buffer.alloc(16, 0); // Initialization vector. 6731cb0ef41Sopenharmony_ci 6741cb0ef41Sopenharmony_ciconst decipher = createDecipheriv(algorithm, key, iv); 6751cb0ef41Sopenharmony_ci 6761cb0ef41Sopenharmony_cilet decrypted = ''; 6771cb0ef41Sopenharmony_cidecipher.on('readable', () => { 6781cb0ef41Sopenharmony_ci let chunk; 6791cb0ef41Sopenharmony_ci while (null !== (chunk = decipher.read())) { 6801cb0ef41Sopenharmony_ci decrypted += chunk.toString('utf8'); 6811cb0ef41Sopenharmony_ci } 6821cb0ef41Sopenharmony_ci}); 6831cb0ef41Sopenharmony_cidecipher.on('end', () => { 6841cb0ef41Sopenharmony_ci console.log(decrypted); 6851cb0ef41Sopenharmony_ci // Prints: some clear text data 6861cb0ef41Sopenharmony_ci}); 6871cb0ef41Sopenharmony_ci 6881cb0ef41Sopenharmony_ci// Encrypted with same algorithm, key and iv. 6891cb0ef41Sopenharmony_ciconst encrypted = 6901cb0ef41Sopenharmony_ci 'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa'; 6911cb0ef41Sopenharmony_cidecipher.write(encrypted, 'hex'); 6921cb0ef41Sopenharmony_cidecipher.end(); 6931cb0ef41Sopenharmony_ci``` 6941cb0ef41Sopenharmony_ci 6951cb0ef41Sopenharmony_ci```cjs 6961cb0ef41Sopenharmony_ciconst { 6971cb0ef41Sopenharmony_ci scryptSync, 6981cb0ef41Sopenharmony_ci createDecipheriv, 6991cb0ef41Sopenharmony_ci} = require('node:crypto'); 7001cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer'); 7011cb0ef41Sopenharmony_ci 7021cb0ef41Sopenharmony_ciconst algorithm = 'aes-192-cbc'; 7031cb0ef41Sopenharmony_ciconst password = 'Password used to generate key'; 7041cb0ef41Sopenharmony_ci// Key length is dependent on the algorithm. In this case for aes192, it is 7051cb0ef41Sopenharmony_ci// 24 bytes (192 bits). 7061cb0ef41Sopenharmony_ci// Use the async `crypto.scrypt()` instead. 7071cb0ef41Sopenharmony_ciconst key = scryptSync(password, 'salt', 24); 7081cb0ef41Sopenharmony_ci// The IV is usually passed along with the ciphertext. 7091cb0ef41Sopenharmony_ciconst iv = Buffer.alloc(16, 0); // Initialization vector. 7101cb0ef41Sopenharmony_ci 7111cb0ef41Sopenharmony_ciconst decipher = createDecipheriv(algorithm, key, iv); 7121cb0ef41Sopenharmony_ci 7131cb0ef41Sopenharmony_cilet decrypted = ''; 7141cb0ef41Sopenharmony_cidecipher.on('readable', () => { 7151cb0ef41Sopenharmony_ci let chunk; 7161cb0ef41Sopenharmony_ci while (null !== (chunk = decipher.read())) { 7171cb0ef41Sopenharmony_ci decrypted += chunk.toString('utf8'); 7181cb0ef41Sopenharmony_ci } 7191cb0ef41Sopenharmony_ci}); 7201cb0ef41Sopenharmony_cidecipher.on('end', () => { 7211cb0ef41Sopenharmony_ci console.log(decrypted); 7221cb0ef41Sopenharmony_ci // Prints: some clear text data 7231cb0ef41Sopenharmony_ci}); 7241cb0ef41Sopenharmony_ci 7251cb0ef41Sopenharmony_ci// Encrypted with same algorithm, key and iv. 7261cb0ef41Sopenharmony_ciconst encrypted = 7271cb0ef41Sopenharmony_ci 'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa'; 7281cb0ef41Sopenharmony_cidecipher.write(encrypted, 'hex'); 7291cb0ef41Sopenharmony_cidecipher.end(); 7301cb0ef41Sopenharmony_ci``` 7311cb0ef41Sopenharmony_ci 7321cb0ef41Sopenharmony_ciExample: Using `Decipher` and piped streams: 7331cb0ef41Sopenharmony_ci 7341cb0ef41Sopenharmony_ci```mjs 7351cb0ef41Sopenharmony_ciimport { 7361cb0ef41Sopenharmony_ci createReadStream, 7371cb0ef41Sopenharmony_ci createWriteStream, 7381cb0ef41Sopenharmony_ci} from 'node:fs'; 7391cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer'; 7401cb0ef41Sopenharmony_ciconst { 7411cb0ef41Sopenharmony_ci scryptSync, 7421cb0ef41Sopenharmony_ci createDecipheriv, 7431cb0ef41Sopenharmony_ci} = await import('node:crypto'); 7441cb0ef41Sopenharmony_ci 7451cb0ef41Sopenharmony_ciconst algorithm = 'aes-192-cbc'; 7461cb0ef41Sopenharmony_ciconst password = 'Password used to generate key'; 7471cb0ef41Sopenharmony_ci// Use the async `crypto.scrypt()` instead. 7481cb0ef41Sopenharmony_ciconst key = scryptSync(password, 'salt', 24); 7491cb0ef41Sopenharmony_ci// The IV is usually passed along with the ciphertext. 7501cb0ef41Sopenharmony_ciconst iv = Buffer.alloc(16, 0); // Initialization vector. 7511cb0ef41Sopenharmony_ci 7521cb0ef41Sopenharmony_ciconst decipher = createDecipheriv(algorithm, key, iv); 7531cb0ef41Sopenharmony_ci 7541cb0ef41Sopenharmony_ciconst input = createReadStream('test.enc'); 7551cb0ef41Sopenharmony_ciconst output = createWriteStream('test.js'); 7561cb0ef41Sopenharmony_ci 7571cb0ef41Sopenharmony_ciinput.pipe(decipher).pipe(output); 7581cb0ef41Sopenharmony_ci``` 7591cb0ef41Sopenharmony_ci 7601cb0ef41Sopenharmony_ci```cjs 7611cb0ef41Sopenharmony_ciconst { 7621cb0ef41Sopenharmony_ci createReadStream, 7631cb0ef41Sopenharmony_ci createWriteStream, 7641cb0ef41Sopenharmony_ci} = require('node:fs'); 7651cb0ef41Sopenharmony_ciconst { 7661cb0ef41Sopenharmony_ci scryptSync, 7671cb0ef41Sopenharmony_ci createDecipheriv, 7681cb0ef41Sopenharmony_ci} = require('node:crypto'); 7691cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer'); 7701cb0ef41Sopenharmony_ci 7711cb0ef41Sopenharmony_ciconst algorithm = 'aes-192-cbc'; 7721cb0ef41Sopenharmony_ciconst password = 'Password used to generate key'; 7731cb0ef41Sopenharmony_ci// Use the async `crypto.scrypt()` instead. 7741cb0ef41Sopenharmony_ciconst key = scryptSync(password, 'salt', 24); 7751cb0ef41Sopenharmony_ci// The IV is usually passed along with the ciphertext. 7761cb0ef41Sopenharmony_ciconst iv = Buffer.alloc(16, 0); // Initialization vector. 7771cb0ef41Sopenharmony_ci 7781cb0ef41Sopenharmony_ciconst decipher = createDecipheriv(algorithm, key, iv); 7791cb0ef41Sopenharmony_ci 7801cb0ef41Sopenharmony_ciconst input = createReadStream('test.enc'); 7811cb0ef41Sopenharmony_ciconst output = createWriteStream('test.js'); 7821cb0ef41Sopenharmony_ci 7831cb0ef41Sopenharmony_ciinput.pipe(decipher).pipe(output); 7841cb0ef41Sopenharmony_ci``` 7851cb0ef41Sopenharmony_ci 7861cb0ef41Sopenharmony_ciExample: Using the [`decipher.update()`][] and [`decipher.final()`][] methods: 7871cb0ef41Sopenharmony_ci 7881cb0ef41Sopenharmony_ci```mjs 7891cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer'; 7901cb0ef41Sopenharmony_ciconst { 7911cb0ef41Sopenharmony_ci scryptSync, 7921cb0ef41Sopenharmony_ci createDecipheriv, 7931cb0ef41Sopenharmony_ci} = await import('node:crypto'); 7941cb0ef41Sopenharmony_ci 7951cb0ef41Sopenharmony_ciconst algorithm = 'aes-192-cbc'; 7961cb0ef41Sopenharmony_ciconst password = 'Password used to generate key'; 7971cb0ef41Sopenharmony_ci// Use the async `crypto.scrypt()` instead. 7981cb0ef41Sopenharmony_ciconst key = scryptSync(password, 'salt', 24); 7991cb0ef41Sopenharmony_ci// The IV is usually passed along with the ciphertext. 8001cb0ef41Sopenharmony_ciconst iv = Buffer.alloc(16, 0); // Initialization vector. 8011cb0ef41Sopenharmony_ci 8021cb0ef41Sopenharmony_ciconst decipher = createDecipheriv(algorithm, key, iv); 8031cb0ef41Sopenharmony_ci 8041cb0ef41Sopenharmony_ci// Encrypted using same algorithm, key and iv. 8051cb0ef41Sopenharmony_ciconst encrypted = 8061cb0ef41Sopenharmony_ci 'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa'; 8071cb0ef41Sopenharmony_cilet decrypted = decipher.update(encrypted, 'hex', 'utf8'); 8081cb0ef41Sopenharmony_cidecrypted += decipher.final('utf8'); 8091cb0ef41Sopenharmony_ciconsole.log(decrypted); 8101cb0ef41Sopenharmony_ci// Prints: some clear text data 8111cb0ef41Sopenharmony_ci``` 8121cb0ef41Sopenharmony_ci 8131cb0ef41Sopenharmony_ci```cjs 8141cb0ef41Sopenharmony_ciconst { 8151cb0ef41Sopenharmony_ci scryptSync, 8161cb0ef41Sopenharmony_ci createDecipheriv, 8171cb0ef41Sopenharmony_ci} = require('node:crypto'); 8181cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer'); 8191cb0ef41Sopenharmony_ci 8201cb0ef41Sopenharmony_ciconst algorithm = 'aes-192-cbc'; 8211cb0ef41Sopenharmony_ciconst password = 'Password used to generate key'; 8221cb0ef41Sopenharmony_ci// Use the async `crypto.scrypt()` instead. 8231cb0ef41Sopenharmony_ciconst key = scryptSync(password, 'salt', 24); 8241cb0ef41Sopenharmony_ci// The IV is usually passed along with the ciphertext. 8251cb0ef41Sopenharmony_ciconst iv = Buffer.alloc(16, 0); // Initialization vector. 8261cb0ef41Sopenharmony_ci 8271cb0ef41Sopenharmony_ciconst decipher = createDecipheriv(algorithm, key, iv); 8281cb0ef41Sopenharmony_ci 8291cb0ef41Sopenharmony_ci// Encrypted using same algorithm, key and iv. 8301cb0ef41Sopenharmony_ciconst encrypted = 8311cb0ef41Sopenharmony_ci 'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa'; 8321cb0ef41Sopenharmony_cilet decrypted = decipher.update(encrypted, 'hex', 'utf8'); 8331cb0ef41Sopenharmony_cidecrypted += decipher.final('utf8'); 8341cb0ef41Sopenharmony_ciconsole.log(decrypted); 8351cb0ef41Sopenharmony_ci// Prints: some clear text data 8361cb0ef41Sopenharmony_ci``` 8371cb0ef41Sopenharmony_ci 8381cb0ef41Sopenharmony_ci### `decipher.final([outputEncoding])` 8391cb0ef41Sopenharmony_ci 8401cb0ef41Sopenharmony_ci<!-- YAML 8411cb0ef41Sopenharmony_ciadded: v0.1.94 8421cb0ef41Sopenharmony_ci--> 8431cb0ef41Sopenharmony_ci 8441cb0ef41Sopenharmony_ci* `outputEncoding` {string} The [encoding][] of the return value. 8451cb0ef41Sopenharmony_ci* Returns: {Buffer | string} Any remaining deciphered contents. 8461cb0ef41Sopenharmony_ci If `outputEncoding` is specified, a string is 8471cb0ef41Sopenharmony_ci returned. If an `outputEncoding` is not provided, a [`Buffer`][] is returned. 8481cb0ef41Sopenharmony_ci 8491cb0ef41Sopenharmony_ciOnce the `decipher.final()` method has been called, the `Decipher` object can 8501cb0ef41Sopenharmony_cino longer be used to decrypt data. Attempts to call `decipher.final()` more 8511cb0ef41Sopenharmony_cithan once will result in an error being thrown. 8521cb0ef41Sopenharmony_ci 8531cb0ef41Sopenharmony_ci### `decipher.setAAD(buffer[, options])` 8541cb0ef41Sopenharmony_ci 8551cb0ef41Sopenharmony_ci<!-- YAML 8561cb0ef41Sopenharmony_ciadded: v1.0.0 8571cb0ef41Sopenharmony_cichanges: 8581cb0ef41Sopenharmony_ci - version: v15.0.0 8591cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/35093 8601cb0ef41Sopenharmony_ci description: The buffer argument can be a string or ArrayBuffer and is 8611cb0ef41Sopenharmony_ci limited to no more than 2 ** 31 - 1 bytes. 8621cb0ef41Sopenharmony_ci - version: v7.2.0 8631cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/9398 8641cb0ef41Sopenharmony_ci description: This method now returns a reference to `decipher`. 8651cb0ef41Sopenharmony_ci--> 8661cb0ef41Sopenharmony_ci 8671cb0ef41Sopenharmony_ci* `buffer` {string|ArrayBuffer|Buffer|TypedArray|DataView} 8681cb0ef41Sopenharmony_ci* `options` {Object} [`stream.transform` options][] 8691cb0ef41Sopenharmony_ci * `plaintextLength` {number} 8701cb0ef41Sopenharmony_ci * `encoding` {string} String encoding to use when `buffer` is a string. 8711cb0ef41Sopenharmony_ci* Returns: {Decipher} for method chaining. 8721cb0ef41Sopenharmony_ci 8731cb0ef41Sopenharmony_ciWhen using an authenticated encryption mode (`GCM`, `CCM`, `OCB`, and 8741cb0ef41Sopenharmony_ci`chacha20-poly1305` are 8751cb0ef41Sopenharmony_cicurrently supported), the `decipher.setAAD()` method sets the value used for the 8761cb0ef41Sopenharmony_ci_additional authenticated data_ (AAD) input parameter. 8771cb0ef41Sopenharmony_ci 8781cb0ef41Sopenharmony_ciThe `options` argument is optional for `GCM`. When using `CCM`, the 8791cb0ef41Sopenharmony_ci`plaintextLength` option must be specified and its value must match the length 8801cb0ef41Sopenharmony_ciof the ciphertext in bytes. See [CCM mode][]. 8811cb0ef41Sopenharmony_ci 8821cb0ef41Sopenharmony_ciThe `decipher.setAAD()` method must be called before [`decipher.update()`][]. 8831cb0ef41Sopenharmony_ci 8841cb0ef41Sopenharmony_ciWhen passing a string as the `buffer`, please consider 8851cb0ef41Sopenharmony_ci[caveats when using strings as inputs to cryptographic APIs][]. 8861cb0ef41Sopenharmony_ci 8871cb0ef41Sopenharmony_ci### `decipher.setAuthTag(buffer[, encoding])` 8881cb0ef41Sopenharmony_ci 8891cb0ef41Sopenharmony_ci<!-- YAML 8901cb0ef41Sopenharmony_ciadded: v1.0.0 8911cb0ef41Sopenharmony_cichanges: 8921cb0ef41Sopenharmony_ci - version: v15.0.0 8931cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/35093 8941cb0ef41Sopenharmony_ci description: The buffer argument can be a string or ArrayBuffer and is 8951cb0ef41Sopenharmony_ci limited to no more than 2 ** 31 - 1 bytes. 8961cb0ef41Sopenharmony_ci - version: v11.0.0 8971cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/17825 8981cb0ef41Sopenharmony_ci description: This method now throws if the GCM tag length is invalid. 8991cb0ef41Sopenharmony_ci - version: v7.2.0 9001cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/9398 9011cb0ef41Sopenharmony_ci description: This method now returns a reference to `decipher`. 9021cb0ef41Sopenharmony_ci--> 9031cb0ef41Sopenharmony_ci 9041cb0ef41Sopenharmony_ci* `buffer` {string|Buffer|ArrayBuffer|TypedArray|DataView} 9051cb0ef41Sopenharmony_ci* `encoding` {string} String encoding to use when `buffer` is a string. 9061cb0ef41Sopenharmony_ci* Returns: {Decipher} for method chaining. 9071cb0ef41Sopenharmony_ci 9081cb0ef41Sopenharmony_ciWhen using an authenticated encryption mode (`GCM`, `CCM`, `OCB`, and 9091cb0ef41Sopenharmony_ci`chacha20-poly1305` are 9101cb0ef41Sopenharmony_cicurrently supported), the `decipher.setAuthTag()` method is used to pass in the 9111cb0ef41Sopenharmony_cireceived _authentication tag_. If no tag is provided, or if the cipher text 9121cb0ef41Sopenharmony_cihas been tampered with, [`decipher.final()`][] will throw, indicating that the 9131cb0ef41Sopenharmony_cicipher text should be discarded due to failed authentication. If the tag length 9141cb0ef41Sopenharmony_ciis invalid according to [NIST SP 800-38D][] or does not match the value of the 9151cb0ef41Sopenharmony_ci`authTagLength` option, `decipher.setAuthTag()` will throw an error. 9161cb0ef41Sopenharmony_ci 9171cb0ef41Sopenharmony_ciThe `decipher.setAuthTag()` method must be called before [`decipher.update()`][] 9181cb0ef41Sopenharmony_cifor `CCM` mode or before [`decipher.final()`][] for `GCM` and `OCB` modes and 9191cb0ef41Sopenharmony_ci`chacha20-poly1305`. 9201cb0ef41Sopenharmony_ci`decipher.setAuthTag()` can only be called once. 9211cb0ef41Sopenharmony_ci 9221cb0ef41Sopenharmony_ciWhen passing a string as the authentication tag, please consider 9231cb0ef41Sopenharmony_ci[caveats when using strings as inputs to cryptographic APIs][]. 9241cb0ef41Sopenharmony_ci 9251cb0ef41Sopenharmony_ci### `decipher.setAutoPadding([autoPadding])` 9261cb0ef41Sopenharmony_ci 9271cb0ef41Sopenharmony_ci<!-- YAML 9281cb0ef41Sopenharmony_ciadded: v0.7.1 9291cb0ef41Sopenharmony_ci--> 9301cb0ef41Sopenharmony_ci 9311cb0ef41Sopenharmony_ci* `autoPadding` {boolean} **Default:** `true` 9321cb0ef41Sopenharmony_ci* Returns: {Decipher} for method chaining. 9331cb0ef41Sopenharmony_ci 9341cb0ef41Sopenharmony_ciWhen data has been encrypted without standard block padding, calling 9351cb0ef41Sopenharmony_ci`decipher.setAutoPadding(false)` will disable automatic padding to prevent 9361cb0ef41Sopenharmony_ci[`decipher.final()`][] from checking for and removing padding. 9371cb0ef41Sopenharmony_ci 9381cb0ef41Sopenharmony_ciTurning auto padding off will only work if the input data's length is a 9391cb0ef41Sopenharmony_cimultiple of the ciphers block size. 9401cb0ef41Sopenharmony_ci 9411cb0ef41Sopenharmony_ciThe `decipher.setAutoPadding()` method must be called before 9421cb0ef41Sopenharmony_ci[`decipher.final()`][]. 9431cb0ef41Sopenharmony_ci 9441cb0ef41Sopenharmony_ci### `decipher.update(data[, inputEncoding][, outputEncoding])` 9451cb0ef41Sopenharmony_ci 9461cb0ef41Sopenharmony_ci<!-- YAML 9471cb0ef41Sopenharmony_ciadded: v0.1.94 9481cb0ef41Sopenharmony_cichanges: 9491cb0ef41Sopenharmony_ci - version: v6.0.0 9501cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/5522 9511cb0ef41Sopenharmony_ci description: The default `inputEncoding` changed from `binary` to `utf8`. 9521cb0ef41Sopenharmony_ci--> 9531cb0ef41Sopenharmony_ci 9541cb0ef41Sopenharmony_ci* `data` {string|Buffer|TypedArray|DataView} 9551cb0ef41Sopenharmony_ci* `inputEncoding` {string} The [encoding][] of the `data` string. 9561cb0ef41Sopenharmony_ci* `outputEncoding` {string} The [encoding][] of the return value. 9571cb0ef41Sopenharmony_ci* Returns: {Buffer | string} 9581cb0ef41Sopenharmony_ci 9591cb0ef41Sopenharmony_ciUpdates the decipher with `data`. If the `inputEncoding` argument is given, 9601cb0ef41Sopenharmony_cithe `data` 9611cb0ef41Sopenharmony_ciargument is a string using the specified encoding. If the `inputEncoding` 9621cb0ef41Sopenharmony_ciargument is not given, `data` must be a [`Buffer`][]. If `data` is a 9631cb0ef41Sopenharmony_ci[`Buffer`][] then `inputEncoding` is ignored. 9641cb0ef41Sopenharmony_ci 9651cb0ef41Sopenharmony_ciThe `outputEncoding` specifies the output format of the enciphered 9661cb0ef41Sopenharmony_cidata. If the `outputEncoding` 9671cb0ef41Sopenharmony_ciis specified, a string using the specified encoding is returned. If no 9681cb0ef41Sopenharmony_ci`outputEncoding` is provided, a [`Buffer`][] is returned. 9691cb0ef41Sopenharmony_ci 9701cb0ef41Sopenharmony_ciThe `decipher.update()` method can be called multiple times with new data until 9711cb0ef41Sopenharmony_ci[`decipher.final()`][] is called. Calling `decipher.update()` after 9721cb0ef41Sopenharmony_ci[`decipher.final()`][] will result in an error being thrown. 9731cb0ef41Sopenharmony_ci 9741cb0ef41Sopenharmony_ci## Class: `DiffieHellman` 9751cb0ef41Sopenharmony_ci 9761cb0ef41Sopenharmony_ci<!-- YAML 9771cb0ef41Sopenharmony_ciadded: v0.5.0 9781cb0ef41Sopenharmony_ci--> 9791cb0ef41Sopenharmony_ci 9801cb0ef41Sopenharmony_ciThe `DiffieHellman` class is a utility for creating Diffie-Hellman key 9811cb0ef41Sopenharmony_ciexchanges. 9821cb0ef41Sopenharmony_ci 9831cb0ef41Sopenharmony_ciInstances of the `DiffieHellman` class can be created using the 9841cb0ef41Sopenharmony_ci[`crypto.createDiffieHellman()`][] function. 9851cb0ef41Sopenharmony_ci 9861cb0ef41Sopenharmony_ci```mjs 9871cb0ef41Sopenharmony_ciimport assert from 'node:assert'; 9881cb0ef41Sopenharmony_ci 9891cb0ef41Sopenharmony_ciconst { 9901cb0ef41Sopenharmony_ci createDiffieHellman, 9911cb0ef41Sopenharmony_ci} = await import('node:crypto'); 9921cb0ef41Sopenharmony_ci 9931cb0ef41Sopenharmony_ci// Generate Alice's keys... 9941cb0ef41Sopenharmony_ciconst alice = createDiffieHellman(2048); 9951cb0ef41Sopenharmony_ciconst aliceKey = alice.generateKeys(); 9961cb0ef41Sopenharmony_ci 9971cb0ef41Sopenharmony_ci// Generate Bob's keys... 9981cb0ef41Sopenharmony_ciconst bob = createDiffieHellman(alice.getPrime(), alice.getGenerator()); 9991cb0ef41Sopenharmony_ciconst bobKey = bob.generateKeys(); 10001cb0ef41Sopenharmony_ci 10011cb0ef41Sopenharmony_ci// Exchange and generate the secret... 10021cb0ef41Sopenharmony_ciconst aliceSecret = alice.computeSecret(bobKey); 10031cb0ef41Sopenharmony_ciconst bobSecret = bob.computeSecret(aliceKey); 10041cb0ef41Sopenharmony_ci 10051cb0ef41Sopenharmony_ci// OK 10061cb0ef41Sopenharmony_ciassert.strictEqual(aliceSecret.toString('hex'), bobSecret.toString('hex')); 10071cb0ef41Sopenharmony_ci``` 10081cb0ef41Sopenharmony_ci 10091cb0ef41Sopenharmony_ci```cjs 10101cb0ef41Sopenharmony_ciconst assert = require('node:assert'); 10111cb0ef41Sopenharmony_ci 10121cb0ef41Sopenharmony_ciconst { 10131cb0ef41Sopenharmony_ci createDiffieHellman, 10141cb0ef41Sopenharmony_ci} = require('node:crypto'); 10151cb0ef41Sopenharmony_ci 10161cb0ef41Sopenharmony_ci// Generate Alice's keys... 10171cb0ef41Sopenharmony_ciconst alice = createDiffieHellman(2048); 10181cb0ef41Sopenharmony_ciconst aliceKey = alice.generateKeys(); 10191cb0ef41Sopenharmony_ci 10201cb0ef41Sopenharmony_ci// Generate Bob's keys... 10211cb0ef41Sopenharmony_ciconst bob = createDiffieHellman(alice.getPrime(), alice.getGenerator()); 10221cb0ef41Sopenharmony_ciconst bobKey = bob.generateKeys(); 10231cb0ef41Sopenharmony_ci 10241cb0ef41Sopenharmony_ci// Exchange and generate the secret... 10251cb0ef41Sopenharmony_ciconst aliceSecret = alice.computeSecret(bobKey); 10261cb0ef41Sopenharmony_ciconst bobSecret = bob.computeSecret(aliceKey); 10271cb0ef41Sopenharmony_ci 10281cb0ef41Sopenharmony_ci// OK 10291cb0ef41Sopenharmony_ciassert.strictEqual(aliceSecret.toString('hex'), bobSecret.toString('hex')); 10301cb0ef41Sopenharmony_ci``` 10311cb0ef41Sopenharmony_ci 10321cb0ef41Sopenharmony_ci### `diffieHellman.computeSecret(otherPublicKey[, inputEncoding][, outputEncoding])` 10331cb0ef41Sopenharmony_ci 10341cb0ef41Sopenharmony_ci<!-- YAML 10351cb0ef41Sopenharmony_ciadded: v0.5.0 10361cb0ef41Sopenharmony_ci--> 10371cb0ef41Sopenharmony_ci 10381cb0ef41Sopenharmony_ci* `otherPublicKey` {string|ArrayBuffer|Buffer|TypedArray|DataView} 10391cb0ef41Sopenharmony_ci* `inputEncoding` {string} The [encoding][] of an `otherPublicKey` string. 10401cb0ef41Sopenharmony_ci* `outputEncoding` {string} The [encoding][] of the return value. 10411cb0ef41Sopenharmony_ci* Returns: {Buffer | string} 10421cb0ef41Sopenharmony_ci 10431cb0ef41Sopenharmony_ciComputes the shared secret using `otherPublicKey` as the other 10441cb0ef41Sopenharmony_ciparty's public key and returns the computed shared secret. The supplied 10451cb0ef41Sopenharmony_cikey is interpreted using the specified `inputEncoding`, and secret is 10461cb0ef41Sopenharmony_ciencoded using specified `outputEncoding`. 10471cb0ef41Sopenharmony_ciIf the `inputEncoding` is not 10481cb0ef41Sopenharmony_ciprovided, `otherPublicKey` is expected to be a [`Buffer`][], 10491cb0ef41Sopenharmony_ci`TypedArray`, or `DataView`. 10501cb0ef41Sopenharmony_ci 10511cb0ef41Sopenharmony_ciIf `outputEncoding` is given a string is returned; otherwise, a 10521cb0ef41Sopenharmony_ci[`Buffer`][] is returned. 10531cb0ef41Sopenharmony_ci 10541cb0ef41Sopenharmony_ci### `diffieHellman.generateKeys([encoding])` 10551cb0ef41Sopenharmony_ci 10561cb0ef41Sopenharmony_ci<!-- YAML 10571cb0ef41Sopenharmony_ciadded: v0.5.0 10581cb0ef41Sopenharmony_ci--> 10591cb0ef41Sopenharmony_ci 10601cb0ef41Sopenharmony_ci* `encoding` {string} The [encoding][] of the return value. 10611cb0ef41Sopenharmony_ci* Returns: {Buffer | string} 10621cb0ef41Sopenharmony_ci 10631cb0ef41Sopenharmony_ciGenerates private and public Diffie-Hellman key values unless they have been 10641cb0ef41Sopenharmony_cigenerated or computed already, and returns 10651cb0ef41Sopenharmony_cithe public key in the specified `encoding`. This key should be 10661cb0ef41Sopenharmony_citransferred to the other party. 10671cb0ef41Sopenharmony_ciIf `encoding` is provided a string is returned; otherwise a 10681cb0ef41Sopenharmony_ci[`Buffer`][] is returned. 10691cb0ef41Sopenharmony_ci 10701cb0ef41Sopenharmony_ciThis function is a thin wrapper around [`DH_generate_key()`][]. In particular, 10711cb0ef41Sopenharmony_cionce a private key has been generated or set, calling this function only updates 10721cb0ef41Sopenharmony_cithe public key but does not generate a new private key. 10731cb0ef41Sopenharmony_ci 10741cb0ef41Sopenharmony_ci### `diffieHellman.getGenerator([encoding])` 10751cb0ef41Sopenharmony_ci 10761cb0ef41Sopenharmony_ci<!-- YAML 10771cb0ef41Sopenharmony_ciadded: v0.5.0 10781cb0ef41Sopenharmony_ci--> 10791cb0ef41Sopenharmony_ci 10801cb0ef41Sopenharmony_ci* `encoding` {string} The [encoding][] of the return value. 10811cb0ef41Sopenharmony_ci* Returns: {Buffer | string} 10821cb0ef41Sopenharmony_ci 10831cb0ef41Sopenharmony_ciReturns the Diffie-Hellman generator in the specified `encoding`. 10841cb0ef41Sopenharmony_ciIf `encoding` is provided a string is 10851cb0ef41Sopenharmony_cireturned; otherwise a [`Buffer`][] is returned. 10861cb0ef41Sopenharmony_ci 10871cb0ef41Sopenharmony_ci### `diffieHellman.getPrime([encoding])` 10881cb0ef41Sopenharmony_ci 10891cb0ef41Sopenharmony_ci<!-- YAML 10901cb0ef41Sopenharmony_ciadded: v0.5.0 10911cb0ef41Sopenharmony_ci--> 10921cb0ef41Sopenharmony_ci 10931cb0ef41Sopenharmony_ci* `encoding` {string} The [encoding][] of the return value. 10941cb0ef41Sopenharmony_ci* Returns: {Buffer | string} 10951cb0ef41Sopenharmony_ci 10961cb0ef41Sopenharmony_ciReturns the Diffie-Hellman prime in the specified `encoding`. 10971cb0ef41Sopenharmony_ciIf `encoding` is provided a string is 10981cb0ef41Sopenharmony_cireturned; otherwise a [`Buffer`][] is returned. 10991cb0ef41Sopenharmony_ci 11001cb0ef41Sopenharmony_ci### `diffieHellman.getPrivateKey([encoding])` 11011cb0ef41Sopenharmony_ci 11021cb0ef41Sopenharmony_ci<!-- YAML 11031cb0ef41Sopenharmony_ciadded: v0.5.0 11041cb0ef41Sopenharmony_ci--> 11051cb0ef41Sopenharmony_ci 11061cb0ef41Sopenharmony_ci* `encoding` {string} The [encoding][] of the return value. 11071cb0ef41Sopenharmony_ci* Returns: {Buffer | string} 11081cb0ef41Sopenharmony_ci 11091cb0ef41Sopenharmony_ciReturns the Diffie-Hellman private key in the specified `encoding`. 11101cb0ef41Sopenharmony_ciIf `encoding` is provided a 11111cb0ef41Sopenharmony_cistring is returned; otherwise a [`Buffer`][] is returned. 11121cb0ef41Sopenharmony_ci 11131cb0ef41Sopenharmony_ci### `diffieHellman.getPublicKey([encoding])` 11141cb0ef41Sopenharmony_ci 11151cb0ef41Sopenharmony_ci<!-- YAML 11161cb0ef41Sopenharmony_ciadded: v0.5.0 11171cb0ef41Sopenharmony_ci--> 11181cb0ef41Sopenharmony_ci 11191cb0ef41Sopenharmony_ci* `encoding` {string} The [encoding][] of the return value. 11201cb0ef41Sopenharmony_ci* Returns: {Buffer | string} 11211cb0ef41Sopenharmony_ci 11221cb0ef41Sopenharmony_ciReturns the Diffie-Hellman public key in the specified `encoding`. 11231cb0ef41Sopenharmony_ciIf `encoding` is provided a 11241cb0ef41Sopenharmony_cistring is returned; otherwise a [`Buffer`][] is returned. 11251cb0ef41Sopenharmony_ci 11261cb0ef41Sopenharmony_ci### `diffieHellman.setPrivateKey(privateKey[, encoding])` 11271cb0ef41Sopenharmony_ci 11281cb0ef41Sopenharmony_ci<!-- YAML 11291cb0ef41Sopenharmony_ciadded: v0.5.0 11301cb0ef41Sopenharmony_ci--> 11311cb0ef41Sopenharmony_ci 11321cb0ef41Sopenharmony_ci* `privateKey` {string|ArrayBuffer|Buffer|TypedArray|DataView} 11331cb0ef41Sopenharmony_ci* `encoding` {string} The [encoding][] of the `privateKey` string. 11341cb0ef41Sopenharmony_ci 11351cb0ef41Sopenharmony_ciSets the Diffie-Hellman private key. If the `encoding` argument is provided, 11361cb0ef41Sopenharmony_ci`privateKey` is expected 11371cb0ef41Sopenharmony_cito be a string. If no `encoding` is provided, `privateKey` is expected 11381cb0ef41Sopenharmony_cito be a [`Buffer`][], `TypedArray`, or `DataView`. 11391cb0ef41Sopenharmony_ci 11401cb0ef41Sopenharmony_ciThis function does not automatically compute the associated public key. Either 11411cb0ef41Sopenharmony_ci[`diffieHellman.setPublicKey()`][] or [`diffieHellman.generateKeys()`][] can be 11421cb0ef41Sopenharmony_ciused to manually provide the public key or to automatically derive it. 11431cb0ef41Sopenharmony_ci 11441cb0ef41Sopenharmony_ci### `diffieHellman.setPublicKey(publicKey[, encoding])` 11451cb0ef41Sopenharmony_ci 11461cb0ef41Sopenharmony_ci<!-- YAML 11471cb0ef41Sopenharmony_ciadded: v0.5.0 11481cb0ef41Sopenharmony_ci--> 11491cb0ef41Sopenharmony_ci 11501cb0ef41Sopenharmony_ci* `publicKey` {string|ArrayBuffer|Buffer|TypedArray|DataView} 11511cb0ef41Sopenharmony_ci* `encoding` {string} The [encoding][] of the `publicKey` string. 11521cb0ef41Sopenharmony_ci 11531cb0ef41Sopenharmony_ciSets the Diffie-Hellman public key. If the `encoding` argument is provided, 11541cb0ef41Sopenharmony_ci`publicKey` is expected 11551cb0ef41Sopenharmony_cito be a string. If no `encoding` is provided, `publicKey` is expected 11561cb0ef41Sopenharmony_cito be a [`Buffer`][], `TypedArray`, or `DataView`. 11571cb0ef41Sopenharmony_ci 11581cb0ef41Sopenharmony_ci### `diffieHellman.verifyError` 11591cb0ef41Sopenharmony_ci 11601cb0ef41Sopenharmony_ci<!-- YAML 11611cb0ef41Sopenharmony_ciadded: v0.11.12 11621cb0ef41Sopenharmony_ci--> 11631cb0ef41Sopenharmony_ci 11641cb0ef41Sopenharmony_ciA bit field containing any warnings and/or errors resulting from a check 11651cb0ef41Sopenharmony_ciperformed during initialization of the `DiffieHellman` object. 11661cb0ef41Sopenharmony_ci 11671cb0ef41Sopenharmony_ciThe following values are valid for this property (as defined in `node:constants` module): 11681cb0ef41Sopenharmony_ci 11691cb0ef41Sopenharmony_ci* `DH_CHECK_P_NOT_SAFE_PRIME` 11701cb0ef41Sopenharmony_ci* `DH_CHECK_P_NOT_PRIME` 11711cb0ef41Sopenharmony_ci* `DH_UNABLE_TO_CHECK_GENERATOR` 11721cb0ef41Sopenharmony_ci* `DH_NOT_SUITABLE_GENERATOR` 11731cb0ef41Sopenharmony_ci 11741cb0ef41Sopenharmony_ci## Class: `DiffieHellmanGroup` 11751cb0ef41Sopenharmony_ci 11761cb0ef41Sopenharmony_ci<!-- YAML 11771cb0ef41Sopenharmony_ciadded: v0.7.5 11781cb0ef41Sopenharmony_ci--> 11791cb0ef41Sopenharmony_ci 11801cb0ef41Sopenharmony_ciThe `DiffieHellmanGroup` class takes a well-known modp group as its argument. 11811cb0ef41Sopenharmony_ciIt works the same as `DiffieHellman`, except that it does not allow changing 11821cb0ef41Sopenharmony_ciits keys after creation. In other words, it does not implement `setPublicKey()` 11831cb0ef41Sopenharmony_cior `setPrivateKey()` methods. 11841cb0ef41Sopenharmony_ci 11851cb0ef41Sopenharmony_ci```mjs 11861cb0ef41Sopenharmony_ciconst { createDiffieHellmanGroup } = await import('node:crypto'); 11871cb0ef41Sopenharmony_ciconst dh = createDiffieHellmanGroup('modp16'); 11881cb0ef41Sopenharmony_ci``` 11891cb0ef41Sopenharmony_ci 11901cb0ef41Sopenharmony_ci```cjs 11911cb0ef41Sopenharmony_ciconst { createDiffieHellmanGroup } = require('node:crypto'); 11921cb0ef41Sopenharmony_ciconst dh = createDiffieHellmanGroup('modp16'); 11931cb0ef41Sopenharmony_ci``` 11941cb0ef41Sopenharmony_ci 11951cb0ef41Sopenharmony_ciThe following groups are supported: 11961cb0ef41Sopenharmony_ci 11971cb0ef41Sopenharmony_ci* `'modp14'` (2048 bits, [RFC 3526][] Section 3) 11981cb0ef41Sopenharmony_ci* `'modp15'` (3072 bits, [RFC 3526][] Section 4) 11991cb0ef41Sopenharmony_ci* `'modp16'` (4096 bits, [RFC 3526][] Section 5) 12001cb0ef41Sopenharmony_ci* `'modp17'` (6144 bits, [RFC 3526][] Section 6) 12011cb0ef41Sopenharmony_ci* `'modp18'` (8192 bits, [RFC 3526][] Section 7) 12021cb0ef41Sopenharmony_ci 12031cb0ef41Sopenharmony_ciThe following groups are still supported but deprecated (see [Caveats][]): 12041cb0ef41Sopenharmony_ci 12051cb0ef41Sopenharmony_ci* `'modp1'` (768 bits, [RFC 2409][] Section 6.1) <span class="deprecated-inline"></span> 12061cb0ef41Sopenharmony_ci* `'modp2'` (1024 bits, [RFC 2409][] Section 6.2) <span class="deprecated-inline"></span> 12071cb0ef41Sopenharmony_ci* `'modp5'` (1536 bits, [RFC 3526][] Section 2) <span class="deprecated-inline"></span> 12081cb0ef41Sopenharmony_ci 12091cb0ef41Sopenharmony_ciThese deprecated groups might be removed in future versions of Node.js. 12101cb0ef41Sopenharmony_ci 12111cb0ef41Sopenharmony_ci## Class: `ECDH` 12121cb0ef41Sopenharmony_ci 12131cb0ef41Sopenharmony_ci<!-- YAML 12141cb0ef41Sopenharmony_ciadded: v0.11.14 12151cb0ef41Sopenharmony_ci--> 12161cb0ef41Sopenharmony_ci 12171cb0ef41Sopenharmony_ciThe `ECDH` class is a utility for creating Elliptic Curve Diffie-Hellman (ECDH) 12181cb0ef41Sopenharmony_cikey exchanges. 12191cb0ef41Sopenharmony_ci 12201cb0ef41Sopenharmony_ciInstances of the `ECDH` class can be created using the 12211cb0ef41Sopenharmony_ci[`crypto.createECDH()`][] function. 12221cb0ef41Sopenharmony_ci 12231cb0ef41Sopenharmony_ci```mjs 12241cb0ef41Sopenharmony_ciimport assert from 'node:assert'; 12251cb0ef41Sopenharmony_ci 12261cb0ef41Sopenharmony_ciconst { 12271cb0ef41Sopenharmony_ci createECDH, 12281cb0ef41Sopenharmony_ci} = await import('node:crypto'); 12291cb0ef41Sopenharmony_ci 12301cb0ef41Sopenharmony_ci// Generate Alice's keys... 12311cb0ef41Sopenharmony_ciconst alice = createECDH('secp521r1'); 12321cb0ef41Sopenharmony_ciconst aliceKey = alice.generateKeys(); 12331cb0ef41Sopenharmony_ci 12341cb0ef41Sopenharmony_ci// Generate Bob's keys... 12351cb0ef41Sopenharmony_ciconst bob = createECDH('secp521r1'); 12361cb0ef41Sopenharmony_ciconst bobKey = bob.generateKeys(); 12371cb0ef41Sopenharmony_ci 12381cb0ef41Sopenharmony_ci// Exchange and generate the secret... 12391cb0ef41Sopenharmony_ciconst aliceSecret = alice.computeSecret(bobKey); 12401cb0ef41Sopenharmony_ciconst bobSecret = bob.computeSecret(aliceKey); 12411cb0ef41Sopenharmony_ci 12421cb0ef41Sopenharmony_ciassert.strictEqual(aliceSecret.toString('hex'), bobSecret.toString('hex')); 12431cb0ef41Sopenharmony_ci// OK 12441cb0ef41Sopenharmony_ci``` 12451cb0ef41Sopenharmony_ci 12461cb0ef41Sopenharmony_ci```cjs 12471cb0ef41Sopenharmony_ciconst assert = require('node:assert'); 12481cb0ef41Sopenharmony_ci 12491cb0ef41Sopenharmony_ciconst { 12501cb0ef41Sopenharmony_ci createECDH, 12511cb0ef41Sopenharmony_ci} = require('node:crypto'); 12521cb0ef41Sopenharmony_ci 12531cb0ef41Sopenharmony_ci// Generate Alice's keys... 12541cb0ef41Sopenharmony_ciconst alice = createECDH('secp521r1'); 12551cb0ef41Sopenharmony_ciconst aliceKey = alice.generateKeys(); 12561cb0ef41Sopenharmony_ci 12571cb0ef41Sopenharmony_ci// Generate Bob's keys... 12581cb0ef41Sopenharmony_ciconst bob = createECDH('secp521r1'); 12591cb0ef41Sopenharmony_ciconst bobKey = bob.generateKeys(); 12601cb0ef41Sopenharmony_ci 12611cb0ef41Sopenharmony_ci// Exchange and generate the secret... 12621cb0ef41Sopenharmony_ciconst aliceSecret = alice.computeSecret(bobKey); 12631cb0ef41Sopenharmony_ciconst bobSecret = bob.computeSecret(aliceKey); 12641cb0ef41Sopenharmony_ci 12651cb0ef41Sopenharmony_ciassert.strictEqual(aliceSecret.toString('hex'), bobSecret.toString('hex')); 12661cb0ef41Sopenharmony_ci// OK 12671cb0ef41Sopenharmony_ci``` 12681cb0ef41Sopenharmony_ci 12691cb0ef41Sopenharmony_ci### Static method: `ECDH.convertKey(key, curve[, inputEncoding[, outputEncoding[, format]]])` 12701cb0ef41Sopenharmony_ci 12711cb0ef41Sopenharmony_ci<!-- YAML 12721cb0ef41Sopenharmony_ciadded: v10.0.0 12731cb0ef41Sopenharmony_ci--> 12741cb0ef41Sopenharmony_ci 12751cb0ef41Sopenharmony_ci* `key` {string|ArrayBuffer|Buffer|TypedArray|DataView} 12761cb0ef41Sopenharmony_ci* `curve` {string} 12771cb0ef41Sopenharmony_ci* `inputEncoding` {string} The [encoding][] of the `key` string. 12781cb0ef41Sopenharmony_ci* `outputEncoding` {string} The [encoding][] of the return value. 12791cb0ef41Sopenharmony_ci* `format` {string} **Default:** `'uncompressed'` 12801cb0ef41Sopenharmony_ci* Returns: {Buffer | string} 12811cb0ef41Sopenharmony_ci 12821cb0ef41Sopenharmony_ciConverts the EC Diffie-Hellman public key specified by `key` and `curve` to the 12831cb0ef41Sopenharmony_ciformat specified by `format`. The `format` argument specifies point encoding 12841cb0ef41Sopenharmony_ciand can be `'compressed'`, `'uncompressed'` or `'hybrid'`. The supplied key is 12851cb0ef41Sopenharmony_ciinterpreted using the specified `inputEncoding`, and the returned key is encoded 12861cb0ef41Sopenharmony_ciusing the specified `outputEncoding`. 12871cb0ef41Sopenharmony_ci 12881cb0ef41Sopenharmony_ciUse [`crypto.getCurves()`][] to obtain a list of available curve names. 12891cb0ef41Sopenharmony_ciOn recent OpenSSL releases, `openssl ecparam -list_curves` will also display 12901cb0ef41Sopenharmony_cithe name and description of each available elliptic curve. 12911cb0ef41Sopenharmony_ci 12921cb0ef41Sopenharmony_ciIf `format` is not specified the point will be returned in `'uncompressed'` 12931cb0ef41Sopenharmony_ciformat. 12941cb0ef41Sopenharmony_ci 12951cb0ef41Sopenharmony_ciIf the `inputEncoding` is not provided, `key` is expected to be a [`Buffer`][], 12961cb0ef41Sopenharmony_ci`TypedArray`, or `DataView`. 12971cb0ef41Sopenharmony_ci 12981cb0ef41Sopenharmony_ciExample (uncompressing a key): 12991cb0ef41Sopenharmony_ci 13001cb0ef41Sopenharmony_ci```mjs 13011cb0ef41Sopenharmony_ciconst { 13021cb0ef41Sopenharmony_ci createECDH, 13031cb0ef41Sopenharmony_ci ECDH, 13041cb0ef41Sopenharmony_ci} = await import('node:crypto'); 13051cb0ef41Sopenharmony_ci 13061cb0ef41Sopenharmony_ciconst ecdh = createECDH('secp256k1'); 13071cb0ef41Sopenharmony_ciecdh.generateKeys(); 13081cb0ef41Sopenharmony_ci 13091cb0ef41Sopenharmony_ciconst compressedKey = ecdh.getPublicKey('hex', 'compressed'); 13101cb0ef41Sopenharmony_ci 13111cb0ef41Sopenharmony_ciconst uncompressedKey = ECDH.convertKey(compressedKey, 13121cb0ef41Sopenharmony_ci 'secp256k1', 13131cb0ef41Sopenharmony_ci 'hex', 13141cb0ef41Sopenharmony_ci 'hex', 13151cb0ef41Sopenharmony_ci 'uncompressed'); 13161cb0ef41Sopenharmony_ci 13171cb0ef41Sopenharmony_ci// The converted key and the uncompressed public key should be the same 13181cb0ef41Sopenharmony_ciconsole.log(uncompressedKey === ecdh.getPublicKey('hex')); 13191cb0ef41Sopenharmony_ci``` 13201cb0ef41Sopenharmony_ci 13211cb0ef41Sopenharmony_ci```cjs 13221cb0ef41Sopenharmony_ciconst { 13231cb0ef41Sopenharmony_ci createECDH, 13241cb0ef41Sopenharmony_ci ECDH, 13251cb0ef41Sopenharmony_ci} = require('node:crypto'); 13261cb0ef41Sopenharmony_ci 13271cb0ef41Sopenharmony_ciconst ecdh = createECDH('secp256k1'); 13281cb0ef41Sopenharmony_ciecdh.generateKeys(); 13291cb0ef41Sopenharmony_ci 13301cb0ef41Sopenharmony_ciconst compressedKey = ecdh.getPublicKey('hex', 'compressed'); 13311cb0ef41Sopenharmony_ci 13321cb0ef41Sopenharmony_ciconst uncompressedKey = ECDH.convertKey(compressedKey, 13331cb0ef41Sopenharmony_ci 'secp256k1', 13341cb0ef41Sopenharmony_ci 'hex', 13351cb0ef41Sopenharmony_ci 'hex', 13361cb0ef41Sopenharmony_ci 'uncompressed'); 13371cb0ef41Sopenharmony_ci 13381cb0ef41Sopenharmony_ci// The converted key and the uncompressed public key should be the same 13391cb0ef41Sopenharmony_ciconsole.log(uncompressedKey === ecdh.getPublicKey('hex')); 13401cb0ef41Sopenharmony_ci``` 13411cb0ef41Sopenharmony_ci 13421cb0ef41Sopenharmony_ci### `ecdh.computeSecret(otherPublicKey[, inputEncoding][, outputEncoding])` 13431cb0ef41Sopenharmony_ci 13441cb0ef41Sopenharmony_ci<!-- YAML 13451cb0ef41Sopenharmony_ciadded: v0.11.14 13461cb0ef41Sopenharmony_cichanges: 13471cb0ef41Sopenharmony_ci - version: v10.0.0 13481cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/16849 13491cb0ef41Sopenharmony_ci description: Changed error format to better support invalid public key 13501cb0ef41Sopenharmony_ci error. 13511cb0ef41Sopenharmony_ci - version: v6.0.0 13521cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/5522 13531cb0ef41Sopenharmony_ci description: The default `inputEncoding` changed from `binary` to `utf8`. 13541cb0ef41Sopenharmony_ci--> 13551cb0ef41Sopenharmony_ci 13561cb0ef41Sopenharmony_ci* `otherPublicKey` {string|ArrayBuffer|Buffer|TypedArray|DataView} 13571cb0ef41Sopenharmony_ci* `inputEncoding` {string} The [encoding][] of the `otherPublicKey` string. 13581cb0ef41Sopenharmony_ci* `outputEncoding` {string} The [encoding][] of the return value. 13591cb0ef41Sopenharmony_ci* Returns: {Buffer | string} 13601cb0ef41Sopenharmony_ci 13611cb0ef41Sopenharmony_ciComputes the shared secret using `otherPublicKey` as the other 13621cb0ef41Sopenharmony_ciparty's public key and returns the computed shared secret. The supplied 13631cb0ef41Sopenharmony_cikey is interpreted using specified `inputEncoding`, and the returned secret 13641cb0ef41Sopenharmony_ciis encoded using the specified `outputEncoding`. 13651cb0ef41Sopenharmony_ciIf the `inputEncoding` is not 13661cb0ef41Sopenharmony_ciprovided, `otherPublicKey` is expected to be a [`Buffer`][], `TypedArray`, or 13671cb0ef41Sopenharmony_ci`DataView`. 13681cb0ef41Sopenharmony_ci 13691cb0ef41Sopenharmony_ciIf `outputEncoding` is given a string will be returned; otherwise a 13701cb0ef41Sopenharmony_ci[`Buffer`][] is returned. 13711cb0ef41Sopenharmony_ci 13721cb0ef41Sopenharmony_ci`ecdh.computeSecret` will throw an 13731cb0ef41Sopenharmony_ci`ERR_CRYPTO_ECDH_INVALID_PUBLIC_KEY` error when `otherPublicKey` 13741cb0ef41Sopenharmony_cilies outside of the elliptic curve. Since `otherPublicKey` is 13751cb0ef41Sopenharmony_ciusually supplied from a remote user over an insecure network, 13761cb0ef41Sopenharmony_cibe sure to handle this exception accordingly. 13771cb0ef41Sopenharmony_ci 13781cb0ef41Sopenharmony_ci### `ecdh.generateKeys([encoding[, format]])` 13791cb0ef41Sopenharmony_ci 13801cb0ef41Sopenharmony_ci<!-- YAML 13811cb0ef41Sopenharmony_ciadded: v0.11.14 13821cb0ef41Sopenharmony_ci--> 13831cb0ef41Sopenharmony_ci 13841cb0ef41Sopenharmony_ci* `encoding` {string} The [encoding][] of the return value. 13851cb0ef41Sopenharmony_ci* `format` {string} **Default:** `'uncompressed'` 13861cb0ef41Sopenharmony_ci* Returns: {Buffer | string} 13871cb0ef41Sopenharmony_ci 13881cb0ef41Sopenharmony_ciGenerates private and public EC Diffie-Hellman key values, and returns 13891cb0ef41Sopenharmony_cithe public key in the specified `format` and `encoding`. This key should be 13901cb0ef41Sopenharmony_citransferred to the other party. 13911cb0ef41Sopenharmony_ci 13921cb0ef41Sopenharmony_ciThe `format` argument specifies point encoding and can be `'compressed'` or 13931cb0ef41Sopenharmony_ci`'uncompressed'`. If `format` is not specified, the point will be returned in 13941cb0ef41Sopenharmony_ci`'uncompressed'` format. 13951cb0ef41Sopenharmony_ci 13961cb0ef41Sopenharmony_ciIf `encoding` is provided a string is returned; otherwise a [`Buffer`][] 13971cb0ef41Sopenharmony_ciis returned. 13981cb0ef41Sopenharmony_ci 13991cb0ef41Sopenharmony_ci### `ecdh.getPrivateKey([encoding])` 14001cb0ef41Sopenharmony_ci 14011cb0ef41Sopenharmony_ci<!-- YAML 14021cb0ef41Sopenharmony_ciadded: v0.11.14 14031cb0ef41Sopenharmony_ci--> 14041cb0ef41Sopenharmony_ci 14051cb0ef41Sopenharmony_ci* `encoding` {string} The [encoding][] of the return value. 14061cb0ef41Sopenharmony_ci* Returns: {Buffer | string} The EC Diffie-Hellman in the specified `encoding`. 14071cb0ef41Sopenharmony_ci 14081cb0ef41Sopenharmony_ciIf `encoding` is specified, a string is returned; otherwise a [`Buffer`][] is 14091cb0ef41Sopenharmony_cireturned. 14101cb0ef41Sopenharmony_ci 14111cb0ef41Sopenharmony_ci### `ecdh.getPublicKey([encoding][, format])` 14121cb0ef41Sopenharmony_ci 14131cb0ef41Sopenharmony_ci<!-- YAML 14141cb0ef41Sopenharmony_ciadded: v0.11.14 14151cb0ef41Sopenharmony_ci--> 14161cb0ef41Sopenharmony_ci 14171cb0ef41Sopenharmony_ci* `encoding` {string} The [encoding][] of the return value. 14181cb0ef41Sopenharmony_ci* `format` {string} **Default:** `'uncompressed'` 14191cb0ef41Sopenharmony_ci* Returns: {Buffer | string} The EC Diffie-Hellman public key in the specified 14201cb0ef41Sopenharmony_ci `encoding` and `format`. 14211cb0ef41Sopenharmony_ci 14221cb0ef41Sopenharmony_ciThe `format` argument specifies point encoding and can be `'compressed'` or 14231cb0ef41Sopenharmony_ci`'uncompressed'`. If `format` is not specified the point will be returned in 14241cb0ef41Sopenharmony_ci`'uncompressed'` format. 14251cb0ef41Sopenharmony_ci 14261cb0ef41Sopenharmony_ciIf `encoding` is specified, a string is returned; otherwise a [`Buffer`][] is 14271cb0ef41Sopenharmony_cireturned. 14281cb0ef41Sopenharmony_ci 14291cb0ef41Sopenharmony_ci### `ecdh.setPrivateKey(privateKey[, encoding])` 14301cb0ef41Sopenharmony_ci 14311cb0ef41Sopenharmony_ci<!-- YAML 14321cb0ef41Sopenharmony_ciadded: v0.11.14 14331cb0ef41Sopenharmony_ci--> 14341cb0ef41Sopenharmony_ci 14351cb0ef41Sopenharmony_ci* `privateKey` {string|ArrayBuffer|Buffer|TypedArray|DataView} 14361cb0ef41Sopenharmony_ci* `encoding` {string} The [encoding][] of the `privateKey` string. 14371cb0ef41Sopenharmony_ci 14381cb0ef41Sopenharmony_ciSets the EC Diffie-Hellman private key. 14391cb0ef41Sopenharmony_ciIf `encoding` is provided, `privateKey` is expected 14401cb0ef41Sopenharmony_cito be a string; otherwise `privateKey` is expected to be a [`Buffer`][], 14411cb0ef41Sopenharmony_ci`TypedArray`, or `DataView`. 14421cb0ef41Sopenharmony_ci 14431cb0ef41Sopenharmony_ciIf `privateKey` is not valid for the curve specified when the `ECDH` object was 14441cb0ef41Sopenharmony_cicreated, an error is thrown. Upon setting the private key, the associated 14451cb0ef41Sopenharmony_cipublic point (key) is also generated and set in the `ECDH` object. 14461cb0ef41Sopenharmony_ci 14471cb0ef41Sopenharmony_ci### `ecdh.setPublicKey(publicKey[, encoding])` 14481cb0ef41Sopenharmony_ci 14491cb0ef41Sopenharmony_ci<!-- YAML 14501cb0ef41Sopenharmony_ciadded: v0.11.14 14511cb0ef41Sopenharmony_cideprecated: v5.2.0 14521cb0ef41Sopenharmony_ci--> 14531cb0ef41Sopenharmony_ci 14541cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated 14551cb0ef41Sopenharmony_ci 14561cb0ef41Sopenharmony_ci* `publicKey` {string|ArrayBuffer|Buffer|TypedArray|DataView} 14571cb0ef41Sopenharmony_ci* `encoding` {string} The [encoding][] of the `publicKey` string. 14581cb0ef41Sopenharmony_ci 14591cb0ef41Sopenharmony_ciSets the EC Diffie-Hellman public key. 14601cb0ef41Sopenharmony_ciIf `encoding` is provided `publicKey` is expected to 14611cb0ef41Sopenharmony_cibe a string; otherwise a [`Buffer`][], `TypedArray`, or `DataView` is expected. 14621cb0ef41Sopenharmony_ci 14631cb0ef41Sopenharmony_ciThere is not normally a reason to call this method because `ECDH` 14641cb0ef41Sopenharmony_cionly requires a private key and the other party's public key to compute the 14651cb0ef41Sopenharmony_cishared secret. Typically either [`ecdh.generateKeys()`][] or 14661cb0ef41Sopenharmony_ci[`ecdh.setPrivateKey()`][] will be called. The [`ecdh.setPrivateKey()`][] method 14671cb0ef41Sopenharmony_ciattempts to generate the public point/key associated with the private key being 14681cb0ef41Sopenharmony_ciset. 14691cb0ef41Sopenharmony_ci 14701cb0ef41Sopenharmony_ciExample (obtaining a shared secret): 14711cb0ef41Sopenharmony_ci 14721cb0ef41Sopenharmony_ci```mjs 14731cb0ef41Sopenharmony_ciconst { 14741cb0ef41Sopenharmony_ci createECDH, 14751cb0ef41Sopenharmony_ci createHash, 14761cb0ef41Sopenharmony_ci} = await import('node:crypto'); 14771cb0ef41Sopenharmony_ci 14781cb0ef41Sopenharmony_ciconst alice = createECDH('secp256k1'); 14791cb0ef41Sopenharmony_ciconst bob = createECDH('secp256k1'); 14801cb0ef41Sopenharmony_ci 14811cb0ef41Sopenharmony_ci// This is a shortcut way of specifying one of Alice's previous private 14821cb0ef41Sopenharmony_ci// keys. It would be unwise to use such a predictable private key in a real 14831cb0ef41Sopenharmony_ci// application. 14841cb0ef41Sopenharmony_cialice.setPrivateKey( 14851cb0ef41Sopenharmony_ci createHash('sha256').update('alice', 'utf8').digest(), 14861cb0ef41Sopenharmony_ci); 14871cb0ef41Sopenharmony_ci 14881cb0ef41Sopenharmony_ci// Bob uses a newly generated cryptographically strong 14891cb0ef41Sopenharmony_ci// pseudorandom key pair 14901cb0ef41Sopenharmony_cibob.generateKeys(); 14911cb0ef41Sopenharmony_ci 14921cb0ef41Sopenharmony_ciconst aliceSecret = alice.computeSecret(bob.getPublicKey(), null, 'hex'); 14931cb0ef41Sopenharmony_ciconst bobSecret = bob.computeSecret(alice.getPublicKey(), null, 'hex'); 14941cb0ef41Sopenharmony_ci 14951cb0ef41Sopenharmony_ci// aliceSecret and bobSecret should be the same shared secret value 14961cb0ef41Sopenharmony_ciconsole.log(aliceSecret === bobSecret); 14971cb0ef41Sopenharmony_ci``` 14981cb0ef41Sopenharmony_ci 14991cb0ef41Sopenharmony_ci```cjs 15001cb0ef41Sopenharmony_ciconst { 15011cb0ef41Sopenharmony_ci createECDH, 15021cb0ef41Sopenharmony_ci createHash, 15031cb0ef41Sopenharmony_ci} = require('node:crypto'); 15041cb0ef41Sopenharmony_ci 15051cb0ef41Sopenharmony_ciconst alice = createECDH('secp256k1'); 15061cb0ef41Sopenharmony_ciconst bob = createECDH('secp256k1'); 15071cb0ef41Sopenharmony_ci 15081cb0ef41Sopenharmony_ci// This is a shortcut way of specifying one of Alice's previous private 15091cb0ef41Sopenharmony_ci// keys. It would be unwise to use such a predictable private key in a real 15101cb0ef41Sopenharmony_ci// application. 15111cb0ef41Sopenharmony_cialice.setPrivateKey( 15121cb0ef41Sopenharmony_ci createHash('sha256').update('alice', 'utf8').digest(), 15131cb0ef41Sopenharmony_ci); 15141cb0ef41Sopenharmony_ci 15151cb0ef41Sopenharmony_ci// Bob uses a newly generated cryptographically strong 15161cb0ef41Sopenharmony_ci// pseudorandom key pair 15171cb0ef41Sopenharmony_cibob.generateKeys(); 15181cb0ef41Sopenharmony_ci 15191cb0ef41Sopenharmony_ciconst aliceSecret = alice.computeSecret(bob.getPublicKey(), null, 'hex'); 15201cb0ef41Sopenharmony_ciconst bobSecret = bob.computeSecret(alice.getPublicKey(), null, 'hex'); 15211cb0ef41Sopenharmony_ci 15221cb0ef41Sopenharmony_ci// aliceSecret and bobSecret should be the same shared secret value 15231cb0ef41Sopenharmony_ciconsole.log(aliceSecret === bobSecret); 15241cb0ef41Sopenharmony_ci``` 15251cb0ef41Sopenharmony_ci 15261cb0ef41Sopenharmony_ci## Class: `Hash` 15271cb0ef41Sopenharmony_ci 15281cb0ef41Sopenharmony_ci<!-- YAML 15291cb0ef41Sopenharmony_ciadded: v0.1.92 15301cb0ef41Sopenharmony_ci--> 15311cb0ef41Sopenharmony_ci 15321cb0ef41Sopenharmony_ci* Extends: {stream.Transform} 15331cb0ef41Sopenharmony_ci 15341cb0ef41Sopenharmony_ciThe `Hash` class is a utility for creating hash digests of data. It can be 15351cb0ef41Sopenharmony_ciused in one of two ways: 15361cb0ef41Sopenharmony_ci 15371cb0ef41Sopenharmony_ci* As a [stream][] that is both readable and writable, where data is written 15381cb0ef41Sopenharmony_ci to produce a computed hash digest on the readable side, or 15391cb0ef41Sopenharmony_ci* Using the [`hash.update()`][] and [`hash.digest()`][] methods to produce the 15401cb0ef41Sopenharmony_ci computed hash. 15411cb0ef41Sopenharmony_ci 15421cb0ef41Sopenharmony_ciThe [`crypto.createHash()`][] method is used to create `Hash` instances. `Hash` 15431cb0ef41Sopenharmony_ciobjects are not to be created directly using the `new` keyword. 15441cb0ef41Sopenharmony_ci 15451cb0ef41Sopenharmony_ciExample: Using `Hash` objects as streams: 15461cb0ef41Sopenharmony_ci 15471cb0ef41Sopenharmony_ci```mjs 15481cb0ef41Sopenharmony_ciconst { 15491cb0ef41Sopenharmony_ci createHash, 15501cb0ef41Sopenharmony_ci} = await import('node:crypto'); 15511cb0ef41Sopenharmony_ci 15521cb0ef41Sopenharmony_ciconst hash = createHash('sha256'); 15531cb0ef41Sopenharmony_ci 15541cb0ef41Sopenharmony_cihash.on('readable', () => { 15551cb0ef41Sopenharmony_ci // Only one element is going to be produced by the 15561cb0ef41Sopenharmony_ci // hash stream. 15571cb0ef41Sopenharmony_ci const data = hash.read(); 15581cb0ef41Sopenharmony_ci if (data) { 15591cb0ef41Sopenharmony_ci console.log(data.toString('hex')); 15601cb0ef41Sopenharmony_ci // Prints: 15611cb0ef41Sopenharmony_ci // 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50 15621cb0ef41Sopenharmony_ci } 15631cb0ef41Sopenharmony_ci}); 15641cb0ef41Sopenharmony_ci 15651cb0ef41Sopenharmony_cihash.write('some data to hash'); 15661cb0ef41Sopenharmony_cihash.end(); 15671cb0ef41Sopenharmony_ci``` 15681cb0ef41Sopenharmony_ci 15691cb0ef41Sopenharmony_ci```cjs 15701cb0ef41Sopenharmony_ciconst { 15711cb0ef41Sopenharmony_ci createHash, 15721cb0ef41Sopenharmony_ci} = require('node:crypto'); 15731cb0ef41Sopenharmony_ci 15741cb0ef41Sopenharmony_ciconst hash = createHash('sha256'); 15751cb0ef41Sopenharmony_ci 15761cb0ef41Sopenharmony_cihash.on('readable', () => { 15771cb0ef41Sopenharmony_ci // Only one element is going to be produced by the 15781cb0ef41Sopenharmony_ci // hash stream. 15791cb0ef41Sopenharmony_ci const data = hash.read(); 15801cb0ef41Sopenharmony_ci if (data) { 15811cb0ef41Sopenharmony_ci console.log(data.toString('hex')); 15821cb0ef41Sopenharmony_ci // Prints: 15831cb0ef41Sopenharmony_ci // 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50 15841cb0ef41Sopenharmony_ci } 15851cb0ef41Sopenharmony_ci}); 15861cb0ef41Sopenharmony_ci 15871cb0ef41Sopenharmony_cihash.write('some data to hash'); 15881cb0ef41Sopenharmony_cihash.end(); 15891cb0ef41Sopenharmony_ci``` 15901cb0ef41Sopenharmony_ci 15911cb0ef41Sopenharmony_ciExample: Using `Hash` and piped streams: 15921cb0ef41Sopenharmony_ci 15931cb0ef41Sopenharmony_ci```mjs 15941cb0ef41Sopenharmony_ciimport { createReadStream } from 'node:fs'; 15951cb0ef41Sopenharmony_ciimport { stdout } from 'node:process'; 15961cb0ef41Sopenharmony_ciconst { createHash } = await import('node:crypto'); 15971cb0ef41Sopenharmony_ci 15981cb0ef41Sopenharmony_ciconst hash = createHash('sha256'); 15991cb0ef41Sopenharmony_ci 16001cb0ef41Sopenharmony_ciconst input = createReadStream('test.js'); 16011cb0ef41Sopenharmony_ciinput.pipe(hash).setEncoding('hex').pipe(stdout); 16021cb0ef41Sopenharmony_ci``` 16031cb0ef41Sopenharmony_ci 16041cb0ef41Sopenharmony_ci```cjs 16051cb0ef41Sopenharmony_ciconst { createReadStream } = require('node:fs'); 16061cb0ef41Sopenharmony_ciconst { createHash } = require('node:crypto'); 16071cb0ef41Sopenharmony_ciconst { stdout } = require('node:process'); 16081cb0ef41Sopenharmony_ci 16091cb0ef41Sopenharmony_ciconst hash = createHash('sha256'); 16101cb0ef41Sopenharmony_ci 16111cb0ef41Sopenharmony_ciconst input = createReadStream('test.js'); 16121cb0ef41Sopenharmony_ciinput.pipe(hash).setEncoding('hex').pipe(stdout); 16131cb0ef41Sopenharmony_ci``` 16141cb0ef41Sopenharmony_ci 16151cb0ef41Sopenharmony_ciExample: Using the [`hash.update()`][] and [`hash.digest()`][] methods: 16161cb0ef41Sopenharmony_ci 16171cb0ef41Sopenharmony_ci```mjs 16181cb0ef41Sopenharmony_ciconst { 16191cb0ef41Sopenharmony_ci createHash, 16201cb0ef41Sopenharmony_ci} = await import('node:crypto'); 16211cb0ef41Sopenharmony_ci 16221cb0ef41Sopenharmony_ciconst hash = createHash('sha256'); 16231cb0ef41Sopenharmony_ci 16241cb0ef41Sopenharmony_cihash.update('some data to hash'); 16251cb0ef41Sopenharmony_ciconsole.log(hash.digest('hex')); 16261cb0ef41Sopenharmony_ci// Prints: 16271cb0ef41Sopenharmony_ci// 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50 16281cb0ef41Sopenharmony_ci``` 16291cb0ef41Sopenharmony_ci 16301cb0ef41Sopenharmony_ci```cjs 16311cb0ef41Sopenharmony_ciconst { 16321cb0ef41Sopenharmony_ci createHash, 16331cb0ef41Sopenharmony_ci} = require('node:crypto'); 16341cb0ef41Sopenharmony_ci 16351cb0ef41Sopenharmony_ciconst hash = createHash('sha256'); 16361cb0ef41Sopenharmony_ci 16371cb0ef41Sopenharmony_cihash.update('some data to hash'); 16381cb0ef41Sopenharmony_ciconsole.log(hash.digest('hex')); 16391cb0ef41Sopenharmony_ci// Prints: 16401cb0ef41Sopenharmony_ci// 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50 16411cb0ef41Sopenharmony_ci``` 16421cb0ef41Sopenharmony_ci 16431cb0ef41Sopenharmony_ci### `hash.copy([options])` 16441cb0ef41Sopenharmony_ci 16451cb0ef41Sopenharmony_ci<!-- YAML 16461cb0ef41Sopenharmony_ciadded: v13.1.0 16471cb0ef41Sopenharmony_ci--> 16481cb0ef41Sopenharmony_ci 16491cb0ef41Sopenharmony_ci* `options` {Object} [`stream.transform` options][] 16501cb0ef41Sopenharmony_ci* Returns: {Hash} 16511cb0ef41Sopenharmony_ci 16521cb0ef41Sopenharmony_ciCreates a new `Hash` object that contains a deep copy of the internal state 16531cb0ef41Sopenharmony_ciof the current `Hash` object. 16541cb0ef41Sopenharmony_ci 16551cb0ef41Sopenharmony_ciThe optional `options` argument controls stream behavior. For XOF hash 16561cb0ef41Sopenharmony_cifunctions such as `'shake256'`, the `outputLength` option can be used to 16571cb0ef41Sopenharmony_cispecify the desired output length in bytes. 16581cb0ef41Sopenharmony_ci 16591cb0ef41Sopenharmony_ciAn error is thrown when an attempt is made to copy the `Hash` object after 16601cb0ef41Sopenharmony_ciits [`hash.digest()`][] method has been called. 16611cb0ef41Sopenharmony_ci 16621cb0ef41Sopenharmony_ci```mjs 16631cb0ef41Sopenharmony_ci// Calculate a rolling hash. 16641cb0ef41Sopenharmony_ciconst { 16651cb0ef41Sopenharmony_ci createHash, 16661cb0ef41Sopenharmony_ci} = await import('node:crypto'); 16671cb0ef41Sopenharmony_ci 16681cb0ef41Sopenharmony_ciconst hash = createHash('sha256'); 16691cb0ef41Sopenharmony_ci 16701cb0ef41Sopenharmony_cihash.update('one'); 16711cb0ef41Sopenharmony_ciconsole.log(hash.copy().digest('hex')); 16721cb0ef41Sopenharmony_ci 16731cb0ef41Sopenharmony_cihash.update('two'); 16741cb0ef41Sopenharmony_ciconsole.log(hash.copy().digest('hex')); 16751cb0ef41Sopenharmony_ci 16761cb0ef41Sopenharmony_cihash.update('three'); 16771cb0ef41Sopenharmony_ciconsole.log(hash.copy().digest('hex')); 16781cb0ef41Sopenharmony_ci 16791cb0ef41Sopenharmony_ci// Etc. 16801cb0ef41Sopenharmony_ci``` 16811cb0ef41Sopenharmony_ci 16821cb0ef41Sopenharmony_ci```cjs 16831cb0ef41Sopenharmony_ci// Calculate a rolling hash. 16841cb0ef41Sopenharmony_ciconst { 16851cb0ef41Sopenharmony_ci createHash, 16861cb0ef41Sopenharmony_ci} = require('node:crypto'); 16871cb0ef41Sopenharmony_ci 16881cb0ef41Sopenharmony_ciconst hash = createHash('sha256'); 16891cb0ef41Sopenharmony_ci 16901cb0ef41Sopenharmony_cihash.update('one'); 16911cb0ef41Sopenharmony_ciconsole.log(hash.copy().digest('hex')); 16921cb0ef41Sopenharmony_ci 16931cb0ef41Sopenharmony_cihash.update('two'); 16941cb0ef41Sopenharmony_ciconsole.log(hash.copy().digest('hex')); 16951cb0ef41Sopenharmony_ci 16961cb0ef41Sopenharmony_cihash.update('three'); 16971cb0ef41Sopenharmony_ciconsole.log(hash.copy().digest('hex')); 16981cb0ef41Sopenharmony_ci 16991cb0ef41Sopenharmony_ci// Etc. 17001cb0ef41Sopenharmony_ci``` 17011cb0ef41Sopenharmony_ci 17021cb0ef41Sopenharmony_ci### `hash.digest([encoding])` 17031cb0ef41Sopenharmony_ci 17041cb0ef41Sopenharmony_ci<!-- YAML 17051cb0ef41Sopenharmony_ciadded: v0.1.92 17061cb0ef41Sopenharmony_ci--> 17071cb0ef41Sopenharmony_ci 17081cb0ef41Sopenharmony_ci* `encoding` {string} The [encoding][] of the return value. 17091cb0ef41Sopenharmony_ci* Returns: {Buffer | string} 17101cb0ef41Sopenharmony_ci 17111cb0ef41Sopenharmony_ciCalculates the digest of all of the data passed to be hashed (using the 17121cb0ef41Sopenharmony_ci[`hash.update()`][] method). 17131cb0ef41Sopenharmony_ciIf `encoding` is provided a string will be returned; otherwise 17141cb0ef41Sopenharmony_cia [`Buffer`][] is returned. 17151cb0ef41Sopenharmony_ci 17161cb0ef41Sopenharmony_ciThe `Hash` object can not be used again after `hash.digest()` method has been 17171cb0ef41Sopenharmony_cicalled. Multiple calls will cause an error to be thrown. 17181cb0ef41Sopenharmony_ci 17191cb0ef41Sopenharmony_ci### `hash.update(data[, inputEncoding])` 17201cb0ef41Sopenharmony_ci 17211cb0ef41Sopenharmony_ci<!-- YAML 17221cb0ef41Sopenharmony_ciadded: v0.1.92 17231cb0ef41Sopenharmony_cichanges: 17241cb0ef41Sopenharmony_ci - version: v6.0.0 17251cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/5522 17261cb0ef41Sopenharmony_ci description: The default `inputEncoding` changed from `binary` to `utf8`. 17271cb0ef41Sopenharmony_ci--> 17281cb0ef41Sopenharmony_ci 17291cb0ef41Sopenharmony_ci* `data` {string|Buffer|TypedArray|DataView} 17301cb0ef41Sopenharmony_ci* `inputEncoding` {string} The [encoding][] of the `data` string. 17311cb0ef41Sopenharmony_ci 17321cb0ef41Sopenharmony_ciUpdates the hash content with the given `data`, the encoding of which 17331cb0ef41Sopenharmony_ciis given in `inputEncoding`. 17341cb0ef41Sopenharmony_ciIf `encoding` is not provided, and the `data` is a string, an 17351cb0ef41Sopenharmony_ciencoding of `'utf8'` is enforced. If `data` is a [`Buffer`][], `TypedArray`, or 17361cb0ef41Sopenharmony_ci`DataView`, then `inputEncoding` is ignored. 17371cb0ef41Sopenharmony_ci 17381cb0ef41Sopenharmony_ciThis can be called many times with new data as it is streamed. 17391cb0ef41Sopenharmony_ci 17401cb0ef41Sopenharmony_ci## Class: `Hmac` 17411cb0ef41Sopenharmony_ci 17421cb0ef41Sopenharmony_ci<!-- YAML 17431cb0ef41Sopenharmony_ciadded: v0.1.94 17441cb0ef41Sopenharmony_ci--> 17451cb0ef41Sopenharmony_ci 17461cb0ef41Sopenharmony_ci* Extends: {stream.Transform} 17471cb0ef41Sopenharmony_ci 17481cb0ef41Sopenharmony_ciThe `Hmac` class is a utility for creating cryptographic HMAC digests. It can 17491cb0ef41Sopenharmony_cibe used in one of two ways: 17501cb0ef41Sopenharmony_ci 17511cb0ef41Sopenharmony_ci* As a [stream][] that is both readable and writable, where data is written 17521cb0ef41Sopenharmony_ci to produce a computed HMAC digest on the readable side, or 17531cb0ef41Sopenharmony_ci* Using the [`hmac.update()`][] and [`hmac.digest()`][] methods to produce the 17541cb0ef41Sopenharmony_ci computed HMAC digest. 17551cb0ef41Sopenharmony_ci 17561cb0ef41Sopenharmony_ciThe [`crypto.createHmac()`][] method is used to create `Hmac` instances. `Hmac` 17571cb0ef41Sopenharmony_ciobjects are not to be created directly using the `new` keyword. 17581cb0ef41Sopenharmony_ci 17591cb0ef41Sopenharmony_ciExample: Using `Hmac` objects as streams: 17601cb0ef41Sopenharmony_ci 17611cb0ef41Sopenharmony_ci```mjs 17621cb0ef41Sopenharmony_ciconst { 17631cb0ef41Sopenharmony_ci createHmac, 17641cb0ef41Sopenharmony_ci} = await import('node:crypto'); 17651cb0ef41Sopenharmony_ci 17661cb0ef41Sopenharmony_ciconst hmac = createHmac('sha256', 'a secret'); 17671cb0ef41Sopenharmony_ci 17681cb0ef41Sopenharmony_cihmac.on('readable', () => { 17691cb0ef41Sopenharmony_ci // Only one element is going to be produced by the 17701cb0ef41Sopenharmony_ci // hash stream. 17711cb0ef41Sopenharmony_ci const data = hmac.read(); 17721cb0ef41Sopenharmony_ci if (data) { 17731cb0ef41Sopenharmony_ci console.log(data.toString('hex')); 17741cb0ef41Sopenharmony_ci // Prints: 17751cb0ef41Sopenharmony_ci // 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e 17761cb0ef41Sopenharmony_ci } 17771cb0ef41Sopenharmony_ci}); 17781cb0ef41Sopenharmony_ci 17791cb0ef41Sopenharmony_cihmac.write('some data to hash'); 17801cb0ef41Sopenharmony_cihmac.end(); 17811cb0ef41Sopenharmony_ci``` 17821cb0ef41Sopenharmony_ci 17831cb0ef41Sopenharmony_ci```cjs 17841cb0ef41Sopenharmony_ciconst { 17851cb0ef41Sopenharmony_ci createHmac, 17861cb0ef41Sopenharmony_ci} = require('node:crypto'); 17871cb0ef41Sopenharmony_ci 17881cb0ef41Sopenharmony_ciconst hmac = createHmac('sha256', 'a secret'); 17891cb0ef41Sopenharmony_ci 17901cb0ef41Sopenharmony_cihmac.on('readable', () => { 17911cb0ef41Sopenharmony_ci // Only one element is going to be produced by the 17921cb0ef41Sopenharmony_ci // hash stream. 17931cb0ef41Sopenharmony_ci const data = hmac.read(); 17941cb0ef41Sopenharmony_ci if (data) { 17951cb0ef41Sopenharmony_ci console.log(data.toString('hex')); 17961cb0ef41Sopenharmony_ci // Prints: 17971cb0ef41Sopenharmony_ci // 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e 17981cb0ef41Sopenharmony_ci } 17991cb0ef41Sopenharmony_ci}); 18001cb0ef41Sopenharmony_ci 18011cb0ef41Sopenharmony_cihmac.write('some data to hash'); 18021cb0ef41Sopenharmony_cihmac.end(); 18031cb0ef41Sopenharmony_ci``` 18041cb0ef41Sopenharmony_ci 18051cb0ef41Sopenharmony_ciExample: Using `Hmac` and piped streams: 18061cb0ef41Sopenharmony_ci 18071cb0ef41Sopenharmony_ci```mjs 18081cb0ef41Sopenharmony_ciimport { createReadStream } from 'node:fs'; 18091cb0ef41Sopenharmony_ciimport { stdout } from 'node:process'; 18101cb0ef41Sopenharmony_ciconst { 18111cb0ef41Sopenharmony_ci createHmac, 18121cb0ef41Sopenharmony_ci} = await import('node:crypto'); 18131cb0ef41Sopenharmony_ci 18141cb0ef41Sopenharmony_ciconst hmac = createHmac('sha256', 'a secret'); 18151cb0ef41Sopenharmony_ci 18161cb0ef41Sopenharmony_ciconst input = createReadStream('test.js'); 18171cb0ef41Sopenharmony_ciinput.pipe(hmac).pipe(stdout); 18181cb0ef41Sopenharmony_ci``` 18191cb0ef41Sopenharmony_ci 18201cb0ef41Sopenharmony_ci```cjs 18211cb0ef41Sopenharmony_ciconst { 18221cb0ef41Sopenharmony_ci createReadStream, 18231cb0ef41Sopenharmony_ci} = require('node:fs'); 18241cb0ef41Sopenharmony_ciconst { 18251cb0ef41Sopenharmony_ci createHmac, 18261cb0ef41Sopenharmony_ci} = require('node:crypto'); 18271cb0ef41Sopenharmony_ciconst { stdout } = require('node:process'); 18281cb0ef41Sopenharmony_ci 18291cb0ef41Sopenharmony_ciconst hmac = createHmac('sha256', 'a secret'); 18301cb0ef41Sopenharmony_ci 18311cb0ef41Sopenharmony_ciconst input = createReadStream('test.js'); 18321cb0ef41Sopenharmony_ciinput.pipe(hmac).pipe(stdout); 18331cb0ef41Sopenharmony_ci``` 18341cb0ef41Sopenharmony_ci 18351cb0ef41Sopenharmony_ciExample: Using the [`hmac.update()`][] and [`hmac.digest()`][] methods: 18361cb0ef41Sopenharmony_ci 18371cb0ef41Sopenharmony_ci```mjs 18381cb0ef41Sopenharmony_ciconst { 18391cb0ef41Sopenharmony_ci createHmac, 18401cb0ef41Sopenharmony_ci} = await import('node:crypto'); 18411cb0ef41Sopenharmony_ci 18421cb0ef41Sopenharmony_ciconst hmac = createHmac('sha256', 'a secret'); 18431cb0ef41Sopenharmony_ci 18441cb0ef41Sopenharmony_cihmac.update('some data to hash'); 18451cb0ef41Sopenharmony_ciconsole.log(hmac.digest('hex')); 18461cb0ef41Sopenharmony_ci// Prints: 18471cb0ef41Sopenharmony_ci// 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e 18481cb0ef41Sopenharmony_ci``` 18491cb0ef41Sopenharmony_ci 18501cb0ef41Sopenharmony_ci```cjs 18511cb0ef41Sopenharmony_ciconst { 18521cb0ef41Sopenharmony_ci createHmac, 18531cb0ef41Sopenharmony_ci} = require('node:crypto'); 18541cb0ef41Sopenharmony_ci 18551cb0ef41Sopenharmony_ciconst hmac = createHmac('sha256', 'a secret'); 18561cb0ef41Sopenharmony_ci 18571cb0ef41Sopenharmony_cihmac.update('some data to hash'); 18581cb0ef41Sopenharmony_ciconsole.log(hmac.digest('hex')); 18591cb0ef41Sopenharmony_ci// Prints: 18601cb0ef41Sopenharmony_ci// 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e 18611cb0ef41Sopenharmony_ci``` 18621cb0ef41Sopenharmony_ci 18631cb0ef41Sopenharmony_ci### `hmac.digest([encoding])` 18641cb0ef41Sopenharmony_ci 18651cb0ef41Sopenharmony_ci<!-- YAML 18661cb0ef41Sopenharmony_ciadded: v0.1.94 18671cb0ef41Sopenharmony_ci--> 18681cb0ef41Sopenharmony_ci 18691cb0ef41Sopenharmony_ci* `encoding` {string} The [encoding][] of the return value. 18701cb0ef41Sopenharmony_ci* Returns: {Buffer | string} 18711cb0ef41Sopenharmony_ci 18721cb0ef41Sopenharmony_ciCalculates the HMAC digest of all of the data passed using [`hmac.update()`][]. 18731cb0ef41Sopenharmony_ciIf `encoding` is 18741cb0ef41Sopenharmony_ciprovided a string is returned; otherwise a [`Buffer`][] is returned; 18751cb0ef41Sopenharmony_ci 18761cb0ef41Sopenharmony_ciThe `Hmac` object can not be used again after `hmac.digest()` has been 18771cb0ef41Sopenharmony_cicalled. Multiple calls to `hmac.digest()` will result in an error being thrown. 18781cb0ef41Sopenharmony_ci 18791cb0ef41Sopenharmony_ci### `hmac.update(data[, inputEncoding])` 18801cb0ef41Sopenharmony_ci 18811cb0ef41Sopenharmony_ci<!-- YAML 18821cb0ef41Sopenharmony_ciadded: v0.1.94 18831cb0ef41Sopenharmony_cichanges: 18841cb0ef41Sopenharmony_ci - version: v6.0.0 18851cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/5522 18861cb0ef41Sopenharmony_ci description: The default `inputEncoding` changed from `binary` to `utf8`. 18871cb0ef41Sopenharmony_ci--> 18881cb0ef41Sopenharmony_ci 18891cb0ef41Sopenharmony_ci* `data` {string|Buffer|TypedArray|DataView} 18901cb0ef41Sopenharmony_ci* `inputEncoding` {string} The [encoding][] of the `data` string. 18911cb0ef41Sopenharmony_ci 18921cb0ef41Sopenharmony_ciUpdates the `Hmac` content with the given `data`, the encoding of which 18931cb0ef41Sopenharmony_ciis given in `inputEncoding`. 18941cb0ef41Sopenharmony_ciIf `encoding` is not provided, and the `data` is a string, an 18951cb0ef41Sopenharmony_ciencoding of `'utf8'` is enforced. If `data` is a [`Buffer`][], `TypedArray`, or 18961cb0ef41Sopenharmony_ci`DataView`, then `inputEncoding` is ignored. 18971cb0ef41Sopenharmony_ci 18981cb0ef41Sopenharmony_ciThis can be called many times with new data as it is streamed. 18991cb0ef41Sopenharmony_ci 19001cb0ef41Sopenharmony_ci## Class: `KeyObject` 19011cb0ef41Sopenharmony_ci 19021cb0ef41Sopenharmony_ci<!-- YAML 19031cb0ef41Sopenharmony_ciadded: v11.6.0 19041cb0ef41Sopenharmony_cichanges: 19051cb0ef41Sopenharmony_ci - version: 19061cb0ef41Sopenharmony_ci - v14.5.0 19071cb0ef41Sopenharmony_ci - v12.19.0 19081cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/33360 19091cb0ef41Sopenharmony_ci description: Instances of this class can now be passed to worker threads 19101cb0ef41Sopenharmony_ci using `postMessage`. 19111cb0ef41Sopenharmony_ci - version: v11.13.0 19121cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/26438 19131cb0ef41Sopenharmony_ci description: This class is now exported. 19141cb0ef41Sopenharmony_ci--> 19151cb0ef41Sopenharmony_ci 19161cb0ef41Sopenharmony_ciNode.js uses a `KeyObject` class to represent a symmetric or asymmetric key, 19171cb0ef41Sopenharmony_ciand each kind of key exposes different functions. The 19181cb0ef41Sopenharmony_ci[`crypto.createSecretKey()`][], [`crypto.createPublicKey()`][] and 19191cb0ef41Sopenharmony_ci[`crypto.createPrivateKey()`][] methods are used to create `KeyObject` 19201cb0ef41Sopenharmony_ciinstances. `KeyObject` objects are not to be created directly using the `new` 19211cb0ef41Sopenharmony_cikeyword. 19221cb0ef41Sopenharmony_ci 19231cb0ef41Sopenharmony_ciMost applications should consider using the new `KeyObject` API instead of 19241cb0ef41Sopenharmony_cipassing keys as strings or `Buffer`s due to improved security features. 19251cb0ef41Sopenharmony_ci 19261cb0ef41Sopenharmony_ci`KeyObject` instances can be passed to other threads via [`postMessage()`][]. 19271cb0ef41Sopenharmony_ciThe receiver obtains a cloned `KeyObject`, and the `KeyObject` does not need to 19281cb0ef41Sopenharmony_cibe listed in the `transferList` argument. 19291cb0ef41Sopenharmony_ci 19301cb0ef41Sopenharmony_ci### Static method: `KeyObject.from(key)` 19311cb0ef41Sopenharmony_ci 19321cb0ef41Sopenharmony_ci<!-- YAML 19331cb0ef41Sopenharmony_ciadded: v15.0.0 19341cb0ef41Sopenharmony_ci--> 19351cb0ef41Sopenharmony_ci 19361cb0ef41Sopenharmony_ci* `key` {CryptoKey} 19371cb0ef41Sopenharmony_ci* Returns: {KeyObject} 19381cb0ef41Sopenharmony_ci 19391cb0ef41Sopenharmony_ciExample: Converting a `CryptoKey` instance to a `KeyObject`: 19401cb0ef41Sopenharmony_ci 19411cb0ef41Sopenharmony_ci```mjs 19421cb0ef41Sopenharmony_ciconst { webcrypto, KeyObject } = await import('node:crypto'); 19431cb0ef41Sopenharmony_ciconst { subtle } = webcrypto; 19441cb0ef41Sopenharmony_ci 19451cb0ef41Sopenharmony_ciconst key = await subtle.generateKey({ 19461cb0ef41Sopenharmony_ci name: 'HMAC', 19471cb0ef41Sopenharmony_ci hash: 'SHA-256', 19481cb0ef41Sopenharmony_ci length: 256, 19491cb0ef41Sopenharmony_ci}, true, ['sign', 'verify']); 19501cb0ef41Sopenharmony_ci 19511cb0ef41Sopenharmony_ciconst keyObject = KeyObject.from(key); 19521cb0ef41Sopenharmony_ciconsole.log(keyObject.symmetricKeySize); 19531cb0ef41Sopenharmony_ci// Prints: 32 (symmetric key size in bytes) 19541cb0ef41Sopenharmony_ci``` 19551cb0ef41Sopenharmony_ci 19561cb0ef41Sopenharmony_ci```cjs 19571cb0ef41Sopenharmony_ciconst { 19581cb0ef41Sopenharmony_ci webcrypto: { 19591cb0ef41Sopenharmony_ci subtle, 19601cb0ef41Sopenharmony_ci }, 19611cb0ef41Sopenharmony_ci KeyObject, 19621cb0ef41Sopenharmony_ci} = require('node:crypto'); 19631cb0ef41Sopenharmony_ci 19641cb0ef41Sopenharmony_ci(async function() { 19651cb0ef41Sopenharmony_ci const key = await subtle.generateKey({ 19661cb0ef41Sopenharmony_ci name: 'HMAC', 19671cb0ef41Sopenharmony_ci hash: 'SHA-256', 19681cb0ef41Sopenharmony_ci length: 256, 19691cb0ef41Sopenharmony_ci }, true, ['sign', 'verify']); 19701cb0ef41Sopenharmony_ci 19711cb0ef41Sopenharmony_ci const keyObject = KeyObject.from(key); 19721cb0ef41Sopenharmony_ci console.log(keyObject.symmetricKeySize); 19731cb0ef41Sopenharmony_ci // Prints: 32 (symmetric key size in bytes) 19741cb0ef41Sopenharmony_ci})(); 19751cb0ef41Sopenharmony_ci``` 19761cb0ef41Sopenharmony_ci 19771cb0ef41Sopenharmony_ci### `keyObject.asymmetricKeyDetails` 19781cb0ef41Sopenharmony_ci 19791cb0ef41Sopenharmony_ci<!-- YAML 19801cb0ef41Sopenharmony_ciadded: v15.7.0 19811cb0ef41Sopenharmony_cichanges: 19821cb0ef41Sopenharmony_ci - version: v16.9.0 19831cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/39851 19841cb0ef41Sopenharmony_ci description: Expose `RSASSA-PSS-params` sequence parameters 19851cb0ef41Sopenharmony_ci for RSA-PSS keys. 19861cb0ef41Sopenharmony_ci--> 19871cb0ef41Sopenharmony_ci 19881cb0ef41Sopenharmony_ci* {Object} 19891cb0ef41Sopenharmony_ci * `modulusLength`: {number} Key size in bits (RSA, DSA). 19901cb0ef41Sopenharmony_ci * `publicExponent`: {bigint} Public exponent (RSA). 19911cb0ef41Sopenharmony_ci * `hashAlgorithm`: {string} Name of the message digest (RSA-PSS). 19921cb0ef41Sopenharmony_ci * `mgf1HashAlgorithm`: {string} Name of the message digest used by 19931cb0ef41Sopenharmony_ci MGF1 (RSA-PSS). 19941cb0ef41Sopenharmony_ci * `saltLength`: {number} Minimal salt length in bytes (RSA-PSS). 19951cb0ef41Sopenharmony_ci * `divisorLength`: {number} Size of `q` in bits (DSA). 19961cb0ef41Sopenharmony_ci * `namedCurve`: {string} Name of the curve (EC). 19971cb0ef41Sopenharmony_ci 19981cb0ef41Sopenharmony_ciThis property exists only on asymmetric keys. Depending on the type of the key, 19991cb0ef41Sopenharmony_cithis object contains information about the key. None of the information obtained 20001cb0ef41Sopenharmony_cithrough this property can be used to uniquely identify a key or to compromise 20011cb0ef41Sopenharmony_cithe security of the key. 20021cb0ef41Sopenharmony_ci 20031cb0ef41Sopenharmony_ciFor RSA-PSS keys, if the key material contains a `RSASSA-PSS-params` sequence, 20041cb0ef41Sopenharmony_cithe `hashAlgorithm`, `mgf1HashAlgorithm`, and `saltLength` properties will be 20051cb0ef41Sopenharmony_ciset. 20061cb0ef41Sopenharmony_ci 20071cb0ef41Sopenharmony_ciOther key details might be exposed via this API using additional attributes. 20081cb0ef41Sopenharmony_ci 20091cb0ef41Sopenharmony_ci### `keyObject.asymmetricKeyType` 20101cb0ef41Sopenharmony_ci 20111cb0ef41Sopenharmony_ci<!-- YAML 20121cb0ef41Sopenharmony_ciadded: v11.6.0 20131cb0ef41Sopenharmony_cichanges: 20141cb0ef41Sopenharmony_ci - version: 20151cb0ef41Sopenharmony_ci - v13.9.0 20161cb0ef41Sopenharmony_ci - v12.17.0 20171cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/31178 20181cb0ef41Sopenharmony_ci description: Added support for `'dh'`. 20191cb0ef41Sopenharmony_ci - version: v12.0.0 20201cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/26960 20211cb0ef41Sopenharmony_ci description: Added support for `'rsa-pss'`. 20221cb0ef41Sopenharmony_ci - version: v12.0.0 20231cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/26786 20241cb0ef41Sopenharmony_ci description: This property now returns `undefined` for KeyObject 20251cb0ef41Sopenharmony_ci instances of unrecognized type instead of aborting. 20261cb0ef41Sopenharmony_ci - version: v12.0.0 20271cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/26774 20281cb0ef41Sopenharmony_ci description: Added support for `'x25519'` and `'x448'`. 20291cb0ef41Sopenharmony_ci - version: v12.0.0 20301cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/26319 20311cb0ef41Sopenharmony_ci description: Added support for `'ed25519'` and `'ed448'`. 20321cb0ef41Sopenharmony_ci--> 20331cb0ef41Sopenharmony_ci 20341cb0ef41Sopenharmony_ci* {string} 20351cb0ef41Sopenharmony_ci 20361cb0ef41Sopenharmony_ciFor asymmetric keys, this property represents the type of the key. Supported key 20371cb0ef41Sopenharmony_citypes are: 20381cb0ef41Sopenharmony_ci 20391cb0ef41Sopenharmony_ci* `'rsa'` (OID 1.2.840.113549.1.1.1) 20401cb0ef41Sopenharmony_ci* `'rsa-pss'` (OID 1.2.840.113549.1.1.10) 20411cb0ef41Sopenharmony_ci* `'dsa'` (OID 1.2.840.10040.4.1) 20421cb0ef41Sopenharmony_ci* `'ec'` (OID 1.2.840.10045.2.1) 20431cb0ef41Sopenharmony_ci* `'x25519'` (OID 1.3.101.110) 20441cb0ef41Sopenharmony_ci* `'x448'` (OID 1.3.101.111) 20451cb0ef41Sopenharmony_ci* `'ed25519'` (OID 1.3.101.112) 20461cb0ef41Sopenharmony_ci* `'ed448'` (OID 1.3.101.113) 20471cb0ef41Sopenharmony_ci* `'dh'` (OID 1.2.840.113549.1.3.1) 20481cb0ef41Sopenharmony_ci 20491cb0ef41Sopenharmony_ciThis property is `undefined` for unrecognized `KeyObject` types and symmetric 20501cb0ef41Sopenharmony_cikeys. 20511cb0ef41Sopenharmony_ci 20521cb0ef41Sopenharmony_ci### `keyObject.export([options])` 20531cb0ef41Sopenharmony_ci 20541cb0ef41Sopenharmony_ci<!-- YAML 20551cb0ef41Sopenharmony_ciadded: v11.6.0 20561cb0ef41Sopenharmony_cichanges: 20571cb0ef41Sopenharmony_ci - version: v15.9.0 20581cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/37081 20591cb0ef41Sopenharmony_ci description: Added support for `'jwk'` format. 20601cb0ef41Sopenharmony_ci--> 20611cb0ef41Sopenharmony_ci 20621cb0ef41Sopenharmony_ci* `options`: {Object} 20631cb0ef41Sopenharmony_ci* Returns: {string | Buffer | Object} 20641cb0ef41Sopenharmony_ci 20651cb0ef41Sopenharmony_ciFor symmetric keys, the following encoding options can be used: 20661cb0ef41Sopenharmony_ci 20671cb0ef41Sopenharmony_ci* `format`: {string} Must be `'buffer'` (default) or `'jwk'`. 20681cb0ef41Sopenharmony_ci 20691cb0ef41Sopenharmony_ciFor public keys, the following encoding options can be used: 20701cb0ef41Sopenharmony_ci 20711cb0ef41Sopenharmony_ci* `type`: {string} Must be one of `'pkcs1'` (RSA only) or `'spki'`. 20721cb0ef41Sopenharmony_ci* `format`: {string} Must be `'pem'`, `'der'`, or `'jwk'`. 20731cb0ef41Sopenharmony_ci 20741cb0ef41Sopenharmony_ciFor private keys, the following encoding options can be used: 20751cb0ef41Sopenharmony_ci 20761cb0ef41Sopenharmony_ci* `type`: {string} Must be one of `'pkcs1'` (RSA only), `'pkcs8'` or 20771cb0ef41Sopenharmony_ci `'sec1'` (EC only). 20781cb0ef41Sopenharmony_ci* `format`: {string} Must be `'pem'`, `'der'`, or `'jwk'`. 20791cb0ef41Sopenharmony_ci* `cipher`: {string} If specified, the private key will be encrypted with 20801cb0ef41Sopenharmony_ci the given `cipher` and `passphrase` using PKCS#5 v2.0 password based 20811cb0ef41Sopenharmony_ci encryption. 20821cb0ef41Sopenharmony_ci* `passphrase`: {string | Buffer} The passphrase to use for encryption, see 20831cb0ef41Sopenharmony_ci `cipher`. 20841cb0ef41Sopenharmony_ci 20851cb0ef41Sopenharmony_ciThe result type depends on the selected encoding format, when PEM the 20861cb0ef41Sopenharmony_ciresult is a string, when DER it will be a buffer containing the data 20871cb0ef41Sopenharmony_ciencoded as DER, when [JWK][] it will be an object. 20881cb0ef41Sopenharmony_ci 20891cb0ef41Sopenharmony_ciWhen [JWK][] encoding format was selected, all other encoding options are 20901cb0ef41Sopenharmony_ciignored. 20911cb0ef41Sopenharmony_ci 20921cb0ef41Sopenharmony_ciPKCS#1, SEC1, and PKCS#8 type keys can be encrypted by using a combination of 20931cb0ef41Sopenharmony_cithe `cipher` and `format` options. The PKCS#8 `type` can be used with any 20941cb0ef41Sopenharmony_ci`format` to encrypt any key algorithm (RSA, EC, or DH) by specifying a 20951cb0ef41Sopenharmony_ci`cipher`. PKCS#1 and SEC1 can only be encrypted by specifying a `cipher` 20961cb0ef41Sopenharmony_ciwhen the PEM `format` is used. For maximum compatibility, use PKCS#8 for 20971cb0ef41Sopenharmony_ciencrypted private keys. Since PKCS#8 defines its own 20981cb0ef41Sopenharmony_ciencryption mechanism, PEM-level encryption is not supported when encrypting 20991cb0ef41Sopenharmony_cia PKCS#8 key. See [RFC 5208][] for PKCS#8 encryption and [RFC 1421][] for 21001cb0ef41Sopenharmony_ciPKCS#1 and SEC1 encryption. 21011cb0ef41Sopenharmony_ci 21021cb0ef41Sopenharmony_ci### `keyObject.equals(otherKeyObject)` 21031cb0ef41Sopenharmony_ci 21041cb0ef41Sopenharmony_ci<!-- YAML 21051cb0ef41Sopenharmony_ciadded: v17.7.0 21061cb0ef41Sopenharmony_ci--> 21071cb0ef41Sopenharmony_ci 21081cb0ef41Sopenharmony_ci* `otherKeyObject`: {KeyObject} A `KeyObject` with which to 21091cb0ef41Sopenharmony_ci compare `keyObject`. 21101cb0ef41Sopenharmony_ci* Returns: {boolean} 21111cb0ef41Sopenharmony_ci 21121cb0ef41Sopenharmony_ciReturns `true` or `false` depending on whether the keys have exactly the same 21131cb0ef41Sopenharmony_citype, value, and parameters. This method is not 21141cb0ef41Sopenharmony_ci[constant time](https://en.wikipedia.org/wiki/Timing_attack). 21151cb0ef41Sopenharmony_ci 21161cb0ef41Sopenharmony_ci### `keyObject.symmetricKeySize` 21171cb0ef41Sopenharmony_ci 21181cb0ef41Sopenharmony_ci<!-- YAML 21191cb0ef41Sopenharmony_ciadded: v11.6.0 21201cb0ef41Sopenharmony_ci--> 21211cb0ef41Sopenharmony_ci 21221cb0ef41Sopenharmony_ci* {number} 21231cb0ef41Sopenharmony_ci 21241cb0ef41Sopenharmony_ciFor secret keys, this property represents the size of the key in bytes. This 21251cb0ef41Sopenharmony_ciproperty is `undefined` for asymmetric keys. 21261cb0ef41Sopenharmony_ci 21271cb0ef41Sopenharmony_ci### `keyObject.type` 21281cb0ef41Sopenharmony_ci 21291cb0ef41Sopenharmony_ci<!-- YAML 21301cb0ef41Sopenharmony_ciadded: v11.6.0 21311cb0ef41Sopenharmony_ci--> 21321cb0ef41Sopenharmony_ci 21331cb0ef41Sopenharmony_ci* {string} 21341cb0ef41Sopenharmony_ci 21351cb0ef41Sopenharmony_ciDepending on the type of this `KeyObject`, this property is either 21361cb0ef41Sopenharmony_ci`'secret'` for secret (symmetric) keys, `'public'` for public (asymmetric) keys 21371cb0ef41Sopenharmony_cior `'private'` for private (asymmetric) keys. 21381cb0ef41Sopenharmony_ci 21391cb0ef41Sopenharmony_ci## Class: `Sign` 21401cb0ef41Sopenharmony_ci 21411cb0ef41Sopenharmony_ci<!-- YAML 21421cb0ef41Sopenharmony_ciadded: v0.1.92 21431cb0ef41Sopenharmony_ci--> 21441cb0ef41Sopenharmony_ci 21451cb0ef41Sopenharmony_ci* Extends: {stream.Writable} 21461cb0ef41Sopenharmony_ci 21471cb0ef41Sopenharmony_ciThe `Sign` class is a utility for generating signatures. It can be used in one 21481cb0ef41Sopenharmony_ciof two ways: 21491cb0ef41Sopenharmony_ci 21501cb0ef41Sopenharmony_ci* As a writable [stream][], where data to be signed is written and the 21511cb0ef41Sopenharmony_ci [`sign.sign()`][] method is used to generate and return the signature, or 21521cb0ef41Sopenharmony_ci* Using the [`sign.update()`][] and [`sign.sign()`][] methods to produce the 21531cb0ef41Sopenharmony_ci signature. 21541cb0ef41Sopenharmony_ci 21551cb0ef41Sopenharmony_ciThe [`crypto.createSign()`][] method is used to create `Sign` instances. The 21561cb0ef41Sopenharmony_ciargument is the string name of the hash function to use. `Sign` objects are not 21571cb0ef41Sopenharmony_cito be created directly using the `new` keyword. 21581cb0ef41Sopenharmony_ci 21591cb0ef41Sopenharmony_ciExample: Using `Sign` and [`Verify`][] objects as streams: 21601cb0ef41Sopenharmony_ci 21611cb0ef41Sopenharmony_ci```mjs 21621cb0ef41Sopenharmony_ciconst { 21631cb0ef41Sopenharmony_ci generateKeyPairSync, 21641cb0ef41Sopenharmony_ci createSign, 21651cb0ef41Sopenharmony_ci createVerify, 21661cb0ef41Sopenharmony_ci} = await import('node:crypto'); 21671cb0ef41Sopenharmony_ci 21681cb0ef41Sopenharmony_ciconst { privateKey, publicKey } = generateKeyPairSync('ec', { 21691cb0ef41Sopenharmony_ci namedCurve: 'sect239k1', 21701cb0ef41Sopenharmony_ci}); 21711cb0ef41Sopenharmony_ci 21721cb0ef41Sopenharmony_ciconst sign = createSign('SHA256'); 21731cb0ef41Sopenharmony_cisign.write('some data to sign'); 21741cb0ef41Sopenharmony_cisign.end(); 21751cb0ef41Sopenharmony_ciconst signature = sign.sign(privateKey, 'hex'); 21761cb0ef41Sopenharmony_ci 21771cb0ef41Sopenharmony_ciconst verify = createVerify('SHA256'); 21781cb0ef41Sopenharmony_civerify.write('some data to sign'); 21791cb0ef41Sopenharmony_civerify.end(); 21801cb0ef41Sopenharmony_ciconsole.log(verify.verify(publicKey, signature, 'hex')); 21811cb0ef41Sopenharmony_ci// Prints: true 21821cb0ef41Sopenharmony_ci``` 21831cb0ef41Sopenharmony_ci 21841cb0ef41Sopenharmony_ci```cjs 21851cb0ef41Sopenharmony_ciconst { 21861cb0ef41Sopenharmony_ci generateKeyPairSync, 21871cb0ef41Sopenharmony_ci createSign, 21881cb0ef41Sopenharmony_ci createVerify, 21891cb0ef41Sopenharmony_ci} = require('node:crypto'); 21901cb0ef41Sopenharmony_ci 21911cb0ef41Sopenharmony_ciconst { privateKey, publicKey } = generateKeyPairSync('ec', { 21921cb0ef41Sopenharmony_ci namedCurve: 'sect239k1', 21931cb0ef41Sopenharmony_ci}); 21941cb0ef41Sopenharmony_ci 21951cb0ef41Sopenharmony_ciconst sign = createSign('SHA256'); 21961cb0ef41Sopenharmony_cisign.write('some data to sign'); 21971cb0ef41Sopenharmony_cisign.end(); 21981cb0ef41Sopenharmony_ciconst signature = sign.sign(privateKey, 'hex'); 21991cb0ef41Sopenharmony_ci 22001cb0ef41Sopenharmony_ciconst verify = createVerify('SHA256'); 22011cb0ef41Sopenharmony_civerify.write('some data to sign'); 22021cb0ef41Sopenharmony_civerify.end(); 22031cb0ef41Sopenharmony_ciconsole.log(verify.verify(publicKey, signature, 'hex')); 22041cb0ef41Sopenharmony_ci// Prints: true 22051cb0ef41Sopenharmony_ci``` 22061cb0ef41Sopenharmony_ci 22071cb0ef41Sopenharmony_ciExample: Using the [`sign.update()`][] and [`verify.update()`][] methods: 22081cb0ef41Sopenharmony_ci 22091cb0ef41Sopenharmony_ci```mjs 22101cb0ef41Sopenharmony_ciconst { 22111cb0ef41Sopenharmony_ci generateKeyPairSync, 22121cb0ef41Sopenharmony_ci createSign, 22131cb0ef41Sopenharmony_ci createVerify, 22141cb0ef41Sopenharmony_ci} = await import('node:crypto'); 22151cb0ef41Sopenharmony_ci 22161cb0ef41Sopenharmony_ciconst { privateKey, publicKey } = generateKeyPairSync('rsa', { 22171cb0ef41Sopenharmony_ci modulusLength: 2048, 22181cb0ef41Sopenharmony_ci}); 22191cb0ef41Sopenharmony_ci 22201cb0ef41Sopenharmony_ciconst sign = createSign('SHA256'); 22211cb0ef41Sopenharmony_cisign.update('some data to sign'); 22221cb0ef41Sopenharmony_cisign.end(); 22231cb0ef41Sopenharmony_ciconst signature = sign.sign(privateKey); 22241cb0ef41Sopenharmony_ci 22251cb0ef41Sopenharmony_ciconst verify = createVerify('SHA256'); 22261cb0ef41Sopenharmony_civerify.update('some data to sign'); 22271cb0ef41Sopenharmony_civerify.end(); 22281cb0ef41Sopenharmony_ciconsole.log(verify.verify(publicKey, signature)); 22291cb0ef41Sopenharmony_ci// Prints: true 22301cb0ef41Sopenharmony_ci``` 22311cb0ef41Sopenharmony_ci 22321cb0ef41Sopenharmony_ci```cjs 22331cb0ef41Sopenharmony_ciconst { 22341cb0ef41Sopenharmony_ci generateKeyPairSync, 22351cb0ef41Sopenharmony_ci createSign, 22361cb0ef41Sopenharmony_ci createVerify, 22371cb0ef41Sopenharmony_ci} = require('node:crypto'); 22381cb0ef41Sopenharmony_ci 22391cb0ef41Sopenharmony_ciconst { privateKey, publicKey } = generateKeyPairSync('rsa', { 22401cb0ef41Sopenharmony_ci modulusLength: 2048, 22411cb0ef41Sopenharmony_ci}); 22421cb0ef41Sopenharmony_ci 22431cb0ef41Sopenharmony_ciconst sign = createSign('SHA256'); 22441cb0ef41Sopenharmony_cisign.update('some data to sign'); 22451cb0ef41Sopenharmony_cisign.end(); 22461cb0ef41Sopenharmony_ciconst signature = sign.sign(privateKey); 22471cb0ef41Sopenharmony_ci 22481cb0ef41Sopenharmony_ciconst verify = createVerify('SHA256'); 22491cb0ef41Sopenharmony_civerify.update('some data to sign'); 22501cb0ef41Sopenharmony_civerify.end(); 22511cb0ef41Sopenharmony_ciconsole.log(verify.verify(publicKey, signature)); 22521cb0ef41Sopenharmony_ci// Prints: true 22531cb0ef41Sopenharmony_ci``` 22541cb0ef41Sopenharmony_ci 22551cb0ef41Sopenharmony_ci### `sign.sign(privateKey[, outputEncoding])` 22561cb0ef41Sopenharmony_ci 22571cb0ef41Sopenharmony_ci<!-- YAML 22581cb0ef41Sopenharmony_ciadded: v0.1.92 22591cb0ef41Sopenharmony_cichanges: 22601cb0ef41Sopenharmony_ci - version: v15.0.0 22611cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/35093 22621cb0ef41Sopenharmony_ci description: The privateKey can also be an ArrayBuffer and CryptoKey. 22631cb0ef41Sopenharmony_ci - version: 22641cb0ef41Sopenharmony_ci - v13.2.0 22651cb0ef41Sopenharmony_ci - v12.16.0 22661cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/29292 22671cb0ef41Sopenharmony_ci description: This function now supports IEEE-P1363 DSA and ECDSA signatures. 22681cb0ef41Sopenharmony_ci - version: v12.0.0 22691cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/26960 22701cb0ef41Sopenharmony_ci description: This function now supports RSA-PSS keys. 22711cb0ef41Sopenharmony_ci - version: v11.6.0 22721cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/24234 22731cb0ef41Sopenharmony_ci description: This function now supports key objects. 22741cb0ef41Sopenharmony_ci - version: v8.0.0 22751cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/11705 22761cb0ef41Sopenharmony_ci description: Support for RSASSA-PSS and additional options was added. 22771cb0ef41Sopenharmony_ci--> 22781cb0ef41Sopenharmony_ci 22791cb0ef41Sopenharmony_ci<!--lint disable maximum-line-length remark-lint--> 22801cb0ef41Sopenharmony_ci 22811cb0ef41Sopenharmony_ci* `privateKey` {Object|string|ArrayBuffer|Buffer|TypedArray|DataView|KeyObject|CryptoKey} 22821cb0ef41Sopenharmony_ci * `dsaEncoding` {string} 22831cb0ef41Sopenharmony_ci * `padding` {integer} 22841cb0ef41Sopenharmony_ci * `saltLength` {integer} 22851cb0ef41Sopenharmony_ci* `outputEncoding` {string} The [encoding][] of the return value. 22861cb0ef41Sopenharmony_ci* Returns: {Buffer | string} 22871cb0ef41Sopenharmony_ci 22881cb0ef41Sopenharmony_ci<!--lint enable maximum-line-length remark-lint--> 22891cb0ef41Sopenharmony_ci 22901cb0ef41Sopenharmony_ciCalculates the signature on all the data passed through using either 22911cb0ef41Sopenharmony_ci[`sign.update()`][] or [`sign.write()`][stream-writable-write]. 22921cb0ef41Sopenharmony_ci 22931cb0ef41Sopenharmony_ciIf `privateKey` is not a [`KeyObject`][], this function behaves as if 22941cb0ef41Sopenharmony_ci`privateKey` had been passed to [`crypto.createPrivateKey()`][]. If it is an 22951cb0ef41Sopenharmony_ciobject, the following additional properties can be passed: 22961cb0ef41Sopenharmony_ci 22971cb0ef41Sopenharmony_ci* `dsaEncoding` {string} For DSA and ECDSA, this option specifies the 22981cb0ef41Sopenharmony_ci format of the generated signature. It can be one of the following: 22991cb0ef41Sopenharmony_ci * `'der'` (default): DER-encoded ASN.1 signature structure encoding `(r, s)`. 23001cb0ef41Sopenharmony_ci * `'ieee-p1363'`: Signature format `r || s` as proposed in IEEE-P1363. 23011cb0ef41Sopenharmony_ci* `padding` {integer} Optional padding value for RSA, one of the following: 23021cb0ef41Sopenharmony_ci 23031cb0ef41Sopenharmony_ci * `crypto.constants.RSA_PKCS1_PADDING` (default) 23041cb0ef41Sopenharmony_ci * `crypto.constants.RSA_PKCS1_PSS_PADDING` 23051cb0ef41Sopenharmony_ci 23061cb0ef41Sopenharmony_ci `RSA_PKCS1_PSS_PADDING` will use MGF1 with the same hash function 23071cb0ef41Sopenharmony_ci used to sign the message as specified in section 3.1 of [RFC 4055][], unless 23081cb0ef41Sopenharmony_ci an MGF1 hash function has been specified as part of the key in compliance with 23091cb0ef41Sopenharmony_ci section 3.3 of [RFC 4055][]. 23101cb0ef41Sopenharmony_ci* `saltLength` {integer} Salt length for when padding is 23111cb0ef41Sopenharmony_ci `RSA_PKCS1_PSS_PADDING`. The special value 23121cb0ef41Sopenharmony_ci `crypto.constants.RSA_PSS_SALTLEN_DIGEST` sets the salt length to the digest 23131cb0ef41Sopenharmony_ci size, `crypto.constants.RSA_PSS_SALTLEN_MAX_SIGN` (default) sets it to the 23141cb0ef41Sopenharmony_ci maximum permissible value. 23151cb0ef41Sopenharmony_ci 23161cb0ef41Sopenharmony_ciIf `outputEncoding` is provided a string is returned; otherwise a [`Buffer`][] 23171cb0ef41Sopenharmony_ciis returned. 23181cb0ef41Sopenharmony_ci 23191cb0ef41Sopenharmony_ciThe `Sign` object can not be again used after `sign.sign()` method has been 23201cb0ef41Sopenharmony_cicalled. Multiple calls to `sign.sign()` will result in an error being thrown. 23211cb0ef41Sopenharmony_ci 23221cb0ef41Sopenharmony_ci### `sign.update(data[, inputEncoding])` 23231cb0ef41Sopenharmony_ci 23241cb0ef41Sopenharmony_ci<!-- YAML 23251cb0ef41Sopenharmony_ciadded: v0.1.92 23261cb0ef41Sopenharmony_cichanges: 23271cb0ef41Sopenharmony_ci - version: v6.0.0 23281cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/5522 23291cb0ef41Sopenharmony_ci description: The default `inputEncoding` changed from `binary` to `utf8`. 23301cb0ef41Sopenharmony_ci--> 23311cb0ef41Sopenharmony_ci 23321cb0ef41Sopenharmony_ci* `data` {string|Buffer|TypedArray|DataView} 23331cb0ef41Sopenharmony_ci* `inputEncoding` {string} The [encoding][] of the `data` string. 23341cb0ef41Sopenharmony_ci 23351cb0ef41Sopenharmony_ciUpdates the `Sign` content with the given `data`, the encoding of which 23361cb0ef41Sopenharmony_ciis given in `inputEncoding`. 23371cb0ef41Sopenharmony_ciIf `encoding` is not provided, and the `data` is a string, an 23381cb0ef41Sopenharmony_ciencoding of `'utf8'` is enforced. If `data` is a [`Buffer`][], `TypedArray`, or 23391cb0ef41Sopenharmony_ci`DataView`, then `inputEncoding` is ignored. 23401cb0ef41Sopenharmony_ci 23411cb0ef41Sopenharmony_ciThis can be called many times with new data as it is streamed. 23421cb0ef41Sopenharmony_ci 23431cb0ef41Sopenharmony_ci## Class: `Verify` 23441cb0ef41Sopenharmony_ci 23451cb0ef41Sopenharmony_ci<!-- YAML 23461cb0ef41Sopenharmony_ciadded: v0.1.92 23471cb0ef41Sopenharmony_ci--> 23481cb0ef41Sopenharmony_ci 23491cb0ef41Sopenharmony_ci* Extends: {stream.Writable} 23501cb0ef41Sopenharmony_ci 23511cb0ef41Sopenharmony_ciThe `Verify` class is a utility for verifying signatures. It can be used in one 23521cb0ef41Sopenharmony_ciof two ways: 23531cb0ef41Sopenharmony_ci 23541cb0ef41Sopenharmony_ci* As a writable [stream][] where written data is used to validate against the 23551cb0ef41Sopenharmony_ci supplied signature, or 23561cb0ef41Sopenharmony_ci* Using the [`verify.update()`][] and [`verify.verify()`][] methods to verify 23571cb0ef41Sopenharmony_ci the signature. 23581cb0ef41Sopenharmony_ci 23591cb0ef41Sopenharmony_ciThe [`crypto.createVerify()`][] method is used to create `Verify` instances. 23601cb0ef41Sopenharmony_ci`Verify` objects are not to be created directly using the `new` keyword. 23611cb0ef41Sopenharmony_ci 23621cb0ef41Sopenharmony_ciSee [`Sign`][] for examples. 23631cb0ef41Sopenharmony_ci 23641cb0ef41Sopenharmony_ci### `verify.update(data[, inputEncoding])` 23651cb0ef41Sopenharmony_ci 23661cb0ef41Sopenharmony_ci<!-- YAML 23671cb0ef41Sopenharmony_ciadded: v0.1.92 23681cb0ef41Sopenharmony_cichanges: 23691cb0ef41Sopenharmony_ci - version: v6.0.0 23701cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/5522 23711cb0ef41Sopenharmony_ci description: The default `inputEncoding` changed from `binary` to `utf8`. 23721cb0ef41Sopenharmony_ci--> 23731cb0ef41Sopenharmony_ci 23741cb0ef41Sopenharmony_ci* `data` {string|Buffer|TypedArray|DataView} 23751cb0ef41Sopenharmony_ci* `inputEncoding` {string} The [encoding][] of the `data` string. 23761cb0ef41Sopenharmony_ci 23771cb0ef41Sopenharmony_ciUpdates the `Verify` content with the given `data`, the encoding of which 23781cb0ef41Sopenharmony_ciis given in `inputEncoding`. 23791cb0ef41Sopenharmony_ciIf `inputEncoding` is not provided, and the `data` is a string, an 23801cb0ef41Sopenharmony_ciencoding of `'utf8'` is enforced. If `data` is a [`Buffer`][], `TypedArray`, or 23811cb0ef41Sopenharmony_ci`DataView`, then `inputEncoding` is ignored. 23821cb0ef41Sopenharmony_ci 23831cb0ef41Sopenharmony_ciThis can be called many times with new data as it is streamed. 23841cb0ef41Sopenharmony_ci 23851cb0ef41Sopenharmony_ci### `verify.verify(object, signature[, signatureEncoding])` 23861cb0ef41Sopenharmony_ci 23871cb0ef41Sopenharmony_ci<!-- YAML 23881cb0ef41Sopenharmony_ciadded: v0.1.92 23891cb0ef41Sopenharmony_cichanges: 23901cb0ef41Sopenharmony_ci - version: v15.0.0 23911cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/35093 23921cb0ef41Sopenharmony_ci description: The object can also be an ArrayBuffer and CryptoKey. 23931cb0ef41Sopenharmony_ci - version: 23941cb0ef41Sopenharmony_ci - v13.2.0 23951cb0ef41Sopenharmony_ci - v12.16.0 23961cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/29292 23971cb0ef41Sopenharmony_ci description: This function now supports IEEE-P1363 DSA and ECDSA signatures. 23981cb0ef41Sopenharmony_ci - version: v12.0.0 23991cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/26960 24001cb0ef41Sopenharmony_ci description: This function now supports RSA-PSS keys. 24011cb0ef41Sopenharmony_ci - version: v11.7.0 24021cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/25217 24031cb0ef41Sopenharmony_ci description: The key can now be a private key. 24041cb0ef41Sopenharmony_ci - version: v8.0.0 24051cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/11705 24061cb0ef41Sopenharmony_ci description: Support for RSASSA-PSS and additional options was added. 24071cb0ef41Sopenharmony_ci--> 24081cb0ef41Sopenharmony_ci 24091cb0ef41Sopenharmony_ci<!--lint disable maximum-line-length remark-lint--> 24101cb0ef41Sopenharmony_ci 24111cb0ef41Sopenharmony_ci* `object` {Object|string|ArrayBuffer|Buffer|TypedArray|DataView|KeyObject|CryptoKey} 24121cb0ef41Sopenharmony_ci * `dsaEncoding` {string} 24131cb0ef41Sopenharmony_ci * `padding` {integer} 24141cb0ef41Sopenharmony_ci * `saltLength` {integer} 24151cb0ef41Sopenharmony_ci* `signature` {string|ArrayBuffer|Buffer|TypedArray|DataView} 24161cb0ef41Sopenharmony_ci* `signatureEncoding` {string} The [encoding][] of the `signature` string. 24171cb0ef41Sopenharmony_ci* Returns: {boolean} `true` or `false` depending on the validity of the 24181cb0ef41Sopenharmony_ci signature for the data and public key. 24191cb0ef41Sopenharmony_ci 24201cb0ef41Sopenharmony_ci<!--lint enable maximum-line-length remark-lint--> 24211cb0ef41Sopenharmony_ci 24221cb0ef41Sopenharmony_ciVerifies the provided data using the given `object` and `signature`. 24231cb0ef41Sopenharmony_ci 24241cb0ef41Sopenharmony_ciIf `object` is not a [`KeyObject`][], this function behaves as if 24251cb0ef41Sopenharmony_ci`object` had been passed to [`crypto.createPublicKey()`][]. If it is an 24261cb0ef41Sopenharmony_ciobject, the following additional properties can be passed: 24271cb0ef41Sopenharmony_ci 24281cb0ef41Sopenharmony_ci* `dsaEncoding` {string} For DSA and ECDSA, this option specifies the 24291cb0ef41Sopenharmony_ci format of the signature. It can be one of the following: 24301cb0ef41Sopenharmony_ci * `'der'` (default): DER-encoded ASN.1 signature structure encoding `(r, s)`. 24311cb0ef41Sopenharmony_ci * `'ieee-p1363'`: Signature format `r || s` as proposed in IEEE-P1363. 24321cb0ef41Sopenharmony_ci* `padding` {integer} Optional padding value for RSA, one of the following: 24331cb0ef41Sopenharmony_ci 24341cb0ef41Sopenharmony_ci * `crypto.constants.RSA_PKCS1_PADDING` (default) 24351cb0ef41Sopenharmony_ci * `crypto.constants.RSA_PKCS1_PSS_PADDING` 24361cb0ef41Sopenharmony_ci 24371cb0ef41Sopenharmony_ci `RSA_PKCS1_PSS_PADDING` will use MGF1 with the same hash function 24381cb0ef41Sopenharmony_ci used to verify the message as specified in section 3.1 of [RFC 4055][], unless 24391cb0ef41Sopenharmony_ci an MGF1 hash function has been specified as part of the key in compliance with 24401cb0ef41Sopenharmony_ci section 3.3 of [RFC 4055][]. 24411cb0ef41Sopenharmony_ci* `saltLength` {integer} Salt length for when padding is 24421cb0ef41Sopenharmony_ci `RSA_PKCS1_PSS_PADDING`. The special value 24431cb0ef41Sopenharmony_ci `crypto.constants.RSA_PSS_SALTLEN_DIGEST` sets the salt length to the digest 24441cb0ef41Sopenharmony_ci size, `crypto.constants.RSA_PSS_SALTLEN_AUTO` (default) causes it to be 24451cb0ef41Sopenharmony_ci determined automatically. 24461cb0ef41Sopenharmony_ci 24471cb0ef41Sopenharmony_ciThe `signature` argument is the previously calculated signature for the data, in 24481cb0ef41Sopenharmony_cithe `signatureEncoding`. 24491cb0ef41Sopenharmony_ciIf a `signatureEncoding` is specified, the `signature` is expected to be a 24501cb0ef41Sopenharmony_cistring; otherwise `signature` is expected to be a [`Buffer`][], 24511cb0ef41Sopenharmony_ci`TypedArray`, or `DataView`. 24521cb0ef41Sopenharmony_ci 24531cb0ef41Sopenharmony_ciThe `verify` object can not be used again after `verify.verify()` has been 24541cb0ef41Sopenharmony_cicalled. Multiple calls to `verify.verify()` will result in an error being 24551cb0ef41Sopenharmony_cithrown. 24561cb0ef41Sopenharmony_ci 24571cb0ef41Sopenharmony_ciBecause public keys can be derived from private keys, a private key may 24581cb0ef41Sopenharmony_cibe passed instead of a public key. 24591cb0ef41Sopenharmony_ci 24601cb0ef41Sopenharmony_ci## Class: `X509Certificate` 24611cb0ef41Sopenharmony_ci 24621cb0ef41Sopenharmony_ci<!-- YAML 24631cb0ef41Sopenharmony_ciadded: v15.6.0 24641cb0ef41Sopenharmony_ci--> 24651cb0ef41Sopenharmony_ci 24661cb0ef41Sopenharmony_ciEncapsulates an X509 certificate and provides read-only access to 24671cb0ef41Sopenharmony_ciits information. 24681cb0ef41Sopenharmony_ci 24691cb0ef41Sopenharmony_ci```mjs 24701cb0ef41Sopenharmony_ciconst { X509Certificate } = await import('node:crypto'); 24711cb0ef41Sopenharmony_ci 24721cb0ef41Sopenharmony_ciconst x509 = new X509Certificate('{... pem encoded cert ...}'); 24731cb0ef41Sopenharmony_ci 24741cb0ef41Sopenharmony_ciconsole.log(x509.subject); 24751cb0ef41Sopenharmony_ci``` 24761cb0ef41Sopenharmony_ci 24771cb0ef41Sopenharmony_ci```cjs 24781cb0ef41Sopenharmony_ciconst { X509Certificate } = require('node:crypto'); 24791cb0ef41Sopenharmony_ci 24801cb0ef41Sopenharmony_ciconst x509 = new X509Certificate('{... pem encoded cert ...}'); 24811cb0ef41Sopenharmony_ci 24821cb0ef41Sopenharmony_ciconsole.log(x509.subject); 24831cb0ef41Sopenharmony_ci``` 24841cb0ef41Sopenharmony_ci 24851cb0ef41Sopenharmony_ci### `new X509Certificate(buffer)` 24861cb0ef41Sopenharmony_ci 24871cb0ef41Sopenharmony_ci<!-- YAML 24881cb0ef41Sopenharmony_ciadded: v15.6.0 24891cb0ef41Sopenharmony_ci--> 24901cb0ef41Sopenharmony_ci 24911cb0ef41Sopenharmony_ci* `buffer` {string|TypedArray|Buffer|DataView} A PEM or DER encoded 24921cb0ef41Sopenharmony_ci X509 Certificate. 24931cb0ef41Sopenharmony_ci 24941cb0ef41Sopenharmony_ci### `x509.ca` 24951cb0ef41Sopenharmony_ci 24961cb0ef41Sopenharmony_ci<!-- YAML 24971cb0ef41Sopenharmony_ciadded: v15.6.0 24981cb0ef41Sopenharmony_ci--> 24991cb0ef41Sopenharmony_ci 25001cb0ef41Sopenharmony_ci* Type: {boolean} Will be `true` if this is a Certificate Authority (CA) 25011cb0ef41Sopenharmony_ci certificate. 25021cb0ef41Sopenharmony_ci 25031cb0ef41Sopenharmony_ci### `x509.checkEmail(email[, options])` 25041cb0ef41Sopenharmony_ci 25051cb0ef41Sopenharmony_ci<!-- YAML 25061cb0ef41Sopenharmony_ciadded: v15.6.0 25071cb0ef41Sopenharmony_cichanges: 25081cb0ef41Sopenharmony_ci - version: v18.0.0 25091cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41600 25101cb0ef41Sopenharmony_ci description: The subject option now defaults to `'default'`. 25111cb0ef41Sopenharmony_ci - version: 25121cb0ef41Sopenharmony_ci - v17.5.0 25131cb0ef41Sopenharmony_ci - v16.14.1 25141cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41599 25151cb0ef41Sopenharmony_ci description: The `wildcards`, `partialWildcards`, `multiLabelWildcards`, and 25161cb0ef41Sopenharmony_ci `singleLabelSubdomains` options have been removed since they 25171cb0ef41Sopenharmony_ci had no effect. 25181cb0ef41Sopenharmony_ci - version: v17.5.0 25191cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41569 25201cb0ef41Sopenharmony_ci description: The subject option can now be set to `'default'`. 25211cb0ef41Sopenharmony_ci--> 25221cb0ef41Sopenharmony_ci 25231cb0ef41Sopenharmony_ci* `email` {string} 25241cb0ef41Sopenharmony_ci* `options` {Object} 25251cb0ef41Sopenharmony_ci * `subject` {string} `'default'`, `'always'`, or `'never'`. 25261cb0ef41Sopenharmony_ci **Default:** `'default'`. 25271cb0ef41Sopenharmony_ci* Returns: {string|undefined} Returns `email` if the certificate matches, 25281cb0ef41Sopenharmony_ci `undefined` if it does not. 25291cb0ef41Sopenharmony_ci 25301cb0ef41Sopenharmony_ciChecks whether the certificate matches the given email address. 25311cb0ef41Sopenharmony_ci 25321cb0ef41Sopenharmony_ciIf the `'subject'` option is undefined or set to `'default'`, the certificate 25331cb0ef41Sopenharmony_cisubject is only considered if the subject alternative name extension either does 25341cb0ef41Sopenharmony_cinot exist or does not contain any email addresses. 25351cb0ef41Sopenharmony_ci 25361cb0ef41Sopenharmony_ciIf the `'subject'` option is set to `'always'` and if the subject alternative 25371cb0ef41Sopenharmony_ciname extension either does not exist or does not contain a matching email 25381cb0ef41Sopenharmony_ciaddress, the certificate subject is considered. 25391cb0ef41Sopenharmony_ci 25401cb0ef41Sopenharmony_ciIf the `'subject'` option is set to `'never'`, the certificate subject is never 25411cb0ef41Sopenharmony_ciconsidered, even if the certificate contains no subject alternative names. 25421cb0ef41Sopenharmony_ci 25431cb0ef41Sopenharmony_ci### `x509.checkHost(name[, options])` 25441cb0ef41Sopenharmony_ci 25451cb0ef41Sopenharmony_ci<!-- YAML 25461cb0ef41Sopenharmony_ciadded: v15.6.0 25471cb0ef41Sopenharmony_cichanges: 25481cb0ef41Sopenharmony_ci - version: v18.0.0 25491cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41600 25501cb0ef41Sopenharmony_ci description: The subject option now defaults to `'default'`. 25511cb0ef41Sopenharmony_ci - version: v17.5.0 25521cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41569 25531cb0ef41Sopenharmony_ci description: The subject option can now be set to `'default'`. 25541cb0ef41Sopenharmony_ci--> 25551cb0ef41Sopenharmony_ci 25561cb0ef41Sopenharmony_ci* `name` {string} 25571cb0ef41Sopenharmony_ci* `options` {Object} 25581cb0ef41Sopenharmony_ci * `subject` {string} `'default'`, `'always'`, or `'never'`. 25591cb0ef41Sopenharmony_ci **Default:** `'default'`. 25601cb0ef41Sopenharmony_ci * `wildcards` {boolean} **Default:** `true`. 25611cb0ef41Sopenharmony_ci * `partialWildcards` {boolean} **Default:** `true`. 25621cb0ef41Sopenharmony_ci * `multiLabelWildcards` {boolean} **Default:** `false`. 25631cb0ef41Sopenharmony_ci * `singleLabelSubdomains` {boolean} **Default:** `false`. 25641cb0ef41Sopenharmony_ci* Returns: {string|undefined} Returns a subject name that matches `name`, 25651cb0ef41Sopenharmony_ci or `undefined` if no subject name matches `name`. 25661cb0ef41Sopenharmony_ci 25671cb0ef41Sopenharmony_ciChecks whether the certificate matches the given host name. 25681cb0ef41Sopenharmony_ci 25691cb0ef41Sopenharmony_ciIf the certificate matches the given host name, the matching subject name is 25701cb0ef41Sopenharmony_cireturned. The returned name might be an exact match (e.g., `foo.example.com`) 25711cb0ef41Sopenharmony_cior it might contain wildcards (e.g., `*.example.com`). Because host name 25721cb0ef41Sopenharmony_cicomparisons are case-insensitive, the returned subject name might also differ 25731cb0ef41Sopenharmony_cifrom the given `name` in capitalization. 25741cb0ef41Sopenharmony_ci 25751cb0ef41Sopenharmony_ciIf the `'subject'` option is undefined or set to `'default'`, the certificate 25761cb0ef41Sopenharmony_cisubject is only considered if the subject alternative name extension either does 25771cb0ef41Sopenharmony_cinot exist or does not contain any DNS names. This behavior is consistent with 25781cb0ef41Sopenharmony_ci[RFC 2818][] ("HTTP Over TLS"). 25791cb0ef41Sopenharmony_ci 25801cb0ef41Sopenharmony_ciIf the `'subject'` option is set to `'always'` and if the subject alternative 25811cb0ef41Sopenharmony_ciname extension either does not exist or does not contain a matching DNS name, 25821cb0ef41Sopenharmony_cithe certificate subject is considered. 25831cb0ef41Sopenharmony_ci 25841cb0ef41Sopenharmony_ciIf the `'subject'` option is set to `'never'`, the certificate subject is never 25851cb0ef41Sopenharmony_ciconsidered, even if the certificate contains no subject alternative names. 25861cb0ef41Sopenharmony_ci 25871cb0ef41Sopenharmony_ci### `x509.checkIP(ip)` 25881cb0ef41Sopenharmony_ci 25891cb0ef41Sopenharmony_ci<!-- YAML 25901cb0ef41Sopenharmony_ciadded: v15.6.0 25911cb0ef41Sopenharmony_cichanges: 25921cb0ef41Sopenharmony_ci - version: 25931cb0ef41Sopenharmony_ci - v17.5.0 25941cb0ef41Sopenharmony_ci - v16.14.1 25951cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41571 25961cb0ef41Sopenharmony_ci description: The `options` argument has been removed since it had no effect. 25971cb0ef41Sopenharmony_ci--> 25981cb0ef41Sopenharmony_ci 25991cb0ef41Sopenharmony_ci* `ip` {string} 26001cb0ef41Sopenharmony_ci* Returns: {string|undefined} Returns `ip` if the certificate matches, 26011cb0ef41Sopenharmony_ci `undefined` if it does not. 26021cb0ef41Sopenharmony_ci 26031cb0ef41Sopenharmony_ciChecks whether the certificate matches the given IP address (IPv4 or IPv6). 26041cb0ef41Sopenharmony_ci 26051cb0ef41Sopenharmony_ciOnly [RFC 5280][] `iPAddress` subject alternative names are considered, and they 26061cb0ef41Sopenharmony_cimust match the given `ip` address exactly. Other subject alternative names as 26071cb0ef41Sopenharmony_ciwell as the subject field of the certificate are ignored. 26081cb0ef41Sopenharmony_ci 26091cb0ef41Sopenharmony_ci### `x509.checkIssued(otherCert)` 26101cb0ef41Sopenharmony_ci 26111cb0ef41Sopenharmony_ci<!-- YAML 26121cb0ef41Sopenharmony_ciadded: v15.6.0 26131cb0ef41Sopenharmony_ci--> 26141cb0ef41Sopenharmony_ci 26151cb0ef41Sopenharmony_ci* `otherCert` {X509Certificate} 26161cb0ef41Sopenharmony_ci* Returns: {boolean} 26171cb0ef41Sopenharmony_ci 26181cb0ef41Sopenharmony_ciChecks whether this certificate was issued by the given `otherCert`. 26191cb0ef41Sopenharmony_ci 26201cb0ef41Sopenharmony_ci### `x509.checkPrivateKey(privateKey)` 26211cb0ef41Sopenharmony_ci 26221cb0ef41Sopenharmony_ci<!-- YAML 26231cb0ef41Sopenharmony_ciadded: v15.6.0 26241cb0ef41Sopenharmony_ci--> 26251cb0ef41Sopenharmony_ci 26261cb0ef41Sopenharmony_ci* `privateKey` {KeyObject} A private key. 26271cb0ef41Sopenharmony_ci* Returns: {boolean} 26281cb0ef41Sopenharmony_ci 26291cb0ef41Sopenharmony_ciChecks whether the public key for this certificate is consistent with 26301cb0ef41Sopenharmony_cithe given private key. 26311cb0ef41Sopenharmony_ci 26321cb0ef41Sopenharmony_ci### `x509.fingerprint` 26331cb0ef41Sopenharmony_ci 26341cb0ef41Sopenharmony_ci<!-- YAML 26351cb0ef41Sopenharmony_ciadded: v15.6.0 26361cb0ef41Sopenharmony_ci--> 26371cb0ef41Sopenharmony_ci 26381cb0ef41Sopenharmony_ci* Type: {string} 26391cb0ef41Sopenharmony_ci 26401cb0ef41Sopenharmony_ciThe SHA-1 fingerprint of this certificate. 26411cb0ef41Sopenharmony_ci 26421cb0ef41Sopenharmony_ciBecause SHA-1 is cryptographically broken and because the security of SHA-1 is 26431cb0ef41Sopenharmony_cisignificantly worse than that of algorithms that are commonly used to sign 26441cb0ef41Sopenharmony_cicertificates, consider using [`x509.fingerprint256`][] instead. 26451cb0ef41Sopenharmony_ci 26461cb0ef41Sopenharmony_ci### `x509.fingerprint256` 26471cb0ef41Sopenharmony_ci 26481cb0ef41Sopenharmony_ci<!-- YAML 26491cb0ef41Sopenharmony_ciadded: v15.6.0 26501cb0ef41Sopenharmony_ci--> 26511cb0ef41Sopenharmony_ci 26521cb0ef41Sopenharmony_ci* Type: {string} 26531cb0ef41Sopenharmony_ci 26541cb0ef41Sopenharmony_ciThe SHA-256 fingerprint of this certificate. 26551cb0ef41Sopenharmony_ci 26561cb0ef41Sopenharmony_ci### `x509.fingerprint512` 26571cb0ef41Sopenharmony_ci 26581cb0ef41Sopenharmony_ci<!-- YAML 26591cb0ef41Sopenharmony_ciadded: 26601cb0ef41Sopenharmony_ci - v17.2.0 26611cb0ef41Sopenharmony_ci - v16.14.0 26621cb0ef41Sopenharmony_ci--> 26631cb0ef41Sopenharmony_ci 26641cb0ef41Sopenharmony_ci* Type: {string} 26651cb0ef41Sopenharmony_ci 26661cb0ef41Sopenharmony_ciThe SHA-512 fingerprint of this certificate. 26671cb0ef41Sopenharmony_ci 26681cb0ef41Sopenharmony_ciBecause computing the SHA-256 fingerprint is usually faster and because it is 26691cb0ef41Sopenharmony_cionly half the size of the SHA-512 fingerprint, [`x509.fingerprint256`][] may be 26701cb0ef41Sopenharmony_cia better choice. While SHA-512 presumably provides a higher level of security in 26711cb0ef41Sopenharmony_cigeneral, the security of SHA-256 matches that of most algorithms that are 26721cb0ef41Sopenharmony_cicommonly used to sign certificates. 26731cb0ef41Sopenharmony_ci 26741cb0ef41Sopenharmony_ci### `x509.infoAccess` 26751cb0ef41Sopenharmony_ci 26761cb0ef41Sopenharmony_ci<!-- YAML 26771cb0ef41Sopenharmony_ciadded: v15.6.0 26781cb0ef41Sopenharmony_cichanges: 26791cb0ef41Sopenharmony_ci - version: 26801cb0ef41Sopenharmony_ci - v17.3.1 26811cb0ef41Sopenharmony_ci - v16.13.2 26821cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs-private/node-private/pull/300 26831cb0ef41Sopenharmony_ci description: Parts of this string may be encoded as JSON string literals 26841cb0ef41Sopenharmony_ci in response to CVE-2021-44532. 26851cb0ef41Sopenharmony_ci--> 26861cb0ef41Sopenharmony_ci 26871cb0ef41Sopenharmony_ci* Type: {string} 26881cb0ef41Sopenharmony_ci 26891cb0ef41Sopenharmony_ciA textual representation of the certificate's authority information access 26901cb0ef41Sopenharmony_ciextension. 26911cb0ef41Sopenharmony_ci 26921cb0ef41Sopenharmony_ciThis is a line feed separated list of access descriptions. Each line begins with 26931cb0ef41Sopenharmony_cithe access method and the kind of the access location, followed by a colon and 26941cb0ef41Sopenharmony_cithe value associated with the access location. 26951cb0ef41Sopenharmony_ci 26961cb0ef41Sopenharmony_ciAfter the prefix denoting the access method and the kind of the access location, 26971cb0ef41Sopenharmony_cithe remainder of each line might be enclosed in quotes to indicate that the 26981cb0ef41Sopenharmony_civalue is a JSON string literal. For backward compatibility, Node.js only uses 26991cb0ef41Sopenharmony_ciJSON string literals within this property when necessary to avoid ambiguity. 27001cb0ef41Sopenharmony_ciThird-party code should be prepared to handle both possible entry formats. 27011cb0ef41Sopenharmony_ci 27021cb0ef41Sopenharmony_ci### `x509.issuer` 27031cb0ef41Sopenharmony_ci 27041cb0ef41Sopenharmony_ci<!-- YAML 27051cb0ef41Sopenharmony_ciadded: v15.6.0 27061cb0ef41Sopenharmony_ci--> 27071cb0ef41Sopenharmony_ci 27081cb0ef41Sopenharmony_ci* Type: {string} 27091cb0ef41Sopenharmony_ci 27101cb0ef41Sopenharmony_ciThe issuer identification included in this certificate. 27111cb0ef41Sopenharmony_ci 27121cb0ef41Sopenharmony_ci### `x509.issuerCertificate` 27131cb0ef41Sopenharmony_ci 27141cb0ef41Sopenharmony_ci<!-- YAML 27151cb0ef41Sopenharmony_ciadded: v15.9.0 27161cb0ef41Sopenharmony_ci--> 27171cb0ef41Sopenharmony_ci 27181cb0ef41Sopenharmony_ci* Type: {X509Certificate} 27191cb0ef41Sopenharmony_ci 27201cb0ef41Sopenharmony_ciThe issuer certificate or `undefined` if the issuer certificate is not 27211cb0ef41Sopenharmony_ciavailable. 27221cb0ef41Sopenharmony_ci 27231cb0ef41Sopenharmony_ci### `x509.keyUsage` 27241cb0ef41Sopenharmony_ci 27251cb0ef41Sopenharmony_ci<!-- YAML 27261cb0ef41Sopenharmony_ciadded: v15.6.0 27271cb0ef41Sopenharmony_ci--> 27281cb0ef41Sopenharmony_ci 27291cb0ef41Sopenharmony_ci* Type: {string\[]} 27301cb0ef41Sopenharmony_ci 27311cb0ef41Sopenharmony_ciAn array detailing the key usages for this certificate. 27321cb0ef41Sopenharmony_ci 27331cb0ef41Sopenharmony_ci### `x509.publicKey` 27341cb0ef41Sopenharmony_ci 27351cb0ef41Sopenharmony_ci<!-- YAML 27361cb0ef41Sopenharmony_ciadded: v15.6.0 27371cb0ef41Sopenharmony_ci--> 27381cb0ef41Sopenharmony_ci 27391cb0ef41Sopenharmony_ci* Type: {KeyObject} 27401cb0ef41Sopenharmony_ci 27411cb0ef41Sopenharmony_ciThe public key {KeyObject} for this certificate. 27421cb0ef41Sopenharmony_ci 27431cb0ef41Sopenharmony_ci### `x509.raw` 27441cb0ef41Sopenharmony_ci 27451cb0ef41Sopenharmony_ci<!-- YAML 27461cb0ef41Sopenharmony_ciadded: v15.6.0 27471cb0ef41Sopenharmony_ci--> 27481cb0ef41Sopenharmony_ci 27491cb0ef41Sopenharmony_ci* Type: {Buffer} 27501cb0ef41Sopenharmony_ci 27511cb0ef41Sopenharmony_ciA `Buffer` containing the DER encoding of this certificate. 27521cb0ef41Sopenharmony_ci 27531cb0ef41Sopenharmony_ci### `x509.serialNumber` 27541cb0ef41Sopenharmony_ci 27551cb0ef41Sopenharmony_ci<!-- YAML 27561cb0ef41Sopenharmony_ciadded: v15.6.0 27571cb0ef41Sopenharmony_ci--> 27581cb0ef41Sopenharmony_ci 27591cb0ef41Sopenharmony_ci* Type: {string} 27601cb0ef41Sopenharmony_ci 27611cb0ef41Sopenharmony_ciThe serial number of this certificate. 27621cb0ef41Sopenharmony_ci 27631cb0ef41Sopenharmony_ciSerial numbers are assigned by certificate authorities and do not uniquely 27641cb0ef41Sopenharmony_ciidentify certificates. Consider using [`x509.fingerprint256`][] as a unique 27651cb0ef41Sopenharmony_ciidentifier instead. 27661cb0ef41Sopenharmony_ci 27671cb0ef41Sopenharmony_ci### `x509.subject` 27681cb0ef41Sopenharmony_ci 27691cb0ef41Sopenharmony_ci<!-- YAML 27701cb0ef41Sopenharmony_ciadded: v15.6.0 27711cb0ef41Sopenharmony_ci--> 27721cb0ef41Sopenharmony_ci 27731cb0ef41Sopenharmony_ci* Type: {string} 27741cb0ef41Sopenharmony_ci 27751cb0ef41Sopenharmony_ciThe complete subject of this certificate. 27761cb0ef41Sopenharmony_ci 27771cb0ef41Sopenharmony_ci### `x509.subjectAltName` 27781cb0ef41Sopenharmony_ci 27791cb0ef41Sopenharmony_ci<!-- YAML 27801cb0ef41Sopenharmony_ciadded: v15.6.0 27811cb0ef41Sopenharmony_cichanges: 27821cb0ef41Sopenharmony_ci - version: 27831cb0ef41Sopenharmony_ci - v17.3.1 27841cb0ef41Sopenharmony_ci - v16.13.2 27851cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs-private/node-private/pull/300 27861cb0ef41Sopenharmony_ci description: Parts of this string may be encoded as JSON string literals 27871cb0ef41Sopenharmony_ci in response to CVE-2021-44532. 27881cb0ef41Sopenharmony_ci--> 27891cb0ef41Sopenharmony_ci 27901cb0ef41Sopenharmony_ci* Type: {string} 27911cb0ef41Sopenharmony_ci 27921cb0ef41Sopenharmony_ciThe subject alternative name specified for this certificate. 27931cb0ef41Sopenharmony_ci 27941cb0ef41Sopenharmony_ciThis is a comma-separated list of subject alternative names. Each entry begins 27951cb0ef41Sopenharmony_ciwith a string identifying the kind of the subject alternative name followed by 27961cb0ef41Sopenharmony_cia colon and the value associated with the entry. 27971cb0ef41Sopenharmony_ci 27981cb0ef41Sopenharmony_ciEarlier versions of Node.js incorrectly assumed that it is safe to split this 27991cb0ef41Sopenharmony_ciproperty at the two-character sequence `', '` (see [CVE-2021-44532][]). However, 28001cb0ef41Sopenharmony_ciboth malicious and legitimate certificates can contain subject alternative names 28011cb0ef41Sopenharmony_cithat include this sequence when represented as a string. 28021cb0ef41Sopenharmony_ci 28031cb0ef41Sopenharmony_ciAfter the prefix denoting the type of the entry, the remainder of each entry 28041cb0ef41Sopenharmony_cimight be enclosed in quotes to indicate that the value is a JSON string literal. 28051cb0ef41Sopenharmony_ciFor backward compatibility, Node.js only uses JSON string literals within this 28061cb0ef41Sopenharmony_ciproperty when necessary to avoid ambiguity. Third-party code should be prepared 28071cb0ef41Sopenharmony_cito handle both possible entry formats. 28081cb0ef41Sopenharmony_ci 28091cb0ef41Sopenharmony_ci### `x509.toJSON()` 28101cb0ef41Sopenharmony_ci 28111cb0ef41Sopenharmony_ci<!-- YAML 28121cb0ef41Sopenharmony_ciadded: v15.6.0 28131cb0ef41Sopenharmony_ci--> 28141cb0ef41Sopenharmony_ci 28151cb0ef41Sopenharmony_ci* Type: {string} 28161cb0ef41Sopenharmony_ci 28171cb0ef41Sopenharmony_ciThere is no standard JSON encoding for X509 certificates. The 28181cb0ef41Sopenharmony_ci`toJSON()` method returns a string containing the PEM encoded 28191cb0ef41Sopenharmony_cicertificate. 28201cb0ef41Sopenharmony_ci 28211cb0ef41Sopenharmony_ci### `x509.toLegacyObject()` 28221cb0ef41Sopenharmony_ci 28231cb0ef41Sopenharmony_ci<!-- YAML 28241cb0ef41Sopenharmony_ciadded: v15.6.0 28251cb0ef41Sopenharmony_ci--> 28261cb0ef41Sopenharmony_ci 28271cb0ef41Sopenharmony_ci* Type: {Object} 28281cb0ef41Sopenharmony_ci 28291cb0ef41Sopenharmony_ciReturns information about this certificate using the legacy 28301cb0ef41Sopenharmony_ci[certificate object][] encoding. 28311cb0ef41Sopenharmony_ci 28321cb0ef41Sopenharmony_ci### `x509.toString()` 28331cb0ef41Sopenharmony_ci 28341cb0ef41Sopenharmony_ci<!-- YAML 28351cb0ef41Sopenharmony_ciadded: v15.6.0 28361cb0ef41Sopenharmony_ci--> 28371cb0ef41Sopenharmony_ci 28381cb0ef41Sopenharmony_ci* Type: {string} 28391cb0ef41Sopenharmony_ci 28401cb0ef41Sopenharmony_ciReturns the PEM-encoded certificate. 28411cb0ef41Sopenharmony_ci 28421cb0ef41Sopenharmony_ci### `x509.validFrom` 28431cb0ef41Sopenharmony_ci 28441cb0ef41Sopenharmony_ci<!-- YAML 28451cb0ef41Sopenharmony_ciadded: v15.6.0 28461cb0ef41Sopenharmony_ci--> 28471cb0ef41Sopenharmony_ci 28481cb0ef41Sopenharmony_ci* Type: {string} 28491cb0ef41Sopenharmony_ci 28501cb0ef41Sopenharmony_ciThe date/time from which this certificate is considered valid. 28511cb0ef41Sopenharmony_ci 28521cb0ef41Sopenharmony_ci### `x509.validTo` 28531cb0ef41Sopenharmony_ci 28541cb0ef41Sopenharmony_ci<!-- YAML 28551cb0ef41Sopenharmony_ciadded: v15.6.0 28561cb0ef41Sopenharmony_ci--> 28571cb0ef41Sopenharmony_ci 28581cb0ef41Sopenharmony_ci* Type: {string} 28591cb0ef41Sopenharmony_ci 28601cb0ef41Sopenharmony_ciThe date/time until which this certificate is considered valid. 28611cb0ef41Sopenharmony_ci 28621cb0ef41Sopenharmony_ci### `x509.verify(publicKey)` 28631cb0ef41Sopenharmony_ci 28641cb0ef41Sopenharmony_ci<!-- YAML 28651cb0ef41Sopenharmony_ciadded: v15.6.0 28661cb0ef41Sopenharmony_ci--> 28671cb0ef41Sopenharmony_ci 28681cb0ef41Sopenharmony_ci* `publicKey` {KeyObject} A public key. 28691cb0ef41Sopenharmony_ci* Returns: {boolean} 28701cb0ef41Sopenharmony_ci 28711cb0ef41Sopenharmony_ciVerifies that this certificate was signed by the given public key. 28721cb0ef41Sopenharmony_ciDoes not perform any other validation checks on the certificate. 28731cb0ef41Sopenharmony_ci 28741cb0ef41Sopenharmony_ci## `node:crypto` module methods and properties 28751cb0ef41Sopenharmony_ci 28761cb0ef41Sopenharmony_ci### `crypto.constants` 28771cb0ef41Sopenharmony_ci 28781cb0ef41Sopenharmony_ci<!-- YAML 28791cb0ef41Sopenharmony_ciadded: v6.3.0 28801cb0ef41Sopenharmony_ci--> 28811cb0ef41Sopenharmony_ci 28821cb0ef41Sopenharmony_ci* {Object} 28831cb0ef41Sopenharmony_ci 28841cb0ef41Sopenharmony_ciAn object containing commonly used constants for crypto and security related 28851cb0ef41Sopenharmony_cioperations. The specific constants currently defined are described in 28861cb0ef41Sopenharmony_ci[Crypto constants][]. 28871cb0ef41Sopenharmony_ci 28881cb0ef41Sopenharmony_ci### `crypto.DEFAULT_ENCODING` 28891cb0ef41Sopenharmony_ci 28901cb0ef41Sopenharmony_ci<!-- YAML 28911cb0ef41Sopenharmony_ciadded: v0.9.3 28921cb0ef41Sopenharmony_cideprecated: v10.0.0 28931cb0ef41Sopenharmony_ci--> 28941cb0ef41Sopenharmony_ci 28951cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated 28961cb0ef41Sopenharmony_ci 28971cb0ef41Sopenharmony_ciThe default encoding to use for functions that can take either strings 28981cb0ef41Sopenharmony_cior [buffers][`Buffer`]. The default value is `'buffer'`, which makes methods 28991cb0ef41Sopenharmony_cidefault to [`Buffer`][] objects. 29001cb0ef41Sopenharmony_ci 29011cb0ef41Sopenharmony_ciThe `crypto.DEFAULT_ENCODING` mechanism is provided for backward compatibility 29021cb0ef41Sopenharmony_ciwith legacy programs that expect `'latin1'` to be the default encoding. 29031cb0ef41Sopenharmony_ci 29041cb0ef41Sopenharmony_ciNew applications should expect the default to be `'buffer'`. 29051cb0ef41Sopenharmony_ci 29061cb0ef41Sopenharmony_ciThis property is deprecated. 29071cb0ef41Sopenharmony_ci 29081cb0ef41Sopenharmony_ci### `crypto.fips` 29091cb0ef41Sopenharmony_ci 29101cb0ef41Sopenharmony_ci<!-- YAML 29111cb0ef41Sopenharmony_ciadded: v6.0.0 29121cb0ef41Sopenharmony_cideprecated: v10.0.0 29131cb0ef41Sopenharmony_ci--> 29141cb0ef41Sopenharmony_ci 29151cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated 29161cb0ef41Sopenharmony_ci 29171cb0ef41Sopenharmony_ciProperty for checking and controlling whether a FIPS compliant crypto provider 29181cb0ef41Sopenharmony_ciis currently in use. Setting to true requires a FIPS build of Node.js. 29191cb0ef41Sopenharmony_ci 29201cb0ef41Sopenharmony_ciThis property is deprecated. Please use `crypto.setFips()` and 29211cb0ef41Sopenharmony_ci`crypto.getFips()` instead. 29221cb0ef41Sopenharmony_ci 29231cb0ef41Sopenharmony_ci### `crypto.checkPrime(candidate[, options], callback)` 29241cb0ef41Sopenharmony_ci 29251cb0ef41Sopenharmony_ci<!-- YAML 29261cb0ef41Sopenharmony_ciadded: v15.8.0 29271cb0ef41Sopenharmony_cichanges: 29281cb0ef41Sopenharmony_ci - version: v18.0.0 29291cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41678 29301cb0ef41Sopenharmony_ci description: Passing an invalid callback to the `callback` argument 29311cb0ef41Sopenharmony_ci now throws `ERR_INVALID_ARG_TYPE` instead of 29321cb0ef41Sopenharmony_ci `ERR_INVALID_CALLBACK`. 29331cb0ef41Sopenharmony_ci--> 29341cb0ef41Sopenharmony_ci 29351cb0ef41Sopenharmony_ci* `candidate` {ArrayBuffer|SharedArrayBuffer|TypedArray|Buffer|DataView|bigint} 29361cb0ef41Sopenharmony_ci A possible prime encoded as a sequence of big endian octets of arbitrary 29371cb0ef41Sopenharmony_ci length. 29381cb0ef41Sopenharmony_ci* `options` {Object} 29391cb0ef41Sopenharmony_ci * `checks` {number} The number of Miller-Rabin probabilistic primality 29401cb0ef41Sopenharmony_ci iterations to perform. When the value is `0` (zero), a number of checks 29411cb0ef41Sopenharmony_ci is used that yields a false positive rate of at most 2<sup>-64</sup> for 29421cb0ef41Sopenharmony_ci random input. Care must be used when selecting a number of checks. Refer 29431cb0ef41Sopenharmony_ci to the OpenSSL documentation for the [`BN_is_prime_ex`][] function `nchecks` 29441cb0ef41Sopenharmony_ci options for more details. **Default:** `0` 29451cb0ef41Sopenharmony_ci* `callback` {Function} 29461cb0ef41Sopenharmony_ci * `err` {Error} Set to an {Error} object if an error occurred during check. 29471cb0ef41Sopenharmony_ci * `result` {boolean} `true` if the candidate is a prime with an error 29481cb0ef41Sopenharmony_ci probability less than `0.25 ** options.checks`. 29491cb0ef41Sopenharmony_ci 29501cb0ef41Sopenharmony_ciChecks the primality of the `candidate`. 29511cb0ef41Sopenharmony_ci 29521cb0ef41Sopenharmony_ci### `crypto.checkPrimeSync(candidate[, options])` 29531cb0ef41Sopenharmony_ci 29541cb0ef41Sopenharmony_ci<!-- YAML 29551cb0ef41Sopenharmony_ciadded: v15.8.0 29561cb0ef41Sopenharmony_ci--> 29571cb0ef41Sopenharmony_ci 29581cb0ef41Sopenharmony_ci* `candidate` {ArrayBuffer|SharedArrayBuffer|TypedArray|Buffer|DataView|bigint} 29591cb0ef41Sopenharmony_ci A possible prime encoded as a sequence of big endian octets of arbitrary 29601cb0ef41Sopenharmony_ci length. 29611cb0ef41Sopenharmony_ci* `options` {Object} 29621cb0ef41Sopenharmony_ci * `checks` {number} The number of Miller-Rabin probabilistic primality 29631cb0ef41Sopenharmony_ci iterations to perform. When the value is `0` (zero), a number of checks 29641cb0ef41Sopenharmony_ci is used that yields a false positive rate of at most 2<sup>-64</sup> for 29651cb0ef41Sopenharmony_ci random input. Care must be used when selecting a number of checks. Refer 29661cb0ef41Sopenharmony_ci to the OpenSSL documentation for the [`BN_is_prime_ex`][] function `nchecks` 29671cb0ef41Sopenharmony_ci options for more details. **Default:** `0` 29681cb0ef41Sopenharmony_ci* Returns: {boolean} `true` if the candidate is a prime with an error 29691cb0ef41Sopenharmony_ci probability less than `0.25 ** options.checks`. 29701cb0ef41Sopenharmony_ci 29711cb0ef41Sopenharmony_ciChecks the primality of the `candidate`. 29721cb0ef41Sopenharmony_ci 29731cb0ef41Sopenharmony_ci### `crypto.createCipher(algorithm, password[, options])` 29741cb0ef41Sopenharmony_ci 29751cb0ef41Sopenharmony_ci<!-- YAML 29761cb0ef41Sopenharmony_ciadded: v0.1.94 29771cb0ef41Sopenharmony_cideprecated: v10.0.0 29781cb0ef41Sopenharmony_cichanges: 29791cb0ef41Sopenharmony_ci - version: v17.9.0 29801cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/42427 29811cb0ef41Sopenharmony_ci description: The `authTagLength` option is now optional when using the 29821cb0ef41Sopenharmony_ci `chacha20-poly1305` cipher and defaults to 16 bytes. 29831cb0ef41Sopenharmony_ci - version: v15.0.0 29841cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/35093 29851cb0ef41Sopenharmony_ci description: The password argument can be an ArrayBuffer and is limited to 29861cb0ef41Sopenharmony_ci a maximum of 2 ** 31 - 1 bytes. 29871cb0ef41Sopenharmony_ci - version: v10.10.0 29881cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/21447 29891cb0ef41Sopenharmony_ci description: Ciphers in OCB mode are now supported. 29901cb0ef41Sopenharmony_ci - version: v10.2.0 29911cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/20235 29921cb0ef41Sopenharmony_ci description: The `authTagLength` option can now be used to produce shorter 29931cb0ef41Sopenharmony_ci authentication tags in GCM mode and defaults to 16 bytes. 29941cb0ef41Sopenharmony_ci--> 29951cb0ef41Sopenharmony_ci 29961cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use [`crypto.createCipheriv()`][] instead. 29971cb0ef41Sopenharmony_ci 29981cb0ef41Sopenharmony_ci* `algorithm` {string} 29991cb0ef41Sopenharmony_ci* `password` {string|ArrayBuffer|Buffer|TypedArray|DataView} 30001cb0ef41Sopenharmony_ci* `options` {Object} [`stream.transform` options][] 30011cb0ef41Sopenharmony_ci* Returns: {Cipher} 30021cb0ef41Sopenharmony_ci 30031cb0ef41Sopenharmony_ciCreates and returns a `Cipher` object that uses the given `algorithm` and 30041cb0ef41Sopenharmony_ci`password`. 30051cb0ef41Sopenharmony_ci 30061cb0ef41Sopenharmony_ciThe `options` argument controls stream behavior and is optional except when a 30071cb0ef41Sopenharmony_cicipher in CCM or OCB mode (e.g. `'aes-128-ccm'`) is used. In that case, the 30081cb0ef41Sopenharmony_ci`authTagLength` option is required and specifies the length of the 30091cb0ef41Sopenharmony_ciauthentication tag in bytes, see [CCM mode][]. In GCM mode, the `authTagLength` 30101cb0ef41Sopenharmony_cioption is not required but can be used to set the length of the authentication 30111cb0ef41Sopenharmony_citag that will be returned by `getAuthTag()` and defaults to 16 bytes. 30121cb0ef41Sopenharmony_ciFor `chacha20-poly1305`, the `authTagLength` option defaults to 16 bytes. 30131cb0ef41Sopenharmony_ci 30141cb0ef41Sopenharmony_ciThe `algorithm` is dependent on OpenSSL, examples are `'aes192'`, etc. On 30151cb0ef41Sopenharmony_cirecent OpenSSL releases, `openssl list -cipher-algorithms` will 30161cb0ef41Sopenharmony_cidisplay the available cipher algorithms. 30171cb0ef41Sopenharmony_ci 30181cb0ef41Sopenharmony_ciThe `password` is used to derive the cipher key and initialization vector (IV). 30191cb0ef41Sopenharmony_ciThe value must be either a `'latin1'` encoded string, a [`Buffer`][], a 30201cb0ef41Sopenharmony_ci`TypedArray`, or a `DataView`. 30211cb0ef41Sopenharmony_ci 30221cb0ef41Sopenharmony_ci<strong class="critical">This function is semantically insecure for all 30231cb0ef41Sopenharmony_cisupported ciphers and fatally flawed for ciphers in counter mode (such as CTR, 30241cb0ef41Sopenharmony_ciGCM, or CCM).</strong> 30251cb0ef41Sopenharmony_ci 30261cb0ef41Sopenharmony_ciThe implementation of `crypto.createCipher()` derives keys using the OpenSSL 30271cb0ef41Sopenharmony_cifunction [`EVP_BytesToKey`][] with the digest algorithm set to MD5, one 30281cb0ef41Sopenharmony_ciiteration, and no salt. The lack of salt allows dictionary attacks as the same 30291cb0ef41Sopenharmony_cipassword always creates the same key. The low iteration count and 30301cb0ef41Sopenharmony_cinon-cryptographically secure hash algorithm allow passwords to be tested very 30311cb0ef41Sopenharmony_cirapidly. 30321cb0ef41Sopenharmony_ci 30331cb0ef41Sopenharmony_ciIn line with OpenSSL's recommendation to use a more modern algorithm instead of 30341cb0ef41Sopenharmony_ci[`EVP_BytesToKey`][] it is recommended that developers derive a key and IV on 30351cb0ef41Sopenharmony_citheir own using [`crypto.scrypt()`][] and to use [`crypto.createCipheriv()`][] 30361cb0ef41Sopenharmony_cito create the `Cipher` object. Users should not use ciphers with counter mode 30371cb0ef41Sopenharmony_ci(e.g. CTR, GCM, or CCM) in `crypto.createCipher()`. A warning is emitted when 30381cb0ef41Sopenharmony_cithey are used in order to avoid the risk of IV reuse that causes 30391cb0ef41Sopenharmony_civulnerabilities. For the case when IV is reused in GCM, see [Nonce-Disrespecting 30401cb0ef41Sopenharmony_ciAdversaries][] for details. 30411cb0ef41Sopenharmony_ci 30421cb0ef41Sopenharmony_ci### `crypto.createCipheriv(algorithm, key, iv[, options])` 30431cb0ef41Sopenharmony_ci 30441cb0ef41Sopenharmony_ci<!-- YAML 30451cb0ef41Sopenharmony_ciadded: v0.1.94 30461cb0ef41Sopenharmony_cichanges: 30471cb0ef41Sopenharmony_ci - version: v17.9.0 30481cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/42427 30491cb0ef41Sopenharmony_ci description: The `authTagLength` option is now optional when using the 30501cb0ef41Sopenharmony_ci `chacha20-poly1305` cipher and defaults to 16 bytes. 30511cb0ef41Sopenharmony_ci - version: v15.0.0 30521cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/35093 30531cb0ef41Sopenharmony_ci description: The password and iv arguments can be an ArrayBuffer and are 30541cb0ef41Sopenharmony_ci each limited to a maximum of 2 ** 31 - 1 bytes. 30551cb0ef41Sopenharmony_ci - version: v11.6.0 30561cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/24234 30571cb0ef41Sopenharmony_ci description: The `key` argument can now be a `KeyObject`. 30581cb0ef41Sopenharmony_ci - version: 30591cb0ef41Sopenharmony_ci - v11.2.0 30601cb0ef41Sopenharmony_ci - v10.17.0 30611cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/24081 30621cb0ef41Sopenharmony_ci description: The cipher `chacha20-poly1305` (the IETF variant of 30631cb0ef41Sopenharmony_ci ChaCha20-Poly1305) is now supported. 30641cb0ef41Sopenharmony_ci - version: v10.10.0 30651cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/21447 30661cb0ef41Sopenharmony_ci description: Ciphers in OCB mode are now supported. 30671cb0ef41Sopenharmony_ci - version: v10.2.0 30681cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/20235 30691cb0ef41Sopenharmony_ci description: The `authTagLength` option can now be used to produce shorter 30701cb0ef41Sopenharmony_ci authentication tags in GCM mode and defaults to 16 bytes. 30711cb0ef41Sopenharmony_ci - version: v9.9.0 30721cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/18644 30731cb0ef41Sopenharmony_ci description: The `iv` parameter may now be `null` for ciphers which do not 30741cb0ef41Sopenharmony_ci need an initialization vector. 30751cb0ef41Sopenharmony_ci--> 30761cb0ef41Sopenharmony_ci 30771cb0ef41Sopenharmony_ci* `algorithm` {string} 30781cb0ef41Sopenharmony_ci* `key` {string|ArrayBuffer|Buffer|TypedArray|DataView|KeyObject|CryptoKey} 30791cb0ef41Sopenharmony_ci* `iv` {string|ArrayBuffer|Buffer|TypedArray|DataView|null} 30801cb0ef41Sopenharmony_ci* `options` {Object} [`stream.transform` options][] 30811cb0ef41Sopenharmony_ci* Returns: {Cipher} 30821cb0ef41Sopenharmony_ci 30831cb0ef41Sopenharmony_ciCreates and returns a `Cipher` object, with the given `algorithm`, `key` and 30841cb0ef41Sopenharmony_ciinitialization vector (`iv`). 30851cb0ef41Sopenharmony_ci 30861cb0ef41Sopenharmony_ciThe `options` argument controls stream behavior and is optional except when a 30871cb0ef41Sopenharmony_cicipher in CCM or OCB mode (e.g. `'aes-128-ccm'`) is used. In that case, the 30881cb0ef41Sopenharmony_ci`authTagLength` option is required and specifies the length of the 30891cb0ef41Sopenharmony_ciauthentication tag in bytes, see [CCM mode][]. In GCM mode, the `authTagLength` 30901cb0ef41Sopenharmony_cioption is not required but can be used to set the length of the authentication 30911cb0ef41Sopenharmony_citag that will be returned by `getAuthTag()` and defaults to 16 bytes. 30921cb0ef41Sopenharmony_ciFor `chacha20-poly1305`, the `authTagLength` option defaults to 16 bytes. 30931cb0ef41Sopenharmony_ci 30941cb0ef41Sopenharmony_ciThe `algorithm` is dependent on OpenSSL, examples are `'aes192'`, etc. On 30951cb0ef41Sopenharmony_cirecent OpenSSL releases, `openssl list -cipher-algorithms` will 30961cb0ef41Sopenharmony_cidisplay the available cipher algorithms. 30971cb0ef41Sopenharmony_ci 30981cb0ef41Sopenharmony_ciThe `key` is the raw key used by the `algorithm` and `iv` is an 30991cb0ef41Sopenharmony_ci[initialization vector][]. Both arguments must be `'utf8'` encoded strings, 31001cb0ef41Sopenharmony_ci[Buffers][`Buffer`], `TypedArray`, or `DataView`s. The `key` may optionally be 31011cb0ef41Sopenharmony_cia [`KeyObject`][] of type `secret`. If the cipher does not need 31021cb0ef41Sopenharmony_cian initialization vector, `iv` may be `null`. 31031cb0ef41Sopenharmony_ci 31041cb0ef41Sopenharmony_ciWhen passing strings for `key` or `iv`, please consider 31051cb0ef41Sopenharmony_ci[caveats when using strings as inputs to cryptographic APIs][]. 31061cb0ef41Sopenharmony_ci 31071cb0ef41Sopenharmony_ciInitialization vectors should be unpredictable and unique; ideally, they will be 31081cb0ef41Sopenharmony_cicryptographically random. They do not have to be secret: IVs are typically just 31091cb0ef41Sopenharmony_ciadded to ciphertext messages unencrypted. It may sound contradictory that 31101cb0ef41Sopenharmony_cisomething has to be unpredictable and unique, but does not have to be secret; 31111cb0ef41Sopenharmony_ciremember that an attacker must not be able to predict ahead of time what a 31121cb0ef41Sopenharmony_cigiven IV will be. 31131cb0ef41Sopenharmony_ci 31141cb0ef41Sopenharmony_ci### `crypto.createDecipher(algorithm, password[, options])` 31151cb0ef41Sopenharmony_ci 31161cb0ef41Sopenharmony_ci<!-- YAML 31171cb0ef41Sopenharmony_ciadded: v0.1.94 31181cb0ef41Sopenharmony_cideprecated: v10.0.0 31191cb0ef41Sopenharmony_cichanges: 31201cb0ef41Sopenharmony_ci - version: v17.9.0 31211cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/42427 31221cb0ef41Sopenharmony_ci description: The `authTagLength` option is now optional when using the 31231cb0ef41Sopenharmony_ci `chacha20-poly1305` cipher and defaults to 16 bytes. 31241cb0ef41Sopenharmony_ci - version: v10.10.0 31251cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/21447 31261cb0ef41Sopenharmony_ci description: Ciphers in OCB mode are now supported. 31271cb0ef41Sopenharmony_ci--> 31281cb0ef41Sopenharmony_ci 31291cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use [`crypto.createDecipheriv()`][] instead. 31301cb0ef41Sopenharmony_ci 31311cb0ef41Sopenharmony_ci* `algorithm` {string} 31321cb0ef41Sopenharmony_ci* `password` {string|ArrayBuffer|Buffer|TypedArray|DataView} 31331cb0ef41Sopenharmony_ci* `options` {Object} [`stream.transform` options][] 31341cb0ef41Sopenharmony_ci* Returns: {Decipher} 31351cb0ef41Sopenharmony_ci 31361cb0ef41Sopenharmony_ciCreates and returns a `Decipher` object that uses the given `algorithm` and 31371cb0ef41Sopenharmony_ci`password` (key). 31381cb0ef41Sopenharmony_ci 31391cb0ef41Sopenharmony_ciThe `options` argument controls stream behavior and is optional except when a 31401cb0ef41Sopenharmony_cicipher in CCM or OCB mode (e.g. `'aes-128-ccm'`) is used. In that case, the 31411cb0ef41Sopenharmony_ci`authTagLength` option is required and specifies the length of the 31421cb0ef41Sopenharmony_ciauthentication tag in bytes, see [CCM mode][]. 31431cb0ef41Sopenharmony_ciFor `chacha20-poly1305`, the `authTagLength` option defaults to 16 bytes. 31441cb0ef41Sopenharmony_ci 31451cb0ef41Sopenharmony_ci<strong class="critical">This function is semantically insecure for all 31461cb0ef41Sopenharmony_cisupported ciphers and fatally flawed for ciphers in counter mode (such as CTR, 31471cb0ef41Sopenharmony_ciGCM, or CCM).</strong> 31481cb0ef41Sopenharmony_ci 31491cb0ef41Sopenharmony_ciThe implementation of `crypto.createDecipher()` derives keys using the OpenSSL 31501cb0ef41Sopenharmony_cifunction [`EVP_BytesToKey`][] with the digest algorithm set to MD5, one 31511cb0ef41Sopenharmony_ciiteration, and no salt. The lack of salt allows dictionary attacks as the same 31521cb0ef41Sopenharmony_cipassword always creates the same key. The low iteration count and 31531cb0ef41Sopenharmony_cinon-cryptographically secure hash algorithm allow passwords to be tested very 31541cb0ef41Sopenharmony_cirapidly. 31551cb0ef41Sopenharmony_ci 31561cb0ef41Sopenharmony_ciIn line with OpenSSL's recommendation to use a more modern algorithm instead of 31571cb0ef41Sopenharmony_ci[`EVP_BytesToKey`][] it is recommended that developers derive a key and IV on 31581cb0ef41Sopenharmony_citheir own using [`crypto.scrypt()`][] and to use [`crypto.createDecipheriv()`][] 31591cb0ef41Sopenharmony_cito create the `Decipher` object. 31601cb0ef41Sopenharmony_ci 31611cb0ef41Sopenharmony_ci### `crypto.createDecipheriv(algorithm, key, iv[, options])` 31621cb0ef41Sopenharmony_ci 31631cb0ef41Sopenharmony_ci<!-- YAML 31641cb0ef41Sopenharmony_ciadded: v0.1.94 31651cb0ef41Sopenharmony_cichanges: 31661cb0ef41Sopenharmony_ci - version: v17.9.0 31671cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/42427 31681cb0ef41Sopenharmony_ci description: The `authTagLength` option is now optional when using the 31691cb0ef41Sopenharmony_ci `chacha20-poly1305` cipher and defaults to 16 bytes. 31701cb0ef41Sopenharmony_ci - version: v11.6.0 31711cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/24234 31721cb0ef41Sopenharmony_ci description: The `key` argument can now be a `KeyObject`. 31731cb0ef41Sopenharmony_ci - version: 31741cb0ef41Sopenharmony_ci - v11.2.0 31751cb0ef41Sopenharmony_ci - v10.17.0 31761cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/24081 31771cb0ef41Sopenharmony_ci description: The cipher `chacha20-poly1305` (the IETF variant of 31781cb0ef41Sopenharmony_ci ChaCha20-Poly1305) is now supported. 31791cb0ef41Sopenharmony_ci - version: v10.10.0 31801cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/21447 31811cb0ef41Sopenharmony_ci description: Ciphers in OCB mode are now supported. 31821cb0ef41Sopenharmony_ci - version: v10.2.0 31831cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/20039 31841cb0ef41Sopenharmony_ci description: The `authTagLength` option can now be used to restrict accepted 31851cb0ef41Sopenharmony_ci GCM authentication tag lengths. 31861cb0ef41Sopenharmony_ci - version: v9.9.0 31871cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/18644 31881cb0ef41Sopenharmony_ci description: The `iv` parameter may now be `null` for ciphers which do not 31891cb0ef41Sopenharmony_ci need an initialization vector. 31901cb0ef41Sopenharmony_ci--> 31911cb0ef41Sopenharmony_ci 31921cb0ef41Sopenharmony_ci* `algorithm` {string} 31931cb0ef41Sopenharmony_ci* `key` {string|ArrayBuffer|Buffer|TypedArray|DataView|KeyObject|CryptoKey} 31941cb0ef41Sopenharmony_ci* `iv` {string|ArrayBuffer|Buffer|TypedArray|DataView|null} 31951cb0ef41Sopenharmony_ci* `options` {Object} [`stream.transform` options][] 31961cb0ef41Sopenharmony_ci* Returns: {Decipher} 31971cb0ef41Sopenharmony_ci 31981cb0ef41Sopenharmony_ciCreates and returns a `Decipher` object that uses the given `algorithm`, `key` 31991cb0ef41Sopenharmony_ciand initialization vector (`iv`). 32001cb0ef41Sopenharmony_ci 32011cb0ef41Sopenharmony_ciThe `options` argument controls stream behavior and is optional except when a 32021cb0ef41Sopenharmony_cicipher in CCM or OCB mode (e.g. `'aes-128-ccm'`) is used. In that case, the 32031cb0ef41Sopenharmony_ci`authTagLength` option is required and specifies the length of the 32041cb0ef41Sopenharmony_ciauthentication tag in bytes, see [CCM mode][]. In GCM mode, the `authTagLength` 32051cb0ef41Sopenharmony_cioption is not required but can be used to restrict accepted authentication tags 32061cb0ef41Sopenharmony_cito those with the specified length. 32071cb0ef41Sopenharmony_ciFor `chacha20-poly1305`, the `authTagLength` option defaults to 16 bytes. 32081cb0ef41Sopenharmony_ci 32091cb0ef41Sopenharmony_ciThe `algorithm` is dependent on OpenSSL, examples are `'aes192'`, etc. On 32101cb0ef41Sopenharmony_cirecent OpenSSL releases, `openssl list -cipher-algorithms` will 32111cb0ef41Sopenharmony_cidisplay the available cipher algorithms. 32121cb0ef41Sopenharmony_ci 32131cb0ef41Sopenharmony_ciThe `key` is the raw key used by the `algorithm` and `iv` is an 32141cb0ef41Sopenharmony_ci[initialization vector][]. Both arguments must be `'utf8'` encoded strings, 32151cb0ef41Sopenharmony_ci[Buffers][`Buffer`], `TypedArray`, or `DataView`s. The `key` may optionally be 32161cb0ef41Sopenharmony_cia [`KeyObject`][] of type `secret`. If the cipher does not need 32171cb0ef41Sopenharmony_cian initialization vector, `iv` may be `null`. 32181cb0ef41Sopenharmony_ci 32191cb0ef41Sopenharmony_ciWhen passing strings for `key` or `iv`, please consider 32201cb0ef41Sopenharmony_ci[caveats when using strings as inputs to cryptographic APIs][]. 32211cb0ef41Sopenharmony_ci 32221cb0ef41Sopenharmony_ciInitialization vectors should be unpredictable and unique; ideally, they will be 32231cb0ef41Sopenharmony_cicryptographically random. They do not have to be secret: IVs are typically just 32241cb0ef41Sopenharmony_ciadded to ciphertext messages unencrypted. It may sound contradictory that 32251cb0ef41Sopenharmony_cisomething has to be unpredictable and unique, but does not have to be secret; 32261cb0ef41Sopenharmony_ciremember that an attacker must not be able to predict ahead of time what a given 32271cb0ef41Sopenharmony_ciIV will be. 32281cb0ef41Sopenharmony_ci 32291cb0ef41Sopenharmony_ci### `crypto.createDiffieHellman(prime[, primeEncoding][, generator][, generatorEncoding])` 32301cb0ef41Sopenharmony_ci 32311cb0ef41Sopenharmony_ci<!-- YAML 32321cb0ef41Sopenharmony_ciadded: v0.11.12 32331cb0ef41Sopenharmony_cichanges: 32341cb0ef41Sopenharmony_ci - version: v8.0.0 32351cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/12223 32361cb0ef41Sopenharmony_ci description: The `prime` argument can be any `TypedArray` or `DataView` now. 32371cb0ef41Sopenharmony_ci - version: v8.0.0 32381cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/11983 32391cb0ef41Sopenharmony_ci description: The `prime` argument can be a `Uint8Array` now. 32401cb0ef41Sopenharmony_ci - version: v6.0.0 32411cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/5522 32421cb0ef41Sopenharmony_ci description: The default for the encoding parameters changed 32431cb0ef41Sopenharmony_ci from `binary` to `utf8`. 32441cb0ef41Sopenharmony_ci--> 32451cb0ef41Sopenharmony_ci 32461cb0ef41Sopenharmony_ci* `prime` {string|ArrayBuffer|Buffer|TypedArray|DataView} 32471cb0ef41Sopenharmony_ci* `primeEncoding` {string} The [encoding][] of the `prime` string. 32481cb0ef41Sopenharmony_ci* `generator` {number|string|ArrayBuffer|Buffer|TypedArray|DataView} 32491cb0ef41Sopenharmony_ci **Default:** `2` 32501cb0ef41Sopenharmony_ci* `generatorEncoding` {string} The [encoding][] of the `generator` string. 32511cb0ef41Sopenharmony_ci* Returns: {DiffieHellman} 32521cb0ef41Sopenharmony_ci 32531cb0ef41Sopenharmony_ciCreates a `DiffieHellman` key exchange object using the supplied `prime` and an 32541cb0ef41Sopenharmony_cioptional specific `generator`. 32551cb0ef41Sopenharmony_ci 32561cb0ef41Sopenharmony_ciThe `generator` argument can be a number, string, or [`Buffer`][]. If 32571cb0ef41Sopenharmony_ci`generator` is not specified, the value `2` is used. 32581cb0ef41Sopenharmony_ci 32591cb0ef41Sopenharmony_ciIf `primeEncoding` is specified, `prime` is expected to be a string; otherwise 32601cb0ef41Sopenharmony_cia [`Buffer`][], `TypedArray`, or `DataView` is expected. 32611cb0ef41Sopenharmony_ci 32621cb0ef41Sopenharmony_ciIf `generatorEncoding` is specified, `generator` is expected to be a string; 32631cb0ef41Sopenharmony_ciotherwise a number, [`Buffer`][], `TypedArray`, or `DataView` is expected. 32641cb0ef41Sopenharmony_ci 32651cb0ef41Sopenharmony_ci### `crypto.createDiffieHellman(primeLength[, generator])` 32661cb0ef41Sopenharmony_ci 32671cb0ef41Sopenharmony_ci<!-- YAML 32681cb0ef41Sopenharmony_ciadded: v0.5.0 32691cb0ef41Sopenharmony_ci--> 32701cb0ef41Sopenharmony_ci 32711cb0ef41Sopenharmony_ci* `primeLength` {number} 32721cb0ef41Sopenharmony_ci* `generator` {number} **Default:** `2` 32731cb0ef41Sopenharmony_ci* Returns: {DiffieHellman} 32741cb0ef41Sopenharmony_ci 32751cb0ef41Sopenharmony_ciCreates a `DiffieHellman` key exchange object and generates a prime of 32761cb0ef41Sopenharmony_ci`primeLength` bits using an optional specific numeric `generator`. 32771cb0ef41Sopenharmony_ciIf `generator` is not specified, the value `2` is used. 32781cb0ef41Sopenharmony_ci 32791cb0ef41Sopenharmony_ci### `crypto.createDiffieHellmanGroup(name)` 32801cb0ef41Sopenharmony_ci 32811cb0ef41Sopenharmony_ci<!-- YAML 32821cb0ef41Sopenharmony_ciadded: v0.9.3 32831cb0ef41Sopenharmony_ci--> 32841cb0ef41Sopenharmony_ci 32851cb0ef41Sopenharmony_ci* `name` {string} 32861cb0ef41Sopenharmony_ci* Returns: {DiffieHellmanGroup} 32871cb0ef41Sopenharmony_ci 32881cb0ef41Sopenharmony_ciAn alias for [`crypto.getDiffieHellman()`][] 32891cb0ef41Sopenharmony_ci 32901cb0ef41Sopenharmony_ci### `crypto.createECDH(curveName)` 32911cb0ef41Sopenharmony_ci 32921cb0ef41Sopenharmony_ci<!-- YAML 32931cb0ef41Sopenharmony_ciadded: v0.11.14 32941cb0ef41Sopenharmony_ci--> 32951cb0ef41Sopenharmony_ci 32961cb0ef41Sopenharmony_ci* `curveName` {string} 32971cb0ef41Sopenharmony_ci* Returns: {ECDH} 32981cb0ef41Sopenharmony_ci 32991cb0ef41Sopenharmony_ciCreates an Elliptic Curve Diffie-Hellman (`ECDH`) key exchange object using a 33001cb0ef41Sopenharmony_cipredefined curve specified by the `curveName` string. Use 33011cb0ef41Sopenharmony_ci[`crypto.getCurves()`][] to obtain a list of available curve names. On recent 33021cb0ef41Sopenharmony_ciOpenSSL releases, `openssl ecparam -list_curves` will also display the name 33031cb0ef41Sopenharmony_ciand description of each available elliptic curve. 33041cb0ef41Sopenharmony_ci 33051cb0ef41Sopenharmony_ci### `crypto.createHash(algorithm[, options])` 33061cb0ef41Sopenharmony_ci 33071cb0ef41Sopenharmony_ci<!-- YAML 33081cb0ef41Sopenharmony_ciadded: v0.1.92 33091cb0ef41Sopenharmony_cichanges: 33101cb0ef41Sopenharmony_ci - version: v12.8.0 33111cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/28805 33121cb0ef41Sopenharmony_ci description: The `outputLength` option was added for XOF hash functions. 33131cb0ef41Sopenharmony_ci--> 33141cb0ef41Sopenharmony_ci 33151cb0ef41Sopenharmony_ci* `algorithm` {string} 33161cb0ef41Sopenharmony_ci* `options` {Object} [`stream.transform` options][] 33171cb0ef41Sopenharmony_ci* Returns: {Hash} 33181cb0ef41Sopenharmony_ci 33191cb0ef41Sopenharmony_ciCreates and returns a `Hash` object that can be used to generate hash digests 33201cb0ef41Sopenharmony_ciusing the given `algorithm`. Optional `options` argument controls stream 33211cb0ef41Sopenharmony_cibehavior. For XOF hash functions such as `'shake256'`, the `outputLength` option 33221cb0ef41Sopenharmony_cican be used to specify the desired output length in bytes. 33231cb0ef41Sopenharmony_ci 33241cb0ef41Sopenharmony_ciThe `algorithm` is dependent on the available algorithms supported by the 33251cb0ef41Sopenharmony_civersion of OpenSSL on the platform. Examples are `'sha256'`, `'sha512'`, etc. 33261cb0ef41Sopenharmony_ciOn recent releases of OpenSSL, `openssl list -digest-algorithms` will 33271cb0ef41Sopenharmony_cidisplay the available digest algorithms. 33281cb0ef41Sopenharmony_ci 33291cb0ef41Sopenharmony_ciExample: generating the sha256 sum of a file 33301cb0ef41Sopenharmony_ci 33311cb0ef41Sopenharmony_ci```mjs 33321cb0ef41Sopenharmony_ciimport { 33331cb0ef41Sopenharmony_ci createReadStream, 33341cb0ef41Sopenharmony_ci} from 'node:fs'; 33351cb0ef41Sopenharmony_ciimport { argv } from 'node:process'; 33361cb0ef41Sopenharmony_ciconst { 33371cb0ef41Sopenharmony_ci createHash, 33381cb0ef41Sopenharmony_ci} = await import('node:crypto'); 33391cb0ef41Sopenharmony_ci 33401cb0ef41Sopenharmony_ciconst filename = argv[2]; 33411cb0ef41Sopenharmony_ci 33421cb0ef41Sopenharmony_ciconst hash = createHash('sha256'); 33431cb0ef41Sopenharmony_ci 33441cb0ef41Sopenharmony_ciconst input = createReadStream(filename); 33451cb0ef41Sopenharmony_ciinput.on('readable', () => { 33461cb0ef41Sopenharmony_ci // Only one element is going to be produced by the 33471cb0ef41Sopenharmony_ci // hash stream. 33481cb0ef41Sopenharmony_ci const data = input.read(); 33491cb0ef41Sopenharmony_ci if (data) 33501cb0ef41Sopenharmony_ci hash.update(data); 33511cb0ef41Sopenharmony_ci else { 33521cb0ef41Sopenharmony_ci console.log(`${hash.digest('hex')} ${filename}`); 33531cb0ef41Sopenharmony_ci } 33541cb0ef41Sopenharmony_ci}); 33551cb0ef41Sopenharmony_ci``` 33561cb0ef41Sopenharmony_ci 33571cb0ef41Sopenharmony_ci```cjs 33581cb0ef41Sopenharmony_ciconst { 33591cb0ef41Sopenharmony_ci createReadStream, 33601cb0ef41Sopenharmony_ci} = require('node:fs'); 33611cb0ef41Sopenharmony_ciconst { 33621cb0ef41Sopenharmony_ci createHash, 33631cb0ef41Sopenharmony_ci} = require('node:crypto'); 33641cb0ef41Sopenharmony_ciconst { argv } = require('node:process'); 33651cb0ef41Sopenharmony_ci 33661cb0ef41Sopenharmony_ciconst filename = argv[2]; 33671cb0ef41Sopenharmony_ci 33681cb0ef41Sopenharmony_ciconst hash = createHash('sha256'); 33691cb0ef41Sopenharmony_ci 33701cb0ef41Sopenharmony_ciconst input = createReadStream(filename); 33711cb0ef41Sopenharmony_ciinput.on('readable', () => { 33721cb0ef41Sopenharmony_ci // Only one element is going to be produced by the 33731cb0ef41Sopenharmony_ci // hash stream. 33741cb0ef41Sopenharmony_ci const data = input.read(); 33751cb0ef41Sopenharmony_ci if (data) 33761cb0ef41Sopenharmony_ci hash.update(data); 33771cb0ef41Sopenharmony_ci else { 33781cb0ef41Sopenharmony_ci console.log(`${hash.digest('hex')} ${filename}`); 33791cb0ef41Sopenharmony_ci } 33801cb0ef41Sopenharmony_ci}); 33811cb0ef41Sopenharmony_ci``` 33821cb0ef41Sopenharmony_ci 33831cb0ef41Sopenharmony_ci### `crypto.createHmac(algorithm, key[, options])` 33841cb0ef41Sopenharmony_ci 33851cb0ef41Sopenharmony_ci<!-- YAML 33861cb0ef41Sopenharmony_ciadded: v0.1.94 33871cb0ef41Sopenharmony_cichanges: 33881cb0ef41Sopenharmony_ci - version: v15.0.0 33891cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/35093 33901cb0ef41Sopenharmony_ci description: The key can also be an ArrayBuffer or CryptoKey. The 33911cb0ef41Sopenharmony_ci encoding option was added. The key cannot contain 33921cb0ef41Sopenharmony_ci more than 2 ** 32 - 1 bytes. 33931cb0ef41Sopenharmony_ci - version: v11.6.0 33941cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/24234 33951cb0ef41Sopenharmony_ci description: The `key` argument can now be a `KeyObject`. 33961cb0ef41Sopenharmony_ci--> 33971cb0ef41Sopenharmony_ci 33981cb0ef41Sopenharmony_ci* `algorithm` {string} 33991cb0ef41Sopenharmony_ci* `key` {string|ArrayBuffer|Buffer|TypedArray|DataView|KeyObject|CryptoKey} 34001cb0ef41Sopenharmony_ci* `options` {Object} [`stream.transform` options][] 34011cb0ef41Sopenharmony_ci * `encoding` {string} The string encoding to use when `key` is a string. 34021cb0ef41Sopenharmony_ci* Returns: {Hmac} 34031cb0ef41Sopenharmony_ci 34041cb0ef41Sopenharmony_ciCreates and returns an `Hmac` object that uses the given `algorithm` and `key`. 34051cb0ef41Sopenharmony_ciOptional `options` argument controls stream behavior. 34061cb0ef41Sopenharmony_ci 34071cb0ef41Sopenharmony_ciThe `algorithm` is dependent on the available algorithms supported by the 34081cb0ef41Sopenharmony_civersion of OpenSSL on the platform. Examples are `'sha256'`, `'sha512'`, etc. 34091cb0ef41Sopenharmony_ciOn recent releases of OpenSSL, `openssl list -digest-algorithms` will 34101cb0ef41Sopenharmony_cidisplay the available digest algorithms. 34111cb0ef41Sopenharmony_ci 34121cb0ef41Sopenharmony_ciThe `key` is the HMAC key used to generate the cryptographic HMAC hash. If it is 34131cb0ef41Sopenharmony_cia [`KeyObject`][], its type must be `secret`. If it is a string, please consider 34141cb0ef41Sopenharmony_ci[caveats when using strings as inputs to cryptographic APIs][]. If it was 34151cb0ef41Sopenharmony_ciobtained from a cryptographically secure source of entropy, such as 34161cb0ef41Sopenharmony_ci[`crypto.randomBytes()`][] or [`crypto.generateKey()`][], its length should not 34171cb0ef41Sopenharmony_ciexceed the block size of `algorithm` (e.g., 512 bits for SHA-256). 34181cb0ef41Sopenharmony_ci 34191cb0ef41Sopenharmony_ciExample: generating the sha256 HMAC of a file 34201cb0ef41Sopenharmony_ci 34211cb0ef41Sopenharmony_ci```mjs 34221cb0ef41Sopenharmony_ciimport { 34231cb0ef41Sopenharmony_ci createReadStream, 34241cb0ef41Sopenharmony_ci} from 'node:fs'; 34251cb0ef41Sopenharmony_ciimport { argv } from 'node:process'; 34261cb0ef41Sopenharmony_ciconst { 34271cb0ef41Sopenharmony_ci createHmac, 34281cb0ef41Sopenharmony_ci} = await import('node:crypto'); 34291cb0ef41Sopenharmony_ci 34301cb0ef41Sopenharmony_ciconst filename = argv[2]; 34311cb0ef41Sopenharmony_ci 34321cb0ef41Sopenharmony_ciconst hmac = createHmac('sha256', 'a secret'); 34331cb0ef41Sopenharmony_ci 34341cb0ef41Sopenharmony_ciconst input = createReadStream(filename); 34351cb0ef41Sopenharmony_ciinput.on('readable', () => { 34361cb0ef41Sopenharmony_ci // Only one element is going to be produced by the 34371cb0ef41Sopenharmony_ci // hash stream. 34381cb0ef41Sopenharmony_ci const data = input.read(); 34391cb0ef41Sopenharmony_ci if (data) 34401cb0ef41Sopenharmony_ci hmac.update(data); 34411cb0ef41Sopenharmony_ci else { 34421cb0ef41Sopenharmony_ci console.log(`${hmac.digest('hex')} ${filename}`); 34431cb0ef41Sopenharmony_ci } 34441cb0ef41Sopenharmony_ci}); 34451cb0ef41Sopenharmony_ci``` 34461cb0ef41Sopenharmony_ci 34471cb0ef41Sopenharmony_ci```cjs 34481cb0ef41Sopenharmony_ciconst { 34491cb0ef41Sopenharmony_ci createReadStream, 34501cb0ef41Sopenharmony_ci} = require('node:fs'); 34511cb0ef41Sopenharmony_ciconst { 34521cb0ef41Sopenharmony_ci createHmac, 34531cb0ef41Sopenharmony_ci} = require('node:crypto'); 34541cb0ef41Sopenharmony_ciconst { argv } = require('node:process'); 34551cb0ef41Sopenharmony_ci 34561cb0ef41Sopenharmony_ciconst filename = argv[2]; 34571cb0ef41Sopenharmony_ci 34581cb0ef41Sopenharmony_ciconst hmac = createHmac('sha256', 'a secret'); 34591cb0ef41Sopenharmony_ci 34601cb0ef41Sopenharmony_ciconst input = createReadStream(filename); 34611cb0ef41Sopenharmony_ciinput.on('readable', () => { 34621cb0ef41Sopenharmony_ci // Only one element is going to be produced by the 34631cb0ef41Sopenharmony_ci // hash stream. 34641cb0ef41Sopenharmony_ci const data = input.read(); 34651cb0ef41Sopenharmony_ci if (data) 34661cb0ef41Sopenharmony_ci hmac.update(data); 34671cb0ef41Sopenharmony_ci else { 34681cb0ef41Sopenharmony_ci console.log(`${hmac.digest('hex')} ${filename}`); 34691cb0ef41Sopenharmony_ci } 34701cb0ef41Sopenharmony_ci}); 34711cb0ef41Sopenharmony_ci``` 34721cb0ef41Sopenharmony_ci 34731cb0ef41Sopenharmony_ci### `crypto.createPrivateKey(key)` 34741cb0ef41Sopenharmony_ci 34751cb0ef41Sopenharmony_ci<!-- YAML 34761cb0ef41Sopenharmony_ciadded: v11.6.0 34771cb0ef41Sopenharmony_cichanges: 34781cb0ef41Sopenharmony_ci - version: v15.12.0 34791cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/37254 34801cb0ef41Sopenharmony_ci description: The key can also be a JWK object. 34811cb0ef41Sopenharmony_ci - version: v15.0.0 34821cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/35093 34831cb0ef41Sopenharmony_ci description: The key can also be an ArrayBuffer. The encoding option was 34841cb0ef41Sopenharmony_ci added. The key cannot contain more than 2 ** 32 - 1 bytes. 34851cb0ef41Sopenharmony_ci--> 34861cb0ef41Sopenharmony_ci 34871cb0ef41Sopenharmony_ci<!--lint disable maximum-line-length remark-lint--> 34881cb0ef41Sopenharmony_ci 34891cb0ef41Sopenharmony_ci* `key` {Object|string|ArrayBuffer|Buffer|TypedArray|DataView} 34901cb0ef41Sopenharmony_ci * `key`: {string|ArrayBuffer|Buffer|TypedArray|DataView|Object} The key 34911cb0ef41Sopenharmony_ci material, either in PEM, DER, or JWK format. 34921cb0ef41Sopenharmony_ci * `format`: {string} Must be `'pem'`, `'der'`, or '`'jwk'`. 34931cb0ef41Sopenharmony_ci **Default:** `'pem'`. 34941cb0ef41Sopenharmony_ci * `type`: {string} Must be `'pkcs1'`, `'pkcs8'` or `'sec1'`. This option is 34951cb0ef41Sopenharmony_ci required only if the `format` is `'der'` and ignored otherwise. 34961cb0ef41Sopenharmony_ci * `passphrase`: {string | Buffer} The passphrase to use for decryption. 34971cb0ef41Sopenharmony_ci * `encoding`: {string} The string encoding to use when `key` is a string. 34981cb0ef41Sopenharmony_ci* Returns: {KeyObject} 34991cb0ef41Sopenharmony_ci 35001cb0ef41Sopenharmony_ci<!--lint enable maximum-line-length remark-lint--> 35011cb0ef41Sopenharmony_ci 35021cb0ef41Sopenharmony_ciCreates and returns a new key object containing a private key. If `key` is a 35031cb0ef41Sopenharmony_cistring or `Buffer`, `format` is assumed to be `'pem'`; otherwise, `key` 35041cb0ef41Sopenharmony_cimust be an object with the properties described above. 35051cb0ef41Sopenharmony_ci 35061cb0ef41Sopenharmony_ciIf the private key is encrypted, a `passphrase` must be specified. The length 35071cb0ef41Sopenharmony_ciof the passphrase is limited to 1024 bytes. 35081cb0ef41Sopenharmony_ci 35091cb0ef41Sopenharmony_ci### `crypto.createPublicKey(key)` 35101cb0ef41Sopenharmony_ci 35111cb0ef41Sopenharmony_ci<!-- YAML 35121cb0ef41Sopenharmony_ciadded: v11.6.0 35131cb0ef41Sopenharmony_cichanges: 35141cb0ef41Sopenharmony_ci - version: v15.12.0 35151cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/37254 35161cb0ef41Sopenharmony_ci description: The key can also be a JWK object. 35171cb0ef41Sopenharmony_ci - version: v15.0.0 35181cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/35093 35191cb0ef41Sopenharmony_ci description: The key can also be an ArrayBuffer. The encoding option was 35201cb0ef41Sopenharmony_ci added. The key cannot contain more than 2 ** 32 - 1 bytes. 35211cb0ef41Sopenharmony_ci - version: v11.13.0 35221cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/26278 35231cb0ef41Sopenharmony_ci description: The `key` argument can now be a `KeyObject` with type 35241cb0ef41Sopenharmony_ci `private`. 35251cb0ef41Sopenharmony_ci - version: v11.7.0 35261cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/25217 35271cb0ef41Sopenharmony_ci description: The `key` argument can now be a private key. 35281cb0ef41Sopenharmony_ci--> 35291cb0ef41Sopenharmony_ci 35301cb0ef41Sopenharmony_ci<!--lint disable maximum-line-length remark-lint--> 35311cb0ef41Sopenharmony_ci 35321cb0ef41Sopenharmony_ci* `key` {Object|string|ArrayBuffer|Buffer|TypedArray|DataView} 35331cb0ef41Sopenharmony_ci * `key`: {string|ArrayBuffer|Buffer|TypedArray|DataView|Object} The key 35341cb0ef41Sopenharmony_ci material, either in PEM, DER, or JWK format. 35351cb0ef41Sopenharmony_ci * `format`: {string} Must be `'pem'`, `'der'`, or `'jwk'`. 35361cb0ef41Sopenharmony_ci **Default:** `'pem'`. 35371cb0ef41Sopenharmony_ci * `type`: {string} Must be `'pkcs1'` or `'spki'`. This option is 35381cb0ef41Sopenharmony_ci required only if the `format` is `'der'` and ignored otherwise. 35391cb0ef41Sopenharmony_ci * `encoding` {string} The string encoding to use when `key` is a string. 35401cb0ef41Sopenharmony_ci* Returns: {KeyObject} 35411cb0ef41Sopenharmony_ci 35421cb0ef41Sopenharmony_ci<!--lint enable maximum-line-length remark-lint--> 35431cb0ef41Sopenharmony_ci 35441cb0ef41Sopenharmony_ciCreates and returns a new key object containing a public key. If `key` is a 35451cb0ef41Sopenharmony_cistring or `Buffer`, `format` is assumed to be `'pem'`; if `key` is a `KeyObject` 35461cb0ef41Sopenharmony_ciwith type `'private'`, the public key is derived from the given private key; 35471cb0ef41Sopenharmony_ciotherwise, `key` must be an object with the properties described above. 35481cb0ef41Sopenharmony_ci 35491cb0ef41Sopenharmony_ciIf the format is `'pem'`, the `'key'` may also be an X.509 certificate. 35501cb0ef41Sopenharmony_ci 35511cb0ef41Sopenharmony_ciBecause public keys can be derived from private keys, a private key may be 35521cb0ef41Sopenharmony_cipassed instead of a public key. In that case, this function behaves as if 35531cb0ef41Sopenharmony_ci[`crypto.createPrivateKey()`][] had been called, except that the type of the 35541cb0ef41Sopenharmony_cireturned `KeyObject` will be `'public'` and that the private key cannot be 35551cb0ef41Sopenharmony_ciextracted from the returned `KeyObject`. Similarly, if a `KeyObject` with type 35561cb0ef41Sopenharmony_ci`'private'` is given, a new `KeyObject` with type `'public'` will be returned 35571cb0ef41Sopenharmony_ciand it will be impossible to extract the private key from the returned object. 35581cb0ef41Sopenharmony_ci 35591cb0ef41Sopenharmony_ci### `crypto.createSecretKey(key[, encoding])` 35601cb0ef41Sopenharmony_ci 35611cb0ef41Sopenharmony_ci<!-- YAML 35621cb0ef41Sopenharmony_ciadded: v11.6.0 35631cb0ef41Sopenharmony_cichanges: 35641cb0ef41Sopenharmony_ci - version: v18.8.0 35651cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/44201 35661cb0ef41Sopenharmony_ci description: The key can now be zero-length. 35671cb0ef41Sopenharmony_ci - version: v15.0.0 35681cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/35093 35691cb0ef41Sopenharmony_ci description: The key can also be an ArrayBuffer or string. The encoding 35701cb0ef41Sopenharmony_ci argument was added. The key cannot contain more than 35711cb0ef41Sopenharmony_ci 2 ** 32 - 1 bytes. 35721cb0ef41Sopenharmony_ci--> 35731cb0ef41Sopenharmony_ci 35741cb0ef41Sopenharmony_ci* `key` {string|ArrayBuffer|Buffer|TypedArray|DataView} 35751cb0ef41Sopenharmony_ci* `encoding` {string} The string encoding when `key` is a string. 35761cb0ef41Sopenharmony_ci* Returns: {KeyObject} 35771cb0ef41Sopenharmony_ci 35781cb0ef41Sopenharmony_ciCreates and returns a new key object containing a secret key for symmetric 35791cb0ef41Sopenharmony_ciencryption or `Hmac`. 35801cb0ef41Sopenharmony_ci 35811cb0ef41Sopenharmony_ci### `crypto.createSign(algorithm[, options])` 35821cb0ef41Sopenharmony_ci 35831cb0ef41Sopenharmony_ci<!-- YAML 35841cb0ef41Sopenharmony_ciadded: v0.1.92 35851cb0ef41Sopenharmony_ci--> 35861cb0ef41Sopenharmony_ci 35871cb0ef41Sopenharmony_ci* `algorithm` {string} 35881cb0ef41Sopenharmony_ci* `options` {Object} [`stream.Writable` options][] 35891cb0ef41Sopenharmony_ci* Returns: {Sign} 35901cb0ef41Sopenharmony_ci 35911cb0ef41Sopenharmony_ciCreates and returns a `Sign` object that uses the given `algorithm`. Use 35921cb0ef41Sopenharmony_ci[`crypto.getHashes()`][] to obtain the names of the available digest algorithms. 35931cb0ef41Sopenharmony_ciOptional `options` argument controls the `stream.Writable` behavior. 35941cb0ef41Sopenharmony_ci 35951cb0ef41Sopenharmony_ciIn some cases, a `Sign` instance can be created using the name of a signature 35961cb0ef41Sopenharmony_cialgorithm, such as `'RSA-SHA256'`, instead of a digest algorithm. This will use 35971cb0ef41Sopenharmony_cithe corresponding digest algorithm. This does not work for all signature 35981cb0ef41Sopenharmony_cialgorithms, such as `'ecdsa-with-SHA256'`, so it is best to always use digest 35991cb0ef41Sopenharmony_cialgorithm names. 36001cb0ef41Sopenharmony_ci 36011cb0ef41Sopenharmony_ci### `crypto.createVerify(algorithm[, options])` 36021cb0ef41Sopenharmony_ci 36031cb0ef41Sopenharmony_ci<!-- YAML 36041cb0ef41Sopenharmony_ciadded: v0.1.92 36051cb0ef41Sopenharmony_ci--> 36061cb0ef41Sopenharmony_ci 36071cb0ef41Sopenharmony_ci* `algorithm` {string} 36081cb0ef41Sopenharmony_ci* `options` {Object} [`stream.Writable` options][] 36091cb0ef41Sopenharmony_ci* Returns: {Verify} 36101cb0ef41Sopenharmony_ci 36111cb0ef41Sopenharmony_ciCreates and returns a `Verify` object that uses the given algorithm. 36121cb0ef41Sopenharmony_ciUse [`crypto.getHashes()`][] to obtain an array of names of the available 36131cb0ef41Sopenharmony_cisigning algorithms. Optional `options` argument controls the 36141cb0ef41Sopenharmony_ci`stream.Writable` behavior. 36151cb0ef41Sopenharmony_ci 36161cb0ef41Sopenharmony_ciIn some cases, a `Verify` instance can be created using the name of a signature 36171cb0ef41Sopenharmony_cialgorithm, such as `'RSA-SHA256'`, instead of a digest algorithm. This will use 36181cb0ef41Sopenharmony_cithe corresponding digest algorithm. This does not work for all signature 36191cb0ef41Sopenharmony_cialgorithms, such as `'ecdsa-with-SHA256'`, so it is best to always use digest 36201cb0ef41Sopenharmony_cialgorithm names. 36211cb0ef41Sopenharmony_ci 36221cb0ef41Sopenharmony_ci### `crypto.diffieHellman(options)` 36231cb0ef41Sopenharmony_ci 36241cb0ef41Sopenharmony_ci<!-- YAML 36251cb0ef41Sopenharmony_ciadded: 36261cb0ef41Sopenharmony_ci - v13.9.0 36271cb0ef41Sopenharmony_ci - v12.17.0 36281cb0ef41Sopenharmony_ci--> 36291cb0ef41Sopenharmony_ci 36301cb0ef41Sopenharmony_ci* `options`: {Object} 36311cb0ef41Sopenharmony_ci * `privateKey`: {KeyObject} 36321cb0ef41Sopenharmony_ci * `publicKey`: {KeyObject} 36331cb0ef41Sopenharmony_ci* Returns: {Buffer} 36341cb0ef41Sopenharmony_ci 36351cb0ef41Sopenharmony_ciComputes the Diffie-Hellman secret based on a `privateKey` and a `publicKey`. 36361cb0ef41Sopenharmony_ciBoth keys must have the same `asymmetricKeyType`, which must be one of `'dh'` 36371cb0ef41Sopenharmony_ci(for Diffie-Hellman), `'ec'` (for ECDH), `'x448'`, or `'x25519'` (for ECDH-ES). 36381cb0ef41Sopenharmony_ci 36391cb0ef41Sopenharmony_ci### `crypto.generateKey(type, options, callback)` 36401cb0ef41Sopenharmony_ci 36411cb0ef41Sopenharmony_ci<!-- YAML 36421cb0ef41Sopenharmony_ciadded: v15.0.0 36431cb0ef41Sopenharmony_cichanges: 36441cb0ef41Sopenharmony_ci - version: v18.0.0 36451cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41678 36461cb0ef41Sopenharmony_ci description: Passing an invalid callback to the `callback` argument 36471cb0ef41Sopenharmony_ci now throws `ERR_INVALID_ARG_TYPE` instead of 36481cb0ef41Sopenharmony_ci `ERR_INVALID_CALLBACK`. 36491cb0ef41Sopenharmony_ci--> 36501cb0ef41Sopenharmony_ci 36511cb0ef41Sopenharmony_ci* `type`: {string} The intended use of the generated secret key. Currently 36521cb0ef41Sopenharmony_ci accepted values are `'hmac'` and `'aes'`. 36531cb0ef41Sopenharmony_ci* `options`: {Object} 36541cb0ef41Sopenharmony_ci * `length`: {number} The bit length of the key to generate. This must be a 36551cb0ef41Sopenharmony_ci value greater than 0. 36561cb0ef41Sopenharmony_ci * If `type` is `'hmac'`, the minimum is 8, and the maximum length is 36571cb0ef41Sopenharmony_ci 2<sup>31</sup>-1. If the value is not a multiple of 8, the generated 36581cb0ef41Sopenharmony_ci key will be truncated to `Math.floor(length / 8)`. 36591cb0ef41Sopenharmony_ci * If `type` is `'aes'`, the length must be one of `128`, `192`, or `256`. 36601cb0ef41Sopenharmony_ci* `callback`: {Function} 36611cb0ef41Sopenharmony_ci * `err`: {Error} 36621cb0ef41Sopenharmony_ci * `key`: {KeyObject} 36631cb0ef41Sopenharmony_ci 36641cb0ef41Sopenharmony_ciAsynchronously generates a new random secret key of the given `length`. The 36651cb0ef41Sopenharmony_ci`type` will determine which validations will be performed on the `length`. 36661cb0ef41Sopenharmony_ci 36671cb0ef41Sopenharmony_ci```mjs 36681cb0ef41Sopenharmony_ciconst { 36691cb0ef41Sopenharmony_ci generateKey, 36701cb0ef41Sopenharmony_ci} = await import('node:crypto'); 36711cb0ef41Sopenharmony_ci 36721cb0ef41Sopenharmony_cigenerateKey('hmac', { length: 512 }, (err, key) => { 36731cb0ef41Sopenharmony_ci if (err) throw err; 36741cb0ef41Sopenharmony_ci console.log(key.export().toString('hex')); // 46e..........620 36751cb0ef41Sopenharmony_ci}); 36761cb0ef41Sopenharmony_ci``` 36771cb0ef41Sopenharmony_ci 36781cb0ef41Sopenharmony_ci```cjs 36791cb0ef41Sopenharmony_ciconst { 36801cb0ef41Sopenharmony_ci generateKey, 36811cb0ef41Sopenharmony_ci} = require('node:crypto'); 36821cb0ef41Sopenharmony_ci 36831cb0ef41Sopenharmony_cigenerateKey('hmac', { length: 512 }, (err, key) => { 36841cb0ef41Sopenharmony_ci if (err) throw err; 36851cb0ef41Sopenharmony_ci console.log(key.export().toString('hex')); // 46e..........620 36861cb0ef41Sopenharmony_ci}); 36871cb0ef41Sopenharmony_ci``` 36881cb0ef41Sopenharmony_ci 36891cb0ef41Sopenharmony_ciThe size of a generated HMAC key should not exceed the block size of the 36901cb0ef41Sopenharmony_ciunderlying hash function. See [`crypto.createHmac()`][] for more information. 36911cb0ef41Sopenharmony_ci 36921cb0ef41Sopenharmony_ci### `crypto.generateKeyPair(type, options, callback)` 36931cb0ef41Sopenharmony_ci 36941cb0ef41Sopenharmony_ci<!-- YAML 36951cb0ef41Sopenharmony_ciadded: v10.12.0 36961cb0ef41Sopenharmony_cichanges: 36971cb0ef41Sopenharmony_ci - version: v18.0.0 36981cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41678 36991cb0ef41Sopenharmony_ci description: Passing an invalid callback to the `callback` argument 37001cb0ef41Sopenharmony_ci now throws `ERR_INVALID_ARG_TYPE` instead of 37011cb0ef41Sopenharmony_ci `ERR_INVALID_CALLBACK`. 37021cb0ef41Sopenharmony_ci - version: v16.10.0 37031cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/39927 37041cb0ef41Sopenharmony_ci description: Add ability to define `RSASSA-PSS-params` sequence parameters 37051cb0ef41Sopenharmony_ci for RSA-PSS keys pairs. 37061cb0ef41Sopenharmony_ci - version: 37071cb0ef41Sopenharmony_ci - v13.9.0 37081cb0ef41Sopenharmony_ci - v12.17.0 37091cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/31178 37101cb0ef41Sopenharmony_ci description: Add support for Diffie-Hellman. 37111cb0ef41Sopenharmony_ci - version: v12.0.0 37121cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/26960 37131cb0ef41Sopenharmony_ci description: Add support for RSA-PSS key pairs. 37141cb0ef41Sopenharmony_ci - version: v12.0.0 37151cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/26774 37161cb0ef41Sopenharmony_ci description: Add ability to generate X25519 and X448 key pairs. 37171cb0ef41Sopenharmony_ci - version: v12.0.0 37181cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/26554 37191cb0ef41Sopenharmony_ci description: Add ability to generate Ed25519 and Ed448 key pairs. 37201cb0ef41Sopenharmony_ci - version: v11.6.0 37211cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/24234 37221cb0ef41Sopenharmony_ci description: The `generateKeyPair` and `generateKeyPairSync` functions now 37231cb0ef41Sopenharmony_ci produce key objects if no encoding was specified. 37241cb0ef41Sopenharmony_ci--> 37251cb0ef41Sopenharmony_ci 37261cb0ef41Sopenharmony_ci* `type`: {string} Must be `'rsa'`, `'rsa-pss'`, `'dsa'`, `'ec'`, `'ed25519'`, 37271cb0ef41Sopenharmony_ci `'ed448'`, `'x25519'`, `'x448'`, or `'dh'`. 37281cb0ef41Sopenharmony_ci* `options`: {Object} 37291cb0ef41Sopenharmony_ci * `modulusLength`: {number} Key size in bits (RSA, DSA). 37301cb0ef41Sopenharmony_ci * `publicExponent`: {number} Public exponent (RSA). **Default:** `0x10001`. 37311cb0ef41Sopenharmony_ci * `hashAlgorithm`: {string} Name of the message digest (RSA-PSS). 37321cb0ef41Sopenharmony_ci * `mgf1HashAlgorithm`: {string} Name of the message digest used by 37331cb0ef41Sopenharmony_ci MGF1 (RSA-PSS). 37341cb0ef41Sopenharmony_ci * `saltLength`: {number} Minimal salt length in bytes (RSA-PSS). 37351cb0ef41Sopenharmony_ci * `divisorLength`: {number} Size of `q` in bits (DSA). 37361cb0ef41Sopenharmony_ci * `namedCurve`: {string} Name of the curve to use (EC). 37371cb0ef41Sopenharmony_ci * `prime`: {Buffer} The prime parameter (DH). 37381cb0ef41Sopenharmony_ci * `primeLength`: {number} Prime length in bits (DH). 37391cb0ef41Sopenharmony_ci * `generator`: {number} Custom generator (DH). **Default:** `2`. 37401cb0ef41Sopenharmony_ci * `groupName`: {string} Diffie-Hellman group name (DH). See 37411cb0ef41Sopenharmony_ci [`crypto.getDiffieHellman()`][]. 37421cb0ef41Sopenharmony_ci * `paramEncoding`: {string} Must be `'named'` or `'explicit'` (EC). 37431cb0ef41Sopenharmony_ci **Default:** `'named'`. 37441cb0ef41Sopenharmony_ci * `publicKeyEncoding`: {Object} See [`keyObject.export()`][]. 37451cb0ef41Sopenharmony_ci * `privateKeyEncoding`: {Object} See [`keyObject.export()`][]. 37461cb0ef41Sopenharmony_ci* `callback`: {Function} 37471cb0ef41Sopenharmony_ci * `err`: {Error} 37481cb0ef41Sopenharmony_ci * `publicKey`: {string | Buffer | KeyObject} 37491cb0ef41Sopenharmony_ci * `privateKey`: {string | Buffer | KeyObject} 37501cb0ef41Sopenharmony_ci 37511cb0ef41Sopenharmony_ciGenerates a new asymmetric key pair of the given `type`. RSA, RSA-PSS, DSA, EC, 37521cb0ef41Sopenharmony_ciEd25519, Ed448, X25519, X448, and DH are currently supported. 37531cb0ef41Sopenharmony_ci 37541cb0ef41Sopenharmony_ciIf a `publicKeyEncoding` or `privateKeyEncoding` was specified, this function 37551cb0ef41Sopenharmony_cibehaves as if [`keyObject.export()`][] had been called on its result. Otherwise, 37561cb0ef41Sopenharmony_cithe respective part of the key is returned as a [`KeyObject`][]. 37571cb0ef41Sopenharmony_ci 37581cb0ef41Sopenharmony_ciIt is recommended to encode public keys as `'spki'` and private keys as 37591cb0ef41Sopenharmony_ci`'pkcs8'` with encryption for long-term storage: 37601cb0ef41Sopenharmony_ci 37611cb0ef41Sopenharmony_ci```mjs 37621cb0ef41Sopenharmony_ciconst { 37631cb0ef41Sopenharmony_ci generateKeyPair, 37641cb0ef41Sopenharmony_ci} = await import('node:crypto'); 37651cb0ef41Sopenharmony_ci 37661cb0ef41Sopenharmony_cigenerateKeyPair('rsa', { 37671cb0ef41Sopenharmony_ci modulusLength: 4096, 37681cb0ef41Sopenharmony_ci publicKeyEncoding: { 37691cb0ef41Sopenharmony_ci type: 'spki', 37701cb0ef41Sopenharmony_ci format: 'pem', 37711cb0ef41Sopenharmony_ci }, 37721cb0ef41Sopenharmony_ci privateKeyEncoding: { 37731cb0ef41Sopenharmony_ci type: 'pkcs8', 37741cb0ef41Sopenharmony_ci format: 'pem', 37751cb0ef41Sopenharmony_ci cipher: 'aes-256-cbc', 37761cb0ef41Sopenharmony_ci passphrase: 'top secret', 37771cb0ef41Sopenharmony_ci }, 37781cb0ef41Sopenharmony_ci}, (err, publicKey, privateKey) => { 37791cb0ef41Sopenharmony_ci // Handle errors and use the generated key pair. 37801cb0ef41Sopenharmony_ci}); 37811cb0ef41Sopenharmony_ci``` 37821cb0ef41Sopenharmony_ci 37831cb0ef41Sopenharmony_ci```cjs 37841cb0ef41Sopenharmony_ciconst { 37851cb0ef41Sopenharmony_ci generateKeyPair, 37861cb0ef41Sopenharmony_ci} = require('node:crypto'); 37871cb0ef41Sopenharmony_ci 37881cb0ef41Sopenharmony_cigenerateKeyPair('rsa', { 37891cb0ef41Sopenharmony_ci modulusLength: 4096, 37901cb0ef41Sopenharmony_ci publicKeyEncoding: { 37911cb0ef41Sopenharmony_ci type: 'spki', 37921cb0ef41Sopenharmony_ci format: 'pem', 37931cb0ef41Sopenharmony_ci }, 37941cb0ef41Sopenharmony_ci privateKeyEncoding: { 37951cb0ef41Sopenharmony_ci type: 'pkcs8', 37961cb0ef41Sopenharmony_ci format: 'pem', 37971cb0ef41Sopenharmony_ci cipher: 'aes-256-cbc', 37981cb0ef41Sopenharmony_ci passphrase: 'top secret', 37991cb0ef41Sopenharmony_ci }, 38001cb0ef41Sopenharmony_ci}, (err, publicKey, privateKey) => { 38011cb0ef41Sopenharmony_ci // Handle errors and use the generated key pair. 38021cb0ef41Sopenharmony_ci}); 38031cb0ef41Sopenharmony_ci``` 38041cb0ef41Sopenharmony_ci 38051cb0ef41Sopenharmony_ciOn completion, `callback` will be called with `err` set to `undefined` and 38061cb0ef41Sopenharmony_ci`publicKey` / `privateKey` representing the generated key pair. 38071cb0ef41Sopenharmony_ci 38081cb0ef41Sopenharmony_ciIf this method is invoked as its [`util.promisify()`][]ed version, it returns 38091cb0ef41Sopenharmony_cia `Promise` for an `Object` with `publicKey` and `privateKey` properties. 38101cb0ef41Sopenharmony_ci 38111cb0ef41Sopenharmony_ci### `crypto.generateKeyPairSync(type, options)` 38121cb0ef41Sopenharmony_ci 38131cb0ef41Sopenharmony_ci<!-- YAML 38141cb0ef41Sopenharmony_ciadded: v10.12.0 38151cb0ef41Sopenharmony_cichanges: 38161cb0ef41Sopenharmony_ci - version: v16.10.0 38171cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/39927 38181cb0ef41Sopenharmony_ci description: Add ability to define `RSASSA-PSS-params` sequence parameters 38191cb0ef41Sopenharmony_ci for RSA-PSS keys pairs. 38201cb0ef41Sopenharmony_ci - version: 38211cb0ef41Sopenharmony_ci - v13.9.0 38221cb0ef41Sopenharmony_ci - v12.17.0 38231cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/31178 38241cb0ef41Sopenharmony_ci description: Add support for Diffie-Hellman. 38251cb0ef41Sopenharmony_ci - version: v12.0.0 38261cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/26960 38271cb0ef41Sopenharmony_ci description: Add support for RSA-PSS key pairs. 38281cb0ef41Sopenharmony_ci - version: v12.0.0 38291cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/26774 38301cb0ef41Sopenharmony_ci description: Add ability to generate X25519 and X448 key pairs. 38311cb0ef41Sopenharmony_ci - version: v12.0.0 38321cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/26554 38331cb0ef41Sopenharmony_ci description: Add ability to generate Ed25519 and Ed448 key pairs. 38341cb0ef41Sopenharmony_ci - version: v11.6.0 38351cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/24234 38361cb0ef41Sopenharmony_ci description: The `generateKeyPair` and `generateKeyPairSync` functions now 38371cb0ef41Sopenharmony_ci produce key objects if no encoding was specified. 38381cb0ef41Sopenharmony_ci--> 38391cb0ef41Sopenharmony_ci 38401cb0ef41Sopenharmony_ci* `type`: {string} Must be `'rsa'`, `'rsa-pss'`, `'dsa'`, `'ec'`, `'ed25519'`, 38411cb0ef41Sopenharmony_ci `'ed448'`, `'x25519'`, `'x448'`, or `'dh'`. 38421cb0ef41Sopenharmony_ci* `options`: {Object} 38431cb0ef41Sopenharmony_ci * `modulusLength`: {number} Key size in bits (RSA, DSA). 38441cb0ef41Sopenharmony_ci * `publicExponent`: {number} Public exponent (RSA). **Default:** `0x10001`. 38451cb0ef41Sopenharmony_ci * `hashAlgorithm`: {string} Name of the message digest (RSA-PSS). 38461cb0ef41Sopenharmony_ci * `mgf1HashAlgorithm`: {string} Name of the message digest used by 38471cb0ef41Sopenharmony_ci MGF1 (RSA-PSS). 38481cb0ef41Sopenharmony_ci * `saltLength`: {number} Minimal salt length in bytes (RSA-PSS). 38491cb0ef41Sopenharmony_ci * `divisorLength`: {number} Size of `q` in bits (DSA). 38501cb0ef41Sopenharmony_ci * `namedCurve`: {string} Name of the curve to use (EC). 38511cb0ef41Sopenharmony_ci * `prime`: {Buffer} The prime parameter (DH). 38521cb0ef41Sopenharmony_ci * `primeLength`: {number} Prime length in bits (DH). 38531cb0ef41Sopenharmony_ci * `generator`: {number} Custom generator (DH). **Default:** `2`. 38541cb0ef41Sopenharmony_ci * `groupName`: {string} Diffie-Hellman group name (DH). See 38551cb0ef41Sopenharmony_ci [`crypto.getDiffieHellman()`][]. 38561cb0ef41Sopenharmony_ci * `paramEncoding`: {string} Must be `'named'` or `'explicit'` (EC). 38571cb0ef41Sopenharmony_ci **Default:** `'named'`. 38581cb0ef41Sopenharmony_ci * `publicKeyEncoding`: {Object} See [`keyObject.export()`][]. 38591cb0ef41Sopenharmony_ci * `privateKeyEncoding`: {Object} See [`keyObject.export()`][]. 38601cb0ef41Sopenharmony_ci* Returns: {Object} 38611cb0ef41Sopenharmony_ci * `publicKey`: {string | Buffer | KeyObject} 38621cb0ef41Sopenharmony_ci * `privateKey`: {string | Buffer | KeyObject} 38631cb0ef41Sopenharmony_ci 38641cb0ef41Sopenharmony_ciGenerates a new asymmetric key pair of the given `type`. RSA, RSA-PSS, DSA, EC, 38651cb0ef41Sopenharmony_ciEd25519, Ed448, X25519, X448, and DH are currently supported. 38661cb0ef41Sopenharmony_ci 38671cb0ef41Sopenharmony_ciIf a `publicKeyEncoding` or `privateKeyEncoding` was specified, this function 38681cb0ef41Sopenharmony_cibehaves as if [`keyObject.export()`][] had been called on its result. Otherwise, 38691cb0ef41Sopenharmony_cithe respective part of the key is returned as a [`KeyObject`][]. 38701cb0ef41Sopenharmony_ci 38711cb0ef41Sopenharmony_ciWhen encoding public keys, it is recommended to use `'spki'`. When encoding 38721cb0ef41Sopenharmony_ciprivate keys, it is recommended to use `'pkcs8'` with a strong passphrase, 38731cb0ef41Sopenharmony_ciand to keep the passphrase confidential. 38741cb0ef41Sopenharmony_ci 38751cb0ef41Sopenharmony_ci```mjs 38761cb0ef41Sopenharmony_ciconst { 38771cb0ef41Sopenharmony_ci generateKeyPairSync, 38781cb0ef41Sopenharmony_ci} = await import('node:crypto'); 38791cb0ef41Sopenharmony_ci 38801cb0ef41Sopenharmony_ciconst { 38811cb0ef41Sopenharmony_ci publicKey, 38821cb0ef41Sopenharmony_ci privateKey, 38831cb0ef41Sopenharmony_ci} = generateKeyPairSync('rsa', { 38841cb0ef41Sopenharmony_ci modulusLength: 4096, 38851cb0ef41Sopenharmony_ci publicKeyEncoding: { 38861cb0ef41Sopenharmony_ci type: 'spki', 38871cb0ef41Sopenharmony_ci format: 'pem', 38881cb0ef41Sopenharmony_ci }, 38891cb0ef41Sopenharmony_ci privateKeyEncoding: { 38901cb0ef41Sopenharmony_ci type: 'pkcs8', 38911cb0ef41Sopenharmony_ci format: 'pem', 38921cb0ef41Sopenharmony_ci cipher: 'aes-256-cbc', 38931cb0ef41Sopenharmony_ci passphrase: 'top secret', 38941cb0ef41Sopenharmony_ci }, 38951cb0ef41Sopenharmony_ci}); 38961cb0ef41Sopenharmony_ci``` 38971cb0ef41Sopenharmony_ci 38981cb0ef41Sopenharmony_ci```cjs 38991cb0ef41Sopenharmony_ciconst { 39001cb0ef41Sopenharmony_ci generateKeyPairSync, 39011cb0ef41Sopenharmony_ci} = require('node:crypto'); 39021cb0ef41Sopenharmony_ci 39031cb0ef41Sopenharmony_ciconst { 39041cb0ef41Sopenharmony_ci publicKey, 39051cb0ef41Sopenharmony_ci privateKey, 39061cb0ef41Sopenharmony_ci} = generateKeyPairSync('rsa', { 39071cb0ef41Sopenharmony_ci modulusLength: 4096, 39081cb0ef41Sopenharmony_ci publicKeyEncoding: { 39091cb0ef41Sopenharmony_ci type: 'spki', 39101cb0ef41Sopenharmony_ci format: 'pem', 39111cb0ef41Sopenharmony_ci }, 39121cb0ef41Sopenharmony_ci privateKeyEncoding: { 39131cb0ef41Sopenharmony_ci type: 'pkcs8', 39141cb0ef41Sopenharmony_ci format: 'pem', 39151cb0ef41Sopenharmony_ci cipher: 'aes-256-cbc', 39161cb0ef41Sopenharmony_ci passphrase: 'top secret', 39171cb0ef41Sopenharmony_ci }, 39181cb0ef41Sopenharmony_ci}); 39191cb0ef41Sopenharmony_ci``` 39201cb0ef41Sopenharmony_ci 39211cb0ef41Sopenharmony_ciThe return value `{ publicKey, privateKey }` represents the generated key pair. 39221cb0ef41Sopenharmony_ciWhen PEM encoding was selected, the respective key will be a string, otherwise 39231cb0ef41Sopenharmony_ciit will be a buffer containing the data encoded as DER. 39241cb0ef41Sopenharmony_ci 39251cb0ef41Sopenharmony_ci### `crypto.generateKeySync(type, options)` 39261cb0ef41Sopenharmony_ci 39271cb0ef41Sopenharmony_ci<!-- YAML 39281cb0ef41Sopenharmony_ciadded: v15.0.0 39291cb0ef41Sopenharmony_ci--> 39301cb0ef41Sopenharmony_ci 39311cb0ef41Sopenharmony_ci* `type`: {string} The intended use of the generated secret key. Currently 39321cb0ef41Sopenharmony_ci accepted values are `'hmac'` and `'aes'`. 39331cb0ef41Sopenharmony_ci* `options`: {Object} 39341cb0ef41Sopenharmony_ci * `length`: {number} The bit length of the key to generate. 39351cb0ef41Sopenharmony_ci * If `type` is `'hmac'`, the minimum is 8, and the maximum length is 39361cb0ef41Sopenharmony_ci 2<sup>31</sup>-1. If the value is not a multiple of 8, the generated 39371cb0ef41Sopenharmony_ci key will be truncated to `Math.floor(length / 8)`. 39381cb0ef41Sopenharmony_ci * If `type` is `'aes'`, the length must be one of `128`, `192`, or `256`. 39391cb0ef41Sopenharmony_ci* Returns: {KeyObject} 39401cb0ef41Sopenharmony_ci 39411cb0ef41Sopenharmony_ciSynchronously generates a new random secret key of the given `length`. The 39421cb0ef41Sopenharmony_ci`type` will determine which validations will be performed on the `length`. 39431cb0ef41Sopenharmony_ci 39441cb0ef41Sopenharmony_ci```mjs 39451cb0ef41Sopenharmony_ciconst { 39461cb0ef41Sopenharmony_ci generateKeySync, 39471cb0ef41Sopenharmony_ci} = await import('node:crypto'); 39481cb0ef41Sopenharmony_ci 39491cb0ef41Sopenharmony_ciconst key = generateKeySync('hmac', { length: 512 }); 39501cb0ef41Sopenharmony_ciconsole.log(key.export().toString('hex')); // e89..........41e 39511cb0ef41Sopenharmony_ci``` 39521cb0ef41Sopenharmony_ci 39531cb0ef41Sopenharmony_ci```cjs 39541cb0ef41Sopenharmony_ciconst { 39551cb0ef41Sopenharmony_ci generateKeySync, 39561cb0ef41Sopenharmony_ci} = require('node:crypto'); 39571cb0ef41Sopenharmony_ci 39581cb0ef41Sopenharmony_ciconst key = generateKeySync('hmac', { length: 512 }); 39591cb0ef41Sopenharmony_ciconsole.log(key.export().toString('hex')); // e89..........41e 39601cb0ef41Sopenharmony_ci``` 39611cb0ef41Sopenharmony_ci 39621cb0ef41Sopenharmony_ciThe size of a generated HMAC key should not exceed the block size of the 39631cb0ef41Sopenharmony_ciunderlying hash function. See [`crypto.createHmac()`][] for more information. 39641cb0ef41Sopenharmony_ci 39651cb0ef41Sopenharmony_ci### `crypto.generatePrime(size[, options[, callback]])` 39661cb0ef41Sopenharmony_ci 39671cb0ef41Sopenharmony_ci<!-- YAML 39681cb0ef41Sopenharmony_ciadded: v15.8.0 39691cb0ef41Sopenharmony_cichanges: 39701cb0ef41Sopenharmony_ci - version: v18.0.0 39711cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41678 39721cb0ef41Sopenharmony_ci description: Passing an invalid callback to the `callback` argument 39731cb0ef41Sopenharmony_ci now throws `ERR_INVALID_ARG_TYPE` instead of 39741cb0ef41Sopenharmony_ci `ERR_INVALID_CALLBACK`. 39751cb0ef41Sopenharmony_ci--> 39761cb0ef41Sopenharmony_ci 39771cb0ef41Sopenharmony_ci* `size` {number} The size (in bits) of the prime to generate. 39781cb0ef41Sopenharmony_ci* `options` {Object} 39791cb0ef41Sopenharmony_ci * `add` {ArrayBuffer|SharedArrayBuffer|TypedArray|Buffer|DataView|bigint} 39801cb0ef41Sopenharmony_ci * `rem` {ArrayBuffer|SharedArrayBuffer|TypedArray|Buffer|DataView|bigint} 39811cb0ef41Sopenharmony_ci * `safe` {boolean} **Default:** `false`. 39821cb0ef41Sopenharmony_ci * `bigint` {boolean} When `true`, the generated prime is returned 39831cb0ef41Sopenharmony_ci as a `bigint`. 39841cb0ef41Sopenharmony_ci* `callback` {Function} 39851cb0ef41Sopenharmony_ci * `err` {Error} 39861cb0ef41Sopenharmony_ci * `prime` {ArrayBuffer|bigint} 39871cb0ef41Sopenharmony_ci 39881cb0ef41Sopenharmony_ciGenerates a pseudorandom prime of `size` bits. 39891cb0ef41Sopenharmony_ci 39901cb0ef41Sopenharmony_ciIf `options.safe` is `true`, the prime will be a safe prime -- that is, 39911cb0ef41Sopenharmony_ci`(prime - 1) / 2` will also be a prime. 39921cb0ef41Sopenharmony_ci 39931cb0ef41Sopenharmony_ciThe `options.add` and `options.rem` parameters can be used to enforce additional 39941cb0ef41Sopenharmony_cirequirements, e.g., for Diffie-Hellman: 39951cb0ef41Sopenharmony_ci 39961cb0ef41Sopenharmony_ci* If `options.add` and `options.rem` are both set, the prime will satisfy the 39971cb0ef41Sopenharmony_ci condition that `prime % add = rem`. 39981cb0ef41Sopenharmony_ci* If only `options.add` is set and `options.safe` is not `true`, the prime will 39991cb0ef41Sopenharmony_ci satisfy the condition that `prime % add = 1`. 40001cb0ef41Sopenharmony_ci* If only `options.add` is set and `options.safe` is set to `true`, the prime 40011cb0ef41Sopenharmony_ci will instead satisfy the condition that `prime % add = 3`. This is necessary 40021cb0ef41Sopenharmony_ci because `prime % add = 1` for `options.add > 2` would contradict the condition 40031cb0ef41Sopenharmony_ci enforced by `options.safe`. 40041cb0ef41Sopenharmony_ci* `options.rem` is ignored if `options.add` is not given. 40051cb0ef41Sopenharmony_ci 40061cb0ef41Sopenharmony_ciBoth `options.add` and `options.rem` must be encoded as big-endian sequences 40071cb0ef41Sopenharmony_ciif given as an `ArrayBuffer`, `SharedArrayBuffer`, `TypedArray`, `Buffer`, or 40081cb0ef41Sopenharmony_ci`DataView`. 40091cb0ef41Sopenharmony_ci 40101cb0ef41Sopenharmony_ciBy default, the prime is encoded as a big-endian sequence of octets 40111cb0ef41Sopenharmony_ciin an {ArrayBuffer}. If the `bigint` option is `true`, then a {bigint} 40121cb0ef41Sopenharmony_ciis provided. 40131cb0ef41Sopenharmony_ci 40141cb0ef41Sopenharmony_ci### `crypto.generatePrimeSync(size[, options])` 40151cb0ef41Sopenharmony_ci 40161cb0ef41Sopenharmony_ci<!-- YAML 40171cb0ef41Sopenharmony_ciadded: v15.8.0 40181cb0ef41Sopenharmony_ci--> 40191cb0ef41Sopenharmony_ci 40201cb0ef41Sopenharmony_ci* `size` {number} The size (in bits) of the prime to generate. 40211cb0ef41Sopenharmony_ci* `options` {Object} 40221cb0ef41Sopenharmony_ci * `add` {ArrayBuffer|SharedArrayBuffer|TypedArray|Buffer|DataView|bigint} 40231cb0ef41Sopenharmony_ci * `rem` {ArrayBuffer|SharedArrayBuffer|TypedArray|Buffer|DataView|bigint} 40241cb0ef41Sopenharmony_ci * `safe` {boolean} **Default:** `false`. 40251cb0ef41Sopenharmony_ci * `bigint` {boolean} When `true`, the generated prime is returned 40261cb0ef41Sopenharmony_ci as a `bigint`. 40271cb0ef41Sopenharmony_ci* Returns: {ArrayBuffer|bigint} 40281cb0ef41Sopenharmony_ci 40291cb0ef41Sopenharmony_ciGenerates a pseudorandom prime of `size` bits. 40301cb0ef41Sopenharmony_ci 40311cb0ef41Sopenharmony_ciIf `options.safe` is `true`, the prime will be a safe prime -- that is, 40321cb0ef41Sopenharmony_ci`(prime - 1) / 2` will also be a prime. 40331cb0ef41Sopenharmony_ci 40341cb0ef41Sopenharmony_ciThe `options.add` and `options.rem` parameters can be used to enforce additional 40351cb0ef41Sopenharmony_cirequirements, e.g., for Diffie-Hellman: 40361cb0ef41Sopenharmony_ci 40371cb0ef41Sopenharmony_ci* If `options.add` and `options.rem` are both set, the prime will satisfy the 40381cb0ef41Sopenharmony_ci condition that `prime % add = rem`. 40391cb0ef41Sopenharmony_ci* If only `options.add` is set and `options.safe` is not `true`, the prime will 40401cb0ef41Sopenharmony_ci satisfy the condition that `prime % add = 1`. 40411cb0ef41Sopenharmony_ci* If only `options.add` is set and `options.safe` is set to `true`, the prime 40421cb0ef41Sopenharmony_ci will instead satisfy the condition that `prime % add = 3`. This is necessary 40431cb0ef41Sopenharmony_ci because `prime % add = 1` for `options.add > 2` would contradict the condition 40441cb0ef41Sopenharmony_ci enforced by `options.safe`. 40451cb0ef41Sopenharmony_ci* `options.rem` is ignored if `options.add` is not given. 40461cb0ef41Sopenharmony_ci 40471cb0ef41Sopenharmony_ciBoth `options.add` and `options.rem` must be encoded as big-endian sequences 40481cb0ef41Sopenharmony_ciif given as an `ArrayBuffer`, `SharedArrayBuffer`, `TypedArray`, `Buffer`, or 40491cb0ef41Sopenharmony_ci`DataView`. 40501cb0ef41Sopenharmony_ci 40511cb0ef41Sopenharmony_ciBy default, the prime is encoded as a big-endian sequence of octets 40521cb0ef41Sopenharmony_ciin an {ArrayBuffer}. If the `bigint` option is `true`, then a {bigint} 40531cb0ef41Sopenharmony_ciis provided. 40541cb0ef41Sopenharmony_ci 40551cb0ef41Sopenharmony_ci### `crypto.getCipherInfo(nameOrNid[, options])` 40561cb0ef41Sopenharmony_ci 40571cb0ef41Sopenharmony_ci<!-- YAML 40581cb0ef41Sopenharmony_ciadded: v15.0.0 40591cb0ef41Sopenharmony_ci--> 40601cb0ef41Sopenharmony_ci 40611cb0ef41Sopenharmony_ci* `nameOrNid`: {string|number} The name or nid of the cipher to query. 40621cb0ef41Sopenharmony_ci* `options`: {Object} 40631cb0ef41Sopenharmony_ci * `keyLength`: {number} A test key length. 40641cb0ef41Sopenharmony_ci * `ivLength`: {number} A test IV length. 40651cb0ef41Sopenharmony_ci* Returns: {Object} 40661cb0ef41Sopenharmony_ci * `name` {string} The name of the cipher 40671cb0ef41Sopenharmony_ci * `nid` {number} The nid of the cipher 40681cb0ef41Sopenharmony_ci * `blockSize` {number} The block size of the cipher in bytes. This property 40691cb0ef41Sopenharmony_ci is omitted when `mode` is `'stream'`. 40701cb0ef41Sopenharmony_ci * `ivLength` {number} The expected or default initialization vector length in 40711cb0ef41Sopenharmony_ci bytes. This property is omitted if the cipher does not use an initialization 40721cb0ef41Sopenharmony_ci vector. 40731cb0ef41Sopenharmony_ci * `keyLength` {number} The expected or default key length in bytes. 40741cb0ef41Sopenharmony_ci * `mode` {string} The cipher mode. One of `'cbc'`, `'ccm'`, `'cfb'`, `'ctr'`, 40751cb0ef41Sopenharmony_ci `'ecb'`, `'gcm'`, `'ocb'`, `'ofb'`, `'stream'`, `'wrap'`, `'xts'`. 40761cb0ef41Sopenharmony_ci 40771cb0ef41Sopenharmony_ciReturns information about a given cipher. 40781cb0ef41Sopenharmony_ci 40791cb0ef41Sopenharmony_ciSome ciphers accept variable length keys and initialization vectors. By default, 40801cb0ef41Sopenharmony_cithe `crypto.getCipherInfo()` method will return the default values for these 40811cb0ef41Sopenharmony_ciciphers. To test if a given key length or iv length is acceptable for given 40821cb0ef41Sopenharmony_cicipher, use the `keyLength` and `ivLength` options. If the given values are 40831cb0ef41Sopenharmony_ciunacceptable, `undefined` will be returned. 40841cb0ef41Sopenharmony_ci 40851cb0ef41Sopenharmony_ci### `crypto.getCiphers()` 40861cb0ef41Sopenharmony_ci 40871cb0ef41Sopenharmony_ci<!-- YAML 40881cb0ef41Sopenharmony_ciadded: v0.9.3 40891cb0ef41Sopenharmony_ci--> 40901cb0ef41Sopenharmony_ci 40911cb0ef41Sopenharmony_ci* Returns: {string\[]} An array with the names of the supported cipher 40921cb0ef41Sopenharmony_ci algorithms. 40931cb0ef41Sopenharmony_ci 40941cb0ef41Sopenharmony_ci```mjs 40951cb0ef41Sopenharmony_ciconst { 40961cb0ef41Sopenharmony_ci getCiphers, 40971cb0ef41Sopenharmony_ci} = await import('node:crypto'); 40981cb0ef41Sopenharmony_ci 40991cb0ef41Sopenharmony_ciconsole.log(getCiphers()); // ['aes-128-cbc', 'aes-128-ccm', ...] 41001cb0ef41Sopenharmony_ci``` 41011cb0ef41Sopenharmony_ci 41021cb0ef41Sopenharmony_ci```cjs 41031cb0ef41Sopenharmony_ciconst { 41041cb0ef41Sopenharmony_ci getCiphers, 41051cb0ef41Sopenharmony_ci} = require('node:crypto'); 41061cb0ef41Sopenharmony_ci 41071cb0ef41Sopenharmony_ciconsole.log(getCiphers()); // ['aes-128-cbc', 'aes-128-ccm', ...] 41081cb0ef41Sopenharmony_ci``` 41091cb0ef41Sopenharmony_ci 41101cb0ef41Sopenharmony_ci### `crypto.getCurves()` 41111cb0ef41Sopenharmony_ci 41121cb0ef41Sopenharmony_ci<!-- YAML 41131cb0ef41Sopenharmony_ciadded: v2.3.0 41141cb0ef41Sopenharmony_ci--> 41151cb0ef41Sopenharmony_ci 41161cb0ef41Sopenharmony_ci* Returns: {string\[]} An array with the names of the supported elliptic curves. 41171cb0ef41Sopenharmony_ci 41181cb0ef41Sopenharmony_ci```mjs 41191cb0ef41Sopenharmony_ciconst { 41201cb0ef41Sopenharmony_ci getCurves, 41211cb0ef41Sopenharmony_ci} = await import('node:crypto'); 41221cb0ef41Sopenharmony_ci 41231cb0ef41Sopenharmony_ciconsole.log(getCurves()); // ['Oakley-EC2N-3', 'Oakley-EC2N-4', ...] 41241cb0ef41Sopenharmony_ci``` 41251cb0ef41Sopenharmony_ci 41261cb0ef41Sopenharmony_ci```cjs 41271cb0ef41Sopenharmony_ciconst { 41281cb0ef41Sopenharmony_ci getCurves, 41291cb0ef41Sopenharmony_ci} = require('node:crypto'); 41301cb0ef41Sopenharmony_ci 41311cb0ef41Sopenharmony_ciconsole.log(getCurves()); // ['Oakley-EC2N-3', 'Oakley-EC2N-4', ...] 41321cb0ef41Sopenharmony_ci``` 41331cb0ef41Sopenharmony_ci 41341cb0ef41Sopenharmony_ci### `crypto.getDiffieHellman(groupName)` 41351cb0ef41Sopenharmony_ci 41361cb0ef41Sopenharmony_ci<!-- YAML 41371cb0ef41Sopenharmony_ciadded: v0.7.5 41381cb0ef41Sopenharmony_ci--> 41391cb0ef41Sopenharmony_ci 41401cb0ef41Sopenharmony_ci* `groupName` {string} 41411cb0ef41Sopenharmony_ci* Returns: {DiffieHellmanGroup} 41421cb0ef41Sopenharmony_ci 41431cb0ef41Sopenharmony_ciCreates a predefined `DiffieHellmanGroup` key exchange object. The 41441cb0ef41Sopenharmony_cisupported groups are listed in the documentation for [`DiffieHellmanGroup`][]. 41451cb0ef41Sopenharmony_ci 41461cb0ef41Sopenharmony_ciThe returned object mimics the interface of objects created by 41471cb0ef41Sopenharmony_ci[`crypto.createDiffieHellman()`][], but will not allow changing 41481cb0ef41Sopenharmony_cithe keys (with [`diffieHellman.setPublicKey()`][], for example). The 41491cb0ef41Sopenharmony_ciadvantage of using this method is that the parties do not have to 41501cb0ef41Sopenharmony_cigenerate nor exchange a group modulus beforehand, saving both processor 41511cb0ef41Sopenharmony_ciand communication time. 41521cb0ef41Sopenharmony_ci 41531cb0ef41Sopenharmony_ciExample (obtaining a shared secret): 41541cb0ef41Sopenharmony_ci 41551cb0ef41Sopenharmony_ci```mjs 41561cb0ef41Sopenharmony_ciconst { 41571cb0ef41Sopenharmony_ci getDiffieHellman, 41581cb0ef41Sopenharmony_ci} = await import('node:crypto'); 41591cb0ef41Sopenharmony_ciconst alice = getDiffieHellman('modp14'); 41601cb0ef41Sopenharmony_ciconst bob = getDiffieHellman('modp14'); 41611cb0ef41Sopenharmony_ci 41621cb0ef41Sopenharmony_cialice.generateKeys(); 41631cb0ef41Sopenharmony_cibob.generateKeys(); 41641cb0ef41Sopenharmony_ci 41651cb0ef41Sopenharmony_ciconst aliceSecret = alice.computeSecret(bob.getPublicKey(), null, 'hex'); 41661cb0ef41Sopenharmony_ciconst bobSecret = bob.computeSecret(alice.getPublicKey(), null, 'hex'); 41671cb0ef41Sopenharmony_ci 41681cb0ef41Sopenharmony_ci/* aliceSecret and bobSecret should be the same */ 41691cb0ef41Sopenharmony_ciconsole.log(aliceSecret === bobSecret); 41701cb0ef41Sopenharmony_ci``` 41711cb0ef41Sopenharmony_ci 41721cb0ef41Sopenharmony_ci```cjs 41731cb0ef41Sopenharmony_ciconst { 41741cb0ef41Sopenharmony_ci getDiffieHellman, 41751cb0ef41Sopenharmony_ci} = require('node:crypto'); 41761cb0ef41Sopenharmony_ci 41771cb0ef41Sopenharmony_ciconst alice = getDiffieHellman('modp14'); 41781cb0ef41Sopenharmony_ciconst bob = getDiffieHellman('modp14'); 41791cb0ef41Sopenharmony_ci 41801cb0ef41Sopenharmony_cialice.generateKeys(); 41811cb0ef41Sopenharmony_cibob.generateKeys(); 41821cb0ef41Sopenharmony_ci 41831cb0ef41Sopenharmony_ciconst aliceSecret = alice.computeSecret(bob.getPublicKey(), null, 'hex'); 41841cb0ef41Sopenharmony_ciconst bobSecret = bob.computeSecret(alice.getPublicKey(), null, 'hex'); 41851cb0ef41Sopenharmony_ci 41861cb0ef41Sopenharmony_ci/* aliceSecret and bobSecret should be the same */ 41871cb0ef41Sopenharmony_ciconsole.log(aliceSecret === bobSecret); 41881cb0ef41Sopenharmony_ci``` 41891cb0ef41Sopenharmony_ci 41901cb0ef41Sopenharmony_ci### `crypto.getFips()` 41911cb0ef41Sopenharmony_ci 41921cb0ef41Sopenharmony_ci<!-- YAML 41931cb0ef41Sopenharmony_ciadded: v10.0.0 41941cb0ef41Sopenharmony_ci--> 41951cb0ef41Sopenharmony_ci 41961cb0ef41Sopenharmony_ci* Returns: {number} `1` if and only if a FIPS compliant crypto provider is 41971cb0ef41Sopenharmony_ci currently in use, `0` otherwise. A future semver-major release may change 41981cb0ef41Sopenharmony_ci the return type of this API to a {boolean}. 41991cb0ef41Sopenharmony_ci 42001cb0ef41Sopenharmony_ci### `crypto.getHashes()` 42011cb0ef41Sopenharmony_ci 42021cb0ef41Sopenharmony_ci<!-- YAML 42031cb0ef41Sopenharmony_ciadded: v0.9.3 42041cb0ef41Sopenharmony_ci--> 42051cb0ef41Sopenharmony_ci 42061cb0ef41Sopenharmony_ci* Returns: {string\[]} An array of the names of the supported hash algorithms, 42071cb0ef41Sopenharmony_ci such as `'RSA-SHA256'`. Hash algorithms are also called "digest" algorithms. 42081cb0ef41Sopenharmony_ci 42091cb0ef41Sopenharmony_ci```mjs 42101cb0ef41Sopenharmony_ciconst { 42111cb0ef41Sopenharmony_ci getHashes, 42121cb0ef41Sopenharmony_ci} = await import('node:crypto'); 42131cb0ef41Sopenharmony_ci 42141cb0ef41Sopenharmony_ciconsole.log(getHashes()); // ['DSA', 'DSA-SHA', 'DSA-SHA1', ...] 42151cb0ef41Sopenharmony_ci``` 42161cb0ef41Sopenharmony_ci 42171cb0ef41Sopenharmony_ci```cjs 42181cb0ef41Sopenharmony_ciconst { 42191cb0ef41Sopenharmony_ci getHashes, 42201cb0ef41Sopenharmony_ci} = require('node:crypto'); 42211cb0ef41Sopenharmony_ci 42221cb0ef41Sopenharmony_ciconsole.log(getHashes()); // ['DSA', 'DSA-SHA', 'DSA-SHA1', ...] 42231cb0ef41Sopenharmony_ci``` 42241cb0ef41Sopenharmony_ci 42251cb0ef41Sopenharmony_ci### `crypto.getRandomValues(typedArray)` 42261cb0ef41Sopenharmony_ci 42271cb0ef41Sopenharmony_ci<!-- YAML 42281cb0ef41Sopenharmony_ciadded: v17.4.0 42291cb0ef41Sopenharmony_ci--> 42301cb0ef41Sopenharmony_ci 42311cb0ef41Sopenharmony_ci* `typedArray` {Buffer|TypedArray|DataView|ArrayBuffer} 42321cb0ef41Sopenharmony_ci* Returns: {Buffer|TypedArray|DataView|ArrayBuffer} Returns `typedArray`. 42331cb0ef41Sopenharmony_ci 42341cb0ef41Sopenharmony_ciA convenient alias for [`crypto.webcrypto.getRandomValues()`][]. This 42351cb0ef41Sopenharmony_ciimplementation is not compliant with the Web Crypto spec, to write 42361cb0ef41Sopenharmony_ciweb-compatible code use [`crypto.webcrypto.getRandomValues()`][] instead. 42371cb0ef41Sopenharmony_ci 42381cb0ef41Sopenharmony_ci### `crypto.hkdf(digest, ikm, salt, info, keylen, callback)` 42391cb0ef41Sopenharmony_ci 42401cb0ef41Sopenharmony_ci<!-- YAML 42411cb0ef41Sopenharmony_ciadded: v15.0.0 42421cb0ef41Sopenharmony_cichanges: 42431cb0ef41Sopenharmony_ci - version: v18.8.0 42441cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/44201 42451cb0ef41Sopenharmony_ci description: The input keying material can now be zero-length. 42461cb0ef41Sopenharmony_ci - version: v18.0.0 42471cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41678 42481cb0ef41Sopenharmony_ci description: Passing an invalid callback to the `callback` argument 42491cb0ef41Sopenharmony_ci now throws `ERR_INVALID_ARG_TYPE` instead of 42501cb0ef41Sopenharmony_ci `ERR_INVALID_CALLBACK`. 42511cb0ef41Sopenharmony_ci--> 42521cb0ef41Sopenharmony_ci 42531cb0ef41Sopenharmony_ci* `digest` {string} The digest algorithm to use. 42541cb0ef41Sopenharmony_ci* `ikm` {string|ArrayBuffer|Buffer|TypedArray|DataView|KeyObject} The input 42551cb0ef41Sopenharmony_ci keying material. Must be provided but can be zero-length. 42561cb0ef41Sopenharmony_ci* `salt` {string|ArrayBuffer|Buffer|TypedArray|DataView} The salt value. Must 42571cb0ef41Sopenharmony_ci be provided but can be zero-length. 42581cb0ef41Sopenharmony_ci* `info` {string|ArrayBuffer|Buffer|TypedArray|DataView} Additional info value. 42591cb0ef41Sopenharmony_ci Must be provided but can be zero-length, and cannot be more than 1024 bytes. 42601cb0ef41Sopenharmony_ci* `keylen` {number} The length of the key to generate. Must be greater than 0. 42611cb0ef41Sopenharmony_ci The maximum allowable value is `255` times the number of bytes produced by 42621cb0ef41Sopenharmony_ci the selected digest function (e.g. `sha512` generates 64-byte hashes, making 42631cb0ef41Sopenharmony_ci the maximum HKDF output 16320 bytes). 42641cb0ef41Sopenharmony_ci* `callback` {Function} 42651cb0ef41Sopenharmony_ci * `err` {Error} 42661cb0ef41Sopenharmony_ci * `derivedKey` {ArrayBuffer} 42671cb0ef41Sopenharmony_ci 42681cb0ef41Sopenharmony_ciHKDF is a simple key derivation function defined in RFC 5869. The given `ikm`, 42691cb0ef41Sopenharmony_ci`salt` and `info` are used with the `digest` to derive a key of `keylen` bytes. 42701cb0ef41Sopenharmony_ci 42711cb0ef41Sopenharmony_ciThe supplied `callback` function is called with two arguments: `err` and 42721cb0ef41Sopenharmony_ci`derivedKey`. If an errors occurs while deriving the key, `err` will be set; 42731cb0ef41Sopenharmony_ciotherwise `err` will be `null`. The successfully generated `derivedKey` will 42741cb0ef41Sopenharmony_cibe passed to the callback as an {ArrayBuffer}. An error will be thrown if any 42751cb0ef41Sopenharmony_ciof the input arguments specify invalid values or types. 42761cb0ef41Sopenharmony_ci 42771cb0ef41Sopenharmony_ci```mjs 42781cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer'; 42791cb0ef41Sopenharmony_ciconst { 42801cb0ef41Sopenharmony_ci hkdf, 42811cb0ef41Sopenharmony_ci} = await import('node:crypto'); 42821cb0ef41Sopenharmony_ci 42831cb0ef41Sopenharmony_cihkdf('sha512', 'key', 'salt', 'info', 64, (err, derivedKey) => { 42841cb0ef41Sopenharmony_ci if (err) throw err; 42851cb0ef41Sopenharmony_ci console.log(Buffer.from(derivedKey).toString('hex')); // '24156e2...5391653' 42861cb0ef41Sopenharmony_ci}); 42871cb0ef41Sopenharmony_ci``` 42881cb0ef41Sopenharmony_ci 42891cb0ef41Sopenharmony_ci```cjs 42901cb0ef41Sopenharmony_ciconst { 42911cb0ef41Sopenharmony_ci hkdf, 42921cb0ef41Sopenharmony_ci} = require('node:crypto'); 42931cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer'); 42941cb0ef41Sopenharmony_ci 42951cb0ef41Sopenharmony_cihkdf('sha512', 'key', 'salt', 'info', 64, (err, derivedKey) => { 42961cb0ef41Sopenharmony_ci if (err) throw err; 42971cb0ef41Sopenharmony_ci console.log(Buffer.from(derivedKey).toString('hex')); // '24156e2...5391653' 42981cb0ef41Sopenharmony_ci}); 42991cb0ef41Sopenharmony_ci``` 43001cb0ef41Sopenharmony_ci 43011cb0ef41Sopenharmony_ci### `crypto.hkdfSync(digest, ikm, salt, info, keylen)` 43021cb0ef41Sopenharmony_ci 43031cb0ef41Sopenharmony_ci<!-- YAML 43041cb0ef41Sopenharmony_ciadded: v15.0.0 43051cb0ef41Sopenharmony_cichanges: 43061cb0ef41Sopenharmony_ci - version: v18.8.0 43071cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/44201 43081cb0ef41Sopenharmony_ci description: The input keying material can now be zero-length. 43091cb0ef41Sopenharmony_ci--> 43101cb0ef41Sopenharmony_ci 43111cb0ef41Sopenharmony_ci* `digest` {string} The digest algorithm to use. 43121cb0ef41Sopenharmony_ci* `ikm` {string|ArrayBuffer|Buffer|TypedArray|DataView|KeyObject} The input 43131cb0ef41Sopenharmony_ci keying material. Must be provided but can be zero-length. 43141cb0ef41Sopenharmony_ci* `salt` {string|ArrayBuffer|Buffer|TypedArray|DataView} The salt value. Must 43151cb0ef41Sopenharmony_ci be provided but can be zero-length. 43161cb0ef41Sopenharmony_ci* `info` {string|ArrayBuffer|Buffer|TypedArray|DataView} Additional info value. 43171cb0ef41Sopenharmony_ci Must be provided but can be zero-length, and cannot be more than 1024 bytes. 43181cb0ef41Sopenharmony_ci* `keylen` {number} The length of the key to generate. Must be greater than 0. 43191cb0ef41Sopenharmony_ci The maximum allowable value is `255` times the number of bytes produced by 43201cb0ef41Sopenharmony_ci the selected digest function (e.g. `sha512` generates 64-byte hashes, making 43211cb0ef41Sopenharmony_ci the maximum HKDF output 16320 bytes). 43221cb0ef41Sopenharmony_ci* Returns: {ArrayBuffer} 43231cb0ef41Sopenharmony_ci 43241cb0ef41Sopenharmony_ciProvides a synchronous HKDF key derivation function as defined in RFC 5869. The 43251cb0ef41Sopenharmony_cigiven `ikm`, `salt` and `info` are used with the `digest` to derive a key of 43261cb0ef41Sopenharmony_ci`keylen` bytes. 43271cb0ef41Sopenharmony_ci 43281cb0ef41Sopenharmony_ciThe successfully generated `derivedKey` will be returned as an {ArrayBuffer}. 43291cb0ef41Sopenharmony_ci 43301cb0ef41Sopenharmony_ciAn error will be thrown if any of the input arguments specify invalid values or 43311cb0ef41Sopenharmony_citypes, or if the derived key cannot be generated. 43321cb0ef41Sopenharmony_ci 43331cb0ef41Sopenharmony_ci```mjs 43341cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer'; 43351cb0ef41Sopenharmony_ciconst { 43361cb0ef41Sopenharmony_ci hkdfSync, 43371cb0ef41Sopenharmony_ci} = await import('node:crypto'); 43381cb0ef41Sopenharmony_ci 43391cb0ef41Sopenharmony_ciconst derivedKey = hkdfSync('sha512', 'key', 'salt', 'info', 64); 43401cb0ef41Sopenharmony_ciconsole.log(Buffer.from(derivedKey).toString('hex')); // '24156e2...5391653' 43411cb0ef41Sopenharmony_ci``` 43421cb0ef41Sopenharmony_ci 43431cb0ef41Sopenharmony_ci```cjs 43441cb0ef41Sopenharmony_ciconst { 43451cb0ef41Sopenharmony_ci hkdfSync, 43461cb0ef41Sopenharmony_ci} = require('node:crypto'); 43471cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer'); 43481cb0ef41Sopenharmony_ci 43491cb0ef41Sopenharmony_ciconst derivedKey = hkdfSync('sha512', 'key', 'salt', 'info', 64); 43501cb0ef41Sopenharmony_ciconsole.log(Buffer.from(derivedKey).toString('hex')); // '24156e2...5391653' 43511cb0ef41Sopenharmony_ci``` 43521cb0ef41Sopenharmony_ci 43531cb0ef41Sopenharmony_ci### `crypto.pbkdf2(password, salt, iterations, keylen, digest, callback)` 43541cb0ef41Sopenharmony_ci 43551cb0ef41Sopenharmony_ci<!-- YAML 43561cb0ef41Sopenharmony_ciadded: v0.5.5 43571cb0ef41Sopenharmony_cichanges: 43581cb0ef41Sopenharmony_ci - version: v18.0.0 43591cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41678 43601cb0ef41Sopenharmony_ci description: Passing an invalid callback to the `callback` argument 43611cb0ef41Sopenharmony_ci now throws `ERR_INVALID_ARG_TYPE` instead of 43621cb0ef41Sopenharmony_ci `ERR_INVALID_CALLBACK`. 43631cb0ef41Sopenharmony_ci - version: v15.0.0 43641cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/35093 43651cb0ef41Sopenharmony_ci description: The password and salt arguments can also be ArrayBuffer 43661cb0ef41Sopenharmony_ci instances. 43671cb0ef41Sopenharmony_ci - version: v14.0.0 43681cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/30578 43691cb0ef41Sopenharmony_ci description: The `iterations` parameter is now restricted to positive 43701cb0ef41Sopenharmony_ci values. Earlier releases treated other values as one. 43711cb0ef41Sopenharmony_ci - version: v8.0.0 43721cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/11305 43731cb0ef41Sopenharmony_ci description: The `digest` parameter is always required now. 43741cb0ef41Sopenharmony_ci - version: v6.0.0 43751cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/4047 43761cb0ef41Sopenharmony_ci description: Calling this function without passing the `digest` parameter 43771cb0ef41Sopenharmony_ci is deprecated now and will emit a warning. 43781cb0ef41Sopenharmony_ci - version: v6.0.0 43791cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/5522 43801cb0ef41Sopenharmony_ci description: The default encoding for `password` if it is a string changed 43811cb0ef41Sopenharmony_ci from `binary` to `utf8`. 43821cb0ef41Sopenharmony_ci--> 43831cb0ef41Sopenharmony_ci 43841cb0ef41Sopenharmony_ci* `password` {string|ArrayBuffer|Buffer|TypedArray|DataView} 43851cb0ef41Sopenharmony_ci* `salt` {string|ArrayBuffer|Buffer|TypedArray|DataView} 43861cb0ef41Sopenharmony_ci* `iterations` {number} 43871cb0ef41Sopenharmony_ci* `keylen` {number} 43881cb0ef41Sopenharmony_ci* `digest` {string} 43891cb0ef41Sopenharmony_ci* `callback` {Function} 43901cb0ef41Sopenharmony_ci * `err` {Error} 43911cb0ef41Sopenharmony_ci * `derivedKey` {Buffer} 43921cb0ef41Sopenharmony_ci 43931cb0ef41Sopenharmony_ciProvides an asynchronous Password-Based Key Derivation Function 2 (PBKDF2) 43941cb0ef41Sopenharmony_ciimplementation. A selected HMAC digest algorithm specified by `digest` is 43951cb0ef41Sopenharmony_ciapplied to derive a key of the requested byte length (`keylen`) from the 43961cb0ef41Sopenharmony_ci`password`, `salt` and `iterations`. 43971cb0ef41Sopenharmony_ci 43981cb0ef41Sopenharmony_ciThe supplied `callback` function is called with two arguments: `err` and 43991cb0ef41Sopenharmony_ci`derivedKey`. If an error occurs while deriving the key, `err` will be set; 44001cb0ef41Sopenharmony_ciotherwise `err` will be `null`. By default, the successfully generated 44011cb0ef41Sopenharmony_ci`derivedKey` will be passed to the callback as a [`Buffer`][]. An error will be 44021cb0ef41Sopenharmony_cithrown if any of the input arguments specify invalid values or types. 44031cb0ef41Sopenharmony_ci 44041cb0ef41Sopenharmony_ciThe `iterations` argument must be a number set as high as possible. The 44051cb0ef41Sopenharmony_cihigher the number of iterations, the more secure the derived key will be, 44061cb0ef41Sopenharmony_cibut will take a longer amount of time to complete. 44071cb0ef41Sopenharmony_ci 44081cb0ef41Sopenharmony_ciThe `salt` should be as unique as possible. It is recommended that a salt is 44091cb0ef41Sopenharmony_cirandom and at least 16 bytes long. See [NIST SP 800-132][] for details. 44101cb0ef41Sopenharmony_ci 44111cb0ef41Sopenharmony_ciWhen passing strings for `password` or `salt`, please consider 44121cb0ef41Sopenharmony_ci[caveats when using strings as inputs to cryptographic APIs][]. 44131cb0ef41Sopenharmony_ci 44141cb0ef41Sopenharmony_ci```mjs 44151cb0ef41Sopenharmony_ciconst { 44161cb0ef41Sopenharmony_ci pbkdf2, 44171cb0ef41Sopenharmony_ci} = await import('node:crypto'); 44181cb0ef41Sopenharmony_ci 44191cb0ef41Sopenharmony_cipbkdf2('secret', 'salt', 100000, 64, 'sha512', (err, derivedKey) => { 44201cb0ef41Sopenharmony_ci if (err) throw err; 44211cb0ef41Sopenharmony_ci console.log(derivedKey.toString('hex')); // '3745e48...08d59ae' 44221cb0ef41Sopenharmony_ci}); 44231cb0ef41Sopenharmony_ci``` 44241cb0ef41Sopenharmony_ci 44251cb0ef41Sopenharmony_ci```cjs 44261cb0ef41Sopenharmony_ciconst { 44271cb0ef41Sopenharmony_ci pbkdf2, 44281cb0ef41Sopenharmony_ci} = require('node:crypto'); 44291cb0ef41Sopenharmony_ci 44301cb0ef41Sopenharmony_cipbkdf2('secret', 'salt', 100000, 64, 'sha512', (err, derivedKey) => { 44311cb0ef41Sopenharmony_ci if (err) throw err; 44321cb0ef41Sopenharmony_ci console.log(derivedKey.toString('hex')); // '3745e48...08d59ae' 44331cb0ef41Sopenharmony_ci}); 44341cb0ef41Sopenharmony_ci``` 44351cb0ef41Sopenharmony_ci 44361cb0ef41Sopenharmony_ciAn array of supported digest functions can be retrieved using 44371cb0ef41Sopenharmony_ci[`crypto.getHashes()`][]. 44381cb0ef41Sopenharmony_ci 44391cb0ef41Sopenharmony_ciThis API uses libuv's threadpool, which can have surprising and 44401cb0ef41Sopenharmony_cinegative performance implications for some applications; see the 44411cb0ef41Sopenharmony_ci[`UV_THREADPOOL_SIZE`][] documentation for more information. 44421cb0ef41Sopenharmony_ci 44431cb0ef41Sopenharmony_ci### `crypto.pbkdf2Sync(password, salt, iterations, keylen, digest)` 44441cb0ef41Sopenharmony_ci 44451cb0ef41Sopenharmony_ci<!-- YAML 44461cb0ef41Sopenharmony_ciadded: v0.9.3 44471cb0ef41Sopenharmony_cichanges: 44481cb0ef41Sopenharmony_ci - version: v14.0.0 44491cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/30578 44501cb0ef41Sopenharmony_ci description: The `iterations` parameter is now restricted to positive 44511cb0ef41Sopenharmony_ci values. Earlier releases treated other values as one. 44521cb0ef41Sopenharmony_ci - version: v6.0.0 44531cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/4047 44541cb0ef41Sopenharmony_ci description: Calling this function without passing the `digest` parameter 44551cb0ef41Sopenharmony_ci is deprecated now and will emit a warning. 44561cb0ef41Sopenharmony_ci - version: v6.0.0 44571cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/5522 44581cb0ef41Sopenharmony_ci description: The default encoding for `password` if it is a string changed 44591cb0ef41Sopenharmony_ci from `binary` to `utf8`. 44601cb0ef41Sopenharmony_ci--> 44611cb0ef41Sopenharmony_ci 44621cb0ef41Sopenharmony_ci* `password` {string|Buffer|TypedArray|DataView} 44631cb0ef41Sopenharmony_ci* `salt` {string|Buffer|TypedArray|DataView} 44641cb0ef41Sopenharmony_ci* `iterations` {number} 44651cb0ef41Sopenharmony_ci* `keylen` {number} 44661cb0ef41Sopenharmony_ci* `digest` {string} 44671cb0ef41Sopenharmony_ci* Returns: {Buffer} 44681cb0ef41Sopenharmony_ci 44691cb0ef41Sopenharmony_ciProvides a synchronous Password-Based Key Derivation Function 2 (PBKDF2) 44701cb0ef41Sopenharmony_ciimplementation. A selected HMAC digest algorithm specified by `digest` is 44711cb0ef41Sopenharmony_ciapplied to derive a key of the requested byte length (`keylen`) from the 44721cb0ef41Sopenharmony_ci`password`, `salt` and `iterations`. 44731cb0ef41Sopenharmony_ci 44741cb0ef41Sopenharmony_ciIf an error occurs an `Error` will be thrown, otherwise the derived key will be 44751cb0ef41Sopenharmony_cireturned as a [`Buffer`][]. 44761cb0ef41Sopenharmony_ci 44771cb0ef41Sopenharmony_ciThe `iterations` argument must be a number set as high as possible. The 44781cb0ef41Sopenharmony_cihigher the number of iterations, the more secure the derived key will be, 44791cb0ef41Sopenharmony_cibut will take a longer amount of time to complete. 44801cb0ef41Sopenharmony_ci 44811cb0ef41Sopenharmony_ciThe `salt` should be as unique as possible. It is recommended that a salt is 44821cb0ef41Sopenharmony_cirandom and at least 16 bytes long. See [NIST SP 800-132][] for details. 44831cb0ef41Sopenharmony_ci 44841cb0ef41Sopenharmony_ciWhen passing strings for `password` or `salt`, please consider 44851cb0ef41Sopenharmony_ci[caveats when using strings as inputs to cryptographic APIs][]. 44861cb0ef41Sopenharmony_ci 44871cb0ef41Sopenharmony_ci```mjs 44881cb0ef41Sopenharmony_ciconst { 44891cb0ef41Sopenharmony_ci pbkdf2Sync, 44901cb0ef41Sopenharmony_ci} = await import('node:crypto'); 44911cb0ef41Sopenharmony_ci 44921cb0ef41Sopenharmony_ciconst key = pbkdf2Sync('secret', 'salt', 100000, 64, 'sha512'); 44931cb0ef41Sopenharmony_ciconsole.log(key.toString('hex')); // '3745e48...08d59ae' 44941cb0ef41Sopenharmony_ci``` 44951cb0ef41Sopenharmony_ci 44961cb0ef41Sopenharmony_ci```cjs 44971cb0ef41Sopenharmony_ciconst { 44981cb0ef41Sopenharmony_ci pbkdf2Sync, 44991cb0ef41Sopenharmony_ci} = require('node:crypto'); 45001cb0ef41Sopenharmony_ci 45011cb0ef41Sopenharmony_ciconst key = pbkdf2Sync('secret', 'salt', 100000, 64, 'sha512'); 45021cb0ef41Sopenharmony_ciconsole.log(key.toString('hex')); // '3745e48...08d59ae' 45031cb0ef41Sopenharmony_ci``` 45041cb0ef41Sopenharmony_ci 45051cb0ef41Sopenharmony_ciAn array of supported digest functions can be retrieved using 45061cb0ef41Sopenharmony_ci[`crypto.getHashes()`][]. 45071cb0ef41Sopenharmony_ci 45081cb0ef41Sopenharmony_ci### `crypto.privateDecrypt(privateKey, buffer)` 45091cb0ef41Sopenharmony_ci 45101cb0ef41Sopenharmony_ci<!-- YAML 45111cb0ef41Sopenharmony_ciadded: v0.11.14 45121cb0ef41Sopenharmony_cichanges: 45131cb0ef41Sopenharmony_ci - version: 45141cb0ef41Sopenharmony_ci - v21.6.2 45151cb0ef41Sopenharmony_ci - v20.11.1 45161cb0ef41Sopenharmony_ci - v18.19.1 45171cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs-private/node-private/pull/515 45181cb0ef41Sopenharmony_ci description: The `RSA_PKCS1_PADDING` padding was disabled unless the 45191cb0ef41Sopenharmony_ci OpenSSL build supports implicit rejection. 45201cb0ef41Sopenharmony_ci - version: v15.0.0 45211cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/35093 45221cb0ef41Sopenharmony_ci description: Added string, ArrayBuffer, and CryptoKey as allowable key 45231cb0ef41Sopenharmony_ci types. The oaepLabel can be an ArrayBuffer. The buffer can 45241cb0ef41Sopenharmony_ci be a string or ArrayBuffer. All types that accept buffers 45251cb0ef41Sopenharmony_ci are limited to a maximum of 2 ** 31 - 1 bytes. 45261cb0ef41Sopenharmony_ci - version: v12.11.0 45271cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/29489 45281cb0ef41Sopenharmony_ci description: The `oaepLabel` option was added. 45291cb0ef41Sopenharmony_ci - version: v12.9.0 45301cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/28335 45311cb0ef41Sopenharmony_ci description: The `oaepHash` option was added. 45321cb0ef41Sopenharmony_ci - version: v11.6.0 45331cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/24234 45341cb0ef41Sopenharmony_ci description: This function now supports key objects. 45351cb0ef41Sopenharmony_ci--> 45361cb0ef41Sopenharmony_ci 45371cb0ef41Sopenharmony_ci<!--lint disable maximum-line-length remark-lint--> 45381cb0ef41Sopenharmony_ci 45391cb0ef41Sopenharmony_ci* `privateKey` {Object|string|ArrayBuffer|Buffer|TypedArray|DataView|KeyObject|CryptoKey} 45401cb0ef41Sopenharmony_ci * `oaepHash` {string} The hash function to use for OAEP padding and MGF1. 45411cb0ef41Sopenharmony_ci **Default:** `'sha1'` 45421cb0ef41Sopenharmony_ci * `oaepLabel` {string|ArrayBuffer|Buffer|TypedArray|DataView} The label to 45431cb0ef41Sopenharmony_ci use for OAEP padding. If not specified, no label is used. 45441cb0ef41Sopenharmony_ci * `padding` {crypto.constants} An optional padding value defined in 45451cb0ef41Sopenharmony_ci `crypto.constants`, which may be: `crypto.constants.RSA_NO_PADDING`, 45461cb0ef41Sopenharmony_ci `crypto.constants.RSA_PKCS1_PADDING`, or 45471cb0ef41Sopenharmony_ci `crypto.constants.RSA_PKCS1_OAEP_PADDING`. 45481cb0ef41Sopenharmony_ci* `buffer` {string|ArrayBuffer|Buffer|TypedArray|DataView} 45491cb0ef41Sopenharmony_ci* Returns: {Buffer} A new `Buffer` with the decrypted content. 45501cb0ef41Sopenharmony_ci 45511cb0ef41Sopenharmony_ci<!--lint enable maximum-line-length remark-lint--> 45521cb0ef41Sopenharmony_ci 45531cb0ef41Sopenharmony_ciDecrypts `buffer` with `privateKey`. `buffer` was previously encrypted using 45541cb0ef41Sopenharmony_cithe corresponding public key, for example using [`crypto.publicEncrypt()`][]. 45551cb0ef41Sopenharmony_ci 45561cb0ef41Sopenharmony_ciIf `privateKey` is not a [`KeyObject`][], this function behaves as if 45571cb0ef41Sopenharmony_ci`privateKey` had been passed to [`crypto.createPrivateKey()`][]. If it is an 45581cb0ef41Sopenharmony_ciobject, the `padding` property can be passed. Otherwise, this function uses 45591cb0ef41Sopenharmony_ci`RSA_PKCS1_OAEP_PADDING`. 45601cb0ef41Sopenharmony_ci 45611cb0ef41Sopenharmony_ciUsing `crypto.constants.RSA_PKCS1_PADDING` in [`crypto.privateDecrypt()`][] 45621cb0ef41Sopenharmony_cirequires OpenSSL to support implicit rejection (`rsa_pkcs1_implicit_rejection`). 45631cb0ef41Sopenharmony_ciIf the version of OpenSSL used by Node.js does not support this feature, 45641cb0ef41Sopenharmony_ciattempting to use `RSA_PKCS1_PADDING` will fail. 45651cb0ef41Sopenharmony_ci 45661cb0ef41Sopenharmony_ci### `crypto.privateEncrypt(privateKey, buffer)` 45671cb0ef41Sopenharmony_ci 45681cb0ef41Sopenharmony_ci<!-- YAML 45691cb0ef41Sopenharmony_ciadded: v1.1.0 45701cb0ef41Sopenharmony_cichanges: 45711cb0ef41Sopenharmony_ci - version: v15.0.0 45721cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/35093 45731cb0ef41Sopenharmony_ci description: Added string, ArrayBuffer, and CryptoKey as allowable key 45741cb0ef41Sopenharmony_ci types. The passphrase can be an ArrayBuffer. The buffer can 45751cb0ef41Sopenharmony_ci be a string or ArrayBuffer. All types that accept buffers 45761cb0ef41Sopenharmony_ci are limited to a maximum of 2 ** 31 - 1 bytes. 45771cb0ef41Sopenharmony_ci - version: v11.6.0 45781cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/24234 45791cb0ef41Sopenharmony_ci description: This function now supports key objects. 45801cb0ef41Sopenharmony_ci--> 45811cb0ef41Sopenharmony_ci 45821cb0ef41Sopenharmony_ci<!--lint disable maximum-line-length remark-lint--> 45831cb0ef41Sopenharmony_ci 45841cb0ef41Sopenharmony_ci* `privateKey` {Object|string|ArrayBuffer|Buffer|TypedArray|DataView|KeyObject|CryptoKey} 45851cb0ef41Sopenharmony_ci * `key` {string|ArrayBuffer|Buffer|TypedArray|DataView|KeyObject|CryptoKey} 45861cb0ef41Sopenharmony_ci A PEM encoded private key. 45871cb0ef41Sopenharmony_ci * `passphrase` {string|ArrayBuffer|Buffer|TypedArray|DataView} An optional 45881cb0ef41Sopenharmony_ci passphrase for the private key. 45891cb0ef41Sopenharmony_ci * `padding` {crypto.constants} An optional padding value defined in 45901cb0ef41Sopenharmony_ci `crypto.constants`, which may be: `crypto.constants.RSA_NO_PADDING` or 45911cb0ef41Sopenharmony_ci `crypto.constants.RSA_PKCS1_PADDING`. 45921cb0ef41Sopenharmony_ci * `encoding` {string} The string encoding to use when `buffer`, `key`, 45931cb0ef41Sopenharmony_ci or `passphrase` are strings. 45941cb0ef41Sopenharmony_ci* `buffer` {string|ArrayBuffer|Buffer|TypedArray|DataView} 45951cb0ef41Sopenharmony_ci* Returns: {Buffer} A new `Buffer` with the encrypted content. 45961cb0ef41Sopenharmony_ci 45971cb0ef41Sopenharmony_ci<!--lint enable maximum-line-length remark-lint--> 45981cb0ef41Sopenharmony_ci 45991cb0ef41Sopenharmony_ciEncrypts `buffer` with `privateKey`. The returned data can be decrypted using 46001cb0ef41Sopenharmony_cithe corresponding public key, for example using [`crypto.publicDecrypt()`][]. 46011cb0ef41Sopenharmony_ci 46021cb0ef41Sopenharmony_ciIf `privateKey` is not a [`KeyObject`][], this function behaves as if 46031cb0ef41Sopenharmony_ci`privateKey` had been passed to [`crypto.createPrivateKey()`][]. If it is an 46041cb0ef41Sopenharmony_ciobject, the `padding` property can be passed. Otherwise, this function uses 46051cb0ef41Sopenharmony_ci`RSA_PKCS1_PADDING`. 46061cb0ef41Sopenharmony_ci 46071cb0ef41Sopenharmony_ci### `crypto.publicDecrypt(key, buffer)` 46081cb0ef41Sopenharmony_ci 46091cb0ef41Sopenharmony_ci<!-- YAML 46101cb0ef41Sopenharmony_ciadded: v1.1.0 46111cb0ef41Sopenharmony_cichanges: 46121cb0ef41Sopenharmony_ci - version: v15.0.0 46131cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/35093 46141cb0ef41Sopenharmony_ci description: Added string, ArrayBuffer, and CryptoKey as allowable key 46151cb0ef41Sopenharmony_ci types. The passphrase can be an ArrayBuffer. The buffer can 46161cb0ef41Sopenharmony_ci be a string or ArrayBuffer. All types that accept buffers 46171cb0ef41Sopenharmony_ci are limited to a maximum of 2 ** 31 - 1 bytes. 46181cb0ef41Sopenharmony_ci - version: v11.6.0 46191cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/24234 46201cb0ef41Sopenharmony_ci description: This function now supports key objects. 46211cb0ef41Sopenharmony_ci--> 46221cb0ef41Sopenharmony_ci 46231cb0ef41Sopenharmony_ci<!--lint disable maximum-line-length remark-lint--> 46241cb0ef41Sopenharmony_ci 46251cb0ef41Sopenharmony_ci* `key` {Object|string|ArrayBuffer|Buffer|TypedArray|DataView|KeyObject|CryptoKey} 46261cb0ef41Sopenharmony_ci * `passphrase` {string|ArrayBuffer|Buffer|TypedArray|DataView} An optional 46271cb0ef41Sopenharmony_ci passphrase for the private key. 46281cb0ef41Sopenharmony_ci * `padding` {crypto.constants} An optional padding value defined in 46291cb0ef41Sopenharmony_ci `crypto.constants`, which may be: `crypto.constants.RSA_NO_PADDING` or 46301cb0ef41Sopenharmony_ci `crypto.constants.RSA_PKCS1_PADDING`. 46311cb0ef41Sopenharmony_ci * `encoding` {string} The string encoding to use when `buffer`, `key`, 46321cb0ef41Sopenharmony_ci or `passphrase` are strings. 46331cb0ef41Sopenharmony_ci* `buffer` {string|ArrayBuffer|Buffer|TypedArray|DataView} 46341cb0ef41Sopenharmony_ci* Returns: {Buffer} A new `Buffer` with the decrypted content. 46351cb0ef41Sopenharmony_ci 46361cb0ef41Sopenharmony_ci<!--lint enable maximum-line-length remark-lint--> 46371cb0ef41Sopenharmony_ci 46381cb0ef41Sopenharmony_ciDecrypts `buffer` with `key`.`buffer` was previously encrypted using 46391cb0ef41Sopenharmony_cithe corresponding private key, for example using [`crypto.privateEncrypt()`][]. 46401cb0ef41Sopenharmony_ci 46411cb0ef41Sopenharmony_ciIf `key` is not a [`KeyObject`][], this function behaves as if 46421cb0ef41Sopenharmony_ci`key` had been passed to [`crypto.createPublicKey()`][]. If it is an 46431cb0ef41Sopenharmony_ciobject, the `padding` property can be passed. Otherwise, this function uses 46441cb0ef41Sopenharmony_ci`RSA_PKCS1_PADDING`. 46451cb0ef41Sopenharmony_ci 46461cb0ef41Sopenharmony_ciBecause RSA public keys can be derived from private keys, a private key may 46471cb0ef41Sopenharmony_cibe passed instead of a public key. 46481cb0ef41Sopenharmony_ci 46491cb0ef41Sopenharmony_ci### `crypto.publicEncrypt(key, buffer)` 46501cb0ef41Sopenharmony_ci 46511cb0ef41Sopenharmony_ci<!-- YAML 46521cb0ef41Sopenharmony_ciadded: v0.11.14 46531cb0ef41Sopenharmony_cichanges: 46541cb0ef41Sopenharmony_ci - version: v15.0.0 46551cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/35093 46561cb0ef41Sopenharmony_ci description: Added string, ArrayBuffer, and CryptoKey as allowable key 46571cb0ef41Sopenharmony_ci types. The oaepLabel and passphrase can be ArrayBuffers. The 46581cb0ef41Sopenharmony_ci buffer can be a string or ArrayBuffer. All types that accept 46591cb0ef41Sopenharmony_ci buffers are limited to a maximum of 2 ** 31 - 1 bytes. 46601cb0ef41Sopenharmony_ci - version: v12.11.0 46611cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/29489 46621cb0ef41Sopenharmony_ci description: The `oaepLabel` option was added. 46631cb0ef41Sopenharmony_ci - version: v12.9.0 46641cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/28335 46651cb0ef41Sopenharmony_ci description: The `oaepHash` option was added. 46661cb0ef41Sopenharmony_ci - version: v11.6.0 46671cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/24234 46681cb0ef41Sopenharmony_ci description: This function now supports key objects. 46691cb0ef41Sopenharmony_ci--> 46701cb0ef41Sopenharmony_ci 46711cb0ef41Sopenharmony_ci<!--lint disable maximum-line-length remark-lint--> 46721cb0ef41Sopenharmony_ci 46731cb0ef41Sopenharmony_ci* `key` {Object|string|ArrayBuffer|Buffer|TypedArray|DataView|KeyObject|CryptoKey} 46741cb0ef41Sopenharmony_ci * `key` {string|ArrayBuffer|Buffer|TypedArray|DataView|KeyObject|CryptoKey} 46751cb0ef41Sopenharmony_ci A PEM encoded public or private key, {KeyObject}, or {CryptoKey}. 46761cb0ef41Sopenharmony_ci * `oaepHash` {string} The hash function to use for OAEP padding and MGF1. 46771cb0ef41Sopenharmony_ci **Default:** `'sha1'` 46781cb0ef41Sopenharmony_ci * `oaepLabel` {string|ArrayBuffer|Buffer|TypedArray|DataView} The label to 46791cb0ef41Sopenharmony_ci use for OAEP padding. If not specified, no label is used. 46801cb0ef41Sopenharmony_ci * `passphrase` {string|ArrayBuffer|Buffer|TypedArray|DataView} An optional 46811cb0ef41Sopenharmony_ci passphrase for the private key. 46821cb0ef41Sopenharmony_ci * `padding` {crypto.constants} An optional padding value defined in 46831cb0ef41Sopenharmony_ci `crypto.constants`, which may be: `crypto.constants.RSA_NO_PADDING`, 46841cb0ef41Sopenharmony_ci `crypto.constants.RSA_PKCS1_PADDING`, or 46851cb0ef41Sopenharmony_ci `crypto.constants.RSA_PKCS1_OAEP_PADDING`. 46861cb0ef41Sopenharmony_ci * `encoding` {string} The string encoding to use when `buffer`, `key`, 46871cb0ef41Sopenharmony_ci `oaepLabel`, or `passphrase` are strings. 46881cb0ef41Sopenharmony_ci* `buffer` {string|ArrayBuffer|Buffer|TypedArray|DataView} 46891cb0ef41Sopenharmony_ci* Returns: {Buffer} A new `Buffer` with the encrypted content. 46901cb0ef41Sopenharmony_ci 46911cb0ef41Sopenharmony_ci<!--lint enable maximum-line-length remark-lint--> 46921cb0ef41Sopenharmony_ci 46931cb0ef41Sopenharmony_ciEncrypts the content of `buffer` with `key` and returns a new 46941cb0ef41Sopenharmony_ci[`Buffer`][] with encrypted content. The returned data can be decrypted using 46951cb0ef41Sopenharmony_cithe corresponding private key, for example using [`crypto.privateDecrypt()`][]. 46961cb0ef41Sopenharmony_ci 46971cb0ef41Sopenharmony_ciIf `key` is not a [`KeyObject`][], this function behaves as if 46981cb0ef41Sopenharmony_ci`key` had been passed to [`crypto.createPublicKey()`][]. If it is an 46991cb0ef41Sopenharmony_ciobject, the `padding` property can be passed. Otherwise, this function uses 47001cb0ef41Sopenharmony_ci`RSA_PKCS1_OAEP_PADDING`. 47011cb0ef41Sopenharmony_ci 47021cb0ef41Sopenharmony_ciBecause RSA public keys can be derived from private keys, a private key may 47031cb0ef41Sopenharmony_cibe passed instead of a public key. 47041cb0ef41Sopenharmony_ci 47051cb0ef41Sopenharmony_ci### `crypto.randomBytes(size[, callback])` 47061cb0ef41Sopenharmony_ci 47071cb0ef41Sopenharmony_ci<!-- YAML 47081cb0ef41Sopenharmony_ciadded: v0.5.8 47091cb0ef41Sopenharmony_cichanges: 47101cb0ef41Sopenharmony_ci - version: v18.0.0 47111cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41678 47121cb0ef41Sopenharmony_ci description: Passing an invalid callback to the `callback` argument 47131cb0ef41Sopenharmony_ci now throws `ERR_INVALID_ARG_TYPE` instead of 47141cb0ef41Sopenharmony_ci `ERR_INVALID_CALLBACK`. 47151cb0ef41Sopenharmony_ci - version: v9.0.0 47161cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/16454 47171cb0ef41Sopenharmony_ci description: Passing `null` as the `callback` argument now throws 47181cb0ef41Sopenharmony_ci `ERR_INVALID_CALLBACK`. 47191cb0ef41Sopenharmony_ci--> 47201cb0ef41Sopenharmony_ci 47211cb0ef41Sopenharmony_ci* `size` {number} The number of bytes to generate. The `size` must 47221cb0ef41Sopenharmony_ci not be larger than `2**31 - 1`. 47231cb0ef41Sopenharmony_ci* `callback` {Function} 47241cb0ef41Sopenharmony_ci * `err` {Error} 47251cb0ef41Sopenharmony_ci * `buf` {Buffer} 47261cb0ef41Sopenharmony_ci* Returns: {Buffer} if the `callback` function is not provided. 47271cb0ef41Sopenharmony_ci 47281cb0ef41Sopenharmony_ciGenerates cryptographically strong pseudorandom data. The `size` argument 47291cb0ef41Sopenharmony_ciis a number indicating the number of bytes to generate. 47301cb0ef41Sopenharmony_ci 47311cb0ef41Sopenharmony_ciIf a `callback` function is provided, the bytes are generated asynchronously 47321cb0ef41Sopenharmony_ciand the `callback` function is invoked with two arguments: `err` and `buf`. 47331cb0ef41Sopenharmony_ciIf an error occurs, `err` will be an `Error` object; otherwise it is `null`. The 47341cb0ef41Sopenharmony_ci`buf` argument is a [`Buffer`][] containing the generated bytes. 47351cb0ef41Sopenharmony_ci 47361cb0ef41Sopenharmony_ci```mjs 47371cb0ef41Sopenharmony_ci// Asynchronous 47381cb0ef41Sopenharmony_ciconst { 47391cb0ef41Sopenharmony_ci randomBytes, 47401cb0ef41Sopenharmony_ci} = await import('node:crypto'); 47411cb0ef41Sopenharmony_ci 47421cb0ef41Sopenharmony_cirandomBytes(256, (err, buf) => { 47431cb0ef41Sopenharmony_ci if (err) throw err; 47441cb0ef41Sopenharmony_ci console.log(`${buf.length} bytes of random data: ${buf.toString('hex')}`); 47451cb0ef41Sopenharmony_ci}); 47461cb0ef41Sopenharmony_ci``` 47471cb0ef41Sopenharmony_ci 47481cb0ef41Sopenharmony_ci```cjs 47491cb0ef41Sopenharmony_ci// Asynchronous 47501cb0ef41Sopenharmony_ciconst { 47511cb0ef41Sopenharmony_ci randomBytes, 47521cb0ef41Sopenharmony_ci} = require('node:crypto'); 47531cb0ef41Sopenharmony_ci 47541cb0ef41Sopenharmony_cirandomBytes(256, (err, buf) => { 47551cb0ef41Sopenharmony_ci if (err) throw err; 47561cb0ef41Sopenharmony_ci console.log(`${buf.length} bytes of random data: ${buf.toString('hex')}`); 47571cb0ef41Sopenharmony_ci}); 47581cb0ef41Sopenharmony_ci``` 47591cb0ef41Sopenharmony_ci 47601cb0ef41Sopenharmony_ciIf the `callback` function is not provided, the random bytes are generated 47611cb0ef41Sopenharmony_cisynchronously and returned as a [`Buffer`][]. An error will be thrown if 47621cb0ef41Sopenharmony_cithere is a problem generating the bytes. 47631cb0ef41Sopenharmony_ci 47641cb0ef41Sopenharmony_ci```mjs 47651cb0ef41Sopenharmony_ci// Synchronous 47661cb0ef41Sopenharmony_ciconst { 47671cb0ef41Sopenharmony_ci randomBytes, 47681cb0ef41Sopenharmony_ci} = await import('node:crypto'); 47691cb0ef41Sopenharmony_ci 47701cb0ef41Sopenharmony_ciconst buf = randomBytes(256); 47711cb0ef41Sopenharmony_ciconsole.log( 47721cb0ef41Sopenharmony_ci `${buf.length} bytes of random data: ${buf.toString('hex')}`); 47731cb0ef41Sopenharmony_ci``` 47741cb0ef41Sopenharmony_ci 47751cb0ef41Sopenharmony_ci```cjs 47761cb0ef41Sopenharmony_ci// Synchronous 47771cb0ef41Sopenharmony_ciconst { 47781cb0ef41Sopenharmony_ci randomBytes, 47791cb0ef41Sopenharmony_ci} = require('node:crypto'); 47801cb0ef41Sopenharmony_ci 47811cb0ef41Sopenharmony_ciconst buf = randomBytes(256); 47821cb0ef41Sopenharmony_ciconsole.log( 47831cb0ef41Sopenharmony_ci `${buf.length} bytes of random data: ${buf.toString('hex')}`); 47841cb0ef41Sopenharmony_ci``` 47851cb0ef41Sopenharmony_ci 47861cb0ef41Sopenharmony_ciThe `crypto.randomBytes()` method will not complete until there is 47871cb0ef41Sopenharmony_cisufficient entropy available. 47881cb0ef41Sopenharmony_ciThis should normally never take longer than a few milliseconds. The only time 47891cb0ef41Sopenharmony_ciwhen generating the random bytes may conceivably block for a longer period of 47901cb0ef41Sopenharmony_citime is right after boot, when the whole system is still low on entropy. 47911cb0ef41Sopenharmony_ci 47921cb0ef41Sopenharmony_ciThis API uses libuv's threadpool, which can have surprising and 47931cb0ef41Sopenharmony_cinegative performance implications for some applications; see the 47941cb0ef41Sopenharmony_ci[`UV_THREADPOOL_SIZE`][] documentation for more information. 47951cb0ef41Sopenharmony_ci 47961cb0ef41Sopenharmony_ciThe asynchronous version of `crypto.randomBytes()` is carried out in a single 47971cb0ef41Sopenharmony_cithreadpool request. To minimize threadpool task length variation, partition 47981cb0ef41Sopenharmony_cilarge `randomBytes` requests when doing so as part of fulfilling a client 47991cb0ef41Sopenharmony_cirequest. 48001cb0ef41Sopenharmony_ci 48011cb0ef41Sopenharmony_ci### `crypto.randomFillSync(buffer[, offset][, size])` 48021cb0ef41Sopenharmony_ci 48031cb0ef41Sopenharmony_ci<!-- YAML 48041cb0ef41Sopenharmony_ciadded: 48051cb0ef41Sopenharmony_ci - v7.10.0 48061cb0ef41Sopenharmony_ci - v6.13.0 48071cb0ef41Sopenharmony_cichanges: 48081cb0ef41Sopenharmony_ci - version: v9.0.0 48091cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/15231 48101cb0ef41Sopenharmony_ci description: The `buffer` argument may be any `TypedArray` or `DataView`. 48111cb0ef41Sopenharmony_ci--> 48121cb0ef41Sopenharmony_ci 48131cb0ef41Sopenharmony_ci* `buffer` {ArrayBuffer|Buffer|TypedArray|DataView} Must be supplied. The 48141cb0ef41Sopenharmony_ci size of the provided `buffer` must not be larger than `2**31 - 1`. 48151cb0ef41Sopenharmony_ci* `offset` {number} **Default:** `0` 48161cb0ef41Sopenharmony_ci* `size` {number} **Default:** `buffer.length - offset`. The `size` must 48171cb0ef41Sopenharmony_ci not be larger than `2**31 - 1`. 48181cb0ef41Sopenharmony_ci* Returns: {ArrayBuffer|Buffer|TypedArray|DataView} The object passed as 48191cb0ef41Sopenharmony_ci `buffer` argument. 48201cb0ef41Sopenharmony_ci 48211cb0ef41Sopenharmony_ciSynchronous version of [`crypto.randomFill()`][]. 48221cb0ef41Sopenharmony_ci 48231cb0ef41Sopenharmony_ci```mjs 48241cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer'; 48251cb0ef41Sopenharmony_ciconst { randomFillSync } = await import('node:crypto'); 48261cb0ef41Sopenharmony_ci 48271cb0ef41Sopenharmony_ciconst buf = Buffer.alloc(10); 48281cb0ef41Sopenharmony_ciconsole.log(randomFillSync(buf).toString('hex')); 48291cb0ef41Sopenharmony_ci 48301cb0ef41Sopenharmony_cirandomFillSync(buf, 5); 48311cb0ef41Sopenharmony_ciconsole.log(buf.toString('hex')); 48321cb0ef41Sopenharmony_ci 48331cb0ef41Sopenharmony_ci// The above is equivalent to the following: 48341cb0ef41Sopenharmony_cirandomFillSync(buf, 5, 5); 48351cb0ef41Sopenharmony_ciconsole.log(buf.toString('hex')); 48361cb0ef41Sopenharmony_ci``` 48371cb0ef41Sopenharmony_ci 48381cb0ef41Sopenharmony_ci```cjs 48391cb0ef41Sopenharmony_ciconst { randomFillSync } = require('node:crypto'); 48401cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer'); 48411cb0ef41Sopenharmony_ci 48421cb0ef41Sopenharmony_ciconst buf = Buffer.alloc(10); 48431cb0ef41Sopenharmony_ciconsole.log(randomFillSync(buf).toString('hex')); 48441cb0ef41Sopenharmony_ci 48451cb0ef41Sopenharmony_cirandomFillSync(buf, 5); 48461cb0ef41Sopenharmony_ciconsole.log(buf.toString('hex')); 48471cb0ef41Sopenharmony_ci 48481cb0ef41Sopenharmony_ci// The above is equivalent to the following: 48491cb0ef41Sopenharmony_cirandomFillSync(buf, 5, 5); 48501cb0ef41Sopenharmony_ciconsole.log(buf.toString('hex')); 48511cb0ef41Sopenharmony_ci``` 48521cb0ef41Sopenharmony_ci 48531cb0ef41Sopenharmony_ciAny `ArrayBuffer`, `TypedArray` or `DataView` instance may be passed as 48541cb0ef41Sopenharmony_ci`buffer`. 48551cb0ef41Sopenharmony_ci 48561cb0ef41Sopenharmony_ci```mjs 48571cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer'; 48581cb0ef41Sopenharmony_ciconst { randomFillSync } = await import('node:crypto'); 48591cb0ef41Sopenharmony_ci 48601cb0ef41Sopenharmony_ciconst a = new Uint32Array(10); 48611cb0ef41Sopenharmony_ciconsole.log(Buffer.from(randomFillSync(a).buffer, 48621cb0ef41Sopenharmony_ci a.byteOffset, a.byteLength).toString('hex')); 48631cb0ef41Sopenharmony_ci 48641cb0ef41Sopenharmony_ciconst b = new DataView(new ArrayBuffer(10)); 48651cb0ef41Sopenharmony_ciconsole.log(Buffer.from(randomFillSync(b).buffer, 48661cb0ef41Sopenharmony_ci b.byteOffset, b.byteLength).toString('hex')); 48671cb0ef41Sopenharmony_ci 48681cb0ef41Sopenharmony_ciconst c = new ArrayBuffer(10); 48691cb0ef41Sopenharmony_ciconsole.log(Buffer.from(randomFillSync(c)).toString('hex')); 48701cb0ef41Sopenharmony_ci``` 48711cb0ef41Sopenharmony_ci 48721cb0ef41Sopenharmony_ci```cjs 48731cb0ef41Sopenharmony_ciconst { randomFillSync } = require('node:crypto'); 48741cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer'); 48751cb0ef41Sopenharmony_ci 48761cb0ef41Sopenharmony_ciconst a = new Uint32Array(10); 48771cb0ef41Sopenharmony_ciconsole.log(Buffer.from(randomFillSync(a).buffer, 48781cb0ef41Sopenharmony_ci a.byteOffset, a.byteLength).toString('hex')); 48791cb0ef41Sopenharmony_ci 48801cb0ef41Sopenharmony_ciconst b = new DataView(new ArrayBuffer(10)); 48811cb0ef41Sopenharmony_ciconsole.log(Buffer.from(randomFillSync(b).buffer, 48821cb0ef41Sopenharmony_ci b.byteOffset, b.byteLength).toString('hex')); 48831cb0ef41Sopenharmony_ci 48841cb0ef41Sopenharmony_ciconst c = new ArrayBuffer(10); 48851cb0ef41Sopenharmony_ciconsole.log(Buffer.from(randomFillSync(c)).toString('hex')); 48861cb0ef41Sopenharmony_ci``` 48871cb0ef41Sopenharmony_ci 48881cb0ef41Sopenharmony_ci### `crypto.randomFill(buffer[, offset][, size], callback)` 48891cb0ef41Sopenharmony_ci 48901cb0ef41Sopenharmony_ci<!-- YAML 48911cb0ef41Sopenharmony_ciadded: 48921cb0ef41Sopenharmony_ci - v7.10.0 48931cb0ef41Sopenharmony_ci - v6.13.0 48941cb0ef41Sopenharmony_cichanges: 48951cb0ef41Sopenharmony_ci - version: v18.0.0 48961cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41678 48971cb0ef41Sopenharmony_ci description: Passing an invalid callback to the `callback` argument 48981cb0ef41Sopenharmony_ci now throws `ERR_INVALID_ARG_TYPE` instead of 48991cb0ef41Sopenharmony_ci `ERR_INVALID_CALLBACK`. 49001cb0ef41Sopenharmony_ci - version: v9.0.0 49011cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/15231 49021cb0ef41Sopenharmony_ci description: The `buffer` argument may be any `TypedArray` or `DataView`. 49031cb0ef41Sopenharmony_ci--> 49041cb0ef41Sopenharmony_ci 49051cb0ef41Sopenharmony_ci* `buffer` {ArrayBuffer|Buffer|TypedArray|DataView} Must be supplied. The 49061cb0ef41Sopenharmony_ci size of the provided `buffer` must not be larger than `2**31 - 1`. 49071cb0ef41Sopenharmony_ci* `offset` {number} **Default:** `0` 49081cb0ef41Sopenharmony_ci* `size` {number} **Default:** `buffer.length - offset`. The `size` must 49091cb0ef41Sopenharmony_ci not be larger than `2**31 - 1`. 49101cb0ef41Sopenharmony_ci* `callback` {Function} `function(err, buf) {}`. 49111cb0ef41Sopenharmony_ci 49121cb0ef41Sopenharmony_ciThis function is similar to [`crypto.randomBytes()`][] but requires the first 49131cb0ef41Sopenharmony_ciargument to be a [`Buffer`][] that will be filled. It also 49141cb0ef41Sopenharmony_cirequires that a callback is passed in. 49151cb0ef41Sopenharmony_ci 49161cb0ef41Sopenharmony_ciIf the `callback` function is not provided, an error will be thrown. 49171cb0ef41Sopenharmony_ci 49181cb0ef41Sopenharmony_ci```mjs 49191cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer'; 49201cb0ef41Sopenharmony_ciconst { randomFill } = await import('node:crypto'); 49211cb0ef41Sopenharmony_ci 49221cb0ef41Sopenharmony_ciconst buf = Buffer.alloc(10); 49231cb0ef41Sopenharmony_cirandomFill(buf, (err, buf) => { 49241cb0ef41Sopenharmony_ci if (err) throw err; 49251cb0ef41Sopenharmony_ci console.log(buf.toString('hex')); 49261cb0ef41Sopenharmony_ci}); 49271cb0ef41Sopenharmony_ci 49281cb0ef41Sopenharmony_cirandomFill(buf, 5, (err, buf) => { 49291cb0ef41Sopenharmony_ci if (err) throw err; 49301cb0ef41Sopenharmony_ci console.log(buf.toString('hex')); 49311cb0ef41Sopenharmony_ci}); 49321cb0ef41Sopenharmony_ci 49331cb0ef41Sopenharmony_ci// The above is equivalent to the following: 49341cb0ef41Sopenharmony_cirandomFill(buf, 5, 5, (err, buf) => { 49351cb0ef41Sopenharmony_ci if (err) throw err; 49361cb0ef41Sopenharmony_ci console.log(buf.toString('hex')); 49371cb0ef41Sopenharmony_ci}); 49381cb0ef41Sopenharmony_ci``` 49391cb0ef41Sopenharmony_ci 49401cb0ef41Sopenharmony_ci```cjs 49411cb0ef41Sopenharmony_ciconst { randomFill } = require('node:crypto'); 49421cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer'); 49431cb0ef41Sopenharmony_ci 49441cb0ef41Sopenharmony_ciconst buf = Buffer.alloc(10); 49451cb0ef41Sopenharmony_cirandomFill(buf, (err, buf) => { 49461cb0ef41Sopenharmony_ci if (err) throw err; 49471cb0ef41Sopenharmony_ci console.log(buf.toString('hex')); 49481cb0ef41Sopenharmony_ci}); 49491cb0ef41Sopenharmony_ci 49501cb0ef41Sopenharmony_cirandomFill(buf, 5, (err, buf) => { 49511cb0ef41Sopenharmony_ci if (err) throw err; 49521cb0ef41Sopenharmony_ci console.log(buf.toString('hex')); 49531cb0ef41Sopenharmony_ci}); 49541cb0ef41Sopenharmony_ci 49551cb0ef41Sopenharmony_ci// The above is equivalent to the following: 49561cb0ef41Sopenharmony_cirandomFill(buf, 5, 5, (err, buf) => { 49571cb0ef41Sopenharmony_ci if (err) throw err; 49581cb0ef41Sopenharmony_ci console.log(buf.toString('hex')); 49591cb0ef41Sopenharmony_ci}); 49601cb0ef41Sopenharmony_ci``` 49611cb0ef41Sopenharmony_ci 49621cb0ef41Sopenharmony_ciAny `ArrayBuffer`, `TypedArray`, or `DataView` instance may be passed as 49631cb0ef41Sopenharmony_ci`buffer`. 49641cb0ef41Sopenharmony_ci 49651cb0ef41Sopenharmony_ciWhile this includes instances of `Float32Array` and `Float64Array`, this 49661cb0ef41Sopenharmony_cifunction should not be used to generate random floating-point numbers. The 49671cb0ef41Sopenharmony_ciresult may contain `+Infinity`, `-Infinity`, and `NaN`, and even if the array 49681cb0ef41Sopenharmony_cicontains finite numbers only, they are not drawn from a uniform random 49691cb0ef41Sopenharmony_cidistribution and have no meaningful lower or upper bounds. 49701cb0ef41Sopenharmony_ci 49711cb0ef41Sopenharmony_ci```mjs 49721cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer'; 49731cb0ef41Sopenharmony_ciconst { randomFill } = await import('node:crypto'); 49741cb0ef41Sopenharmony_ci 49751cb0ef41Sopenharmony_ciconst a = new Uint32Array(10); 49761cb0ef41Sopenharmony_cirandomFill(a, (err, buf) => { 49771cb0ef41Sopenharmony_ci if (err) throw err; 49781cb0ef41Sopenharmony_ci console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength) 49791cb0ef41Sopenharmony_ci .toString('hex')); 49801cb0ef41Sopenharmony_ci}); 49811cb0ef41Sopenharmony_ci 49821cb0ef41Sopenharmony_ciconst b = new DataView(new ArrayBuffer(10)); 49831cb0ef41Sopenharmony_cirandomFill(b, (err, buf) => { 49841cb0ef41Sopenharmony_ci if (err) throw err; 49851cb0ef41Sopenharmony_ci console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength) 49861cb0ef41Sopenharmony_ci .toString('hex')); 49871cb0ef41Sopenharmony_ci}); 49881cb0ef41Sopenharmony_ci 49891cb0ef41Sopenharmony_ciconst c = new ArrayBuffer(10); 49901cb0ef41Sopenharmony_cirandomFill(c, (err, buf) => { 49911cb0ef41Sopenharmony_ci if (err) throw err; 49921cb0ef41Sopenharmony_ci console.log(Buffer.from(buf).toString('hex')); 49931cb0ef41Sopenharmony_ci}); 49941cb0ef41Sopenharmony_ci``` 49951cb0ef41Sopenharmony_ci 49961cb0ef41Sopenharmony_ci```cjs 49971cb0ef41Sopenharmony_ciconst { randomFill } = require('node:crypto'); 49981cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer'); 49991cb0ef41Sopenharmony_ci 50001cb0ef41Sopenharmony_ciconst a = new Uint32Array(10); 50011cb0ef41Sopenharmony_cirandomFill(a, (err, buf) => { 50021cb0ef41Sopenharmony_ci if (err) throw err; 50031cb0ef41Sopenharmony_ci console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength) 50041cb0ef41Sopenharmony_ci .toString('hex')); 50051cb0ef41Sopenharmony_ci}); 50061cb0ef41Sopenharmony_ci 50071cb0ef41Sopenharmony_ciconst b = new DataView(new ArrayBuffer(10)); 50081cb0ef41Sopenharmony_cirandomFill(b, (err, buf) => { 50091cb0ef41Sopenharmony_ci if (err) throw err; 50101cb0ef41Sopenharmony_ci console.log(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength) 50111cb0ef41Sopenharmony_ci .toString('hex')); 50121cb0ef41Sopenharmony_ci}); 50131cb0ef41Sopenharmony_ci 50141cb0ef41Sopenharmony_ciconst c = new ArrayBuffer(10); 50151cb0ef41Sopenharmony_cirandomFill(c, (err, buf) => { 50161cb0ef41Sopenharmony_ci if (err) throw err; 50171cb0ef41Sopenharmony_ci console.log(Buffer.from(buf).toString('hex')); 50181cb0ef41Sopenharmony_ci}); 50191cb0ef41Sopenharmony_ci``` 50201cb0ef41Sopenharmony_ci 50211cb0ef41Sopenharmony_ciThis API uses libuv's threadpool, which can have surprising and 50221cb0ef41Sopenharmony_cinegative performance implications for some applications; see the 50231cb0ef41Sopenharmony_ci[`UV_THREADPOOL_SIZE`][] documentation for more information. 50241cb0ef41Sopenharmony_ci 50251cb0ef41Sopenharmony_ciThe asynchronous version of `crypto.randomFill()` is carried out in a single 50261cb0ef41Sopenharmony_cithreadpool request. To minimize threadpool task length variation, partition 50271cb0ef41Sopenharmony_cilarge `randomFill` requests when doing so as part of fulfilling a client 50281cb0ef41Sopenharmony_cirequest. 50291cb0ef41Sopenharmony_ci 50301cb0ef41Sopenharmony_ci### `crypto.randomInt([min, ]max[, callback])` 50311cb0ef41Sopenharmony_ci 50321cb0ef41Sopenharmony_ci<!-- YAML 50331cb0ef41Sopenharmony_ciadded: 50341cb0ef41Sopenharmony_ci - v14.10.0 50351cb0ef41Sopenharmony_ci - v12.19.0 50361cb0ef41Sopenharmony_cichanges: 50371cb0ef41Sopenharmony_ci - version: v18.0.0 50381cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41678 50391cb0ef41Sopenharmony_ci description: Passing an invalid callback to the `callback` argument 50401cb0ef41Sopenharmony_ci now throws `ERR_INVALID_ARG_TYPE` instead of 50411cb0ef41Sopenharmony_ci `ERR_INVALID_CALLBACK`. 50421cb0ef41Sopenharmony_ci--> 50431cb0ef41Sopenharmony_ci 50441cb0ef41Sopenharmony_ci* `min` {integer} Start of random range (inclusive). **Default:** `0`. 50451cb0ef41Sopenharmony_ci* `max` {integer} End of random range (exclusive). 50461cb0ef41Sopenharmony_ci* `callback` {Function} `function(err, n) {}`. 50471cb0ef41Sopenharmony_ci 50481cb0ef41Sopenharmony_ciReturn a random integer `n` such that `min <= n < max`. This 50491cb0ef41Sopenharmony_ciimplementation avoids [modulo bias][]. 50501cb0ef41Sopenharmony_ci 50511cb0ef41Sopenharmony_ciThe range (`max - min`) must be less than 2<sup>48</sup>. `min` and `max` must 50521cb0ef41Sopenharmony_cibe [safe integers][]. 50531cb0ef41Sopenharmony_ci 50541cb0ef41Sopenharmony_ciIf the `callback` function is not provided, the random integer is 50551cb0ef41Sopenharmony_cigenerated synchronously. 50561cb0ef41Sopenharmony_ci 50571cb0ef41Sopenharmony_ci```mjs 50581cb0ef41Sopenharmony_ci// Asynchronous 50591cb0ef41Sopenharmony_ciconst { 50601cb0ef41Sopenharmony_ci randomInt, 50611cb0ef41Sopenharmony_ci} = await import('node:crypto'); 50621cb0ef41Sopenharmony_ci 50631cb0ef41Sopenharmony_cirandomInt(3, (err, n) => { 50641cb0ef41Sopenharmony_ci if (err) throw err; 50651cb0ef41Sopenharmony_ci console.log(`Random number chosen from (0, 1, 2): ${n}`); 50661cb0ef41Sopenharmony_ci}); 50671cb0ef41Sopenharmony_ci``` 50681cb0ef41Sopenharmony_ci 50691cb0ef41Sopenharmony_ci```cjs 50701cb0ef41Sopenharmony_ci// Asynchronous 50711cb0ef41Sopenharmony_ciconst { 50721cb0ef41Sopenharmony_ci randomInt, 50731cb0ef41Sopenharmony_ci} = require('node:crypto'); 50741cb0ef41Sopenharmony_ci 50751cb0ef41Sopenharmony_cirandomInt(3, (err, n) => { 50761cb0ef41Sopenharmony_ci if (err) throw err; 50771cb0ef41Sopenharmony_ci console.log(`Random number chosen from (0, 1, 2): ${n}`); 50781cb0ef41Sopenharmony_ci}); 50791cb0ef41Sopenharmony_ci``` 50801cb0ef41Sopenharmony_ci 50811cb0ef41Sopenharmony_ci```mjs 50821cb0ef41Sopenharmony_ci// Synchronous 50831cb0ef41Sopenharmony_ciconst { 50841cb0ef41Sopenharmony_ci randomInt, 50851cb0ef41Sopenharmony_ci} = await import('node:crypto'); 50861cb0ef41Sopenharmony_ci 50871cb0ef41Sopenharmony_ciconst n = randomInt(3); 50881cb0ef41Sopenharmony_ciconsole.log(`Random number chosen from (0, 1, 2): ${n}`); 50891cb0ef41Sopenharmony_ci``` 50901cb0ef41Sopenharmony_ci 50911cb0ef41Sopenharmony_ci```cjs 50921cb0ef41Sopenharmony_ci// Synchronous 50931cb0ef41Sopenharmony_ciconst { 50941cb0ef41Sopenharmony_ci randomInt, 50951cb0ef41Sopenharmony_ci} = require('node:crypto'); 50961cb0ef41Sopenharmony_ci 50971cb0ef41Sopenharmony_ciconst n = randomInt(3); 50981cb0ef41Sopenharmony_ciconsole.log(`Random number chosen from (0, 1, 2): ${n}`); 50991cb0ef41Sopenharmony_ci``` 51001cb0ef41Sopenharmony_ci 51011cb0ef41Sopenharmony_ci```mjs 51021cb0ef41Sopenharmony_ci// With `min` argument 51031cb0ef41Sopenharmony_ciconst { 51041cb0ef41Sopenharmony_ci randomInt, 51051cb0ef41Sopenharmony_ci} = await import('node:crypto'); 51061cb0ef41Sopenharmony_ci 51071cb0ef41Sopenharmony_ciconst n = randomInt(1, 7); 51081cb0ef41Sopenharmony_ciconsole.log(`The dice rolled: ${n}`); 51091cb0ef41Sopenharmony_ci``` 51101cb0ef41Sopenharmony_ci 51111cb0ef41Sopenharmony_ci```cjs 51121cb0ef41Sopenharmony_ci// With `min` argument 51131cb0ef41Sopenharmony_ciconst { 51141cb0ef41Sopenharmony_ci randomInt, 51151cb0ef41Sopenharmony_ci} = require('node:crypto'); 51161cb0ef41Sopenharmony_ci 51171cb0ef41Sopenharmony_ciconst n = randomInt(1, 7); 51181cb0ef41Sopenharmony_ciconsole.log(`The dice rolled: ${n}`); 51191cb0ef41Sopenharmony_ci``` 51201cb0ef41Sopenharmony_ci 51211cb0ef41Sopenharmony_ci### `crypto.randomUUID([options])` 51221cb0ef41Sopenharmony_ci 51231cb0ef41Sopenharmony_ci<!-- YAML 51241cb0ef41Sopenharmony_ciadded: 51251cb0ef41Sopenharmony_ci - v15.6.0 51261cb0ef41Sopenharmony_ci - v14.17.0 51271cb0ef41Sopenharmony_ci--> 51281cb0ef41Sopenharmony_ci 51291cb0ef41Sopenharmony_ci* `options` {Object} 51301cb0ef41Sopenharmony_ci * `disableEntropyCache` {boolean} By default, to improve performance, 51311cb0ef41Sopenharmony_ci Node.js generates and caches enough 51321cb0ef41Sopenharmony_ci random data to generate up to 128 random UUIDs. To generate a UUID 51331cb0ef41Sopenharmony_ci without using the cache, set `disableEntropyCache` to `true`. 51341cb0ef41Sopenharmony_ci **Default:** `false`. 51351cb0ef41Sopenharmony_ci* Returns: {string} 51361cb0ef41Sopenharmony_ci 51371cb0ef41Sopenharmony_ciGenerates a random [RFC 4122][] version 4 UUID. The UUID is generated using a 51381cb0ef41Sopenharmony_cicryptographic pseudorandom number generator. 51391cb0ef41Sopenharmony_ci 51401cb0ef41Sopenharmony_ci### `crypto.scrypt(password, salt, keylen[, options], callback)` 51411cb0ef41Sopenharmony_ci 51421cb0ef41Sopenharmony_ci<!-- YAML 51431cb0ef41Sopenharmony_ciadded: v10.5.0 51441cb0ef41Sopenharmony_cichanges: 51451cb0ef41Sopenharmony_ci - version: v18.0.0 51461cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41678 51471cb0ef41Sopenharmony_ci description: Passing an invalid callback to the `callback` argument 51481cb0ef41Sopenharmony_ci now throws `ERR_INVALID_ARG_TYPE` instead of 51491cb0ef41Sopenharmony_ci `ERR_INVALID_CALLBACK`. 51501cb0ef41Sopenharmony_ci - version: v15.0.0 51511cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/35093 51521cb0ef41Sopenharmony_ci description: The password and salt arguments can also be ArrayBuffer 51531cb0ef41Sopenharmony_ci instances. 51541cb0ef41Sopenharmony_ci - version: 51551cb0ef41Sopenharmony_ci - v12.8.0 51561cb0ef41Sopenharmony_ci - v10.17.0 51571cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/28799 51581cb0ef41Sopenharmony_ci description: The `maxmem` value can now be any safe integer. 51591cb0ef41Sopenharmony_ci - version: v10.9.0 51601cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/21525 51611cb0ef41Sopenharmony_ci description: The `cost`, `blockSize` and `parallelization` option names 51621cb0ef41Sopenharmony_ci have been added. 51631cb0ef41Sopenharmony_ci--> 51641cb0ef41Sopenharmony_ci 51651cb0ef41Sopenharmony_ci* `password` {string|ArrayBuffer|Buffer|TypedArray|DataView} 51661cb0ef41Sopenharmony_ci* `salt` {string|ArrayBuffer|Buffer|TypedArray|DataView} 51671cb0ef41Sopenharmony_ci* `keylen` {number} 51681cb0ef41Sopenharmony_ci* `options` {Object} 51691cb0ef41Sopenharmony_ci * `cost` {number} CPU/memory cost parameter. Must be a power of two greater 51701cb0ef41Sopenharmony_ci than one. **Default:** `16384`. 51711cb0ef41Sopenharmony_ci * `blockSize` {number} Block size parameter. **Default:** `8`. 51721cb0ef41Sopenharmony_ci * `parallelization` {number} Parallelization parameter. **Default:** `1`. 51731cb0ef41Sopenharmony_ci * `N` {number} Alias for `cost`. Only one of both may be specified. 51741cb0ef41Sopenharmony_ci * `r` {number} Alias for `blockSize`. Only one of both may be specified. 51751cb0ef41Sopenharmony_ci * `p` {number} Alias for `parallelization`. Only one of both may be specified. 51761cb0ef41Sopenharmony_ci * `maxmem` {number} Memory upper bound. It is an error when (approximately) 51771cb0ef41Sopenharmony_ci `128 * N * r > maxmem`. **Default:** `32 * 1024 * 1024`. 51781cb0ef41Sopenharmony_ci* `callback` {Function} 51791cb0ef41Sopenharmony_ci * `err` {Error} 51801cb0ef41Sopenharmony_ci * `derivedKey` {Buffer} 51811cb0ef41Sopenharmony_ci 51821cb0ef41Sopenharmony_ciProvides an asynchronous [scrypt][] implementation. Scrypt is a password-based 51831cb0ef41Sopenharmony_cikey derivation function that is designed to be expensive computationally and 51841cb0ef41Sopenharmony_cimemory-wise in order to make brute-force attacks unrewarding. 51851cb0ef41Sopenharmony_ci 51861cb0ef41Sopenharmony_ciThe `salt` should be as unique as possible. It is recommended that a salt is 51871cb0ef41Sopenharmony_cirandom and at least 16 bytes long. See [NIST SP 800-132][] for details. 51881cb0ef41Sopenharmony_ci 51891cb0ef41Sopenharmony_ciWhen passing strings for `password` or `salt`, please consider 51901cb0ef41Sopenharmony_ci[caveats when using strings as inputs to cryptographic APIs][]. 51911cb0ef41Sopenharmony_ci 51921cb0ef41Sopenharmony_ciThe `callback` function is called with two arguments: `err` and `derivedKey`. 51931cb0ef41Sopenharmony_ci`err` is an exception object when key derivation fails, otherwise `err` is 51941cb0ef41Sopenharmony_ci`null`. `derivedKey` is passed to the callback as a [`Buffer`][]. 51951cb0ef41Sopenharmony_ci 51961cb0ef41Sopenharmony_ciAn exception is thrown when any of the input arguments specify invalid values 51971cb0ef41Sopenharmony_cior types. 51981cb0ef41Sopenharmony_ci 51991cb0ef41Sopenharmony_ci```mjs 52001cb0ef41Sopenharmony_ciconst { 52011cb0ef41Sopenharmony_ci scrypt, 52021cb0ef41Sopenharmony_ci} = await import('node:crypto'); 52031cb0ef41Sopenharmony_ci 52041cb0ef41Sopenharmony_ci// Using the factory defaults. 52051cb0ef41Sopenharmony_ciscrypt('password', 'salt', 64, (err, derivedKey) => { 52061cb0ef41Sopenharmony_ci if (err) throw err; 52071cb0ef41Sopenharmony_ci console.log(derivedKey.toString('hex')); // '3745e48...08d59ae' 52081cb0ef41Sopenharmony_ci}); 52091cb0ef41Sopenharmony_ci// Using a custom N parameter. Must be a power of two. 52101cb0ef41Sopenharmony_ciscrypt('password', 'salt', 64, { N: 1024 }, (err, derivedKey) => { 52111cb0ef41Sopenharmony_ci if (err) throw err; 52121cb0ef41Sopenharmony_ci console.log(derivedKey.toString('hex')); // '3745e48...aa39b34' 52131cb0ef41Sopenharmony_ci}); 52141cb0ef41Sopenharmony_ci``` 52151cb0ef41Sopenharmony_ci 52161cb0ef41Sopenharmony_ci```cjs 52171cb0ef41Sopenharmony_ciconst { 52181cb0ef41Sopenharmony_ci scrypt, 52191cb0ef41Sopenharmony_ci} = require('node:crypto'); 52201cb0ef41Sopenharmony_ci 52211cb0ef41Sopenharmony_ci// Using the factory defaults. 52221cb0ef41Sopenharmony_ciscrypt('password', 'salt', 64, (err, derivedKey) => { 52231cb0ef41Sopenharmony_ci if (err) throw err; 52241cb0ef41Sopenharmony_ci console.log(derivedKey.toString('hex')); // '3745e48...08d59ae' 52251cb0ef41Sopenharmony_ci}); 52261cb0ef41Sopenharmony_ci// Using a custom N parameter. Must be a power of two. 52271cb0ef41Sopenharmony_ciscrypt('password', 'salt', 64, { N: 1024 }, (err, derivedKey) => { 52281cb0ef41Sopenharmony_ci if (err) throw err; 52291cb0ef41Sopenharmony_ci console.log(derivedKey.toString('hex')); // '3745e48...aa39b34' 52301cb0ef41Sopenharmony_ci}); 52311cb0ef41Sopenharmony_ci``` 52321cb0ef41Sopenharmony_ci 52331cb0ef41Sopenharmony_ci### `crypto.scryptSync(password, salt, keylen[, options])` 52341cb0ef41Sopenharmony_ci 52351cb0ef41Sopenharmony_ci<!-- YAML 52361cb0ef41Sopenharmony_ciadded: v10.5.0 52371cb0ef41Sopenharmony_cichanges: 52381cb0ef41Sopenharmony_ci - version: 52391cb0ef41Sopenharmony_ci - v12.8.0 52401cb0ef41Sopenharmony_ci - v10.17.0 52411cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/28799 52421cb0ef41Sopenharmony_ci description: The `maxmem` value can now be any safe integer. 52431cb0ef41Sopenharmony_ci - version: v10.9.0 52441cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/21525 52451cb0ef41Sopenharmony_ci description: The `cost`, `blockSize` and `parallelization` option names 52461cb0ef41Sopenharmony_ci have been added. 52471cb0ef41Sopenharmony_ci--> 52481cb0ef41Sopenharmony_ci 52491cb0ef41Sopenharmony_ci* `password` {string|Buffer|TypedArray|DataView} 52501cb0ef41Sopenharmony_ci* `salt` {string|Buffer|TypedArray|DataView} 52511cb0ef41Sopenharmony_ci* `keylen` {number} 52521cb0ef41Sopenharmony_ci* `options` {Object} 52531cb0ef41Sopenharmony_ci * `cost` {number} CPU/memory cost parameter. Must be a power of two greater 52541cb0ef41Sopenharmony_ci than one. **Default:** `16384`. 52551cb0ef41Sopenharmony_ci * `blockSize` {number} Block size parameter. **Default:** `8`. 52561cb0ef41Sopenharmony_ci * `parallelization` {number} Parallelization parameter. **Default:** `1`. 52571cb0ef41Sopenharmony_ci * `N` {number} Alias for `cost`. Only one of both may be specified. 52581cb0ef41Sopenharmony_ci * `r` {number} Alias for `blockSize`. Only one of both may be specified. 52591cb0ef41Sopenharmony_ci * `p` {number} Alias for `parallelization`. Only one of both may be specified. 52601cb0ef41Sopenharmony_ci * `maxmem` {number} Memory upper bound. It is an error when (approximately) 52611cb0ef41Sopenharmony_ci `128 * N * r > maxmem`. **Default:** `32 * 1024 * 1024`. 52621cb0ef41Sopenharmony_ci* Returns: {Buffer} 52631cb0ef41Sopenharmony_ci 52641cb0ef41Sopenharmony_ciProvides a synchronous [scrypt][] implementation. Scrypt is a password-based 52651cb0ef41Sopenharmony_cikey derivation function that is designed to be expensive computationally and 52661cb0ef41Sopenharmony_cimemory-wise in order to make brute-force attacks unrewarding. 52671cb0ef41Sopenharmony_ci 52681cb0ef41Sopenharmony_ciThe `salt` should be as unique as possible. It is recommended that a salt is 52691cb0ef41Sopenharmony_cirandom and at least 16 bytes long. See [NIST SP 800-132][] for details. 52701cb0ef41Sopenharmony_ci 52711cb0ef41Sopenharmony_ciWhen passing strings for `password` or `salt`, please consider 52721cb0ef41Sopenharmony_ci[caveats when using strings as inputs to cryptographic APIs][]. 52731cb0ef41Sopenharmony_ci 52741cb0ef41Sopenharmony_ciAn exception is thrown when key derivation fails, otherwise the derived key is 52751cb0ef41Sopenharmony_cireturned as a [`Buffer`][]. 52761cb0ef41Sopenharmony_ci 52771cb0ef41Sopenharmony_ciAn exception is thrown when any of the input arguments specify invalid values 52781cb0ef41Sopenharmony_cior types. 52791cb0ef41Sopenharmony_ci 52801cb0ef41Sopenharmony_ci```mjs 52811cb0ef41Sopenharmony_ciconst { 52821cb0ef41Sopenharmony_ci scryptSync, 52831cb0ef41Sopenharmony_ci} = await import('node:crypto'); 52841cb0ef41Sopenharmony_ci// Using the factory defaults. 52851cb0ef41Sopenharmony_ci 52861cb0ef41Sopenharmony_ciconst key1 = scryptSync('password', 'salt', 64); 52871cb0ef41Sopenharmony_ciconsole.log(key1.toString('hex')); // '3745e48...08d59ae' 52881cb0ef41Sopenharmony_ci// Using a custom N parameter. Must be a power of two. 52891cb0ef41Sopenharmony_ciconst key2 = scryptSync('password', 'salt', 64, { N: 1024 }); 52901cb0ef41Sopenharmony_ciconsole.log(key2.toString('hex')); // '3745e48...aa39b34' 52911cb0ef41Sopenharmony_ci``` 52921cb0ef41Sopenharmony_ci 52931cb0ef41Sopenharmony_ci```cjs 52941cb0ef41Sopenharmony_ciconst { 52951cb0ef41Sopenharmony_ci scryptSync, 52961cb0ef41Sopenharmony_ci} = require('node:crypto'); 52971cb0ef41Sopenharmony_ci// Using the factory defaults. 52981cb0ef41Sopenharmony_ci 52991cb0ef41Sopenharmony_ciconst key1 = scryptSync('password', 'salt', 64); 53001cb0ef41Sopenharmony_ciconsole.log(key1.toString('hex')); // '3745e48...08d59ae' 53011cb0ef41Sopenharmony_ci// Using a custom N parameter. Must be a power of two. 53021cb0ef41Sopenharmony_ciconst key2 = scryptSync('password', 'salt', 64, { N: 1024 }); 53031cb0ef41Sopenharmony_ciconsole.log(key2.toString('hex')); // '3745e48...aa39b34' 53041cb0ef41Sopenharmony_ci``` 53051cb0ef41Sopenharmony_ci 53061cb0ef41Sopenharmony_ci### `crypto.secureHeapUsed()` 53071cb0ef41Sopenharmony_ci 53081cb0ef41Sopenharmony_ci<!-- YAML 53091cb0ef41Sopenharmony_ciadded: v15.6.0 53101cb0ef41Sopenharmony_ci--> 53111cb0ef41Sopenharmony_ci 53121cb0ef41Sopenharmony_ci* Returns: {Object} 53131cb0ef41Sopenharmony_ci * `total` {number} The total allocated secure heap size as specified 53141cb0ef41Sopenharmony_ci using the `--secure-heap=n` command-line flag. 53151cb0ef41Sopenharmony_ci * `min` {number} The minimum allocation from the secure heap as 53161cb0ef41Sopenharmony_ci specified using the `--secure-heap-min` command-line flag. 53171cb0ef41Sopenharmony_ci * `used` {number} The total number of bytes currently allocated from 53181cb0ef41Sopenharmony_ci the secure heap. 53191cb0ef41Sopenharmony_ci * `utilization` {number} The calculated ratio of `used` to `total` 53201cb0ef41Sopenharmony_ci allocated bytes. 53211cb0ef41Sopenharmony_ci 53221cb0ef41Sopenharmony_ci### `crypto.setEngine(engine[, flags])` 53231cb0ef41Sopenharmony_ci 53241cb0ef41Sopenharmony_ci<!-- YAML 53251cb0ef41Sopenharmony_ciadded: v0.11.11 53261cb0ef41Sopenharmony_ci--> 53271cb0ef41Sopenharmony_ci 53281cb0ef41Sopenharmony_ci* `engine` {string} 53291cb0ef41Sopenharmony_ci* `flags` {crypto.constants} **Default:** `crypto.constants.ENGINE_METHOD_ALL` 53301cb0ef41Sopenharmony_ci 53311cb0ef41Sopenharmony_ciLoad and set the `engine` for some or all OpenSSL functions (selected by flags). 53321cb0ef41Sopenharmony_ci 53331cb0ef41Sopenharmony_ci`engine` could be either an id or a path to the engine's shared library. 53341cb0ef41Sopenharmony_ci 53351cb0ef41Sopenharmony_ciThe optional `flags` argument uses `ENGINE_METHOD_ALL` by default. The `flags` 53361cb0ef41Sopenharmony_ciis a bit field taking one of or a mix of the following flags (defined in 53371cb0ef41Sopenharmony_ci`crypto.constants`): 53381cb0ef41Sopenharmony_ci 53391cb0ef41Sopenharmony_ci* `crypto.constants.ENGINE_METHOD_RSA` 53401cb0ef41Sopenharmony_ci* `crypto.constants.ENGINE_METHOD_DSA` 53411cb0ef41Sopenharmony_ci* `crypto.constants.ENGINE_METHOD_DH` 53421cb0ef41Sopenharmony_ci* `crypto.constants.ENGINE_METHOD_RAND` 53431cb0ef41Sopenharmony_ci* `crypto.constants.ENGINE_METHOD_EC` 53441cb0ef41Sopenharmony_ci* `crypto.constants.ENGINE_METHOD_CIPHERS` 53451cb0ef41Sopenharmony_ci* `crypto.constants.ENGINE_METHOD_DIGESTS` 53461cb0ef41Sopenharmony_ci* `crypto.constants.ENGINE_METHOD_PKEY_METHS` 53471cb0ef41Sopenharmony_ci* `crypto.constants.ENGINE_METHOD_PKEY_ASN1_METHS` 53481cb0ef41Sopenharmony_ci* `crypto.constants.ENGINE_METHOD_ALL` 53491cb0ef41Sopenharmony_ci* `crypto.constants.ENGINE_METHOD_NONE` 53501cb0ef41Sopenharmony_ci 53511cb0ef41Sopenharmony_ci### `crypto.setFips(bool)` 53521cb0ef41Sopenharmony_ci 53531cb0ef41Sopenharmony_ci<!-- YAML 53541cb0ef41Sopenharmony_ciadded: v10.0.0 53551cb0ef41Sopenharmony_ci--> 53561cb0ef41Sopenharmony_ci 53571cb0ef41Sopenharmony_ci* `bool` {boolean} `true` to enable FIPS mode. 53581cb0ef41Sopenharmony_ci 53591cb0ef41Sopenharmony_ciEnables the FIPS compliant crypto provider in a FIPS-enabled Node.js build. 53601cb0ef41Sopenharmony_ciThrows an error if FIPS mode is not available. 53611cb0ef41Sopenharmony_ci 53621cb0ef41Sopenharmony_ci### `crypto.sign(algorithm, data, key[, callback])` 53631cb0ef41Sopenharmony_ci 53641cb0ef41Sopenharmony_ci<!-- YAML 53651cb0ef41Sopenharmony_ciadded: v12.0.0 53661cb0ef41Sopenharmony_cichanges: 53671cb0ef41Sopenharmony_ci - version: v18.0.0 53681cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41678 53691cb0ef41Sopenharmony_ci description: Passing an invalid callback to the `callback` argument 53701cb0ef41Sopenharmony_ci now throws `ERR_INVALID_ARG_TYPE` instead of 53711cb0ef41Sopenharmony_ci `ERR_INVALID_CALLBACK`. 53721cb0ef41Sopenharmony_ci - version: v15.12.0 53731cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/37500 53741cb0ef41Sopenharmony_ci description: Optional callback argument added. 53751cb0ef41Sopenharmony_ci - version: 53761cb0ef41Sopenharmony_ci - v13.2.0 53771cb0ef41Sopenharmony_ci - v12.16.0 53781cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/29292 53791cb0ef41Sopenharmony_ci description: This function now supports IEEE-P1363 DSA and ECDSA signatures. 53801cb0ef41Sopenharmony_ci--> 53811cb0ef41Sopenharmony_ci 53821cb0ef41Sopenharmony_ci<!--lint disable maximum-line-length remark-lint--> 53831cb0ef41Sopenharmony_ci 53841cb0ef41Sopenharmony_ci* `algorithm` {string | null | undefined} 53851cb0ef41Sopenharmony_ci* `data` {ArrayBuffer|Buffer|TypedArray|DataView} 53861cb0ef41Sopenharmony_ci* `key` {Object|string|ArrayBuffer|Buffer|TypedArray|DataView|KeyObject|CryptoKey} 53871cb0ef41Sopenharmony_ci* `callback` {Function} 53881cb0ef41Sopenharmony_ci * `err` {Error} 53891cb0ef41Sopenharmony_ci * `signature` {Buffer} 53901cb0ef41Sopenharmony_ci* Returns: {Buffer} if the `callback` function is not provided. 53911cb0ef41Sopenharmony_ci 53921cb0ef41Sopenharmony_ci<!--lint enable maximum-line-length remark-lint--> 53931cb0ef41Sopenharmony_ci 53941cb0ef41Sopenharmony_ciCalculates and returns the signature for `data` using the given private key and 53951cb0ef41Sopenharmony_cialgorithm. If `algorithm` is `null` or `undefined`, then the algorithm is 53961cb0ef41Sopenharmony_cidependent upon the key type (especially Ed25519 and Ed448). 53971cb0ef41Sopenharmony_ci 53981cb0ef41Sopenharmony_ciIf `key` is not a [`KeyObject`][], this function behaves as if `key` had been 53991cb0ef41Sopenharmony_cipassed to [`crypto.createPrivateKey()`][]. If it is an object, the following 54001cb0ef41Sopenharmony_ciadditional properties can be passed: 54011cb0ef41Sopenharmony_ci 54021cb0ef41Sopenharmony_ci* `dsaEncoding` {string} For DSA and ECDSA, this option specifies the 54031cb0ef41Sopenharmony_ci format of the generated signature. It can be one of the following: 54041cb0ef41Sopenharmony_ci * `'der'` (default): DER-encoded ASN.1 signature structure encoding `(r, s)`. 54051cb0ef41Sopenharmony_ci * `'ieee-p1363'`: Signature format `r || s` as proposed in IEEE-P1363. 54061cb0ef41Sopenharmony_ci* `padding` {integer} Optional padding value for RSA, one of the following: 54071cb0ef41Sopenharmony_ci 54081cb0ef41Sopenharmony_ci * `crypto.constants.RSA_PKCS1_PADDING` (default) 54091cb0ef41Sopenharmony_ci * `crypto.constants.RSA_PKCS1_PSS_PADDING` 54101cb0ef41Sopenharmony_ci 54111cb0ef41Sopenharmony_ci `RSA_PKCS1_PSS_PADDING` will use MGF1 with the same hash function 54121cb0ef41Sopenharmony_ci used to sign the message as specified in section 3.1 of [RFC 4055][]. 54131cb0ef41Sopenharmony_ci* `saltLength` {integer} Salt length for when padding is 54141cb0ef41Sopenharmony_ci `RSA_PKCS1_PSS_PADDING`. The special value 54151cb0ef41Sopenharmony_ci `crypto.constants.RSA_PSS_SALTLEN_DIGEST` sets the salt length to the digest 54161cb0ef41Sopenharmony_ci size, `crypto.constants.RSA_PSS_SALTLEN_MAX_SIGN` (default) sets it to the 54171cb0ef41Sopenharmony_ci maximum permissible value. 54181cb0ef41Sopenharmony_ci 54191cb0ef41Sopenharmony_ciIf the `callback` function is provided this function uses libuv's threadpool. 54201cb0ef41Sopenharmony_ci 54211cb0ef41Sopenharmony_ci### `crypto.subtle` 54221cb0ef41Sopenharmony_ci 54231cb0ef41Sopenharmony_ci<!-- YAML 54241cb0ef41Sopenharmony_ciadded: v17.4.0 54251cb0ef41Sopenharmony_ci--> 54261cb0ef41Sopenharmony_ci 54271cb0ef41Sopenharmony_ci* Type: {SubtleCrypto} 54281cb0ef41Sopenharmony_ci 54291cb0ef41Sopenharmony_ciA convenient alias for [`crypto.webcrypto.subtle`][]. 54301cb0ef41Sopenharmony_ci 54311cb0ef41Sopenharmony_ci### `crypto.timingSafeEqual(a, b)` 54321cb0ef41Sopenharmony_ci 54331cb0ef41Sopenharmony_ci<!-- YAML 54341cb0ef41Sopenharmony_ciadded: v6.6.0 54351cb0ef41Sopenharmony_cichanges: 54361cb0ef41Sopenharmony_ci - version: v15.0.0 54371cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/35093 54381cb0ef41Sopenharmony_ci description: The a and b arguments can also be ArrayBuffer. 54391cb0ef41Sopenharmony_ci--> 54401cb0ef41Sopenharmony_ci 54411cb0ef41Sopenharmony_ci* `a` {ArrayBuffer|Buffer|TypedArray|DataView} 54421cb0ef41Sopenharmony_ci* `b` {ArrayBuffer|Buffer|TypedArray|DataView} 54431cb0ef41Sopenharmony_ci* Returns: {boolean} 54441cb0ef41Sopenharmony_ci 54451cb0ef41Sopenharmony_ciThis function compares the underlying bytes that represent the given 54461cb0ef41Sopenharmony_ci`ArrayBuffer`, `TypedArray`, or `DataView` instances using a constant-time 54471cb0ef41Sopenharmony_cialgorithm. 54481cb0ef41Sopenharmony_ci 54491cb0ef41Sopenharmony_ciThis function does not leak timing information that 54501cb0ef41Sopenharmony_ciwould allow an attacker to guess one of the values. This is suitable for 54511cb0ef41Sopenharmony_cicomparing HMAC digests or secret values like authentication cookies or 54521cb0ef41Sopenharmony_ci[capability urls](https://www.w3.org/TR/capability-urls/). 54531cb0ef41Sopenharmony_ci 54541cb0ef41Sopenharmony_ci`a` and `b` must both be `Buffer`s, `TypedArray`s, or `DataView`s, and they 54551cb0ef41Sopenharmony_cimust have the same byte length. An error is thrown if `a` and `b` have 54561cb0ef41Sopenharmony_cidifferent byte lengths. 54571cb0ef41Sopenharmony_ci 54581cb0ef41Sopenharmony_ciIf at least one of `a` and `b` is a `TypedArray` with more than one byte per 54591cb0ef41Sopenharmony_cientry, such as `Uint16Array`, the result will be computed using the platform 54601cb0ef41Sopenharmony_cibyte order. 54611cb0ef41Sopenharmony_ci 54621cb0ef41Sopenharmony_ci<strong class="critical">When both of the inputs are `Float32Array`s or 54631cb0ef41Sopenharmony_ci`Float64Array`s, this function might return unexpected results due to IEEE 754 54641cb0ef41Sopenharmony_ciencoding of floating-point numbers. In particular, neither `x === y` nor 54651cb0ef41Sopenharmony_ci`Object.is(x, y)` implies that the byte representations of two floating-point 54661cb0ef41Sopenharmony_cinumbers `x` and `y` are equal.</strong> 54671cb0ef41Sopenharmony_ci 54681cb0ef41Sopenharmony_ciUse of `crypto.timingSafeEqual` does not guarantee that the _surrounding_ code 54691cb0ef41Sopenharmony_ciis timing-safe. Care should be taken to ensure that the surrounding code does 54701cb0ef41Sopenharmony_cinot introduce timing vulnerabilities. 54711cb0ef41Sopenharmony_ci 54721cb0ef41Sopenharmony_ci### `crypto.verify(algorithm, data, key, signature[, callback])` 54731cb0ef41Sopenharmony_ci 54741cb0ef41Sopenharmony_ci<!-- YAML 54751cb0ef41Sopenharmony_ciadded: v12.0.0 54761cb0ef41Sopenharmony_cichanges: 54771cb0ef41Sopenharmony_ci - version: v18.0.0 54781cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41678 54791cb0ef41Sopenharmony_ci description: Passing an invalid callback to the `callback` argument 54801cb0ef41Sopenharmony_ci now throws `ERR_INVALID_ARG_TYPE` instead of 54811cb0ef41Sopenharmony_ci `ERR_INVALID_CALLBACK`. 54821cb0ef41Sopenharmony_ci - version: v15.12.0 54831cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/37500 54841cb0ef41Sopenharmony_ci description: Optional callback argument added. 54851cb0ef41Sopenharmony_ci - version: v15.0.0 54861cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/35093 54871cb0ef41Sopenharmony_ci description: The data, key, and signature arguments can also be ArrayBuffer. 54881cb0ef41Sopenharmony_ci - version: 54891cb0ef41Sopenharmony_ci - v13.2.0 54901cb0ef41Sopenharmony_ci - v12.16.0 54911cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/29292 54921cb0ef41Sopenharmony_ci description: This function now supports IEEE-P1363 DSA and ECDSA signatures. 54931cb0ef41Sopenharmony_ci--> 54941cb0ef41Sopenharmony_ci 54951cb0ef41Sopenharmony_ci<!--lint disable maximum-line-length remark-lint--> 54961cb0ef41Sopenharmony_ci 54971cb0ef41Sopenharmony_ci* `algorithm` {string|null|undefined} 54981cb0ef41Sopenharmony_ci* `data` {ArrayBuffer| Buffer|TypedArray|DataView} 54991cb0ef41Sopenharmony_ci* `key` {Object|string|ArrayBuffer|Buffer|TypedArray|DataView|KeyObject|CryptoKey} 55001cb0ef41Sopenharmony_ci* `signature` {ArrayBuffer|Buffer|TypedArray|DataView} 55011cb0ef41Sopenharmony_ci* `callback` {Function} 55021cb0ef41Sopenharmony_ci * `err` {Error} 55031cb0ef41Sopenharmony_ci * `result` {boolean} 55041cb0ef41Sopenharmony_ci* Returns: {boolean} `true` or `false` depending on the validity of the 55051cb0ef41Sopenharmony_ci signature for the data and public key if the `callback` function is not 55061cb0ef41Sopenharmony_ci provided. 55071cb0ef41Sopenharmony_ci 55081cb0ef41Sopenharmony_ci<!--lint enable maximum-line-length remark-lint--> 55091cb0ef41Sopenharmony_ci 55101cb0ef41Sopenharmony_ciVerifies the given signature for `data` using the given key and algorithm. If 55111cb0ef41Sopenharmony_ci`algorithm` is `null` or `undefined`, then the algorithm is dependent upon the 55121cb0ef41Sopenharmony_cikey type (especially Ed25519 and Ed448). 55131cb0ef41Sopenharmony_ci 55141cb0ef41Sopenharmony_ciIf `key` is not a [`KeyObject`][], this function behaves as if `key` had been 55151cb0ef41Sopenharmony_cipassed to [`crypto.createPublicKey()`][]. If it is an object, the following 55161cb0ef41Sopenharmony_ciadditional properties can be passed: 55171cb0ef41Sopenharmony_ci 55181cb0ef41Sopenharmony_ci* `dsaEncoding` {string} For DSA and ECDSA, this option specifies the 55191cb0ef41Sopenharmony_ci format of the signature. It can be one of the following: 55201cb0ef41Sopenharmony_ci * `'der'` (default): DER-encoded ASN.1 signature structure encoding `(r, s)`. 55211cb0ef41Sopenharmony_ci * `'ieee-p1363'`: Signature format `r || s` as proposed in IEEE-P1363. 55221cb0ef41Sopenharmony_ci* `padding` {integer} Optional padding value for RSA, one of the following: 55231cb0ef41Sopenharmony_ci 55241cb0ef41Sopenharmony_ci * `crypto.constants.RSA_PKCS1_PADDING` (default) 55251cb0ef41Sopenharmony_ci * `crypto.constants.RSA_PKCS1_PSS_PADDING` 55261cb0ef41Sopenharmony_ci 55271cb0ef41Sopenharmony_ci `RSA_PKCS1_PSS_PADDING` will use MGF1 with the same hash function 55281cb0ef41Sopenharmony_ci used to sign the message as specified in section 3.1 of [RFC 4055][]. 55291cb0ef41Sopenharmony_ci* `saltLength` {integer} Salt length for when padding is 55301cb0ef41Sopenharmony_ci `RSA_PKCS1_PSS_PADDING`. The special value 55311cb0ef41Sopenharmony_ci `crypto.constants.RSA_PSS_SALTLEN_DIGEST` sets the salt length to the digest 55321cb0ef41Sopenharmony_ci size, `crypto.constants.RSA_PSS_SALTLEN_MAX_SIGN` (default) sets it to the 55331cb0ef41Sopenharmony_ci maximum permissible value. 55341cb0ef41Sopenharmony_ci 55351cb0ef41Sopenharmony_ciThe `signature` argument is the previously calculated signature for the `data`. 55361cb0ef41Sopenharmony_ci 55371cb0ef41Sopenharmony_ciBecause public keys can be derived from private keys, a private key or a public 55381cb0ef41Sopenharmony_cikey may be passed for `key`. 55391cb0ef41Sopenharmony_ci 55401cb0ef41Sopenharmony_ciIf the `callback` function is provided this function uses libuv's threadpool. 55411cb0ef41Sopenharmony_ci 55421cb0ef41Sopenharmony_ci### `crypto.webcrypto` 55431cb0ef41Sopenharmony_ci 55441cb0ef41Sopenharmony_ci<!-- YAML 55451cb0ef41Sopenharmony_ciadded: v15.0.0 55461cb0ef41Sopenharmony_ci--> 55471cb0ef41Sopenharmony_ci 55481cb0ef41Sopenharmony_ciType: {Crypto} An implementation of the Web Crypto API standard. 55491cb0ef41Sopenharmony_ci 55501cb0ef41Sopenharmony_ciSee the [Web Crypto API documentation][] for details. 55511cb0ef41Sopenharmony_ci 55521cb0ef41Sopenharmony_ci## Notes 55531cb0ef41Sopenharmony_ci 55541cb0ef41Sopenharmony_ci### Using strings as inputs to cryptographic APIs 55551cb0ef41Sopenharmony_ci 55561cb0ef41Sopenharmony_ciFor historical reasons, many cryptographic APIs provided by Node.js accept 55571cb0ef41Sopenharmony_cistrings as inputs where the underlying cryptographic algorithm works on byte 55581cb0ef41Sopenharmony_cisequences. These instances include plaintexts, ciphertexts, symmetric keys, 55591cb0ef41Sopenharmony_ciinitialization vectors, passphrases, salts, authentication tags, 55601cb0ef41Sopenharmony_ciand additional authenticated data. 55611cb0ef41Sopenharmony_ci 55621cb0ef41Sopenharmony_ciWhen passing strings to cryptographic APIs, consider the following factors. 55631cb0ef41Sopenharmony_ci 55641cb0ef41Sopenharmony_ci* Not all byte sequences are valid UTF-8 strings. Therefore, when a byte 55651cb0ef41Sopenharmony_ci sequence of length `n` is derived from a string, its entropy is generally 55661cb0ef41Sopenharmony_ci lower than the entropy of a random or pseudorandom `n` byte sequence. 55671cb0ef41Sopenharmony_ci For example, no UTF-8 string will result in the byte sequence `c0 af`. Secret 55681cb0ef41Sopenharmony_ci keys should almost exclusively be random or pseudorandom byte sequences. 55691cb0ef41Sopenharmony_ci* Similarly, when converting random or pseudorandom byte sequences to UTF-8 55701cb0ef41Sopenharmony_ci strings, subsequences that do not represent valid code points may be replaced 55711cb0ef41Sopenharmony_ci by the Unicode replacement character (`U+FFFD`). The byte representation of 55721cb0ef41Sopenharmony_ci the resulting Unicode string may, therefore, not be equal to the byte sequence 55731cb0ef41Sopenharmony_ci that the string was created from. 55741cb0ef41Sopenharmony_ci 55751cb0ef41Sopenharmony_ci ```js 55761cb0ef41Sopenharmony_ci const original = [0xc0, 0xaf]; 55771cb0ef41Sopenharmony_ci const bytesAsString = Buffer.from(original).toString('utf8'); 55781cb0ef41Sopenharmony_ci const stringAsBytes = Buffer.from(bytesAsString, 'utf8'); 55791cb0ef41Sopenharmony_ci console.log(stringAsBytes); 55801cb0ef41Sopenharmony_ci // Prints '<Buffer ef bf bd ef bf bd>'. 55811cb0ef41Sopenharmony_ci ``` 55821cb0ef41Sopenharmony_ci 55831cb0ef41Sopenharmony_ci The outputs of ciphers, hash functions, signature algorithms, and key 55841cb0ef41Sopenharmony_ci derivation functions are pseudorandom byte sequences and should not be 55851cb0ef41Sopenharmony_ci used as Unicode strings. 55861cb0ef41Sopenharmony_ci* When strings are obtained from user input, some Unicode characters can be 55871cb0ef41Sopenharmony_ci represented in multiple equivalent ways that result in different byte 55881cb0ef41Sopenharmony_ci sequences. For example, when passing a user passphrase to a key derivation 55891cb0ef41Sopenharmony_ci function, such as PBKDF2 or scrypt, the result of the key derivation function 55901cb0ef41Sopenharmony_ci depends on whether the string uses composed or decomposed characters. Node.js 55911cb0ef41Sopenharmony_ci does not normalize character representations. Developers should consider using 55921cb0ef41Sopenharmony_ci [`String.prototype.normalize()`][] on user inputs before passing them to 55931cb0ef41Sopenharmony_ci cryptographic APIs. 55941cb0ef41Sopenharmony_ci 55951cb0ef41Sopenharmony_ci### Legacy streams API (prior to Node.js 0.10) 55961cb0ef41Sopenharmony_ci 55971cb0ef41Sopenharmony_ciThe Crypto module was added to Node.js before there was the concept of a 55981cb0ef41Sopenharmony_ciunified Stream API, and before there were [`Buffer`][] objects for handling 55991cb0ef41Sopenharmony_cibinary data. As such, many `crypto` classes have methods not 56001cb0ef41Sopenharmony_citypically found on other Node.js classes that implement the [streams][stream] 56011cb0ef41Sopenharmony_ciAPI (e.g. `update()`, `final()`, or `digest()`). Also, many methods accepted 56021cb0ef41Sopenharmony_ciand returned `'latin1'` encoded strings by default rather than `Buffer`s. This 56031cb0ef41Sopenharmony_cidefault was changed after Node.js v0.8 to use [`Buffer`][] objects by default 56041cb0ef41Sopenharmony_ciinstead. 56051cb0ef41Sopenharmony_ci 56061cb0ef41Sopenharmony_ci### Support for weak or compromised algorithms 56071cb0ef41Sopenharmony_ci 56081cb0ef41Sopenharmony_ciThe `node:crypto` module still supports some algorithms which are already 56091cb0ef41Sopenharmony_cicompromised and are not recommended for use. The API also allows 56101cb0ef41Sopenharmony_cithe use of ciphers and hashes with a small key size that are too weak for safe 56111cb0ef41Sopenharmony_ciuse. 56121cb0ef41Sopenharmony_ci 56131cb0ef41Sopenharmony_ciUsers should take full responsibility for selecting the crypto 56141cb0ef41Sopenharmony_cialgorithm and key size according to their security requirements. 56151cb0ef41Sopenharmony_ci 56161cb0ef41Sopenharmony_ciBased on the recommendations of [NIST SP 800-131A][]: 56171cb0ef41Sopenharmony_ci 56181cb0ef41Sopenharmony_ci* MD5 and SHA-1 are no longer acceptable where collision resistance is 56191cb0ef41Sopenharmony_ci required such as digital signatures. 56201cb0ef41Sopenharmony_ci* The key used with RSA, DSA, and DH algorithms is recommended to have 56211cb0ef41Sopenharmony_ci at least 2048 bits and that of the curve of ECDSA and ECDH at least 56221cb0ef41Sopenharmony_ci 224 bits, to be safe to use for several years. 56231cb0ef41Sopenharmony_ci* The DH groups of `modp1`, `modp2` and `modp5` have a key size 56241cb0ef41Sopenharmony_ci smaller than 2048 bits and are not recommended. 56251cb0ef41Sopenharmony_ci 56261cb0ef41Sopenharmony_ciSee the reference for other recommendations and details. 56271cb0ef41Sopenharmony_ci 56281cb0ef41Sopenharmony_ciSome algorithms that have known weaknesses and are of little relevance in 56291cb0ef41Sopenharmony_cipractice are only available through the [legacy provider][], which is not 56301cb0ef41Sopenharmony_cienabled by default. 56311cb0ef41Sopenharmony_ci 56321cb0ef41Sopenharmony_ci### CCM mode 56331cb0ef41Sopenharmony_ci 56341cb0ef41Sopenharmony_ciCCM is one of the supported [AEAD algorithms][]. Applications which use this 56351cb0ef41Sopenharmony_cimode must adhere to certain restrictions when using the cipher API: 56361cb0ef41Sopenharmony_ci 56371cb0ef41Sopenharmony_ci* The authentication tag length must be specified during cipher creation by 56381cb0ef41Sopenharmony_ci setting the `authTagLength` option and must be one of 4, 6, 8, 10, 12, 14 or 56391cb0ef41Sopenharmony_ci 16 bytes. 56401cb0ef41Sopenharmony_ci* The length of the initialization vector (nonce) `N` must be between 7 and 13 56411cb0ef41Sopenharmony_ci bytes (`7 ≤ N ≤ 13`). 56421cb0ef41Sopenharmony_ci* The length of the plaintext is limited to `2 ** (8 * (15 - N))` bytes. 56431cb0ef41Sopenharmony_ci* When decrypting, the authentication tag must be set via `setAuthTag()` before 56441cb0ef41Sopenharmony_ci calling `update()`. 56451cb0ef41Sopenharmony_ci Otherwise, decryption will fail and `final()` will throw an error in 56461cb0ef41Sopenharmony_ci compliance with section 2.6 of [RFC 3610][]. 56471cb0ef41Sopenharmony_ci* Using stream methods such as `write(data)`, `end(data)` or `pipe()` in CCM 56481cb0ef41Sopenharmony_ci mode might fail as CCM cannot handle more than one chunk of data per instance. 56491cb0ef41Sopenharmony_ci* When passing additional authenticated data (AAD), the length of the actual 56501cb0ef41Sopenharmony_ci message in bytes must be passed to `setAAD()` via the `plaintextLength` 56511cb0ef41Sopenharmony_ci option. 56521cb0ef41Sopenharmony_ci Many crypto libraries include the authentication tag in the ciphertext, 56531cb0ef41Sopenharmony_ci which means that they produce ciphertexts of the length 56541cb0ef41Sopenharmony_ci `plaintextLength + authTagLength`. Node.js does not include the authentication 56551cb0ef41Sopenharmony_ci tag, so the ciphertext length is always `plaintextLength`. 56561cb0ef41Sopenharmony_ci This is not necessary if no AAD is used. 56571cb0ef41Sopenharmony_ci* As CCM processes the whole message at once, `update()` must be called exactly 56581cb0ef41Sopenharmony_ci once. 56591cb0ef41Sopenharmony_ci* Even though calling `update()` is sufficient to encrypt/decrypt the message, 56601cb0ef41Sopenharmony_ci applications _must_ call `final()` to compute or verify the 56611cb0ef41Sopenharmony_ci authentication tag. 56621cb0ef41Sopenharmony_ci 56631cb0ef41Sopenharmony_ci```mjs 56641cb0ef41Sopenharmony_ciimport { Buffer } from 'node:buffer'; 56651cb0ef41Sopenharmony_ciconst { 56661cb0ef41Sopenharmony_ci createCipheriv, 56671cb0ef41Sopenharmony_ci createDecipheriv, 56681cb0ef41Sopenharmony_ci randomBytes, 56691cb0ef41Sopenharmony_ci} = await import('node:crypto'); 56701cb0ef41Sopenharmony_ci 56711cb0ef41Sopenharmony_ciconst key = 'keykeykeykeykeykeykeykey'; 56721cb0ef41Sopenharmony_ciconst nonce = randomBytes(12); 56731cb0ef41Sopenharmony_ci 56741cb0ef41Sopenharmony_ciconst aad = Buffer.from('0123456789', 'hex'); 56751cb0ef41Sopenharmony_ci 56761cb0ef41Sopenharmony_ciconst cipher = createCipheriv('aes-192-ccm', key, nonce, { 56771cb0ef41Sopenharmony_ci authTagLength: 16, 56781cb0ef41Sopenharmony_ci}); 56791cb0ef41Sopenharmony_ciconst plaintext = 'Hello world'; 56801cb0ef41Sopenharmony_cicipher.setAAD(aad, { 56811cb0ef41Sopenharmony_ci plaintextLength: Buffer.byteLength(plaintext), 56821cb0ef41Sopenharmony_ci}); 56831cb0ef41Sopenharmony_ciconst ciphertext = cipher.update(plaintext, 'utf8'); 56841cb0ef41Sopenharmony_cicipher.final(); 56851cb0ef41Sopenharmony_ciconst tag = cipher.getAuthTag(); 56861cb0ef41Sopenharmony_ci 56871cb0ef41Sopenharmony_ci// Now transmit { ciphertext, nonce, tag }. 56881cb0ef41Sopenharmony_ci 56891cb0ef41Sopenharmony_ciconst decipher = createDecipheriv('aes-192-ccm', key, nonce, { 56901cb0ef41Sopenharmony_ci authTagLength: 16, 56911cb0ef41Sopenharmony_ci}); 56921cb0ef41Sopenharmony_cidecipher.setAuthTag(tag); 56931cb0ef41Sopenharmony_cidecipher.setAAD(aad, { 56941cb0ef41Sopenharmony_ci plaintextLength: ciphertext.length, 56951cb0ef41Sopenharmony_ci}); 56961cb0ef41Sopenharmony_ciconst receivedPlaintext = decipher.update(ciphertext, null, 'utf8'); 56971cb0ef41Sopenharmony_ci 56981cb0ef41Sopenharmony_citry { 56991cb0ef41Sopenharmony_ci decipher.final(); 57001cb0ef41Sopenharmony_ci} catch (err) { 57011cb0ef41Sopenharmony_ci throw new Error('Authentication failed!', { cause: err }); 57021cb0ef41Sopenharmony_ci} 57031cb0ef41Sopenharmony_ci 57041cb0ef41Sopenharmony_ciconsole.log(receivedPlaintext); 57051cb0ef41Sopenharmony_ci``` 57061cb0ef41Sopenharmony_ci 57071cb0ef41Sopenharmony_ci```cjs 57081cb0ef41Sopenharmony_ciconst { Buffer } = require('node:buffer'); 57091cb0ef41Sopenharmony_ciconst { 57101cb0ef41Sopenharmony_ci createCipheriv, 57111cb0ef41Sopenharmony_ci createDecipheriv, 57121cb0ef41Sopenharmony_ci randomBytes, 57131cb0ef41Sopenharmony_ci} = require('node:crypto'); 57141cb0ef41Sopenharmony_ci 57151cb0ef41Sopenharmony_ciconst key = 'keykeykeykeykeykeykeykey'; 57161cb0ef41Sopenharmony_ciconst nonce = randomBytes(12); 57171cb0ef41Sopenharmony_ci 57181cb0ef41Sopenharmony_ciconst aad = Buffer.from('0123456789', 'hex'); 57191cb0ef41Sopenharmony_ci 57201cb0ef41Sopenharmony_ciconst cipher = createCipheriv('aes-192-ccm', key, nonce, { 57211cb0ef41Sopenharmony_ci authTagLength: 16, 57221cb0ef41Sopenharmony_ci}); 57231cb0ef41Sopenharmony_ciconst plaintext = 'Hello world'; 57241cb0ef41Sopenharmony_cicipher.setAAD(aad, { 57251cb0ef41Sopenharmony_ci plaintextLength: Buffer.byteLength(plaintext), 57261cb0ef41Sopenharmony_ci}); 57271cb0ef41Sopenharmony_ciconst ciphertext = cipher.update(plaintext, 'utf8'); 57281cb0ef41Sopenharmony_cicipher.final(); 57291cb0ef41Sopenharmony_ciconst tag = cipher.getAuthTag(); 57301cb0ef41Sopenharmony_ci 57311cb0ef41Sopenharmony_ci// Now transmit { ciphertext, nonce, tag }. 57321cb0ef41Sopenharmony_ci 57331cb0ef41Sopenharmony_ciconst decipher = createDecipheriv('aes-192-ccm', key, nonce, { 57341cb0ef41Sopenharmony_ci authTagLength: 16, 57351cb0ef41Sopenharmony_ci}); 57361cb0ef41Sopenharmony_cidecipher.setAuthTag(tag); 57371cb0ef41Sopenharmony_cidecipher.setAAD(aad, { 57381cb0ef41Sopenharmony_ci plaintextLength: ciphertext.length, 57391cb0ef41Sopenharmony_ci}); 57401cb0ef41Sopenharmony_ciconst receivedPlaintext = decipher.update(ciphertext, null, 'utf8'); 57411cb0ef41Sopenharmony_ci 57421cb0ef41Sopenharmony_citry { 57431cb0ef41Sopenharmony_ci decipher.final(); 57441cb0ef41Sopenharmony_ci} catch (err) { 57451cb0ef41Sopenharmony_ci throw new Error('Authentication failed!', { cause: err }); 57461cb0ef41Sopenharmony_ci} 57471cb0ef41Sopenharmony_ci 57481cb0ef41Sopenharmony_ciconsole.log(receivedPlaintext); 57491cb0ef41Sopenharmony_ci``` 57501cb0ef41Sopenharmony_ci 57511cb0ef41Sopenharmony_ci### FIPS mode 57521cb0ef41Sopenharmony_ci 57531cb0ef41Sopenharmony_ciWhen using OpenSSL 3, Node.js supports FIPS 140-2 when used with an appropriate 57541cb0ef41Sopenharmony_ciOpenSSL 3 provider, such as the [FIPS provider from OpenSSL 3][] which can be 57551cb0ef41Sopenharmony_ciinstalled by following the instructions in [OpenSSL's FIPS README file][]. 57561cb0ef41Sopenharmony_ci 57571cb0ef41Sopenharmony_ciFor FIPS support in Node.js you will need: 57581cb0ef41Sopenharmony_ci 57591cb0ef41Sopenharmony_ci* A correctly installed OpenSSL 3 FIPS provider. 57601cb0ef41Sopenharmony_ci* An OpenSSL 3 [FIPS module configuration file][]. 57611cb0ef41Sopenharmony_ci* An OpenSSL 3 configuration file that references the FIPS module 57621cb0ef41Sopenharmony_ci configuration file. 57631cb0ef41Sopenharmony_ci 57641cb0ef41Sopenharmony_ciNode.js will need to be configured with an OpenSSL configuration file that 57651cb0ef41Sopenharmony_cipoints to the FIPS provider. An example configuration file looks like this: 57661cb0ef41Sopenharmony_ci 57671cb0ef41Sopenharmony_ci```text 57681cb0ef41Sopenharmony_cinodejs_conf = nodejs_init 57691cb0ef41Sopenharmony_ci 57701cb0ef41Sopenharmony_ci.include /<absolute path>/fipsmodule.cnf 57711cb0ef41Sopenharmony_ci 57721cb0ef41Sopenharmony_ci[nodejs_init] 57731cb0ef41Sopenharmony_ciproviders = provider_sect 57741cb0ef41Sopenharmony_ci 57751cb0ef41Sopenharmony_ci[provider_sect] 57761cb0ef41Sopenharmony_cidefault = default_sect 57771cb0ef41Sopenharmony_ci# The fips section name should match the section name inside the 57781cb0ef41Sopenharmony_ci# included fipsmodule.cnf. 57791cb0ef41Sopenharmony_cifips = fips_sect 57801cb0ef41Sopenharmony_ci 57811cb0ef41Sopenharmony_ci[default_sect] 57821cb0ef41Sopenharmony_ciactivate = 1 57831cb0ef41Sopenharmony_ci``` 57841cb0ef41Sopenharmony_ci 57851cb0ef41Sopenharmony_ciwhere `fipsmodule.cnf` is the FIPS module configuration file generated from the 57861cb0ef41Sopenharmony_ciFIPS provider installation step: 57871cb0ef41Sopenharmony_ci 57881cb0ef41Sopenharmony_ci```bash 57891cb0ef41Sopenharmony_ciopenssl fipsinstall 57901cb0ef41Sopenharmony_ci``` 57911cb0ef41Sopenharmony_ci 57921cb0ef41Sopenharmony_ciSet the `OPENSSL_CONF` environment variable to point to 57931cb0ef41Sopenharmony_ciyour configuration file and `OPENSSL_MODULES` to the location of the FIPS 57941cb0ef41Sopenharmony_ciprovider dynamic library. e.g. 57951cb0ef41Sopenharmony_ci 57961cb0ef41Sopenharmony_ci```bash 57971cb0ef41Sopenharmony_ciexport OPENSSL_CONF=/<path to configuration file>/nodejs.cnf 57981cb0ef41Sopenharmony_ciexport OPENSSL_MODULES=/<path to openssl lib>/ossl-modules 57991cb0ef41Sopenharmony_ci``` 58001cb0ef41Sopenharmony_ci 58011cb0ef41Sopenharmony_ciFIPS mode can then be enabled in Node.js either by: 58021cb0ef41Sopenharmony_ci 58031cb0ef41Sopenharmony_ci* Starting Node.js with `--enable-fips` or `--force-fips` command line flags. 58041cb0ef41Sopenharmony_ci* Programmatically calling `crypto.setFips(true)`. 58051cb0ef41Sopenharmony_ci 58061cb0ef41Sopenharmony_ciOptionally FIPS mode can be enabled in Node.js via the OpenSSL configuration 58071cb0ef41Sopenharmony_cifile. e.g. 58081cb0ef41Sopenharmony_ci 58091cb0ef41Sopenharmony_ci```text 58101cb0ef41Sopenharmony_cinodejs_conf = nodejs_init 58111cb0ef41Sopenharmony_ci 58121cb0ef41Sopenharmony_ci.include /<absolute path>/fipsmodule.cnf 58131cb0ef41Sopenharmony_ci 58141cb0ef41Sopenharmony_ci[nodejs_init] 58151cb0ef41Sopenharmony_ciproviders = provider_sect 58161cb0ef41Sopenharmony_cialg_section = algorithm_sect 58171cb0ef41Sopenharmony_ci 58181cb0ef41Sopenharmony_ci[provider_sect] 58191cb0ef41Sopenharmony_cidefault = default_sect 58201cb0ef41Sopenharmony_ci# The fips section name should match the section name inside the 58211cb0ef41Sopenharmony_ci# included fipsmodule.cnf. 58221cb0ef41Sopenharmony_cifips = fips_sect 58231cb0ef41Sopenharmony_ci 58241cb0ef41Sopenharmony_ci[default_sect] 58251cb0ef41Sopenharmony_ciactivate = 1 58261cb0ef41Sopenharmony_ci 58271cb0ef41Sopenharmony_ci[algorithm_sect] 58281cb0ef41Sopenharmony_cidefault_properties = fips=yes 58291cb0ef41Sopenharmony_ci``` 58301cb0ef41Sopenharmony_ci 58311cb0ef41Sopenharmony_ci## Crypto constants 58321cb0ef41Sopenharmony_ci 58331cb0ef41Sopenharmony_ciThe following constants exported by `crypto.constants` apply to various uses of 58341cb0ef41Sopenharmony_cithe `node:crypto`, `node:tls`, and `node:https` modules and are generally 58351cb0ef41Sopenharmony_cispecific to OpenSSL. 58361cb0ef41Sopenharmony_ci 58371cb0ef41Sopenharmony_ci### OpenSSL options 58381cb0ef41Sopenharmony_ci 58391cb0ef41Sopenharmony_ciSee the [list of SSL OP Flags][] for details. 58401cb0ef41Sopenharmony_ci 58411cb0ef41Sopenharmony_ci<table> 58421cb0ef41Sopenharmony_ci <tr> 58431cb0ef41Sopenharmony_ci <th>Constant</th> 58441cb0ef41Sopenharmony_ci <th>Description</th> 58451cb0ef41Sopenharmony_ci </tr> 58461cb0ef41Sopenharmony_ci <tr> 58471cb0ef41Sopenharmony_ci <td><code>SSL_OP_ALL</code></td> 58481cb0ef41Sopenharmony_ci <td>Applies multiple bug workarounds within OpenSSL. See 58491cb0ef41Sopenharmony_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> 58501cb0ef41Sopenharmony_ci for detail.</td> 58511cb0ef41Sopenharmony_ci </tr> 58521cb0ef41Sopenharmony_ci <tr> 58531cb0ef41Sopenharmony_ci <td><code>SSL_OP_ALLOW_NO_DHE_KEX</code></td> 58541cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to allow a non-[EC]DHE-based key exchange mode 58551cb0ef41Sopenharmony_ci for TLS v1.3</td> 58561cb0ef41Sopenharmony_ci </tr> 58571cb0ef41Sopenharmony_ci <tr> 58581cb0ef41Sopenharmony_ci <td><code>SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION</code></td> 58591cb0ef41Sopenharmony_ci <td>Allows legacy insecure renegotiation between OpenSSL and unpatched 58601cb0ef41Sopenharmony_ci clients or servers. See 58611cb0ef41Sopenharmony_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> 58621cb0ef41Sopenharmony_ci </tr> 58631cb0ef41Sopenharmony_ci <tr> 58641cb0ef41Sopenharmony_ci <td><code>SSL_OP_CIPHER_SERVER_PREFERENCE</code></td> 58651cb0ef41Sopenharmony_ci <td>Attempts to use the server's preferences instead of the client's when 58661cb0ef41Sopenharmony_ci selecting a cipher. Behavior depends on protocol version. See 58671cb0ef41Sopenharmony_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> 58681cb0ef41Sopenharmony_ci </tr> 58691cb0ef41Sopenharmony_ci <tr> 58701cb0ef41Sopenharmony_ci <td><code>SSL_OP_CISCO_ANYCONNECT</code></td> 58711cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to use Cisco's "speshul" version of DTLS_BAD_VER.</td> 58721cb0ef41Sopenharmony_ci </tr> 58731cb0ef41Sopenharmony_ci <tr> 58741cb0ef41Sopenharmony_ci <td><code>SSL_OP_COOKIE_EXCHANGE</code></td> 58751cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to turn on cookie exchange.</td> 58761cb0ef41Sopenharmony_ci </tr> 58771cb0ef41Sopenharmony_ci <tr> 58781cb0ef41Sopenharmony_ci <td><code>SSL_OP_CRYPTOPRO_TLSEXT_BUG</code></td> 58791cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to add server-hello extension from an early version 58801cb0ef41Sopenharmony_ci of the cryptopro draft.</td> 58811cb0ef41Sopenharmony_ci </tr> 58821cb0ef41Sopenharmony_ci <tr> 58831cb0ef41Sopenharmony_ci <td><code>SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS</code></td> 58841cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to disable a SSL 3.0/TLS 1.0 vulnerability 58851cb0ef41Sopenharmony_ci workaround added in OpenSSL 0.9.6d.</td> 58861cb0ef41Sopenharmony_ci </tr> 58871cb0ef41Sopenharmony_ci <tr> 58881cb0ef41Sopenharmony_ci <td><code>SSL_OP_LEGACY_SERVER_CONNECT</code></td> 58891cb0ef41Sopenharmony_ci <td>Allows initial connection to servers that do not support RI.</td> 58901cb0ef41Sopenharmony_ci </tr> 58911cb0ef41Sopenharmony_ci <tr> 58921cb0ef41Sopenharmony_ci <td><code>SSL_OP_NO_COMPRESSION</code></td> 58931cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to disable support for SSL/TLS compression.</td> 58941cb0ef41Sopenharmony_ci </tr> 58951cb0ef41Sopenharmony_ci <tr> 58961cb0ef41Sopenharmony_ci <td><code>SSL_OP_NO_ENCRYPT_THEN_MAC</code></td> 58971cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to disable encrypt-then-MAC.</td> 58981cb0ef41Sopenharmony_ci </tr> 58991cb0ef41Sopenharmony_ci <tr> 59001cb0ef41Sopenharmony_ci <td><code>SSL_OP_NO_QUERY_MTU</code></td> 59011cb0ef41Sopenharmony_ci <td></td> 59021cb0ef41Sopenharmony_ci </tr> 59031cb0ef41Sopenharmony_ci <tr> 59041cb0ef41Sopenharmony_ci <td><code>SSL_OP_NO_RENEGOTIATION</code></td> 59051cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to disable renegotiation.</td> 59061cb0ef41Sopenharmony_ci </tr> 59071cb0ef41Sopenharmony_ci <tr> 59081cb0ef41Sopenharmony_ci <td><code>SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION</code></td> 59091cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to always start a new session when performing 59101cb0ef41Sopenharmony_ci renegotiation.</td> 59111cb0ef41Sopenharmony_ci </tr> 59121cb0ef41Sopenharmony_ci <tr> 59131cb0ef41Sopenharmony_ci <td><code>SSL_OP_NO_SSLv2</code></td> 59141cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to turn off SSL v2</td> 59151cb0ef41Sopenharmony_ci </tr> 59161cb0ef41Sopenharmony_ci <tr> 59171cb0ef41Sopenharmony_ci <td><code>SSL_OP_NO_SSLv3</code></td> 59181cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to turn off SSL v3</td> 59191cb0ef41Sopenharmony_ci </tr> 59201cb0ef41Sopenharmony_ci <tr> 59211cb0ef41Sopenharmony_ci <td><code>SSL_OP_NO_TICKET</code></td> 59221cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to disable use of RFC4507bis tickets.</td> 59231cb0ef41Sopenharmony_ci </tr> 59241cb0ef41Sopenharmony_ci <tr> 59251cb0ef41Sopenharmony_ci <td><code>SSL_OP_NO_TLSv1</code></td> 59261cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to turn off TLS v1</td> 59271cb0ef41Sopenharmony_ci </tr> 59281cb0ef41Sopenharmony_ci <tr> 59291cb0ef41Sopenharmony_ci <td><code>SSL_OP_NO_TLSv1_1</code></td> 59301cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to turn off TLS v1.1</td> 59311cb0ef41Sopenharmony_ci </tr> 59321cb0ef41Sopenharmony_ci <tr> 59331cb0ef41Sopenharmony_ci <td><code>SSL_OP_NO_TLSv1_2</code></td> 59341cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to turn off TLS v1.2</td> 59351cb0ef41Sopenharmony_ci </tr> 59361cb0ef41Sopenharmony_ci <tr> 59371cb0ef41Sopenharmony_ci <td><code>SSL_OP_NO_TLSv1_3</code></td> 59381cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to turn off TLS v1.3</td> 59391cb0ef41Sopenharmony_ci </tr> 59401cb0ef41Sopenharmony_ci <tr> 59411cb0ef41Sopenharmony_ci <td><code>SSL_OP_PRIORITIZE_CHACHA</code></td> 59421cb0ef41Sopenharmony_ci <td>Instructs OpenSSL server to prioritize ChaCha20-Poly1305 59431cb0ef41Sopenharmony_ci when the client does. 59441cb0ef41Sopenharmony_ci This option has no effect if 59451cb0ef41Sopenharmony_ci <code>SSL_OP_CIPHER_SERVER_PREFERENCE</code> 59461cb0ef41Sopenharmony_ci is not enabled.</td> 59471cb0ef41Sopenharmony_ci </tr> 59481cb0ef41Sopenharmony_ci <tr> 59491cb0ef41Sopenharmony_ci <td><code>SSL_OP_TLS_ROLLBACK_BUG</code></td> 59501cb0ef41Sopenharmony_ci <td>Instructs OpenSSL to disable version rollback attack detection.</td> 59511cb0ef41Sopenharmony_ci </tr> 59521cb0ef41Sopenharmony_ci</table> 59531cb0ef41Sopenharmony_ci 59541cb0ef41Sopenharmony_ci### OpenSSL engine constants 59551cb0ef41Sopenharmony_ci 59561cb0ef41Sopenharmony_ci<table> 59571cb0ef41Sopenharmony_ci <tr> 59581cb0ef41Sopenharmony_ci <th>Constant</th> 59591cb0ef41Sopenharmony_ci <th>Description</th> 59601cb0ef41Sopenharmony_ci </tr> 59611cb0ef41Sopenharmony_ci <tr> 59621cb0ef41Sopenharmony_ci <td><code>ENGINE_METHOD_RSA</code></td> 59631cb0ef41Sopenharmony_ci <td>Limit engine usage to RSA</td> 59641cb0ef41Sopenharmony_ci </tr> 59651cb0ef41Sopenharmony_ci <tr> 59661cb0ef41Sopenharmony_ci <td><code>ENGINE_METHOD_DSA</code></td> 59671cb0ef41Sopenharmony_ci <td>Limit engine usage to DSA</td> 59681cb0ef41Sopenharmony_ci </tr> 59691cb0ef41Sopenharmony_ci <tr> 59701cb0ef41Sopenharmony_ci <td><code>ENGINE_METHOD_DH</code></td> 59711cb0ef41Sopenharmony_ci <td>Limit engine usage to DH</td> 59721cb0ef41Sopenharmony_ci </tr> 59731cb0ef41Sopenharmony_ci <tr> 59741cb0ef41Sopenharmony_ci <td><code>ENGINE_METHOD_RAND</code></td> 59751cb0ef41Sopenharmony_ci <td>Limit engine usage to RAND</td> 59761cb0ef41Sopenharmony_ci </tr> 59771cb0ef41Sopenharmony_ci <tr> 59781cb0ef41Sopenharmony_ci <td><code>ENGINE_METHOD_EC</code></td> 59791cb0ef41Sopenharmony_ci <td>Limit engine usage to EC</td> 59801cb0ef41Sopenharmony_ci </tr> 59811cb0ef41Sopenharmony_ci <tr> 59821cb0ef41Sopenharmony_ci <td><code>ENGINE_METHOD_CIPHERS</code></td> 59831cb0ef41Sopenharmony_ci <td>Limit engine usage to CIPHERS</td> 59841cb0ef41Sopenharmony_ci </tr> 59851cb0ef41Sopenharmony_ci <tr> 59861cb0ef41Sopenharmony_ci <td><code>ENGINE_METHOD_DIGESTS</code></td> 59871cb0ef41Sopenharmony_ci <td>Limit engine usage to DIGESTS</td> 59881cb0ef41Sopenharmony_ci </tr> 59891cb0ef41Sopenharmony_ci <tr> 59901cb0ef41Sopenharmony_ci <td><code>ENGINE_METHOD_PKEY_METHS</code></td> 59911cb0ef41Sopenharmony_ci <td>Limit engine usage to PKEY_METHDS</td> 59921cb0ef41Sopenharmony_ci </tr> 59931cb0ef41Sopenharmony_ci <tr> 59941cb0ef41Sopenharmony_ci <td><code>ENGINE_METHOD_PKEY_ASN1_METHS</code></td> 59951cb0ef41Sopenharmony_ci <td>Limit engine usage to PKEY_ASN1_METHS</td> 59961cb0ef41Sopenharmony_ci </tr> 59971cb0ef41Sopenharmony_ci <tr> 59981cb0ef41Sopenharmony_ci <td><code>ENGINE_METHOD_ALL</code></td> 59991cb0ef41Sopenharmony_ci <td></td> 60001cb0ef41Sopenharmony_ci </tr> 60011cb0ef41Sopenharmony_ci <tr> 60021cb0ef41Sopenharmony_ci <td><code>ENGINE_METHOD_NONE</code></td> 60031cb0ef41Sopenharmony_ci <td></td> 60041cb0ef41Sopenharmony_ci </tr> 60051cb0ef41Sopenharmony_ci</table> 60061cb0ef41Sopenharmony_ci 60071cb0ef41Sopenharmony_ci### Other OpenSSL constants 60081cb0ef41Sopenharmony_ci 60091cb0ef41Sopenharmony_ci<table> 60101cb0ef41Sopenharmony_ci <tr> 60111cb0ef41Sopenharmony_ci <th>Constant</th> 60121cb0ef41Sopenharmony_ci <th>Description</th> 60131cb0ef41Sopenharmony_ci </tr> 60141cb0ef41Sopenharmony_ci <tr> 60151cb0ef41Sopenharmony_ci <td><code>DH_CHECK_P_NOT_SAFE_PRIME</code></td> 60161cb0ef41Sopenharmony_ci <td></td> 60171cb0ef41Sopenharmony_ci </tr> 60181cb0ef41Sopenharmony_ci <tr> 60191cb0ef41Sopenharmony_ci <td><code>DH_CHECK_P_NOT_PRIME</code></td> 60201cb0ef41Sopenharmony_ci <td></td> 60211cb0ef41Sopenharmony_ci </tr> 60221cb0ef41Sopenharmony_ci <tr> 60231cb0ef41Sopenharmony_ci <td><code>DH_UNABLE_TO_CHECK_GENERATOR</code></td> 60241cb0ef41Sopenharmony_ci <td></td> 60251cb0ef41Sopenharmony_ci </tr> 60261cb0ef41Sopenharmony_ci <tr> 60271cb0ef41Sopenharmony_ci <td><code>DH_NOT_SUITABLE_GENERATOR</code></td> 60281cb0ef41Sopenharmony_ci <td></td> 60291cb0ef41Sopenharmony_ci </tr> 60301cb0ef41Sopenharmony_ci <tr> 60311cb0ef41Sopenharmony_ci <td><code>ALPN_ENABLED</code></td> 60321cb0ef41Sopenharmony_ci <td></td> 60331cb0ef41Sopenharmony_ci </tr> 60341cb0ef41Sopenharmony_ci <tr> 60351cb0ef41Sopenharmony_ci <td><code>RSA_PKCS1_PADDING</code></td> 60361cb0ef41Sopenharmony_ci <td></td> 60371cb0ef41Sopenharmony_ci </tr> 60381cb0ef41Sopenharmony_ci <tr> 60391cb0ef41Sopenharmony_ci <td><code>RSA_SSLV23_PADDING</code></td> 60401cb0ef41Sopenharmony_ci <td></td> 60411cb0ef41Sopenharmony_ci </tr> 60421cb0ef41Sopenharmony_ci <tr> 60431cb0ef41Sopenharmony_ci <td><code>RSA_NO_PADDING</code></td> 60441cb0ef41Sopenharmony_ci <td></td> 60451cb0ef41Sopenharmony_ci </tr> 60461cb0ef41Sopenharmony_ci <tr> 60471cb0ef41Sopenharmony_ci <td><code>RSA_PKCS1_OAEP_PADDING</code></td> 60481cb0ef41Sopenharmony_ci <td></td> 60491cb0ef41Sopenharmony_ci </tr> 60501cb0ef41Sopenharmony_ci <tr> 60511cb0ef41Sopenharmony_ci <td><code>RSA_X931_PADDING</code></td> 60521cb0ef41Sopenharmony_ci <td></td> 60531cb0ef41Sopenharmony_ci </tr> 60541cb0ef41Sopenharmony_ci <tr> 60551cb0ef41Sopenharmony_ci <td><code>RSA_PKCS1_PSS_PADDING</code></td> 60561cb0ef41Sopenharmony_ci <td></td> 60571cb0ef41Sopenharmony_ci </tr> 60581cb0ef41Sopenharmony_ci <tr> 60591cb0ef41Sopenharmony_ci <td><code>RSA_PSS_SALTLEN_DIGEST</code></td> 60601cb0ef41Sopenharmony_ci <td>Sets the salt length for <code>RSA_PKCS1_PSS_PADDING</code> to the 60611cb0ef41Sopenharmony_ci digest size when signing or verifying.</td> 60621cb0ef41Sopenharmony_ci </tr> 60631cb0ef41Sopenharmony_ci <tr> 60641cb0ef41Sopenharmony_ci <td><code>RSA_PSS_SALTLEN_MAX_SIGN</code></td> 60651cb0ef41Sopenharmony_ci <td>Sets the salt length for <code>RSA_PKCS1_PSS_PADDING</code> to the 60661cb0ef41Sopenharmony_ci maximum permissible value when signing data.</td> 60671cb0ef41Sopenharmony_ci </tr> 60681cb0ef41Sopenharmony_ci <tr> 60691cb0ef41Sopenharmony_ci <td><code>RSA_PSS_SALTLEN_AUTO</code></td> 60701cb0ef41Sopenharmony_ci <td>Causes the salt length for <code>RSA_PKCS1_PSS_PADDING</code> to be 60711cb0ef41Sopenharmony_ci determined automatically when verifying a signature.</td> 60721cb0ef41Sopenharmony_ci </tr> 60731cb0ef41Sopenharmony_ci <tr> 60741cb0ef41Sopenharmony_ci <td><code>POINT_CONVERSION_COMPRESSED</code></td> 60751cb0ef41Sopenharmony_ci <td></td> 60761cb0ef41Sopenharmony_ci </tr> 60771cb0ef41Sopenharmony_ci <tr> 60781cb0ef41Sopenharmony_ci <td><code>POINT_CONVERSION_UNCOMPRESSED</code></td> 60791cb0ef41Sopenharmony_ci <td></td> 60801cb0ef41Sopenharmony_ci </tr> 60811cb0ef41Sopenharmony_ci <tr> 60821cb0ef41Sopenharmony_ci <td><code>POINT_CONVERSION_HYBRID</code></td> 60831cb0ef41Sopenharmony_ci <td></td> 60841cb0ef41Sopenharmony_ci </tr> 60851cb0ef41Sopenharmony_ci</table> 60861cb0ef41Sopenharmony_ci 60871cb0ef41Sopenharmony_ci### Node.js crypto constants 60881cb0ef41Sopenharmony_ci 60891cb0ef41Sopenharmony_ci<table> 60901cb0ef41Sopenharmony_ci <tr> 60911cb0ef41Sopenharmony_ci <th>Constant</th> 60921cb0ef41Sopenharmony_ci <th>Description</th> 60931cb0ef41Sopenharmony_ci </tr> 60941cb0ef41Sopenharmony_ci <tr> 60951cb0ef41Sopenharmony_ci <td><code>defaultCoreCipherList</code></td> 60961cb0ef41Sopenharmony_ci <td>Specifies the built-in default cipher list used by Node.js.</td> 60971cb0ef41Sopenharmony_ci </tr> 60981cb0ef41Sopenharmony_ci <tr> 60991cb0ef41Sopenharmony_ci <td><code>defaultCipherList</code></td> 61001cb0ef41Sopenharmony_ci <td>Specifies the active default cipher list used by the current Node.js 61011cb0ef41Sopenharmony_ci process.</td> 61021cb0ef41Sopenharmony_ci </tr> 61031cb0ef41Sopenharmony_ci</table> 61041cb0ef41Sopenharmony_ci 61051cb0ef41Sopenharmony_ci[AEAD algorithms]: https://en.wikipedia.org/wiki/Authenticated_encryption 61061cb0ef41Sopenharmony_ci[CCM mode]: #ccm-mode 61071cb0ef41Sopenharmony_ci[CVE-2021-44532]: https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-44532 61081cb0ef41Sopenharmony_ci[Caveats]: #support-for-weak-or-compromised-algorithms 61091cb0ef41Sopenharmony_ci[Crypto constants]: #crypto-constants 61101cb0ef41Sopenharmony_ci[FIPS module configuration file]: https://www.openssl.org/docs/man3.0/man5/fips_config.html 61111cb0ef41Sopenharmony_ci[FIPS provider from OpenSSL 3]: https://www.openssl.org/docs/man3.0/man7/crypto.html#FIPS-provider 61121cb0ef41Sopenharmony_ci[HTML 5.2]: https://www.w3.org/TR/html52/changes.html#features-removed 61131cb0ef41Sopenharmony_ci[JWK]: https://tools.ietf.org/html/rfc7517 61141cb0ef41Sopenharmony_ci[NIST SP 800-131A]: https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf 61151cb0ef41Sopenharmony_ci[NIST SP 800-132]: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf 61161cb0ef41Sopenharmony_ci[NIST SP 800-38D]: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf 61171cb0ef41Sopenharmony_ci[Nonce-Disrespecting Adversaries]: https://github.com/nonce-disrespect/nonce-disrespect 61181cb0ef41Sopenharmony_ci[OpenSSL's FIPS README file]: https://github.com/openssl/openssl/blob/openssl-3.0/README-FIPS.md 61191cb0ef41Sopenharmony_ci[OpenSSL's SPKAC implementation]: https://www.openssl.org/docs/man3.0/man1/openssl-spkac.html 61201cb0ef41Sopenharmony_ci[RFC 1421]: https://www.rfc-editor.org/rfc/rfc1421.txt 61211cb0ef41Sopenharmony_ci[RFC 2409]: https://www.rfc-editor.org/rfc/rfc2409.txt 61221cb0ef41Sopenharmony_ci[RFC 2818]: https://www.rfc-editor.org/rfc/rfc2818.txt 61231cb0ef41Sopenharmony_ci[RFC 3526]: https://www.rfc-editor.org/rfc/rfc3526.txt 61241cb0ef41Sopenharmony_ci[RFC 3610]: https://www.rfc-editor.org/rfc/rfc3610.txt 61251cb0ef41Sopenharmony_ci[RFC 4055]: https://www.rfc-editor.org/rfc/rfc4055.txt 61261cb0ef41Sopenharmony_ci[RFC 4122]: https://www.rfc-editor.org/rfc/rfc4122.txt 61271cb0ef41Sopenharmony_ci[RFC 5208]: https://www.rfc-editor.org/rfc/rfc5208.txt 61281cb0ef41Sopenharmony_ci[RFC 5280]: https://www.rfc-editor.org/rfc/rfc5280.txt 61291cb0ef41Sopenharmony_ci[Web Crypto API documentation]: webcrypto.md 61301cb0ef41Sopenharmony_ci[`BN_is_prime_ex`]: https://www.openssl.org/docs/man1.1.1/man3/BN_is_prime_ex.html 61311cb0ef41Sopenharmony_ci[`Buffer`]: buffer.md 61321cb0ef41Sopenharmony_ci[`DH_generate_key()`]: https://www.openssl.org/docs/man3.0/man3/DH_generate_key.html 61331cb0ef41Sopenharmony_ci[`DiffieHellmanGroup`]: #class-diffiehellmangroup 61341cb0ef41Sopenharmony_ci[`EVP_BytesToKey`]: https://www.openssl.org/docs/man3.0/man3/EVP_BytesToKey.html 61351cb0ef41Sopenharmony_ci[`KeyObject`]: #class-keyobject 61361cb0ef41Sopenharmony_ci[`Sign`]: #class-sign 61371cb0ef41Sopenharmony_ci[`String.prototype.normalize()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize 61381cb0ef41Sopenharmony_ci[`UV_THREADPOOL_SIZE`]: cli.md#uv_threadpool_sizesize 61391cb0ef41Sopenharmony_ci[`Verify`]: #class-verify 61401cb0ef41Sopenharmony_ci[`cipher.final()`]: #cipherfinaloutputencoding 61411cb0ef41Sopenharmony_ci[`cipher.update()`]: #cipherupdatedata-inputencoding-outputencoding 61421cb0ef41Sopenharmony_ci[`crypto.createCipher()`]: #cryptocreatecipheralgorithm-password-options 61431cb0ef41Sopenharmony_ci[`crypto.createCipheriv()`]: #cryptocreatecipherivalgorithm-key-iv-options 61441cb0ef41Sopenharmony_ci[`crypto.createDecipher()`]: #cryptocreatedecipheralgorithm-password-options 61451cb0ef41Sopenharmony_ci[`crypto.createDecipheriv()`]: #cryptocreatedecipherivalgorithm-key-iv-options 61461cb0ef41Sopenharmony_ci[`crypto.createDiffieHellman()`]: #cryptocreatediffiehellmanprime-primeencoding-generator-generatorencoding 61471cb0ef41Sopenharmony_ci[`crypto.createECDH()`]: #cryptocreateecdhcurvename 61481cb0ef41Sopenharmony_ci[`crypto.createHash()`]: #cryptocreatehashalgorithm-options 61491cb0ef41Sopenharmony_ci[`crypto.createHmac()`]: #cryptocreatehmacalgorithm-key-options 61501cb0ef41Sopenharmony_ci[`crypto.createPrivateKey()`]: #cryptocreateprivatekeykey 61511cb0ef41Sopenharmony_ci[`crypto.createPublicKey()`]: #cryptocreatepublickeykey 61521cb0ef41Sopenharmony_ci[`crypto.createSecretKey()`]: #cryptocreatesecretkeykey-encoding 61531cb0ef41Sopenharmony_ci[`crypto.createSign()`]: #cryptocreatesignalgorithm-options 61541cb0ef41Sopenharmony_ci[`crypto.createVerify()`]: #cryptocreateverifyalgorithm-options 61551cb0ef41Sopenharmony_ci[`crypto.generateKey()`]: #cryptogeneratekeytype-options-callback 61561cb0ef41Sopenharmony_ci[`crypto.getCurves()`]: #cryptogetcurves 61571cb0ef41Sopenharmony_ci[`crypto.getDiffieHellman()`]: #cryptogetdiffiehellmangroupname 61581cb0ef41Sopenharmony_ci[`crypto.getHashes()`]: #cryptogethashes 61591cb0ef41Sopenharmony_ci[`crypto.privateDecrypt()`]: #cryptoprivatedecryptprivatekey-buffer 61601cb0ef41Sopenharmony_ci[`crypto.privateEncrypt()`]: #cryptoprivateencryptprivatekey-buffer 61611cb0ef41Sopenharmony_ci[`crypto.publicDecrypt()`]: #cryptopublicdecryptkey-buffer 61621cb0ef41Sopenharmony_ci[`crypto.publicEncrypt()`]: #cryptopublicencryptkey-buffer 61631cb0ef41Sopenharmony_ci[`crypto.randomBytes()`]: #cryptorandombytessize-callback 61641cb0ef41Sopenharmony_ci[`crypto.randomFill()`]: #cryptorandomfillbuffer-offset-size-callback 61651cb0ef41Sopenharmony_ci[`crypto.scrypt()`]: #cryptoscryptpassword-salt-keylen-options-callback 61661cb0ef41Sopenharmony_ci[`crypto.webcrypto.getRandomValues()`]: webcrypto.md#cryptogetrandomvaluestypedarray 61671cb0ef41Sopenharmony_ci[`crypto.webcrypto.subtle`]: webcrypto.md#class-subtlecrypto 61681cb0ef41Sopenharmony_ci[`decipher.final()`]: #decipherfinaloutputencoding 61691cb0ef41Sopenharmony_ci[`decipher.update()`]: #decipherupdatedata-inputencoding-outputencoding 61701cb0ef41Sopenharmony_ci[`diffieHellman.generateKeys()`]: #diffiehellmangeneratekeysencoding 61711cb0ef41Sopenharmony_ci[`diffieHellman.setPublicKey()`]: #diffiehellmansetpublickeypublickey-encoding 61721cb0ef41Sopenharmony_ci[`ecdh.generateKeys()`]: #ecdhgeneratekeysencoding-format 61731cb0ef41Sopenharmony_ci[`ecdh.setPrivateKey()`]: #ecdhsetprivatekeyprivatekey-encoding 61741cb0ef41Sopenharmony_ci[`hash.digest()`]: #hashdigestencoding 61751cb0ef41Sopenharmony_ci[`hash.update()`]: #hashupdatedata-inputencoding 61761cb0ef41Sopenharmony_ci[`hmac.digest()`]: #hmacdigestencoding 61771cb0ef41Sopenharmony_ci[`hmac.update()`]: #hmacupdatedata-inputencoding 61781cb0ef41Sopenharmony_ci[`import()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import 61791cb0ef41Sopenharmony_ci[`keyObject.export()`]: #keyobjectexportoptions 61801cb0ef41Sopenharmony_ci[`postMessage()`]: worker_threads.md#portpostmessagevalue-transferlist 61811cb0ef41Sopenharmony_ci[`sign.sign()`]: #signsignprivatekey-outputencoding 61821cb0ef41Sopenharmony_ci[`sign.update()`]: #signupdatedata-inputencoding 61831cb0ef41Sopenharmony_ci[`stream.Writable` options]: stream.md#new-streamwritableoptions 61841cb0ef41Sopenharmony_ci[`stream.transform` options]: stream.md#new-streamtransformoptions 61851cb0ef41Sopenharmony_ci[`util.promisify()`]: util.md#utilpromisifyoriginal 61861cb0ef41Sopenharmony_ci[`verify.update()`]: #verifyupdatedata-inputencoding 61871cb0ef41Sopenharmony_ci[`verify.verify()`]: #verifyverifyobject-signature-signatureencoding 61881cb0ef41Sopenharmony_ci[`x509.fingerprint256`]: #x509fingerprint256 61891cb0ef41Sopenharmony_ci[caveats when using strings as inputs to cryptographic APIs]: #using-strings-as-inputs-to-cryptographic-apis 61901cb0ef41Sopenharmony_ci[certificate object]: tls.md#certificate-object 61911cb0ef41Sopenharmony_ci[encoding]: buffer.md#buffers-and-character-encodings 61921cb0ef41Sopenharmony_ci[initialization vector]: https://en.wikipedia.org/wiki/Initialization_vector 61931cb0ef41Sopenharmony_ci[legacy provider]: cli.md#--openssl-legacy-provider 61941cb0ef41Sopenharmony_ci[list of SSL OP Flags]: https://wiki.openssl.org/index.php/List_of_SSL_OP_Flags#Table_of_Options 61951cb0ef41Sopenharmony_ci[modulo bias]: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#Modulo_bias 61961cb0ef41Sopenharmony_ci[safe integers]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isSafeInteger 61971cb0ef41Sopenharmony_ci[scrypt]: https://en.wikipedia.org/wiki/Scrypt 61981cb0ef41Sopenharmony_ci[stream]: stream.md 61991cb0ef41Sopenharmony_ci[stream-writable-write]: stream.md#writablewritechunk-encoding-callback 6200