← Java / Spring · Contents

JAVA-053 / Advanced

Using equals for password comparison (timing)

A02:2021 Cryptographic Failures · CWE-208 Observable Timing Discrepancy

Why is this a problem?

Response time varies with the mismatch position, allowing tokens to be guessed.

How to fix it

java
if (MessageDigest.isEqual(storedToken.getBytes(UTF_8), request.getToken().getBytes(UTF_8))) { ... }

Further reading