Web Crypto API
Web Crypto API
Web Crypto API
The **Web Crypto API** is an interface allowing a script to use cryptographic primitives in order to build systems using cryptography.
Some browsers implemented an interface called Crypto without having it well defined or being cryptographically sound.
In order to avoid confusion, methods and properties of this interface have been removed from browsers implementing the Web Crypto API, and all Web Crypto API methods are available on a new interface: SubtleCrypto.
The Crypto.subtle property gives access to an object implementing it.
> [!WARNING]
> The Web Crypto API provides a number of low-level cryptographic primitives. It's very easy to misuse them, and the pitfalls involved can be very subtle.
>
> Even assuming you use the basic cryptographic functions correctly, secure key management and overall security system design are extremely hard to get right, and are generally the domain of specialist security experts.
>
> Errors in security system design and implementation can make the security of the system completely ineffective.
>
> Please learn and experiment, but don't guarantee or imply the security of your work before an individual knowledgeable in this subject matter thoroughly reviews it. The [Crypto 101 Course](https://www.crypto101.io/) can be a great place to start learning about the design and implementation of secure systems.
## Interfaces
- Crypto
- : Provides basic cryptography features, such as a cryptographically strong random number generator, and access to cryptographic primitives via a SubtleCrypto object.
An object of this type can be accessed in the global scope using Window.crypto or WorkerGlobalScope.crypto.
- SubtleCrypto
- : Represents an object that provides low-level cryptographic functions for key generation, encryption, decryption, key wrapping and unwrapping, and so on.
- CryptoKey
- : Represents a cryptographic key obtained from one of the SubtleCrypto methods generateKey(), deriveKey(), importKey(), or unwrapKey().
### Dictionaries
- AesCbcParams
- : Represents the object that should be passed as the `algorithm` parameter into SubtleCrypto.encrypt(), SubtleCrypto.decrypt(), SubtleCrypto.wrapKey(), or SubtleCrypto.unwrapKey(), when using the [AES-CBC](/docs/Web/API/SubtleCrypto/encrypt#aes-cbc) algorithm.
- AesCtrParams
- : Represents the object that should be passed as the `algorithm` parameter into SubtleCrypto.encrypt(), SubtleCrypto.decrypt(), SubtleCrypto.wrapKey(), or SubtleCrypto.unwrapKey(), when using the [AES-CTR](/docs/Web/API/SubtleCrypto/encrypt#aes-ctr) algorithm.
- AesGcmParams
- : Represents the object that should be passed as the `algorithm` parameter into SubtleCrypto.encrypt(), SubtleCrypto.decrypt(), SubtleCrypto.wrapKey(), or SubtleCrypto.unwrapKey(), when using the [AES-GCM](/docs/Web/API/SubtleCrypto/encrypt#aes-gcm) algorithm.
- AesKeyGenParams
- : Represents the object that should be passed as the `algorithm` parameter into SubtleCrypto.generateKey(), when generating an AES key: that is, when the algorithm is identified as any of [AES-CBC](/docs/Web/API/SubtleCrypto/encrypt#aes-cbc), [AES-CTR](/docs/Web/API/SubtleCrypto/encrypt#aes-ctr), [AES-GCM](/docs/Web/API/SubtleCrypto/encrypt#aes-gcm), or [AES-KW](/docs/Web/API/SubtleCrypto/wrapKey#aes-kw).
- CryptoKeyPair
- : Represents a public and private key pair used for an asymmetric cryptography algorithm.
- EcKeyGenParams
- : Represents the object that should be passed as the `algorithm` parameter into SubtleCrypto.generateKey(), when generating any elliptic-curve-based key pair: that is, when the algorithm is identified as either of [ECDSA](/docs/Web/API/SubtleCrypto/sign#ecdsa) or [ECDH](/docs/Web/API/SubtleCrypto/deriveKey#ecdh).
- EcKeyImportParams
- : Represents the object that should be passed as the `algorithm` parameter into SubtleCrypto.importKey() or SubtleCrypto.unwrapKey(), when generating any elliptic-curve-based key pair: that is, when the algorithm is identified as either of [ECDSA](/docs/Web/API/SubtleCrypto/sign#ecdsa) or [ECDH](/docs/Web/API/SubtleCrypto/deriveKey#ecdh).
- EcdhKeyDeriveParams
- : Represents the object that should be passed as the `algorithm` parameter into SubtleCrypto.deriveKey(), when using the [ECDH](/docs/Web/API/SubtleCrypto/deriveKey#ecdh) algorithm.
- EcdsaParams
- : Represents the object that should be passed as the `algorithm` parameter into SubtleCrypto.sign() or SubtleCrypto.verify() when using the [ECDSA](/docs/Web/API/SubtleCrypto/sign#ecdsa) algorithm.
- HkdfParams
- : Represents the object that should be passed as the `algorithm` parameter into SubtleCrypto.deriveKey(), when using the [HKDF](/docs/Web/API/SubtleCrypto/deriveKey#hkdf) algorithm.
- HmacImportParams
- : Represents the object that should be passed as the `algorithm` parameter into SubtleCrypto.importKey() or SubtleCrypto.unwrapKey(), when generating a key for the [HMAC](/docs/Web/API/SubtleCrypto/sign#hmac) algorithm.
- HmacKeyGenParams
- : Represents the object that should be passed as the `algorithm` parameter into SubtleCrypto.generateKey(), when generating a key for the [HMAC](/docs/Web/API/SubtleCrypto/sign#hmac) algorithm.
- Pbkdf2Params
- : Represents the object that should be passed as the `algorithm` parameter into SubtleCrypto.deriveKey(), when using the [PBKDF2](/docs/Web/API/SubtleCrypto/deriveKey#pbkdf2) algorithm.
- RsaHashedImportParams
- : Represents the object that should be passed as the `algorithm` parameter into SubtleCrypto.importKey() or SubtleCrypto.unwrapKey(), when importing any RSA-based key pair: that is, when the algorithm is identified as any of [RSASSA-PKCS1-v1_5](/docs/Web/API/SubtleCrypto/sign#rsassa-pkcs1-v1_5), [RSA-PSS](/docs/Web/API/SubtleCrypto/sign#rsa-pss), or [RSA-OAEP](/docs/Web/API/SubtleCrypto/encrypt#rsa-oaep).
- RsaHashedKeyGenParams
- : Represents the object that should be passed as the `algorithm` parameter into SubtleCrypto.generateKey(), when generating any RSA-based key pair: that is, when the algorithm is identified as any of [RSASSA-PKCS1-v1_5](/docs/Web/API/SubtleCrypto/sign#rsassa-pkcs1-v1_5), [RSA-PSS](/docs/Web/API/SubtleCrypto/sign#rsa-pss), or [RSA-OAEP](/docs/Web/API/SubtleCrypto/encrypt#rsa-oaep).
- RsaOaepParams
- : Represents the object that should be passed as the `algorithm` parameter into SubtleCrypto.encrypt(), SubtleCrypto.decrypt(), SubtleCrypto.wrapKey(), or SubtleCrypto.unwrapKey(), when using the [RSA_OAEP](/docs/Web/API/SubtleCrypto/encrypt#rsa-oaep) algorithm.
- RsaPssParams
- : Represents the object that should be passed as the `algorithm` parameter into SubtleCrypto.sign() or SubtleCrypto.verify(), when using the [RSA-PSS](/docs/Web/API/SubtleCrypto/sign#rsa-pss) algorithm.
### Extensions to other interfaces
- Window.crypto
- : Represents the Crypto object associated with the global object in the main thread scope.
- WorkerGlobalScope.crypto
- : Represents Crypto object associated with the global object in worker scope.
## Specifications
## Browser compatibility
Cite this
"Web Crypto API". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API