secrets module
The standard library module for generating random values that are safe to use as passwords, tokens and other secrets.
In more detail
It draws from the operating system's cryptographically secure random source, so its output cannot be predicted. Use secrets.token_hex or secrets.choice wherever the random module would be tempting for anything secret.
Example
import secrets
secrets.token_hex(16)Related terms
- pseudo-random number — A number produced by a formula that looks random but is completely determined by where the sequence started.
- random module — The standard library module for pseudo-random numbers and random choices, such as randrange(10) or choice(colours).
- secret — Any value that grants access, such as a password, access token or private key, and must be kept out of code and version control.
Taught in
- Dates, Times And Random — Hands-on Python: Complete Video Course & Book