What Is a Nonce?
A nonce is a “number used once” — a value that must appear only a single time in a given context to guarantee freshness. Its job is to make each message, request, or challenge unique so that a captured one cannot simply be replayed. Small and unglamorous, nonces are load-bearing across cryptography, web security, and authentication.
What problem does a nonce solve?#
Chiefly, replay attacks. If an attacker captures a valid request, what stops them resending it? A nonce does: because the server expects a fresh, never-before-seen value each time, the replayed request carries a stale nonce and is rejected. The same idea guarantees uniqueness in cryptographic operations where reusing an input would be catastrophic.
Where do you encounter nonces?#
| Context | Role of the nonce |
|---|---|
| Encryption (e.g. AES-GCM) | Ensures identical plaintexts encrypt differently |
| CSP | Marks which inline scripts are allowed to run |
| Authentication challenges | Prevents replay of a captured login response |
| Blockchain / proof-of-work | The value miners vary to find a valid hash |
In a Content Security Policy, a fresh random nonce per response is what lets your legitimate scripts run while an injected XSS payload — which cannot guess the nonce — is blocked.
Why is reuse so dangerous?#
Because “used once” is the security property. Reuse a nonce with the same key in AES-GCM and you can leak plaintext and even forge messages. Reuse or make a CSP nonce predictable and injected scripts execute. Nonces must therefore come from a cryptographically secure random source and never repeat.
Nonces depend on entropy and enable perfect forward secrecy and replay protection. More at the Security Fundamentals hub.
Frequently asked questions#
What is a nonce used for?
A nonce — "number used once" — is a value that must appear only a single time in a given context, to guarantee freshness. It prevents replay attacks by making each request or message unique, so a captured one cannot be resent. Nonces appear in cryptographic protocols, authentication challenges, and Content Security Policy.
Why is reusing a nonce dangerous?
Because the entire security guarantee is that the value is used once. In many encryption modes, reusing a nonce with the same key catastrophically leaks information about the plaintext or the key. In a CSP, a predictable or reused nonce lets injected scripts run. "Used once" is not a suggestion — it is the whole point.