Overview
The Crypto API provides:- Cryptographically secure random number generation
- Hash functions (SHA-256, SHA-384, SHA-512, etc.)
- Symmetric encryption (AES-GCM, AES-CBC, AES-CTR)
- Asymmetric encryption (RSA-OAEP)
- Digital signatures (RSA-PSS, ECDSA, EdDSA)
- Key derivation (PBKDF2, HKDF)
- Key generation and management
src/workerd/api/crypto/ directory
Random values
Generate cryptographically secure random values:Hashing
Compute message digests:Supported hash algorithms
src/workerd/api/crypto/digest.h and digest.c++
Symmetric encryption
AES-GCM
Encrypt and decrypt with authenticated encryption:AES-CBC
Block cipher mode with padding:AES-CTR
Counter mode for stream encryption:src/workerd/api/crypto/aes.c++
Asymmetric encryption
RSA-OAEP
Public key encryption:src/workerd/api/crypto/rsa.h and rsa.c++
Digital signatures
ECDSA
Elliptic curve signatures:EdDSA
Edwards curve signatures:RSA-PSS
RSA probabilistic signature scheme:src/workerd/api/crypto/ec.h and ec.c++
Key derivation
PBKDF2
Password-based key derivation:HKDF
HMAC-based key derivation:src/workerd/api/crypto/pbkdf2.c++ and hkdf.c++
Key management
Generate keys
Generate cryptographic keys:Export keys
Export keys in various formats:Import keys
Import keys from various formats:src/workerd/api/crypto/keys.h and keys.c++, jwk.h and jwk.c++
DigestStream
Compute hashes from streams:Best practices
Use secure algorithms
Use secure algorithms
Prefer modern algorithms:
Use random IVs and salts
Use random IVs and salts
Always generate new random values:
Store keys securely
Store keys securely
Never hardcode keys in source code:
Use sufficient key lengths
Use sufficient key lengths
Use recommended key sizes:
Implementation details
The Crypto API is implemented across multiple files insrc/workerd/api/crypto/:
crypto.h/.c++- Public API (SubtleCrypto, CryptoKey)impl.h/.c++- Internal base classes and algorithm dispatchkeys.h/.c++- Key management and encodingaes.c++- AES algorithms (CBC, CTR, GCM, KW)rsa.h/.c++- RSA algorithms (OAEP, PSS, PKCS1)ec.h/.c++- Elliptic curve algorithms (ECDSA, ECDH, EdDSA)digest.h/.c++- Hash functionskdf.h,pbkdf2.c++,hkdf.c++- Key derivationjwk.h/.c++- JWK import/export
OSSLCALL() macro for error handling.
Key usage tracking from src/workerd/api/crypto/crypto.h:28:
Related APIs
- Encoding APIs - TextEncoder/TextDecoder for crypto operations
- Streams API - DigestStream for streaming hashes