Web Crypto API | Node.js v22.23.2 Documentation
https://nodejs.org/docs/latest-v22.x/api/webcrypto.html • 162 KB fetched
Open original page
Web Crypto API | Node.js v22.23.2 Documentation
Skip to content
Node.js
* About this documentation
* Usage and example
* Assertion testing
* Asynchronous context tracking
* Async hooks
* Buffer
* C++ addons
* C/C++ addons with Node-API
* C++ embedder API
* Child processes
* Cluster
* Command-line options
* Console
* Crypto
* Debugger
* Deprecated APIs
* Diagnostics Channel
* DNS
* Domain
* Environment Variables
* Errors
* Events
* File system
* Globals
* HTTP
* HTTP/2
* HTTPS
* Inspector
* Internationalization
* Modules: CommonJS modules
* Modules: ECMAScript modules
* Modules: node:module API
* Modules: Packages
* Modules: TypeScript
* Net
* OS
* Path
* Performance hooks
* Permissions
* Process
* Punycode
* Query strings
* Readline
* REPL
* Report
* Single executable applications
* SQLite
* Stream
* String decoder
* Test runner
* Timers
* TLS/SSL
* Trace events
* TTY
* UDP/datagram
* URL
* Utilities
* V8
* VM
* WASI
* Web Crypto API
* Web Streams API
* Worker threads
* Zlib
* Code repository and issue tracker
Node.js v22.23.2 documentation
* Node.js v22.23.2
*
Table of contents
* Web Crypto API
* Examples
* Generating keys
* AES keys
* ECDSA key pairs
* Ed25519/X25519 key pairs
* HMAC keys
* RSA key pairs
* Encryption and decryption
* Exporting and importing keys
* Wrapping and unwrapping keys
* Sign and verify
* Deriving bits and keys
* Digest
* Algorithm matrix
* Class: Crypto
* crypto.subtle
* crypto.getRandomValues(typedArray)
* crypto.randomUUID()
* Class: CryptoKey
* cryptoKey.algorithm
* cryptoKey.extractable
* cryptoKey.type
* cryptoKey.usages
* Class: CryptoKeyPair
* cryptoKeyPair.privateKey
* cryptoKeyPair.publicKey
* Class: SubtleCrypto
* subtle.decrypt(algorithm, key, data)
* subtle.deriveBits(algorithm, baseKey[, length])
* subtle.deriveKey(algorithm, baseKey, derivedKeyAlgorithm, extractable, keyUsages)
* subtle.digest(algorithm, data)
* subtle.encrypt(algorithm, key, data)
* subtle.exportKey(format, key)
* subtle.generateKey(algorithm, extractable, keyUsages)
* subtle.importKey(format, keyData, algorithm, extractable, keyUsages)
* subtle.sign(algorithm, key, data)
* subtle.unwrapKey(format, wrappedKey, unwrappingKey, unwrapAlgo, unwrappedKeyAlgo, extractable, keyUsages)
* subtle.verify(algorithm, key, signature, data)
* subtle.wrapKey(format, key, wrappingKey, wrapAlgo)
* Algorithm parameters
* Class: Algorithm
* Algorithm.name
* Class: AesDerivedKeyParams
* aesDerivedKeyParams.name
* aesDerivedKeyParams.length
* Class: AesCbcParams
* aesCbcParams.iv
* aesCbcParams.name
* Class: AesCtrParams
* aesCtrParams.counter
* aesCtrParams.length
* aesCtrParams.name
* Class: AesGcmParams
* aesGcmParams.additionalData
* aesGcmParams.iv
* aesGcmParams.name
* aesGcmParams.tagLength
* Class: AesKeyAlgorithm
* aesKeyAlgorithm.length
* aesKeyAlgorithm.name
* Class: AesKeyGenParams
* aesKeyGenParams.length
* aesKeyGenParams.name
* Class: EcdhKeyDeriveParams
* ecdhKeyDeriveParams.name
* ecdhKeyDeriveParams.public
* Class: EcdsaParams
* ecdsaParams.hash
* ecdsaParams.name
* Class: EcKeyAlgorithm
* ecKeyAlgorithm.name
* ecKeyAlgorithm.namedCurve
* Class: EcKeyGenParams
* ecKeyGenParams.name
* ecKeyGenParams.namedCurve
* Class: EcKeyImportParams
* ecKeyImportParams.name
* ecKeyImportParams.namedCurve
* Class: Ed448Params
* ed448Params.name
* ed448Params.context
* Class: HkdfParams
* hkdfParams.hash
* hkdfParams.info
* hkdfParams.name
* hkdfParams.salt
* Class: HmacImportParams
* hmacImportParams.hash
* hmacImportParams.length
* hmacImportParams.name
* Class: HmacKeyAlgorithm
* hmacKeyAlgorithm.hash
* hmacKeyAlgorithm.length
* hmacKeyAlgorithm.name
* Class: HmacKeyGenParams
* hmacKeyGenParams.hash
* hmacKeyGenParams.length
* hmacKeyGenParams.name
* Class: KeyAlgorithm
* keyAlgorithm.name
* Class: Pbkdf2Params
* pbkdf2Params.hash
* pbkdf2Params.iterations
* pbkdf2Params.name
* pbkdf2Params.salt
* Class: RsaHashedImportParams
* rsaHashedImportParams.hash
* rsaHashedImportParams.name
* Class: RsaHashedKeyAlgorithm
* rsaHashedKeyAlgorithm.hash
* rsaHashedKeyAlgorithm.modulusLength
* rsaHashedKeyAlgorithm.name
* rsaHashedKeyAlgorithm.publicExponent
* Class: RsaHashedKeyGenParams
* rsaHashedKeyGenParams.hash
* rsaHashedKeyGenParams.modulusLength
* rsaHashedKeyGenParams.name
* rsaHashedKeyGenParams.publicExponent
* Class: RsaOaepParams
* rsaOaepParams.label
* rsaOaepParams.name
* Class: RsaPssParams
* rsaPssParams.name
* rsaPssParams.saltLength
*
Index
* About this documentation
* Usage and example
*
Index
* Assertion testing
* Asynchronous context tracking
* Async hooks
* Buffer
* C++ addons
* C/C++ addons with Node-API
* C++ embedder API
* Child processes
* Cluster
* Command-line options
* Console
* Crypto
* Debugger
* Deprecated APIs
* Diagnostics Channel
* DNS
* Domain
* Environment Variables
* Errors
* Events
* File system
* Globals
* HTTP
* HTTP/2
* HTTPS
* Inspector
* Internationalization
* Modules: CommonJS modules
* Modules: ECMAScript modules
* Modules: node:module API
* Modules: Packages
* Modules: TypeScript
* Net
* OS
* Path
* Performance hooks
* Permissions
* Process
* Punycode
* Query strings
* Readline
* REPL
* Report
* Single executable applications
* SQLite
* Stream
* String decoder
* Test runner
* Timers
* TLS/SSL
* Trace events
* TTY
* UDP/datagram
* URL
* Utilities
* V8
* VM
* WASI
* Web Crypto API
* Web Streams API
* Worker threads
* Zlib
* Code repository and issue tracker
*
Other versions
* 26.x
* 25.x
* 24.x LTS
* 23.x
* 22.x LTS
* 21.x
* 20.x
* 19.x
* 18.x
* 17.x
* 16.x
* 15.x
*
Options
*
View on single page
*
View as JSON
* Edit on GitHub
Table of contents
* Web Crypto API
* Examples
* Generating keys
* AES keys
* ECDSA key pairs
* Ed25519/X25519 key pairs
* HMAC keys
* RSA key pairs
* Encryption and decryption
* Exporting and importing keys
* Wrapping and unwrapping keys
* Sign and verify
* Deriving bits and keys
* Digest
* Algorithm matrix
* Class: Crypto
* crypto.subtle
* crypto.getRandomValues(typedArray)
* crypto.randomUUID()
* Class: CryptoKey
* cryptoKey.algorithm
* cryptoKey.extractable
* cryptoKey.type
* cryptoKey.usages
* Class: CryptoKeyPair
* cryptoKeyPair.privateKey
* cryptoKeyPair.publicKey
* Class: SubtleCrypto
* subtle.decrypt(algorithm, key, data)
* subtle.deriveBits(algorithm, baseKey[, length])
* subtle.deriveKey(algorithm, baseKey, derivedKeyAlgorithm, extractable, keyUsages)
* subtle.digest(algorithm, data)
* subtle.encrypt(algorithm, key, data)
* subtle.exportKey(format, key)
* subtle.generateKey(algorithm, extractable, keyUsages)
* subtle.importKey(format, keyData, algorithm, extractable, keyUsages)
* subtle.sign(algorithm, key, data)
* subtle.unwrapKey(format, wrappedKey, unwrappingKey, unwrapAlgo, unwrappedKeyAlgo, extractable, keyUsages)
* subtle.verify(algorithm, key, signature, data)
* subtle.wrapKey(format, key, wrappingKey, wrapAlgo)
* Algorithm parameters
* Class: Algorithm
* Algorithm.name
* Class: AesDerivedKeyParams
* aesDerivedKeyParams.name
* aesDerivedKeyParams.length
* Class: AesCbcParams
* aesCbcParams.iv
* aesCbcParams.name
* Class: AesCtrParams
* aesCtrParams.counter
* aesCtrParams.length
* aesCtrParams.name
* Class: AesGcmParams
* aesGcmParams.additionalData
* aesGcmParams.iv
* aesGcmParams.name
* aesGcmParams.tagLength
* Class: AesKeyAlgorithm
* aesKeyAlgorithm.length
* aesKeyAlgorithm.name
* Class: AesKeyGenParams
* aesKeyGenParams.length
* aesKeyGenParams.name
* Class: EcdhKeyDeriveParams
* ecdhKeyDeriveParams.name
* ecdhKeyDeriveParams.public
* Class: EcdsaParams
* ecdsaParams.hash
* ecdsaParams.name
* Class: EcKeyAlgorithm
* ecKeyAlgorithm.name
* ecKeyAlgorithm.namedCurve
* Class: EcKeyGenParams
* ecKeyGenParams.name
* ecKeyGenParams.namedCurve
* Class: EcKeyImportParams
* ecKeyImportParams.name
* ecKeyImportParams.namedCurve
* Class: Ed448Params
* ed448Params.name
* ed448Params.context
* Class: HkdfParams
* hkdfParams.hash
* hkdfParams.info
* hkdfParams.name
* hkdfParams.salt
* Class: HmacImportParams
* hmacImportParams.hash
* hmacImportParams.length
* hmacImportParams.name
* Class: HmacKeyAlgorithm
* hmacKeyAlgorithm.hash
* hmacKeyAlgorithm.length
* hmacKeyAlgorithm.name
* Class: HmacKeyGenParams
* hmacKeyGenParams.hash
* hmacKeyGenParams.length
* hmacKeyGenParams.name
* Class: KeyAlgorithm
* keyAlgorithm.name
* Class: Pbkdf2Params
* pbkdf2Params.hash
* pbkdf2Params.iterations
* pbkdf2Params.name
* pbkdf2Params.salt
* Class: RsaHashedImportParams
* rsaHashedImportParams.hash
* rsaHashedImportParams.name
* Class: RsaHashedKeyAlgorithm
* rsaHashedKeyAlgorithm.hash
* rsaHashedKeyAlgorithm.modulusLength
* rsaHashedKeyAlgorithm.name
* rsaHashedKeyAlgorithm.publicExponent
* Class: RsaHashedKeyGenParams
* rsaHashedKeyGenParams.hash
* rsaHashedKeyGenParams.modulusLength
* rsaHashedKeyGenParams.name
* rsaHashedKeyGenParams.publicExponent
* Class: RsaOaepParams
* rsaOaepParams.label
* rsaOaepParams.name
* Class: RsaPssParams
* rsaPssParams.name
* rsaPssParams.saltLength
Web Crypto API #
History
Version Changes
v22.13.0
Algorithms Ed25519 and X25519 are now stable.
v19.0.0
No longer experimental except for the Ed25519 , Ed448 , X25519 , and X448 algorithms.
v20.0.0, v18.17.0
Arguments are now coerced and validated as per their WebIDL definitions like in other Web Crypto API implementations.
v18.4.0, v16.17.0
Removed proprietary 'node.keyObject' import/export format.
v18.4.0, v16.17.0
Removed proprietary 'NODE-DSA' , 'NODE-DH' , and 'NODE-SCRYPT' algorithms.
v18.4.0, v16.17.0
Added 'Ed25519' , 'Ed448' , 'X25519' , and 'X448' algorithms.
v18.4.0, v16.17.0
Removed proprietary 'NODE-ED25519' and 'NODE-ED448' algorithms.
v18.4.0, v16.17.0
Removed proprietary 'NODE-X25519' and 'NODE-X448' named curves from the 'ECDH' algorithm.
Stability: 2 - Stable
Node.js provides an implementation of the standard Web Crypto API .
Use globalThis.crypto or require('node:crypto').webcrypto to access this
module.
const { subtle } = globalThis. crypto ;
( async function ( ) {
const key = await subtle. generateKey ({
name : 'HMAC' ,
hash : 'SHA-256' ,
length : 256 ,
}, true , [ 'sign' , 'verify' ]);
const enc = new TextEncoder ();
const message = enc. encode ( 'I love cupcakes' );
const digest = await subtle. sign ({
name : 'HMAC' ,
}, key, message);
})(); copy
Examples #
Generating keys #
The <SubtleCrypto> class can be used to generate symmetric (secret) keys
or asymmetric key pairs (public key and private key).
AES keys #
const { subtle } = globalThis. crypto ;
async function generateAesKey ( length = 256 ) {
const key = await subtle. generateKey ({
name : 'AES-CBC' ,
length,
}, true , [ 'encrypt' , 'decrypt' ]);
return key;
} copy
ECDSA key pairs #
const { subtle } = globalThis. crypto ;
async function generateEcKey ( namedCurve = 'P-521' ) {
const {
publicKey,
privateKey,
} = await subtle. generateKey ({
name : 'ECDSA' ,
namedCurve,
}, true , [ 'sign' , 'verify' ]);
return { publicKey, privateKey };
} copy
Ed25519/X25519 key pairs #
const { subtle } = globalThis. crypto ;
async function generateEd25519Key ( ) {
return subtle. generateKey ({
name : 'Ed25519' ,
}, true , [ 'sign' , 'verify' ]);
}
async function generateX25519Key ( ) {
return subtle. generateKey ({
name : 'X25519' ,
}, true , [ 'deriveKey' ]);
} copy
HMAC keys #
const { subtle } = globalThis. crypto ;
async function generateHmacKey ( hash = 'SHA-256' ) {
const key = await subtle. generateKey ({
name : 'HMAC' ,
hash,
}, true , [ 'sign' , 'verify' ]);
return key;
} copy
RSA key pairs #
const { subtle } = globalThis. crypto ;
const publicExponent = new Uint8Array ([ 1 , 0 , 1 ]);
async function generateRsaKey ( modulusLength = 2048 , hash = 'SHA-256' ) {
const {
publicKey,
privateKey,
} = await subtle. generateKey ({
name : 'RSASSA-PKCS1-v1_5' ,
modulusLength,
publicExponent,
hash,
}, true , [ 'sign' , 'verify' ]);
return { publicKey, privateKey };
} copy
Encryption and decryption #
const crypto = globalThis. crypto ;
async function aesEncrypt ( plaintext ) {
const ec = new TextEncoder ();
const key = await generateAesKey ();
const iv = crypto. getRandomValues ( new Uint8Array ( 16 ));
const ciphertext = await crypto. subtle . encrypt ({
name : 'AES-CBC' ,
iv,
}, key, ec. encode (plaintext));
return {
key,
iv,
ciphertext,
};
}
async function aesDecrypt ( ciphertext, key, iv ) {
const dec = new TextDecoder ();
const plaintext = await crypto. subtle . decrypt ({
name : 'AES-CBC' ,
iv,
}, key, ciphertext);
return dec. decode (plaintext);
} copy
Exporting and importing keys #
const { subtle } = globalThis. crypto ;
async function generateAndExportHmacKey ( format = 'jwk' , hash = 'SHA-512' ) {
const key = await subtle. generateKey ({
name : 'HMAC' ,
hash,
}, true , [ 'sign' , 'verify' ]);
return subtle. exportKey (format, key);
}
async function importHmacKey ( keyData, format = 'jwk' , hash = 'SHA-512' ) {
const key = await subtle. importKey (format, keyData, {
name : 'HMAC' ,
hash,
}, true , [ 'sign' , 'verify' ]);
return key;
} copy
Wrapping and unwrapping keys #
const { subtle } = globalThis. crypto ;
async function generateAndWrapHmacKey ( format = 'jwk' , hash = 'SHA-512' ) {
const [
key,
wrappingKey,
] = await Promise . all ([
subtle. generateKey ({
name : 'HMAC' , hash,
}, true , [ 'sign' , 'verify' ]),
subtle. generateKey ({
name : 'AES-KW' ,
length : 256 ,
}, true , [ 'wrapKey' , 'unwrapKey' ]),
]);
const wrappedKey = await subtle. wrapKey (format, key, wrappingKey, 'AES-KW' );
return { wrappedKey, wrappingKey };
}
async function unwrapHmacKey (
wrappedKey,
wrappingKey,
format = 'jwk' ,
hash = 'SHA-512' ) {
const key = await subtle. unwrapKey (
format,
wrappedKey,
wrappingKey,
'AES-KW' ,
{ name : 'HMAC' , hash },
true ,
[ 'sign' , 'verify' ]);
return key;
} copy
Sign and verify #
const { subtle } = globalThis. crypto ;
async function sign ( key, data ) {
const ec = new TextEncoder ();
const signature =
await subtle. sign ( 'RSASSA-PKCS1-v1_5' , key, ec. encode (data));
return signature;
}
async function verify ( key, signature, data ) {
const ec = new TextEncoder ();
const verified =
await subtle. verify (
'RSASSA-PKCS1-v1_5' ,
key,
signature,
ec. encode (data));
return verified;
} copy
Deriving bits and keys #
const { subtle } = globalThis. crypto ;
async function pbkdf2 ( pass, salt, iterations = 1000 , length = 256 ) {
const ec = new TextEncoder ();
const key = await subtle. importKey (
'raw' ,
ec. encode (pass),
'PBKDF2' ,
false ,
[ 'deriveBits' ]);
const bits = await subtle. deriveBits ({
name : 'PBKDF2' ,
hash : 'SHA-512' ,
salt : ec. encode (salt),
iterations,
}, key, length);
return bits;
}
async function pbkdf2Key ( pass, salt, iterations = 1000 , length = 256 ) {
const ec = new TextEncoder ();
const keyMaterial = await subtle. importKey (
'raw' ,
ec. encode (pass),
'PBKDF2' ,
false ,
[ 'deriveKey' ]);
const key = await subtle. deriveKey ({
name : 'PBKDF2' ,
hash : 'SHA-512' ,
salt : ec. encode (salt),
iterations,
}, keyMaterial, {
name : 'AES-GCM' ,
length,
}, true , [ 'encrypt' , 'decrypt' ]);
return key;
} copy
Digest #
const { subtle } = globalThis. crypto ;
async function digest ( data, algorithm = 'SHA-512' ) {
const ec = new TextEncoder ();
const digest = await subtle. digest (algorithm, ec. encode (data));
return digest;
} copy
Algorithm matrix #
The table details the algorithms supported by the Node.js Web Crypto API
implementation and the APIs supported for each:
Algorithm generateKey exportKey importKey encrypt decrypt wrapKey unwrapKey deriveBits deriveKey sign verify digest
'RSASSA-PKCS1-v1_5' ✔ ✔ ✔ ✔ ✔
'RSA-PSS' ✔ ✔ ✔ ✔ ✔
'RSA-OAEP' ✔ ✔ ✔ ✔ ✔ ✔ ✔
'ECDSA' ✔ ✔ ✔ ✔ ✔
'Ed25519' ✔ ✔ ✔ ✔ ✔
'Ed448' 1 ✔ ✔ ✔ ✔ ✔
'ECDH' ✔ ✔ ✔ ✔ ✔
'X25519' ✔ ✔ ✔ ✔ ✔
'X448' 1 ✔ ✔ ✔ ✔ ✔
'AES-CTR' ✔ ✔ ✔ ✔ ✔ ✔ ✔
'AES-CBC' ✔ ✔ ✔ ✔ ✔ ✔ ✔
'AES-GCM' ✔ ✔ ✔ ✔ ✔ ✔ ✔
'AES-KW' ✔ ✔ ✔ ✔ ✔
'HMAC' ✔ ✔ ✔ ✔ ✔
'HKDF' ✔ ✔ ✔ ✔
'PBKDF2' ✔ ✔ ✔ ✔
'SHA-1' ✔
'SHA-256' ✔
'SHA-384' ✔
'SHA-512' ✔
Class: Crypto #
Added in: v15.0.0
globalThis.crypto is an instance of the Crypto
class. Crypto is a singleton that provides access to the remainder of the
crypto API.
crypto.subtle #
Added in: v15.0.0
* Type: <SubtleCrypto>
Provides access to the SubtleCrypto API.
crypto.getRandomValues(typedArray) #
Added in: v15.0.0
* typedArray <Buffer> | <TypedArray>
* Returns: <Buffer> | <TypedArray>
Generates cryptographically strong random values. The given typedArray is
filled with random values, and a reference to typedArray is returned.
The given typedArray must be an integer-based instance of <TypedArray> ,
i.e. Float32Array and Float64Array are not accepted.
An error will be thrown if the given typedArray is larger than 65,536 bytes.
crypto.randomUUID() #
Added in: v16.7.0
* Returns: <string>
Generates a random RFC 4122 version 4 UUID. The UUID is generated using a
cryptographic pseudorandom number generator.
Class: CryptoKey #
Added in: v15.0.0
cryptoKey.algorithm #
Added in: v15.0.0
* Type: <KeyAlgorithm> | <RsaHashedKeyAlgorithm> | <EcKeyAlgorithm> | <AesKeyAlgorithm> | <HmacKeyAlgorithm>
An object detailing the algorithm for which the key can be used along with
additional algorithm-specific parameters.
Read-only.
cryptoKey.extractable #
Added in: v15.0.0
* Type: <boolean>
When true , the <CryptoKey> can be extracted using either
subtleCrypto.exportKey() or subtleCrypto.wrapKey() .
Read-only.
cryptoKey.type #
Added in: v15.0.0
* Type: <string> One of 'secret' , 'private' , or 'public' .
A string identifying whether the key is a symmetric ( 'secret' ) or
asymmetric ( 'private' or 'public' ) key.
cryptoKey.usages #
Added in: v15.0.0
* Type: <string[]>
An array of strings identifying the o
Links found on this page
- Skip to content [direct]
- Node.js [direct]
- About this documentation [direct]
- Usage and example [direct]
- Assertion testing [direct]
- Asynchronous context tracking [direct]
- Async hooks [direct]
- Buffer [direct]
- C++ addons [direct]
- C/C++ addons with Node-API [direct]
- C++ embedder API [direct]
- Child processes [direct]
- Cluster [direct]
- Command-line options [direct]
- Console [direct]
- Crypto [direct]
- Debugger [direct]
- Deprecated APIs [direct]
- Diagnostics Channel [direct]
- DNS [direct]
- Domain [direct]
- Environment Variables [direct]
- Errors [direct]
- Events [direct]
- File system [direct]
- Globals [direct]
- HTTP [direct]
- HTTP/2 [direct]
- HTTPS [direct]
- Inspector [direct]
- Internationalization [direct]
- Modules: CommonJS modules [direct]
- Modules: ECMAScript modules [direct]
- Modules: node:module API [direct]
- Modules: Packages [direct]
- Modules: TypeScript [direct]
- Net [direct]
- OS [direct]
- Path [direct]
- Performance hooks [direct]
- Permissions [direct]
- Process [direct]
- Punycode [direct]
- Query strings [direct]
- Readline [direct]
- REPL [direct]
- Report [direct]
- Single executable applications [direct]
- SQLite [direct]
- Stream [direct]
- String decoder [direct]
- Test runner [direct]
- Timers [direct]
- TLS/SSL [direct]
- Trace events [direct]
- TTY [direct]
- UDP/datagram [direct]
- URL [direct]
- Utilities [direct]
- V8 [direct]
- VM [direct]
- WASI [direct]
- Web Streams API [direct]
- Worker threads [direct]
- Zlib [direct]
- Code repository and issue tracker [direct]
- Index [direct]
- 26.x [direct]
- 25.x [direct]
- 24.x LTS [direct]
- 23.x [direct]
- 21.x [direct]
- 20.x [direct]
- 19.x [direct]
- 18.x [direct]
- 17.x [direct]
- 16.x [direct]
- 15.x [direct]
- View on single page [direct]
- View as JSON [direct]