Cryptography in the Browser: A Deep Dive into the Web Crypto API
Historically, implementing cryptography inside web browsers was considered a dangerous anti-pattern. Early JavaScript libraries (like Crypto-JS) had to emulate binary arithmetic, were vulnerable to side-channel timing attacks, and suffered from notoriously slow execution speeds.
With the introduction and universal adoption of the W3C Web Cryptography API (accessible globally via window.crypto.subtle), browsers now provide native, hardware-accelerated cryptographic primitives written in C++ and assembly.
1. Why `Math.random()` Is Not Cryptographically Secure
One of the most dangerous vulnerabilities in frontend applications is using Math.random() to generate session tokens, password salts, or temporary authorization codes.
Math.random() uses pseudo-random algorithms (such as xoshiro128+ in V8). These algorithms are deterministic: an adversary who observes a sequence of generated values can reconstruct the internal PRNG state and accurately predict all future values.
In contrast, Cryptographically Secure Pseudo-Random Number Generators (CSPRNG) poll system entropy pools (such as Linux /dev/urandom or macOS arc4random):
2. Computing Cryptographic Digests with `crypto.subtle.digest`
Computing a one-way hash (such as SHA-256 or SHA-512) is fundamental for verifying file integrity, hashing passwords with salts, or building deduplication caches.
Here is how to compute a SHA-256 hash asynchronously without any third-party dependencies:
3. Message Authentication with HMAC (Hash-based MAC)
A cryptographic hash confirms that data has not changed, but it does not prove who authored the data. HMAC combines a secret key with a cryptographic hash function to provide both integrity and authenticity (commonly used in AWS v4 request signing and webhook signature verification).
4. Zero-Knowledge Utility Architecture
Because the Web Cryptography API executes entirely in your local browser sandbox, client-side tools can compute hashes, passwords, and HMAC signatures without any risk of database theft, network man-in-the-middle attacks, or log interception.