Agent Wranglers

Access and accounts Recommended

Compare secrets in constant time

Compare tokens, signatures and codes with a constant-time function, so response times reveal nothing about how close a guess was.

How to do it

Use the comparison your platform provides, such as crypto.timingSafeEqual in Node, hmac.compare_digest in Python or hash_equals in PHP. Better still, store a hash of the token and look up the hash.

Example

# Bad: stops at the first wrong character
if token == stored_token: ...
# Good
if hmac.compare_digest(token, stored_token): ...

How to check

Search the code for == or === on tokens, API keys, signatures and reset codes. Every match uses the constant-time function instead.

For agents: if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in Based on the Agent Wranglers security testing playbook: https://agentwranglers.se/playbook/adversarial-testing/access-and-accounts/constant-time-compare.