{
  "id": "python-secrets-table",
  "kind": "table",
  "slug": "python-secrets",
  "iil_id": null,
  "note_url": "https://iinventedlight.com/library/notes/python-secrets/",
  "title": "Python secrets generates secure tokens and passwords; use it instead of random for security",
  "columns": [
    "name",
    "source",
    "detail"
  ],
  "rows": [
    {
      "name": "token_bytes(nbytes)",
      "source": "Python docs",
      "detail": "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."
    },
    {
      "name": "choice(seq)",
      "source": "Python docs",
      "detail": "returns a randomly chosen element from a non-empty sequence; randbelow(n) returns a random int in the range [0, n)."
    },
    {
      "name": "compare_digest(a, b)",
      "source": "Python docs",
      "detail": "uses a \"constant-time compare\" to reduce the risk of timing attacks."
    }
  ],
  "related_slugs": [
    "python-uuid",
    "python-f-strings",
    "python-json"
  ],
  "license": "CC-BY-4.0",
  "source_license": "PSF-2.0 (Python docs); Public-Domain (Python PEPs); prose restates cited source lines in own words",
  "as_of": "2026-11-18",
  "status": "fill-draft-20261118",
  "note": "Restated from the note's GREEN sources; check those sources before relying on a value."
}