Skip to main content

secrets

Cryptographically strong random source. Use instead of random for security-sensitive values.

Source-of-record: Lib/secrets.py, secrets docs.

Functions

FunctionReturns
token_bytes(nbytes=None)Random bytes.
token_hex(nbytes=None)Hex str.
token_urlsafe(nbytes=None)URL-safe base64 str.
choice(seq)Random element.
randbelow(n)Random int in [0, n).
randbits(k)Random int with k random bits.
compare_digest(a, b)Constant-time compare (re-exported).

SystemRandom re-exported from random.

DEFAULT_ENTROPY = 32 bytes drives the nbytes=None defaults.

Gopy status

AreaState
All functionsComplete via crypto/rand.
SystemRandomComplete.

Reference

  • CPython 3.14: secrets.
  • Lib/secrets.py.
  • module/secrets/. gopy port.
  • PEP 506.