constant-time comparison

term

cryptographyprogramming

Kept in this browser · My desk

A comparison of two secret values, such as a received MAC tag and the expected one, whose running time does not depend on where the two first differ, so an attacker cannot learn the expected value by timing failed attempts.

In Python it is hmac.compare_digest(), also exported as secrets.compare_digest(). The library reference recommends it instead of == when checking a digest against one supplied from outside; both arguments must be bytes-like objects or ASCII-only strings, and the documentation notes that differing lengths could reveal the types and lengths, though not the values. Use it for HMAC tags, API tokens and stored password hashes alike.

Also known as: compare_digest, hmac.compare_digest, secrets.compare_digest, timing-safe comparison

← Back to C Back to the glossary