How to generate secrets in Python 3.6 and later

Erdal TAŞKESENErdal TAŞKESEN
1 min read

There are a lot of ways to generate random values. But some of them are not suitable for secret keys. There is a new module named secrets in version 3.6 and later. You can use the code below to generate cryptographically strong random values.

import secrets
secrets.token_hex()

This module will generate truly random values, not pseudo-random ones. Pseudo-random generators should be used only for simulation, not important random values or anything security related. You can use secrets for data such as passwords, security tokens, etc.

References

  1. https://docs.python.org/3/library/secrets.html
0
Subscribe to my newsletter

Read articles from Erdal TAŞKESEN directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Erdal TAŞKESEN
Erdal TAŞKESEN

Hey, y’all 🖖🏽 I am a passionate self-taught developer, open-source advocate, and tech enthusiast with a hands-on approach to problem-solving and an unending thirst for knowledge. Anything and everything classified as technology fascinate me.