---
source: https://iinventedlight.com/library/notes/python-secrets.md
title: "Python secrets generates secure tokens and passwords; use it instead of random for security"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs); Public-Domain (Python PEPs); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Python docs, secrets — Generate secure random numbers for managing secrets; Python PEPs, PEP 506 – Adding A Secrets Module To The Standard Library. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Public-Domain (https://peps.python.org/pep-0001/). PEP text is placed in the public domain (some also CC0-1.0)."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: secrets"
tags: [python, secrets, tokens, passwords, security, stdlib]
client: Librarian
sources:
  - id: S1
    title: "secrets — Generate secure random numbers for managing secrets"
    publisher: Python docs
    url: https://docs.python.org/3/library/secrets.html
    accessed: 2026-11-18
    license: "PSF-2.0"
  - id: S2
    title: "PEP 506 – Adding A Secrets Module To The Standard Library"
    publisher: Python PEPs
    url: https://peps.python.org/pep-0506/
    accessed: 2026-11-18
    license: "Public-Domain"
---
# Python secrets generates secure tokens and passwords; use it instead of random for security
**Answer:** The secrets module provides functions for generating secure tokens, suitable for password resets, hard-to-guess URLs, and similar. Prefer secrets over the `random` module's default pseudo-random number generator, which is designed for modelling and simulation, not security.

## Examples

```python
import secrets, string

secrets.token_urlsafe(16)    # 'Drmhze6EPcv0fN_81Bj-nA'
secrets.token_hex(16)        # 32 hex digits

alphabet = string.ascii_letters + string.digits
password = ''.join(secrets.choice(alphabet) for i in range(8))

secrets.compare_digest(a, b) # constant-time compare
```

## Functions

- `token_bytes(nbytes)` returns a random byte string; `token_hex` converts each byte to two hex digits; `token_urlsafe` is Base64 encoded, so each byte results in approximately 1.3 characters on average.
- `choice(seq)` returns a randomly chosen element from a non-empty sequence; `randbelow(n)` returns a random int in the range [0, n).
- `compare_digest(a, b)` uses a "constant-time compare" to reduce the risk of timing attacks.

## How long should tokens be

As of 2015, 32 bytes (256 bits) of randomness is believed sufficient for the typical use-case.

## See also

- [python-uuid](https://iinventedlight.com/library/notes/python-uuid/)
- [python-f-strings](https://iinventedlight.com/library/notes/python-f-strings/)
- [python-json](https://iinventedlight.com/library/notes/python-json/)

## Claims

- id: C1
  claim: The random module is designed for modelling and simulation, not security.
  source: S1
  evidence: "which is designed for modelling and simulation, not security or cryptography"

- id: C2
  claim: 32 bytes of randomness is believed sufficient for typical use.
  source: S1
  evidence: "it is believed that 32 bytes (256 bits) of randomness is sufficient"

- id: C3
  claim: compare_digest reduces the risk of timing attacks.
  source: S1
  evidence: "to reduce the risk of timing attacks"

- id: C4
  claim: PEP 506 proposes a module for security-related functions.
  source: S2
  evidence: "This PEP proposes the addition of a module for common security-related functions"

## Sources

- [S1] secrets — Generate secure random numbers for managing secrets. Python docs. https://docs.python.org/3/library/secrets.html . License: PSF-2.0. Accessed 2026-11-18.

- [S2] PEP 506 – Adding A Secrets Module To The Standard Library. Python PEPs. https://peps.python.org/pep-0506/ . License: Public-Domain. Accessed 2026-11-18.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, secrets — Generate secure random numbers for managing secrets; Python PEPs, PEP 506 – Adding A Secrets Module To The Standard Library. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Public-Domain (https://peps.python.org/pep-0001/). PEP text is placed in the public domain (some also CC0-1.0). Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
