Nonces

Difficulty: Intermediate

Question

What is a nonce, where is it used, and how is it different from a salt and an IV?

Answer

A nonce is a 'number used once': a value that must not repeat within a given context. Its job is to make each message, request or operation unique so that an attacker cannot replay an old one or exploit a repeated value. Depending on the protocol a nonce may be random, sequential or a timestamp, but the one non-negotiable rule is uniqueness for the key or session it is tied to.

What nonces protect against

The main threat is the replay attack: an attacker captures a valid, authenticated message and sends it again later to repeat its effect, for example re-submitting a 'transfer 5000 rupees' request. If every request carries a fresh nonce that the server records and rejects on reuse, the replayed copy is dropped because its nonce has already been seen.

Where you meet nonces

  • Content Security Policy (CSP): the server puts a random nonce in the CSP header and the same value on each trusted <script> tag. The browser runs only scripts whose nonce matches, which blocks injected inline scripts. A new nonce is generated per response, so it is unguessable.
  • Authentication protocols: OAuth and OpenID Connect use a nonce to bind an ID token to a specific login request and stop token replay. HTTP Digest auth uses a server nonce.
  • Cryptography (AEAD): ciphers like AES-GCM and ChaCha20-Poly1305 take a nonce per message. Reusing a nonce with the same key is catastrophic for GCM; it can leak the authentication key and reveal plaintext relationships.
  • Blockchain: an Ethereum account nonce is a per-account counter that orders transactions and prevents replaying the same signed transaction.
  • Anti-CSRF and payment flows: a one-time token tied to a form or order prevents double submission and replay.

Nonce vs salt vs IV

These three are often confused because all add variation, but they serve different goals.

ValuePurposeMust be unique?Secret?
NoncePrevent replay and repetition per operationYes, per key/sessionNot necessarily
SaltMake identical passwords hash differentlyYes, per passwordNo, stored with the hash
IVRandomise the start of a cipher so equal plaintexts differYes, and for CBC unpredictableNo, sent with the ciphertext

A salt defends against precomputation and rainbow tables on password hashes. An IV ensures encrypting the same plaintext twice gives different ciphertext. A nonce ensures a message or request is fresh. None of them needs to be secret; what matters is that they do not repeat.

Common mistakes

  • Reusing a nonce with the same key in AES-GCM, which breaks its security entirely.
  • Making a CSP nonce static or predictable instead of random per response, which defeats the point.
  • Treating a nonce as a secret; it usually travels in the open, uniqueness is what counts.
  • Not storing and checking used nonces server-side, so replays are never actually detected.
A nonce defeating a replay attack. The server remembers used nonces and rejects any repeat.
  1. Client → Server: Request with nonce N1
  2. Server → Client: Accepts, records N1 as used
  3. Attacker → Server: Replays the captured request with N1
  4. Server → Attacker: Rejected: N1 already used

Code examples

Per-response CSP nonce (Express)

import crypto from 'node:crypto';

app.use((req, res, next) => {
  // fresh, unguessable value for each response
  res.locals.nonce = crypto.randomBytes(16).toString('base64');
  res.setHeader(
    'Content-Security-Policy',
    `script-src 'self' 'nonce-${res.locals.nonce}'`
  );
  next();
});

// in the template, only tagged scripts run
// <script nonce="<%= nonce %>">...</script>

A new random nonce per response means an attacker who injects an inline script cannot guess the valid nonce, so the browser refuses to execute the injected script.

Key points

  • A nonce is a value used once per context to guarantee freshness and prevent replay.
  • CSP nonces must be random per response; AES-GCM nonces must never repeat per key.
  • Salt stops password precomputation; IV randomises ciphertext; nonce stops replay.
  • None of nonce, salt or IV needs to be secret; uniqueness is the key property.
  • The server must record used nonces to actually reject replays.

Common follow-up questions

  • Why is reusing a nonce in AES-GCM catastrophic?
  • How does a CSP nonce help prevent XSS?
  • What is the difference between a nonce and an IV in AES-CBC?
  • How does the Ethereum account nonce prevent transaction replay?

Interview tips

  • Clearly distinguish nonce, salt and IV; interviewers often probe whether you conflate them.
  • Give two concrete uses such as CSP and AES-GCM to show breadth.
  • Stress uniqueness over secrecy; that is the common misconception.

Concepts covered

Nonce, Replay Protection, CSP, Initialization Vector, Cryptography

More Web Security (OWASP, JWT, XSS, CSRF) questions