The Web Crypto API, formally the Web Cryptography API, is the browser interface that gives JavaScript access to native cryptographic primitives: secure random numbers, hashing, symmetric and asymmetric encryption, digital signatures, key derivation and key management. Among Web APIs, it is the standard way to perform cryptography in browsers, workers and server runtimes such as Node.js and Deno.
Published as a W3C Recommendation in 2017, the API is exposed through the global crypto object, whose subtle property holds the SubtleCrypto interface. Operations run in audited native code rather than in JavaScript libraries, and keys can be marked so that script can never read the raw key material.
How the Web Crypto API is structured
The Web Crypto API has two layers: a small set of synchronous helpers on crypto and the asynchronous SubtleCrypto interface on crypto.subtle. The helpers getRandomValues() and randomUUID() produce cryptographically secure randomness, while SubtleCrypto methods return Promises and operate on CryptoKey objects.
A CryptoKey carries its algorithm, its permitted key usages such as encrypt or sign, and an extractable flag. When extractable is false, the browser refuses to export the key, so even code with full page access can use the key but cannot copy its bytes. SubtleCrypto is available only in secure contexts, meaning HTTPS pages or localhost.
The following table lists the main SubtleCrypto operations and what each one produces.
| Method | Input | Output |
|---|---|---|
| digest | Algorithm and data | Hash as an ArrayBuffer |
| encrypt and decrypt | Key, algorithm parameters, data | Ciphertext or plaintext |
| sign and verify | Key, data, signature | Signature or Boolean result |
| generateKey | Algorithm and usages | CryptoKey or key pair |
| deriveKey and deriveBits | Base key and parameters | Derived key or raw bits |
| importKey and exportKey | Key data and format | CryptoKey or exported key |
| wrapKey and unwrapKey | Key plus wrapping key | Encrypted key or CryptoKey |
Supported algorithms and key formats
The specification defines a fixed set of algorithms rather than an open plugin model, which keeps implementations consistent across browsers. Modern code should prefer AES-GCM, ECDSA, ECDH, HKDF and SHA-256 or stronger; older algorithms remain for interoperability.
| Purpose | Algorithms | Recommendation |
|---|---|---|
| Symmetric encryption | AES-GCM, AES-CBC, AES-CTR | AES-GCM for authenticated encryption |
| Key wrapping | AES-KW, RSA-OAEP | AES-KW for wrapping symmetric keys |
| Signatures | ECDSA, RSA-PSS, RSASSA-PKCS1-v1_5, Ed25519 | ECDSA P-256 or Ed25519 where supported |
| Key agreement | ECDH, X25519 | ECDH followed by HKDF |
| Hashing | SHA-1, SHA-256, SHA-384, SHA-512 | Avoid SHA-1 for security uses |
| Key derivation | HKDF, PBKDF2 | PBKDF2 only for passwords, high iterations |
| Message authentication | HMAC | HMAC with SHA-256 |
Keys move in and out of the API in four formats: raw bytes, PKCS #8, SPKI and JSON Web Key. JWK is convenient for exchange with web services, while SPKI and PKCS #8 match the formats used by OpenSSL and most server libraries. The Curve25519 algorithms Ed25519 and X25519 were added later, so their availability should be checked per browser version.
Web Crypto API compared with JavaScript libraries
The Web Crypto API replaces most pure JavaScript cryptography libraries for standard primitives. Native code is faster, resists timing attacks better and supports non-extractable keys, which no library can emulate because a library always holds keys in ordinary memory.
| Aspect | Web Crypto API | Pure JavaScript library |
|---|---|---|
| Performance | Native, often hardware accelerated | Slower, interpreted or JIT code |
| Key protection | Non-extractable CryptoKey objects | Keys always readable by script |
| Algorithm range | Fixed standard set | Any algorithm the library adds |
| Bundle size | Zero, built into the platform | Adds code to every page |
| Streaming | Whole buffers only | Incremental hashing often available |
Libraries remain useful for algorithms outside the standard, such as Argon2 password hashing or post-quantum schemes, and for incremental hashing of very large files. Good practice is to use native primitives wherever they exist and add a library only for the gap.
Practical use and secure configuration
Typical uses include end-to-end encrypted messaging, client-side encryption of files before upload, signing requests with device-bound keys and verifying data integrity. Non-extractable keys can be stored in IndexedDB as CryptoKey objects, which survive page reloads without ever exposing raw bytes.
- Generate a fresh 96-bit IV for every AES-GCM encryption with
getRandomValues()and never reuse it with the same key. - Derive keys from passwords with PBKDF2 and several hundred thousand iterations, plus a random salt per user.
- Set extractable to false unless a key must be exported or wrapped.
- Limit key usages to the minimum, for example sign only for a signing key.
Limitations and common mistakes
The Web Crypto API secures operations, not the code that calls them. If an attacker can inject script through XSS or alter the JavaScript served to the page, they can use non-extractable keys or read plaintext before encryption, so Content Security Policy and code integrity matter as much as the algorithm.
| Mistake | Consequence | Fix |
|---|---|---|
| Reusing an AES-GCM IV | Loss of confidentiality and integrity | Random IV per message |
| Using Math.random() for keys | Predictable secrets | Use crypto.getRandomValues() |
| Calling crypto.subtle over HTTP | API undefined in insecure context | Serve the page over HTTPS |
| Hashing passwords with SHA-256 alone | Fast offline brute force | Use PBKDF2 or a dedicated hash |
The API also offers no key storage, no certificate handling and no streaming encryption, so applications must design those parts themselves with care.
The short version
The Web Crypto API is a built-in toolbox for locking and checking data inside the browser. A website can use it to scramble a message for one reader. It can prove that a file is unchanged. It can also create truly unpredictable random numbers.
The tools are part of the browser itself, so they are fast and carefully tested. Think of a bank safe deposit box. The website can ask the box to lock or unlock something. Yet the key itself never leaves the box.
Where it makes a difference
A health clinic offers patients a web portal for uploading scanned documents. To limit exposure if the storage service is ever breached, the developers encrypt every file in the browser before upload with AES-GCM using a fresh random IV and a 256-bit key.
Each document key is wrapped with the patient’s key, derived with PBKDF2 from a passphrase using a per-user salt and 600,000 iterations, and stored only in wrapped form. The server therefore stores ciphertext it cannot read. When a storage bucket is later misconfigured and briefly exposed, the leaked files are unreadable, and the incident response focuses on configuration rather than patient notification of readable records.