Web Crypto API#
History
| Version | Changes |
|---|---|
| v24.8.0 | KMAC algorithms are now supported. |
| v24.8.0 | Argon2 algorithms are now supported. |
| v24.7.0 | AES-OCB algorithm is now supported. |
| v24.7.0 | ML-KEM algorithms are now supported. |
| v24.7.0 | ChaCha20-Poly1305 algorithm is now supported. |
| v24.7.0 | SHA-3 algorithms are now supported. |
| v24.7.0 | SHAKE algorithms are now supported. |
| v24.7.0 | ML-DSA algorithms are now supported. |
| v23.5.0, v22.13.0, v20.19.3 | Algorithms |
| v19.0.0 | No longer experimental except for the |
| 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 |
| v18.4.0, v16.17.0 | Removed proprietary |
| v18.4.0, v16.17.0 | Added |
| v18.4.0, v16.17.0 | Removed proprietary |
| v18.4.0, v16.17.0 | Removed proprietary |
Node.js provides an implementation of theWeb Crypto API standard.
UseglobalThis.crypto orrequire('node:crypto').webcrypto to access thismodule.
const { subtle } = globalThis.crypto;(asyncfunction() {const key =await subtle.generateKey({name:'HMAC',hash:'SHA-256',length:256, },true, ['sign','verify']);const enc =newTextEncoder();const message = enc.encode('I love cupcakes');const digest =await subtle.sign({name:'HMAC', }, key, message);})();Modern Algorithms in the Web Cryptography API#
Node.js provides an implementation of the following features from theModern Algorithms in the Web Cryptography APIWICG proposal:
Algorithms:
'AES-OCB'1'Argon2d'2'Argon2i'2'Argon2id'2'ChaCha20-Poly1305''cSHAKE128''cSHAKE256''KMAC128'1'KMAC256'1'ML-DSA-44'3'ML-DSA-65'3'ML-DSA-87'3'ML-KEM-512'3'ML-KEM-768'3'ML-KEM-1024'3'SHA3-256''SHA3-384''SHA3-512'
Key Formats:
'raw-public''raw-secret''raw-seed'
Methods:
Secure Curves in the Web Cryptography API#
Node.js provides an implementation of the following features from theSecure Curves in the Web Cryptography APIWICG proposal:
Algorithms:
'Ed448''X448'
Examples#
Generating keys#
The<SubtleCrypto> class can be used to generate symmetric (secret) keysor asymmetric key pairs (public key and private key).
AES keys#
const { subtle } = globalThis.crypto;asyncfunctiongenerateAesKey(length =256) {const key =await subtle.generateKey({name:'AES-CBC', length, },true, ['encrypt','decrypt']);return key;}ECDSA key pairs#
const { subtle } = globalThis.crypto;asyncfunctiongenerateEcKey(namedCurve ='P-521') {const { publicKey, privateKey, } =await subtle.generateKey({name:'ECDSA', namedCurve, },true, ['sign','verify']);return { publicKey, privateKey };}Ed25519/X25519 key pairs#
const { subtle } = globalThis.crypto;asyncfunctiongenerateEd25519Key() {return subtle.generateKey({name:'Ed25519', },true, ['sign','verify']);}asyncfunctiongenerateX25519Key() {return subtle.generateKey({name:'X25519', },true, ['deriveKey']);}HMAC keys#
const { subtle } = globalThis.crypto;asyncfunctiongenerateHmacKey(hash ='SHA-256') {const key =await subtle.generateKey({name:'HMAC', hash, },true, ['sign','verify']);return key;}RSA key pairs#
const { subtle } = globalThis.crypto;const publicExponent =newUint8Array([1,0,1]);asyncfunctiongenerateRsaKey(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 };}Encryption and decryption#
const crypto = globalThis.crypto;asyncfunctionaesEncrypt(plaintext) {const ec =newTextEncoder();const key =awaitgenerateAesKey();const iv = crypto.getRandomValues(newUint8Array(16));const ciphertext =await crypto.subtle.encrypt({name:'AES-CBC', iv, }, key, ec.encode(plaintext));return { key, iv, ciphertext, };}asyncfunctionaesDecrypt(ciphertext, key, iv) {const dec =newTextDecoder();const plaintext =await crypto.subtle.decrypt({name:'AES-CBC', iv, }, key, ciphertext);return dec.decode(plaintext);}Exporting and importing keys#
const { subtle } = globalThis.crypto;asyncfunctiongenerateAndExportHmacKey(format ='jwk', hash ='SHA-512') {const key =await subtle.generateKey({name:'HMAC', hash, },true, ['sign','verify']);return subtle.exportKey(format, key);}asyncfunctionimportHmacKey(keyData, format ='jwk', hash ='SHA-512') {const key =await subtle.importKey(format, keyData, {name:'HMAC', hash, },true, ['sign','verify']);return key;}Wrapping and unwrapping keys#
const { subtle } = globalThis.crypto;asyncfunctiongenerateAndWrapHmacKey(format ='jwk', hash ='SHA-512') {const [ key, wrappingKey, ] =awaitPromise.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 };}asyncfunctionunwrapHmacKey( 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;}Sign and verify#
const { subtle } = globalThis.crypto;asyncfunctionsign(key, data) {const ec =newTextEncoder();const signature =await subtle.sign('RSASSA-PKCS1-v1_5', key, ec.encode(data));return signature;}asyncfunctionverify(key, signature, data) {const ec =newTextEncoder();const verified =await subtle.verify('RSASSA-PKCS1-v1_5', key, signature, ec.encode(data));return verified;}Deriving bits and keys#
const { subtle } = globalThis.crypto;asyncfunctionpbkdf2(pass, salt, iterations =1000, length =256) {const ec =newTextEncoder();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;}asyncfunctionpbkdf2Key(pass, salt, iterations =1000, length =256) {const ec =newTextEncoder();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;}Digest#
const { subtle } = globalThis.crypto;asyncfunctiondigest(data, algorithm ='SHA-512') {const ec =newTextEncoder();const digest =await subtle.digest(algorithm, ec.encode(data));return digest;}Checking for runtime algorithm support#
SubtleCrypto.supports() allows feature detection in Web Crypto API,which can be used to detect whether a given algorithm identifier(including its parameters) is supported for the given operation.
This example derives a key from a password using Argon2, if available,or PBKDF2, otherwise; and then encrypts and decrypts some text with itusing AES-OCB, if available, and AES-GCM, otherwise.
const {SubtleCrypto, crypto } = globalThis;const password ='correct horse battery staple';const derivationAlg =SubtleCrypto.supports?.('importKey','Argon2id') ?'Argon2id' :'PBKDF2';const encryptionAlg =SubtleCrypto.supports?.('importKey','AES-OCB') ?'AES-OCB' :'AES-GCM';const passwordKey =await crypto.subtle.importKey( derivationAlg ==='Argon2id' ?'raw-secret' :'raw',newTextEncoder().encode(password), derivationAlg,false, ['deriveKey'],);const nonce = crypto.getRandomValues(newUint8Array(16));const derivationParams = derivationAlg ==='Argon2id' ? { nonce,parallelism:4,memory:2 **21,passes:1, } : {salt: nonce,iterations:100_000,hash:'SHA-256', };const key =await crypto.subtle.deriveKey( {name: derivationAlg, ...derivationParams, }, passwordKey, {name: encryptionAlg,length:256, },false, ['encrypt','decrypt'],);const plaintext ='Hello, world!';const iv = crypto.getRandomValues(newUint8Array(16));const encrypted =await crypto.subtle.encrypt( {name: encryptionAlg, iv }, key,newTextEncoder().encode(plaintext),);const decrypted =newTextDecoder().decode(await crypto.subtle.decrypt( {name: encryptionAlg, iv }, key, encrypted,));Algorithm matrix#
The tables details the algorithms supported by the Node.js Web Crypto APIimplementation and the APIs supported for each:
Key Management APIs#
| Algorithm | subtle.generateKey() | subtle.exportKey() | subtle.importKey() | subtle.getPublicKey() |
|---|---|---|---|---|
'AES-CBC' | ✔ | ✔ | ✔ | |
'AES-CTR' | ✔ | ✔ | ✔ | |
'AES-GCM' | ✔ | ✔ | ✔ | |
'AES-KW' | ✔ | ✔ | ✔ | |
'AES-OCB' | ✔ | ✔ | ✔ | |
'Argon2d' | ✔ | |||
'Argon2i' | ✔ | |||
'Argon2id' | ✔ | |||
'ChaCha20-Poly1305'4 | ✔ | ✔ | ✔ | |
'ECDH' | ✔ | ✔ | ✔ | ✔ |
'ECDSA' | ✔ | ✔ | ✔ | ✔ |
'Ed25519' | ✔ | ✔ | ✔ | ✔ |
'Ed448'5 | ✔ | ✔ | ✔ | ✔ |
'HKDF' | ✔ | |||
'HMAC' | ✔ | ✔ | ✔ | |
'KMAC128'4 | ✔ | ✔ | ✔ | |
'KMAC256'4 | ✔ | ✔ | ✔ | |
'ML-DSA-44'4 | ✔ | ✔ | ✔ | ✔ |
'ML-DSA-65'4 | ✔ | ✔ | ✔ | ✔ |
'ML-DSA-87'4 | ✔ | ✔ | ✔ | ✔ |
'ML-KEM-512'4 | ✔ | ✔ | ✔ | ✔ |
'ML-KEM-768'4 | ✔ | ✔ | ✔ | ✔ |
'ML-KEM-1024'4 | ✔ | ✔ | ✔ | ✔ |
'PBKDF2' | ✔ | |||
'RSA-OAEP' | ✔ | ✔ | ✔ | ✔ |
'RSA-PSS' | ✔ | ✔ | ✔ | ✔ |
'RSASSA-PKCS1-v1_5' | ✔ | ✔ | ✔ | ✔ |
'X25519' | ✔ | ✔ | ✔ | ✔ |
'X448'5 | ✔ | ✔ | ✔ | ✔ |
Crypto Operation APIs#
Column Legend:
- Encryption:
subtle.encrypt()/subtle.decrypt() - Signatures and MAC:
subtle.sign()/subtle.verify() - Key or Bits Derivation:
subtle.deriveBits()/subtle.deriveKey() - Key Wrapping:
subtle.wrapKey()/subtle.unwrapKey() - Key Encapsulation:
subtle.encapsulateBits()/subtle.decapsulateBits()/subtle.encapsulateKey()/subtle.decapsulateKey() - Digest:
subtle.digest()
| Algorithm | Encryption | Signatures and MAC | Key or Bits Derivation | Key Wrapping | Key Encapsulation | Digest |
|---|---|---|---|---|---|---|
'AES-CBC' | ✔ | ✔ | ||||
'AES-CTR' | ✔ | ✔ | ||||
'AES-GCM' | ✔ | ✔ | ||||
'AES-KW' | ✔ | |||||
'AES-OCB' | ✔ | ✔ | ||||
'Argon2d' | ✔ | |||||
'Argon2i' | ✔ | |||||
'Argon2id' | ✔ | |||||
'ChaCha20-Poly1305'4 | ✔ | ✔ | ||||
'cSHAKE128'4 | ✔ | |||||
'cSHAKE256'4 | ✔ | |||||
'ECDH' | ✔ | |||||
'ECDSA' | ✔ | |||||
'Ed25519' | ✔ | |||||
'Ed448'5 | ✔ | |||||
'HKDF' | ✔ | |||||
'HMAC' | ✔ | |||||
'KMAC128'4 | ✔ | |||||
'KMAC256'4 | ✔ | |||||
'ML-DSA-44'4 | ✔ | |||||
'ML-DSA-65'4 | ✔ | |||||
'ML-DSA-87'4 | ✔ | |||||
'ML-KEM-512'4 | ✔ | |||||
'ML-KEM-768'4 | ✔ | |||||
'ML-KEM-1024'4 | ✔ | |||||
'PBKDF2' | ✔ | |||||
'RSA-OAEP' | ✔ | ✔ | ||||
'RSA-PSS' | ✔ | |||||
'RSASSA-PKCS1-v1_5' | ✔ | |||||
'SHA-1' | ✔ | |||||
'SHA-256' | ✔ | |||||
'SHA-384' | ✔ | |||||
'SHA-512' | ✔ | |||||
'SHA3-256'4 | ✔ | |||||
'SHA3-384'4 | ✔ | |||||
'SHA3-512'4 | ✔ | |||||
'X25519' | ✔ | |||||
'X448'5 | ✔ |
Class:Crypto#
globalThis.crypto is an instance of theCryptoclass.Crypto is a singleton that provides access to the remainder of thecrypto API.
crypto.getRandomValues(typedArray)#
typedArray<Buffer> |<TypedArray>- Returns:<Buffer> |<TypedArray>
Generates cryptographically strong random values. The giventypedArray isfilled with random values, and a reference totypedArray is returned.
The giventypedArray must be an integer-based instance of<TypedArray>,i.e.Float32Array andFloat64Array are not accepted.
An error will be thrown if the giventypedArray is larger than 65,536 bytes.
Class:CryptoKey#
cryptoKey.algorithm#
- Type:<KeyAlgorithm> |<RsaHashedKeyAlgorithm> |<EcKeyAlgorithm> |<AesKeyAlgorithm> |<HmacKeyAlgorithm> |<KmacKeyAlgorithm>
An object detailing the algorithm for which the key can be used along withadditional algorithm-specific parameters.
Read-only.
cryptoKey.extractable#
- Type:<boolean>
Whentrue, the<CryptoKey> can be extracted using eithersubtle.exportKey() orsubtle.wrapKey().
Read-only.
cryptoKey.type#
- Type:<string> One of
'secret','private', or'public'.
A string identifying whether the key is a symmetric ('secret') orasymmetric ('private' or'public') key.
cryptoKey.usages#
- Type:<string[]>
An array of strings identifying the operations for which thekey may be used.
The possible usages are:
'encrypt'- Enable using the key withsubtle.encrypt()'decrypt'- Enable using the key withsubtle.decrypt()'sign'- Enable using the key withsubtle.sign()'verify'- Enable using the key withsubtle.verify()'deriveKey'- Enable using the key withsubtle.deriveKey()'deriveBits'- Enable using the key withsubtle.deriveBits()'encapsulateBits'- Enable using the key withsubtle.encapsulateBits()'decapsulateBits'- Enable using the key withsubtle.decapsulateBits()'encapsulateKey'- Enable using the key withsubtle.encapsulateKey()'decapsulateKey'- Enable using the key withsubtle.decapsulateKey()'wrapKey'- Enable using the key withsubtle.wrapKey()'unwrapKey'- Enable using the key withsubtle.unwrapKey()
Valid key usages depend on the key algorithm (identified bycryptokey.algorithm.name).
Column Legend:
- Encryption:
subtle.encrypt()/subtle.decrypt() - Signatures and MAC:
subtle.sign()/subtle.verify() - Key or Bits Derivation:
subtle.deriveBits()/subtle.deriveKey() - Key Wrapping:
subtle.wrapKey()/subtle.unwrapKey() - Key Encapsulation:
subtle.encapsulateBits()/subtle.decapsulateBits()/subtle.encapsulateKey()/subtle.decapsulateKey()
| Supported Key Algorithm | Encryption | Signatures and MAC | Key or Bits Derivation | Key Wrapping | Key Encapsulation |
|---|---|---|---|---|---|
'AES-CBC' | ✔ | ✔ | |||
'AES-CTR' | ✔ | ✔ | |||
'AES-GCM' | ✔ | ✔ | |||
'AES-KW' | ✔ | ||||
'AES-OCB' | ✔ | ✔ | |||
'Argon2d' | ✔ | ||||
'Argon2i' | ✔ | ||||
'Argon2id' | ✔ | ||||
'ChaCha20-Poly1305'4 | ✔ | ✔ | |||
'ECDH' | ✔ | ||||
'ECDSA' | ✔ | ||||
'Ed25519' | ✔ | ||||
'Ed448'5 | ✔ | ||||
'HDKF' | ✔ | ||||
'HMAC' | ✔ | ||||
'KMAC128'4 | ✔ | ||||
'KMAC256'4 | ✔ | ||||
'ML-DSA-44'4 | ✔ | ||||
'ML-DSA-65'4 | ✔ | ||||
'ML-DSA-87'4 | ✔ | ||||
'ML-KEM-512'4 | ✔ | ||||
'ML-KEM-768'4 | ✔ | ||||
'ML-KEM-1024'4 | ✔ | ||||
'PBKDF2' | ✔ | ||||
'RSA-OAEP' | ✔ | ✔ | |||
'RSA-PSS' | ✔ | ||||
'RSASSA-PKCS1-v1_5' | ✔ | ||||
'X25519' | ✔ | ||||
'X448'5 | ✔ |
Class:CryptoKeyPair#
TheCryptoKeyPair is a simple dictionary object withpublicKey andprivateKey properties, representing an asymmetric key pair.
Class:SubtleCrypto#
Static method:SubtleCrypto.supports(operation, algorithm[, lengthOrAdditionalAlgorithm])#
operation<string> "encrypt", "decrypt", "sign", "verify", "digest", "generateKey", "deriveKey", "deriveBits", "importKey", "exportKey", "getPublicKey", "wrapKey", "unwrapKey", "encapsulateBits", "encapsulateKey", "decapsulateBits", or "decapsulateKey"algorithm<string> |<Algorithm>lengthOrAdditionalAlgorithm<null> |<number> |<string> |<Algorithm> |<undefined> Depending on the operation this is either ignored, the value of the length argument when operation is "deriveBits", the algorithm of key to be derived when operation is "deriveKey", the algorithm of key to be exported before wrapping when operation is "wrapKey", the algorithm of key to be imported after unwrapping when operation is "unwrapKey", or the algorithm of key to be imported after en/decapsulating a key when operation is "encapsulateKey" or "decapsulateKey".Default:nullwhen operation is "deriveBits",undefinedotherwise.- Returns:<boolean> Indicating whether the implementation supports the given operation
Allows feature detection in Web Crypto API,which can be used to detect whether a given algorithm identifier(including its parameters) is supported for the given operation.
SeeChecking for runtime algorithm support for an example use of this method.
subtle.decapsulateBits(decapsulationAlgorithm, decapsulationKey, ciphertext)#
decapsulationAlgorithm<string> |<Algorithm>decapsulationKey<CryptoKey>ciphertext<ArrayBuffer> |<TypedArray> |<DataView> |<Buffer>- Returns:<Promise> Fulfills with<ArrayBuffer> upon success.
A message recipient uses their asymmetric private key to decrypt an"encapsulated key" (ciphertext), thereby recovering a temporary symmetrickey (represented as<ArrayBuffer>) which is then used to decrypt a message.
The algorithms currently supported include:
subtle.decapsulateKey(decapsulationAlgorithm, decapsulationKey, ciphertext, sharedKeyAlgorithm, extractable, usages)#
decapsulationAlgorithm<string> |<Algorithm>decapsulationKey<CryptoKey>ciphertext<ArrayBuffer> |<TypedArray> |<DataView> |<Buffer>sharedKeyAlgorithm<string> |<Algorithm> |<HmacImportParams> |<AesDerivedKeyParams> |<KmacImportParams>extractable<boolean>usages<string[]> SeeKey usages.- Returns:<Promise> Fulfills with<CryptoKey> upon success.
A message recipient uses their asymmetric private key to decrypt an"encapsulated key" (ciphertext), thereby recovering a temporary symmetrickey (represented as<CryptoKey>) which is then used to decrypt a message.
The algorithms currently supported include:
subtle.decrypt(algorithm, key, data)#
History
| Version | Changes |
|---|---|
| v24.7.0 | AES-OCB algorithm is now supported. |
| v24.7.0 | ChaCha20-Poly1305 algorithm is now supported. |
| v15.0.0 | Added in: v15.0.0 |
algorithm<RsaOaepParams> |<AesCtrParams> |<AesCbcParams> |<AeadParams>key<CryptoKey>data<ArrayBuffer> |<TypedArray> |<DataView> |<Buffer>- Returns:<Promise> Fulfills with an<ArrayBuffer> upon success.
Using the method and parameters specified inalgorithm and the keyingmaterial provided bykey, this method attempts to decipher theprovideddata. If successful, the returned promise will be resolved withan<ArrayBuffer> containing the plaintext result.
The algorithms currently supported include:
subtle.deriveBits(algorithm, baseKey[, length])#
History
| Version | Changes |
|---|---|
| v24.8.0 | Argon2 algorithms are now supported. |
| v22.5.0, v20.17.0, v18.20.5 | The length parameter is now optional for |
| v18.4.0, v16.17.0 | Added |
| v15.0.0 | Added in: v15.0.0 |
algorithm<EcdhKeyDeriveParams> |<HkdfParams> |<Pbkdf2Params> |<Argon2Params>baseKey<CryptoKey>length<number> |<null>Default:null- Returns:<Promise> Fulfills with an<ArrayBuffer> upon success.
Using the method and parameters specified inalgorithm and the keyingmaterial provided bybaseKey, this method attempts to generatelength bits.
Whenlength is not provided ornull the maximum number of bits for a givenalgorithm is generated. This is allowed for the'ECDH','X25519', and'X448'5algorithms, for other algorithmslength is required to be a number.
If successful, the returned promise will be resolved with an<ArrayBuffer>containing the generated data.
The algorithms currently supported include:
subtle.deriveKey(algorithm, baseKey, derivedKeyAlgorithm, extractable, keyUsages)#
History
| Version | Changes |
|---|---|
| v24.8.0 | Argon2 algorithms are now supported. |
| v18.4.0, v16.17.0 | Added |
| v15.0.0 | Added in: v15.0.0 |
algorithm<EcdhKeyDeriveParams> |<HkdfParams> |<Pbkdf2Params> |<Argon2Params>baseKey<CryptoKey>derivedKeyAlgorithm<string> |<Algorithm> |<HmacImportParams> |<AesDerivedKeyParams> |<KmacImportParams>extractable<boolean>keyUsages<string[]> SeeKey usages.- Returns:<Promise> Fulfills with a<CryptoKey> upon success.
Using the method and parameters specified inalgorithm, and the keyingmaterial provided bybaseKey, this method attempts to generatea new<CryptoKey> based on the method and parameters inderivedKeyAlgorithm.
Calling this method is equivalent to callingsubtle.deriveBits() togenerate raw keying material, then passing the result into thesubtle.importKey() method using thederiveKeyAlgorithm,extractable, andkeyUsages parameters as input.
The algorithms currently supported include:
subtle.digest(algorithm, data)#
History
| Version | Changes |
|---|---|
| v24.7.0 | SHA-3 algorithms are now supported. |
| v24.7.0 | SHAKE algorithms are now supported. |
| v15.0.0 | Added in: v15.0.0 |
algorithm<string> |<Algorithm> |<CShakeParams>data<ArrayBuffer> |<TypedArray> |<DataView> |<Buffer>- Returns:<Promise> Fulfills with an<ArrayBuffer> upon success.
Using the method identified byalgorithm, this method attempts togenerate a digest ofdata. If successful, the returned promise is resolvedwith an<ArrayBuffer> containing the computed digest.
Ifalgorithm is provided as a<string>, it must be one of:
Ifalgorithm is provided as an<Object>, it must have aname propertywhose value is one of the above.
subtle.encapsulateBits(encapsulationAlgorithm, encapsulationKey)#
encapsulationAlgorithm<string> |<Algorithm>encapsulationKey<CryptoKey>- Returns:<Promise> Fulfills with<EncapsulatedBits> upon success.
Uses a message recipient's asymmetric public key to encrypt a temporary symmetric key.This encrypted key is the "encapsulated key" represented as<EncapsulatedBits>.
The algorithms currently supported include:
subtle.encapsulateKey(encapsulationAlgorithm, encapsulationKey, sharedKeyAlgorithm, extractable, usages)#
encapsulationAlgorithm<string> |<Algorithm>encapsulationKey<CryptoKey>sharedKeyAlgorithm<string> |<Algorithm> |<HmacImportParams> |<AesDerivedKeyParams> |<KmacImportParams>extractable<boolean>usages<string[]> SeeKey usages.- Returns:<Promise> Fulfills with<EncapsulatedKey> upon success.
Uses a message recipient's asymmetric public key to encrypt a temporary symmetric key.This encrypted key is the "encapsulated key" represented as<EncapsulatedKey>.
The algorithms currently supported include:
subtle.encrypt(algorithm, key, data)#
History
| Version | Changes |
|---|---|
| v24.7.0 | AES-OCB algorithm is now supported. |
| v24.7.0 | ChaCha20-Poly1305 algorithm is now supported. |
| v15.0.0 | Added in: v15.0.0 |
algorithm<RsaOaepParams> |<AesCtrParams> |<AesCbcParams> |<AeadParams>key<CryptoKey>data<ArrayBuffer> |<TypedArray> |<DataView> |<Buffer>- Returns:<Promise> Fulfills with an<ArrayBuffer> upon success.
Using the method and parameters specified byalgorithm and the keyingmaterial provided bykey, this method attempts to encipherdata.If successful, the returned promise is resolved with an<ArrayBuffer>containing the encrypted result.
The algorithms currently supported include:
subtle.exportKey(format, key)#
History
| Version | Changes |
|---|---|
| v24.8.0 | KMAC algorithms are now supported. |
| v24.7.0 | ML-KEM algorithms are now supported. |
| v24.7.0 | ChaCha20-Poly1305 algorithm is now supported. |
| v24.7.0 | ML-DSA algorithms are now supported. |
| v18.4.0, v16.17.0 | Added |
| v15.9.0 | Removed |
| v15.0.0 | Added in: v15.0.0 |
format<string> Must be one of'raw','pkcs8','spki','jwk','raw-secret'4,'raw-public'4, or'raw-seed'4.key<CryptoKey>- Returns:<Promise> Fulfills with an<ArrayBuffer> |<Object> upon success.
Exports the given key into the specified format, if supported.
If the<CryptoKey> is not extractable, the returned promise will reject.
Whenformat is either'pkcs8' or'spki' and the export is successful,the returned promise will be resolved with an<ArrayBuffer> containing theexported key data.
Whenformat is'jwk' and the export is successful, the returned promisewill be resolved with a JavaScript object conforming to theJSON Web Keyspecification.
| Supported Key Algorithm | 'spki' | 'pkcs8' | 'jwk' | 'raw' | 'raw-secret' | 'raw-public' | 'raw-seed' |
|---|---|---|---|---|---|---|---|
'AES-CBC' | ✔ | ✔ | ✔ | ||||
'AES-CTR' | ✔ | ✔ | ✔ | ||||
'AES-GCM' | ✔ | ✔ | ✔ | ||||
'AES-KW' | ✔ | ✔ | ✔ | ||||
'AES-OCB'4 | ✔ | ✔ | |||||
'ChaCha20-Poly1305'4 | ✔ | ✔ | |||||
'ECDH' | ✔ | ✔ | ✔ | ✔ | ✔ | ||
'ECDSA' | ✔ | ✔ | ✔ | ✔ | ✔ | ||
'Ed25519' | ✔ | ✔ | ✔ | ✔ | ✔ | ||
'Ed448'5 | ✔ | ✔ | ✔ | ✔ | ✔ | ||
'HMAC' | ✔ | ✔ | ✔ | ||||
'KMAC128'4 | ✔ | ✔ | |||||
'KMAC256'4 | ✔ | ✔ | |||||
'ML-DSA-44'4 | ✔ | ✔ | ✔ | ✔ | ✔ | ||
'ML-DSA-65'4 | ✔ | ✔ | ✔ | ✔ | ✔ | ||
'ML-DSA-87'4 | ✔ | ✔ | ✔ | ✔ | ✔ | ||
'ML-KEM-512'4 | ✔ | ✔ | ✔ | ✔ | |||
'ML-KEM-768'4 | ✔ | ✔ | ✔ | ✔ | |||
'ML-KEM-1024'4 | ✔ | ✔ | ✔ | ✔ | |||
'RSA-OAEP' | ✔ | ✔ | ✔ | ||||
'RSA-PSS' | ✔ | ✔ | ✔ | ||||
'RSASSA-PKCS1-v1_5' | ✔ | ✔ | ✔ |
subtle.getPublicKey(key, keyUsages)#
key<CryptoKey> A private key from which to derive the corresponding public key.keyUsages<string[]> SeeKey usages.- Returns:<Promise> Fulfills with a<CryptoKey> upon success.
Derives the public key from a given private key.
subtle.generateKey(algorithm, extractable, keyUsages)#
History
| Version | Changes |
|---|---|
| v24.8.0 | KMAC algorithms are now supported. |
| v24.7.0 | ML-KEM algorithms are now supported. |
| v24.7.0 | ChaCha20-Poly1305 algorithm is now supported. |
| v24.7.0 | ML-DSA algorithms are now supported. |
| v15.0.0 | Added in: v15.0.0 |
algorithm<string> |<Algorithm> |<RsaHashedKeyGenParams> |<EcKeyGenParams> |<HmacKeyGenParams> |<AesKeyGenParams> |<KmacKeyGenParams>
extractable<boolean>keyUsages<string[]> SeeKey usages.- Returns:<Promise> Fulfills with a<CryptoKey> |<CryptoKeyPair> upon success.
Using the parameters provided inalgorithm, this methodattempts to generate new keying material. Depending on the algorithm usedeither a single<CryptoKey> or a<CryptoKeyPair> is generated.
The<CryptoKeyPair> (public and private key) generating algorithms supportedinclude:
'ECDH''ECDSA''Ed25519''Ed448'5'ML-DSA-44'4'ML-DSA-65'4'ML-DSA-87'4'ML-KEM-512'4'ML-KEM-768'4'ML-KEM-1024'4'RSA-OAEP''RSA-PSS''RSASSA-PKCS1-v1_5''X25519''X448'5
The<CryptoKey> (secret key) generating algorithms supported include:
subtle.importKey(format, keyData, algorithm, extractable, keyUsages)#
History
| Version | Changes |
|---|---|
| v24.8.0 | KMAC algorithms are now supported. |
| v24.7.0 | ML-KEM algorithms are now supported. |
| v24.7.0 | ChaCha20-Poly1305 algorithm is now supported. |
| v24.7.0 | ML-DSA algorithms are now supported. |
| v18.4.0, v16.17.0 | Added |
| v15.9.0 | Removed |
| v15.0.0 | Added in: v15.0.0 |
format<string> Must be one of'raw','pkcs8','spki','jwk','raw-secret'4,'raw-public'4, or'raw-seed'4.keyData<ArrayBuffer> |<TypedArray> |<DataView> |<Buffer> |<Object>
algorithm<string> |<Algorithm> |<RsaHashedImportParams> |<EcKeyImportParams> |<HmacImportParams> |<KmacImportParams>
extractable<boolean>keyUsages<string[]> SeeKey usages.- Returns:<Promise> Fulfills with a<CryptoKey> upon success.
This method attempts to interpret the providedkeyDataas the givenformat to create a<CryptoKey> instance using the providedalgorithm,extractable, andkeyUsages arguments. If the import issuccessful, the returned promise will be resolved with a<CryptoKey>representation of the key material.
If importing KDF algorithm keys,extractable must befalse.
The algorithms currently supported include:
| Supported Key Algorithm | 'spki' | 'pkcs8' | 'jwk' | 'raw' | 'raw-secret' | 'raw-public' | 'raw-seed' |
|---|---|---|---|---|---|---|---|
'AES-CBC' | ✔ | ✔ | ✔ | ||||
'AES-CTR' | ✔ | ✔ | ✔ | ||||
'AES-GCM' | ✔ | ✔ | ✔ | ||||
'AES-KW' | ✔ | ✔ | ✔ | ||||
'AES-OCB'4 | ✔ | ✔ | |||||
'Argon2d'4 | ✔ | ||||||
'Argon2i'4 | ✔ | ||||||
'Argon2id'4 | ✔ | ||||||
'ChaCha20-Poly1305'4 | ✔ | ✔ | |||||
'ECDH' | ✔ | ✔ | ✔ | ✔ | ✔ | ||
'ECDSA' | ✔ | ✔ | ✔ | ✔ | ✔ | ||
'Ed25519' | ✔ | ✔ | ✔ | ✔ | ✔ | ||
'Ed448'5 | ✔ | ✔ | ✔ | ✔ | ✔ | ||
'HDKF' | ✔ | ✔ | |||||
'HMAC' | ✔ | ✔ | ✔ | ||||
'KMAC128'4 | ✔ | ✔ | |||||
'KMAC256'4 | ✔ | ✔ | |||||
'ML-DSA-44'4 | ✔ | ✔ | ✔ | ✔ | ✔ | ||
'ML-DSA-65'4 | ✔ | ✔ | ✔ | ✔ | ✔ | ||
'ML-DSA-87'4 | ✔ | ✔ | ✔ | ✔ | ✔ | ||
'ML-KEM-512'4 | ✔ | ✔ | ✔ | ✔ | |||
'ML-KEM-768'4 | ✔ | ✔ | ✔ | ✔ | |||
'ML-KEM-1024'4 | ✔ | ✔ | ✔ | ✔ | |||
'PBKDF2' | ✔ | ✔ | |||||
'RSA-OAEP' | ✔ | ✔ | ✔ | ||||
'RSA-PSS' | ✔ | ✔ | ✔ | ||||
'RSASSA-PKCS1-v1_5' | ✔ | ✔ | ✔ | ||||
'X25519' | ✔ | ✔ | ✔ | ✔ | ✔ | ||
'X448'5 | ✔ | ✔ | ✔ | ✔ | ✔ |
subtle.sign(algorithm, key, data)#
History
| Version | Changes |
|---|---|
| v24.8.0 | KMAC algorithms are now supported. |
| v24.7.0 | ML-DSA algorithms are now supported. |
| v18.4.0, v16.17.0 | Added |
| v15.0.0 | Added in: v15.0.0 |
algorithm<string> |<Algorithm> |<RsaPssParams> |<EcdsaParams> |<ContextParams> |<KmacParams>key<CryptoKey>data<ArrayBuffer> |<TypedArray> |<DataView> |<Buffer>- Returns:<Promise> Fulfills with an<ArrayBuffer> upon success.
Using the method and parameters given byalgorithm and the keying materialprovided bykey, this method attempts to generate a cryptographicsignature ofdata. If successful, the returned promise is resolved withan<ArrayBuffer> containing the generated signature.
The algorithms currently supported include:
subtle.unwrapKey(format, wrappedKey, unwrappingKey, unwrapAlgo, unwrappedKeyAlgo, extractable, keyUsages)#
History
| Version | Changes |
|---|---|
| v24.7.0 | AES-OCB algorithm is now supported. |
| v24.7.0 | ChaCha20-Poly1305 algorithm is now supported. |
| v15.0.0 | Added in: v15.0.0 |
format<string> Must be one of'raw','pkcs8','spki','jwk','raw-secret'4,'raw-public'4, or'raw-seed'4.wrappedKey<ArrayBuffer> |<TypedArray> |<DataView> |<Buffer>unwrappingKey<CryptoKey>
unwrapAlgo<string> |<Algorithm> |<RsaOaepParams> |<AesCtrParams> |<AesCbcParams> |<AeadParams>unwrappedKeyAlgo<string> |<Algorithm> |<RsaHashedImportParams> |<EcKeyImportParams> |<HmacImportParams> |<KmacImportParams>
extractable<boolean>keyUsages<string[]> SeeKey usages.- Returns:<Promise> Fulfills with a<CryptoKey> upon success.
In cryptography, "wrapping a key" refers to exporting and then encrypting thekeying material. This method attempts to decrypt a wrappedkey and create a<CryptoKey> instance. It is equivalent to callingsubtle.decrypt() first on the encrypted key data (using thewrappedKey,unwrapAlgo, andunwrappingKey arguments as input) then passing the resultsto thesubtle.importKey() method using theunwrappedKeyAlgo,extractable, andkeyUsages arguments as inputs. If successful, the returnedpromise is resolved with a<CryptoKey> object.
The wrapping algorithms currently supported include:
The unwrapped key algorithms supported include:
subtle.verify(algorithm, key, signature, data)#
History
| Version | Changes |
|---|---|
| v24.8.0 | KMAC algorithms are now supported. |
| v24.7.0 | ML-DSA algorithms are now supported. |
| v18.4.0, v16.17.0 | Added |
| v15.0.0 | Added in: v15.0.0 |
algorithm<string> |<Algorithm> |<RsaPssParams> |<EcdsaParams> |<ContextParams> |<KmacParams>key<CryptoKey>signature<ArrayBuffer> |<TypedArray> |<DataView> |<Buffer>data<ArrayBuffer> |<TypedArray> |<DataView> |<Buffer>- Returns:<Promise> Fulfills with a<boolean> upon success.
Using the method and parameters given inalgorithm and the keying materialprovided bykey, this method attempts to verify thatsignature isa valid cryptographic signature ofdata. The returned promise is resolvedwith eithertrue orfalse.
The algorithms currently supported include:
subtle.wrapKey(format, key, wrappingKey, wrapAlgo)#
History
| Version | Changes |
|---|---|
| v24.7.0 | AES-OCB algorithm is now supported. |
| v24.7.0 | ChaCha20-Poly1305 algorithm is now supported. |
| v15.0.0 | Added in: v15.0.0 |
format<string> Must be one of'raw','pkcs8','spki','jwk','raw-secret'4,'raw-public'4, or'raw-seed'4.key<CryptoKey>wrappingKey<CryptoKey>wrapAlgo<string> |<Algorithm> |<RsaOaepParams> |<AesCtrParams> |<AesCbcParams> |<AeadParams>- Returns:<Promise> Fulfills with an<ArrayBuffer> upon success.
In cryptography, "wrapping a key" refers to exporting and then encrypting thekeying material. This method exports the keying material intothe format identified byformat, then encrypts it using the method andparameters specified bywrapAlgo and the keying material provided bywrappingKey. It is the equivalent to callingsubtle.exportKey() usingformat andkey as the arguments, then passing the result to thesubtle.encrypt() method usingwrappingKey andwrapAlgo as inputs. Ifsuccessful, the returned promise will be resolved with an<ArrayBuffer>containing the encrypted key data.
The wrapping algorithms currently supported include:
Algorithm parameters#
The algorithm parameter objects define the methods and parameters used bythe various<SubtleCrypto> methods. While described here as "classes", theyare simple JavaScript dictionary objects.
Class:AeadParams#
aeadParams.additionalData#
Extra input that is not encrypted but is included in the authenticationof the data. The use ofadditionalData is optional.
aeadParams.iv#
- Type:<ArrayBuffer> |<TypedArray> |<DataView> |<Buffer>
The initialization vector must be unique for every encryption operation using agiven key.
Class:AesDerivedKeyParams#
Class:AesCbcParams#
aesCbcParams.iv#
- Type:<ArrayBuffer> |<TypedArray> |<DataView> |<Buffer>
Provides the initialization vector. It must be exactly 16-bytes in lengthand should be unpredictable and cryptographically random.
Class:AesCtrParams#
aesCtrParams.counter#
- Type:<ArrayBuffer> |<TypedArray> |<DataView> |<Buffer>
The initial value of the counter block. This must be exactly 16 bytes long.
TheAES-CTR method uses the rightmostlength bits of the block as thecounter and the remaining bits as the nonce.
Class:AesKeyAlgorithm#
Class:AesKeyGenParams#
Class:Argon2Params#
argon2Params.associatedData#
- Type:<ArrayBuffer> |<TypedArray> |<DataView> |<Buffer>
Represents the optional associated data.
argon2Params.memory#
- Type:<number>
Represents the memory size in kibibytes. It must be at least 8 times the degree of parallelism.
argon2Params.nonce#
- Type:<ArrayBuffer> |<TypedArray> |<DataView> |<Buffer>
Represents the nonce, which is a salt for password hashing applications.
argon2Params.secretValue#
- Type:<ArrayBuffer> |<TypedArray> |<DataView> |<Buffer>
Represents the optional secret value.
Class:ContextParams#
contextParams.name#
contextParams.context#
History
| Version | Changes |
|---|---|
| v24.8.0 | Non-empty context is now supported. |
| v24.7.0 | Added in: v24.7.0 |
Thecontext member represents the optional context data to associate withthe message.
Class:CShakeParams#
cShakeParams.customization#
Thecustomization member represents the customization string.The Node.js Web Crypto API implementation only supports zero-length customizationwhich is equivalent to not providing customization at all.
cShakeParams.functionName#
ThefunctionName member represents represents the function name, used by NIST to definefunctions based on cSHAKE.The Node.js Web Crypto API implementation only supports zero-length functionNamewhich is equivalent to not providing functionName at all.
Class:EcdhKeyDeriveParams#
ecdhKeyDeriveParams.public#
- Type:<CryptoKey>
ECDH key derivation operates by taking as input one parties private key andanother parties public key -- using both to generate a common shared secret.TheecdhKeyDeriveParams.public property is set to the other parties publickey.
Class:EcdsaParams#
ecdsaParams.hash#
History
| Version | Changes |
|---|---|
| v24.7.0 | SHA-3 algorithms are now supported. |
| v15.0.0 | Added in: v15.0.0 |
- Type:<string> |<Algorithm>
If represented as a<string>, the value must be one of:
If represented as an<Algorithm>, the object'sname propertymust be one of the above listed values.
Class:EcKeyAlgorithm#
Class:EcKeyGenParams#
Class:EcKeyImportParams#
Class:EncapsulatedBits#
A temporary symmetric secret key (represented as<ArrayBuffer>) for message encryptionand the ciphertext (that can be transmitted to the message recipient along with themessage) encrypted by this shared key. The recipient uses their private key to determinewhat the shared key is which then allows them to decrypt the message.
Class:EncapsulatedKey#
A temporary symmetric secret key (represented as<CryptoKey>) for message encryptionand the ciphertext (that can be transmitted to the message recipient along with themessage) encrypted by this shared key. The recipient uses their private key to determinewhat the shared key is which then allows them to decrypt the message.
Class:HkdfParams#
hkdfParams.hash#
History
| Version | Changes |
|---|---|
| v24.7.0 | SHA-3 algorithms are now supported. |
| v15.0.0 | Added in: v15.0.0 |
- Type:<string> |<Algorithm>
If represented as a<string>, the value must be one of:
If represented as an<Algorithm>, the object'sname propertymust be one of the above listed values.
hkdfParams.info#
- Type:<ArrayBuffer> |<TypedArray> |<DataView> |<Buffer>
Provides application-specific contextual input to the HKDF algorithm.This can be zero-length but must be provided.
hkdfParams.salt#
- Type:<ArrayBuffer> |<TypedArray> |<DataView> |<Buffer>
The salt value significantly improves the strength of the HKDF algorithm.It should be random or pseudorandom and should be the same length as theoutput of the digest function (for instance, if using'SHA-256' as thedigest, the salt should be 256-bits of random data).
Class:HmacImportParams#
hmacImportParams.hash#
History
| Version | Changes |
|---|---|
| v24.7.0 | SHA-3 algorithms are now supported. |
| v15.0.0 | Added in: v15.0.0 |
- Type:<string> |<Algorithm>
If represented as a<string>, the value must be one of:
If represented as an<Algorithm>, the object'sname propertymust be one of the above listed values.
Class:HmacKeyAlgorithm#
Class:HmacKeyGenParams#
hmacKeyGenParams.hash#
History
| Version | Changes |
|---|---|
| v24.7.0 | SHA-3 algorithms are now supported. |
| v15.0.0 | Added in: v15.0.0 |
- Type:<string> |<Algorithm>
If represented as a<string>, the value must be one of:
If represented as an<Algorithm>, the object'sname propertymust be one of the above listed values.
Class:KmacImportParams#
Class:KmacKeyAlgorithm#
Class:KmacKeyGenParams#
Class:KmacParams#
kmacParams.customization#
Thecustomization member represents the optional customization string.
Class:Pbkdf2Params#
pbkdf2Params.hash#
History
| Version | Changes |
|---|---|
| v24.7.0 | SHA-3 algorithms are now supported. |
| v15.0.0 | Added in: v15.0.0 |
- Type:<string> |<Algorithm>
If represented as a<string>, the value must be one of:
If represented as an<Algorithm>, the object'sname propertymust be one of the above listed values.
pbkdf2Params.iterations#
- Type:<number>
The number of iterations the PBKDF2 algorithm should make when deriving bits.
pbkdf2Params.salt#
- Type:<ArrayBuffer> |<TypedArray> |<DataView> |<Buffer>
Should be at least 16 random or pseudorandom bytes.
Class:RsaHashedImportParams#
rsaHashedImportParams.hash#
History
| Version | Changes |
|---|---|
| v24.7.0 | SHA-3 algorithms are now supported. |
| v15.0.0 | Added in: v15.0.0 |
- Type:<string> |<Algorithm>
If represented as a<string>, the value must be one of:
If represented as an<Algorithm>, the object'sname propertymust be one of the above listed values.
Class:RsaHashedKeyAlgorithm#
Class:RsaHashedKeyGenParams#
rsaHashedKeyGenParams.hash#
History
| Version | Changes |
|---|---|
| v24.7.0 | SHA-3 algorithms are now supported. |
| v15.0.0 | Added in: v15.0.0 |
- Type:<string> |<Algorithm>
If represented as a<string>, the value must be one of:
If represented as an<Algorithm>, the object'sname propertymust be one of the above listed values.
rsaHashedKeyGenParams.modulusLength#
- Type:<number>
The length in bits of the RSA modulus. As a best practice, this should beat least2048.
rsaHashedKeyGenParams.name#
- Type:<string> Must be one of
'RSASSA-PKCS1-v1_5','RSA-PSS', or'RSA-OAEP'.
rsaHashedKeyGenParams.publicExponent#
- Type:<Uint8Array>
The RSA public exponent. This must be a<Uint8Array> containing a big-endian,unsigned integer that must fit within 32-bits. The<Uint8Array> may contain anarbitrary number of leading zero-bits. The value must be a prime number. Unlessthere is reason to use a different value, usenew Uint8Array([1, 0, 1])(65537) as the public exponent.
Class:RsaOaepParams#
rsaOaepParams.label#
- Type:<ArrayBuffer> |<TypedArray> |<DataView> |<Buffer>
An additional collection of bytes that will not be encrypted, but will be boundto the generated ciphertext.
ThersaOaepParams.label parameter is optional.
Class:RsaPssParams#
rsaPssParams.saltLength#
- Type:<number>
The length (in bytes) of the random salt to use.
Footnotes
SeeModern Algorithms in the Web Cryptography API↩↩2↩3↩4↩5↩6↩7↩8↩9↩10↩11↩12↩13↩14↩15↩16↩17↩18↩19↩20↩21↩22↩23↩24↩25↩26↩27↩28↩29↩30↩31↩32↩33↩34↩35↩36↩37↩38↩39↩40↩41↩42↩43↩44↩45↩46↩47↩48↩49↩50↩51↩52↩53↩54↩55↩56↩57↩58↩59↩60↩61↩62↩63↩64↩65↩66↩67↩68↩69↩70↩71↩72↩73↩74↩75↩76↩77↩78↩79↩80↩81↩82↩83↩84↩85↩86↩87↩88↩89↩90↩91↩92↩93↩94↩95↩96↩97↩98↩99↩100↩101↩102↩103↩104↩105↩106↩107↩108↩109↩110↩111↩112↩113↩114↩115↩116↩117↩118↩119↩120↩121↩122↩123↩124↩125↩126↩127↩128↩129↩130↩131↩132↩133↩134↩135↩136↩137↩138↩139↩140↩141↩142↩143↩144↩145↩146↩147↩148↩149↩150
SeeSecure Curves in the Web Cryptography API↩↩2↩3↩4↩5↩6↩7↩8↩9↩10↩11↩12↩13↩14↩15↩16↩17↩18↩19↩20↩21↩22↩23↩24