Nonce uniqueness and random generation
A nonce is not a secret key
A nonce is an input used once in the relevant key context. For AES-GCM and ChaCha20-Poly1305, repeating a nonce with the same key can undermine confidentiality and integrity. The nonce generally travels with ciphertext; secrecy is not its purpose. A key, by contrast, must be generated and protected according to its algorithm and usage policy.
Randomness and uniqueness differ
Cryptographically secure randomness comes from an appropriate system generator, not the simulation-oriented random module or a timestamp. Random selection can still collide; a protocol must specify nonce allocation and per-key usage limits. A persistent counter profile requires safe allocation across workers and restarts. Follow an established profile rather than assuming any random byte string guarantees uniqueness.
Generate bytes locally
This illustrates the return type and length of a cryptographically secure generator, not a complete nonce allocator. It prints 12 and does not print the random value.
from secrets import token_bytesvalue = token_bytes(12)print(len(value))Use library key generators for the selected algorithm. Public deterministic test fixtures are separate from production key generation.
Think about the lifecycle
Consider two workers that both resume from the same backed-up counter under key K1. A previously allocated value could be repeated. Stop new writes when uniqueness cannot be assured, investigate affected records, and follow the approved incident and key-rotation process. Rotating a key does not retroactively restore confidentiality. Keep key identity and allocation state consistent; never simply reset a counter under the same key.
Review synthetic allocations
Inspect the lab allocation records. A repeated pair of actual key identity and nonce is a failure; the same nonce under a different independently generated key is not the same pair. A repeated key label might hide aliases, so production inventories must track actual key identity. Identify restart risks and propose a reviewed allocation strategy without implementing a custom allocator.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Nonce uniqueness and random generation — knowledge check
Apply cryptographic library principles to fictional scenarios.
Which pair must not repeat for GCM?
Which Python generator is intended for secret randomness?
What can happen with random nonces?
A worker restores an old counter with the same key. What should happen?
What does key rotation not do?