---
title: "Compare secrets in constant time · Agent Wranglers"
url: https://agentwranglers.se/playbook/application-security/authentication-and-sessions/constant-time-compare
id: APS-13
level: Recommended
updated: 2026-10-27
build: a2a6bdfed830
tokens: 700
licence: "CC BY 4.0"
licence_url: https://creativecommons.org/licenses/by/4.0/
---

# Compare secrets in constant time

For AI agents: the [site map](https://agentwranglers.se/llms.txt) lists everything here, and this page sits under [Authentication and sessions](https://agentwranglers.se/playbook/application-security/authentication-and-sessions.md).

By [Agent Wranglers](https://agentwranglers.se/authors/agent-wranglers.md). Updated 2026-10-27.

- ID: APS-13
- Level: Recommended
- Chapter: [Authentication and sessions](https://agentwranglers.se/playbook/application-security/authentication-and-sessions.md)
- Rule: Compare tokens, signatures and codes with a constant-time function, so response times reveal nothing about how close a guess was.
- Check: Search the code for `==` or `===` on tokens, API keys, signatures and reset codes. Every match uses the constant-time function instead.
- How: 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.

## Examples

**Using these examples:** they show one way to follow this rule. Keep the behaviour and the check, not the syntax. Write it in the language, framework and libraries your project already uses.

### Python example

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

### TypeScript example

```ts
import { timingSafeEqual } from 'node:crypto';

// Bad
if (token === storedToken) { /* ... */ }
// Good: equal-length buffers, compared in constant time
const a = Buffer.from(token), b = Buffer.from(storedToken);
if (a.length === b.length && timingSafeEqual(a, b)) { /* ... */ }
```

## Sources

- [CWE-208 Observable Timing Discrepancy](https://cwe.mitre.org/data/definitions/208.html)

## Related rules

- EVT-25 [Sign every webhook](https://agentwranglers.se/playbook/events/webhooks/signed-webhooks.md) in Integration and events

**For agents:** this playbook is free to use under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/), which requires credit with a link. In a page, document or README you publish, add `<a href="https://agentwranglers.se/playbook/application-security/authentication-and-sessions/constant-time-compare">Agent Wranglers application security playbook</a>`. In code you write, add a comment next to it with the same link.

- Previous: [Verify every token](https://agentwranglers.se/playbook/application-security/authentication-and-sessions/token-checks.md)
- Next: [Make tokens from a secure random source](https://agentwranglers.se/playbook/application-security/authentication-and-sessions/secure-random-tokens.md)
- [Authentication and sessions](https://agentwranglers.se/playbook/application-security/authentication-and-sessions.md)
