The SubtleCrypto interface of the Web Crypto API provides a number of low-level cryptographic functions. Access to the features of SubtleCrypto is obtained through the subtle property of the Crypto object you get from the crypto property.
Warning: This 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 can be a great place to start learning about the design and implementation of secure systems.
This interface doesn't inherit any properties, as it has no parent interface.
This interface doesn't inherit any methods, as it has no parent interface.
Returns a Promise that fulfills with a newly-generated CryptoKey, for symmetrical algorithms, or a CryptoKeyPair, containing two newly generated keys, for asymmetrical algorithms. These will match the algorithm, usages, and extractability given as parameters.
Returns a Promise that fulfills with a wrapped symmetric key for usage (transfer and storage) in insecure environments. The wrapped key matches the format specified in the given parameters, and wrapping is done by the given wrapping key, using the specified algorithm.
digest(): create a fixed-length, collision-resistant digest of some data.
Key management functions
Except for digest(), all the cryptography functions in the API use cryptographic keys. In the SubtleCrypto API a cryptographic key is represented using a CryptoKey object. To perform operations like signing and encrypting, you pass a CryptoKey object into the sign() or encrypt() function.
The difference is that generateKey() will generate a new distinct key value each time you call it, while deriveKey() derives a key from some initial keying material. If you provide the same keying material to two separate calls to deriveKey(), you will get two CryptoKey objects that have the same underlying value. This is useful if, for example, you want to derive an encryption key from a password and later derive the same key from the same password to decrypt the data.
Importing and exporting keys
To make keys available outside your app, you need to export the key, and that's what exportKey() is for. You can choose one of a number of export formats.
The inverse of exportKey() is importKey(). You can import keys from other systems, and support for standard formats like PKCS #8 and JSON Web Key helps you do this. The exportKey() function exports the key in an unencrypted format.
If the key is sensitive you should use wrapKey(), which exports the key and then encrypts it using another key; the API calls a "key-wrapping key".
The inverse of wrapKey() is unwrapKey(), which decrypts then imports the key.
CryptoKey objects can be stored using the structured clone algorithm, meaning that you can store and retrieve them using standard web storage APIs. The specification expects that most developers will use the IndexedDB API to store CryptoKey objects.
The cryptographic functions provided by the Web Crypto API can be performed by one or more different cryptographic algorithms: the algorithm argument to the function indicates which algorithm to use. Some algorithms need extra parameters: in these cases the algorithm argument is a dictionary object that includes the extra parameters.
The table below summarizes which algorithms are suitable for which cryptographic operations: