00:00

#0703

Timing Attack on Password Verification

Medium+100 XPA07:2021 Identification and Authentication FailuresCWE-916
Timing AttackConstant-Time ComparisonPassword Verification

Scenario

A login service compares a supplied password hash against the stored hash using the === operator (or equivalent). This short-circuits on the first mismatched character.

An attacker can measure how long each comparison takes. A guess that matches the first character takes slightly longer than one that doesn't — leaking one character at a time until the full hash is reconstructed.

The fix is to compare every character in both strings regardless of early mismatches, ensuring a constant execution time.

Timing side-channels allow remote attackers to reconstruct secrets one bit at a time. Constant-time comparison is mandatory for any security-sensitive equality check — tokens, HMACs, and password hashes.

Your Tasks

  1. Fix verifyPassword to use a constant-time comparison that does not short-circuit on a mismatch.
  2. Return true when both hashes match, false otherwise.
  3. The function must never throw — return false for all non-matching inputs.

Examples

Example 1Match — returns true

verifyPassword('hash:abc', 'hash:abc')
// → true

Example 2Mismatch — returns false

verifyPassword('hash:xyz', 'hash:abc')
// → false

Constraints

  • Do not use === or == for the final comparison.
  • Iterate over every character and accumulate a difference flag — only return the result after the full loop.
  • Return a boolean; do not throw.

Hint

References

solution.js
Ln 1, Col 1UTF-8JavaScript
Sandbox ready
0/0/0not run