Prevent timing attacks on cryptographic operations
Timing attacks exploit execution time variations to leak secrets. Learn constant-time comparison, uniform processing, and blinding techniques.
- 1
A timing attack measures how long an operation takes to infer information about the secret. For example, a string comparison that returns early on the first mismatch leaks how many characters match. Defense: all operations on secret data must take time independent of the data value.
- 2 Hash Compare →
Compare hashes in constant time. This tool performs a byte-by-byte comparison that always processes all bytes, regardless of where the first mismatch occurs. Never use === to compare hashes or HMACs — it short-circuits and leaks timing information.
- 3 HMAC Generator →
Generate an HMAC for authentication. When verifying, use a constant-time comparison function (crypto.timingSafeEqual in Node.js). The comparison must touch every byte regardless of match position.
- 4
For RSA, use blinding: multiply the message by a random value r^e before signing, then divide the result by r. This randomizes the computation time and prevents timing-based key recovery. Modern crypto libraries (OpenSSL, Web Crypto) do this internally — always use library functions, never implement crypto from scratch.
Frequently asked questions
- How does a timing attack work?
- The attacker sends many requests and measures response time. If the first byte of the secret matches, the comparison takes slightly longer. By trying all values for each byte and observing timing, the attacker recovers the secret one byte at a time. This has been demonstrated over network (within ~15ms precision).
- Is === safe for comparing non-secret data?
- Yes. Timing attacks only matter when the compared value is secret (passwords, hashes, tokens). For non-sensitive comparisons (e.g., checking if a user role is "admin"), === is fine. The rule: use constant-time comparison only for secrets.
Workflows that solve this
Learn more
Want to understand why?