[Python Tips] Secrets Module - New in 3.6

Another new Python 3.6 feature is the addition of the secrets module, a replacement for random when generating cryptographically strong random numbers.

When creating random numbers for with passwords or encryption, the secret module will offer the most secure source of randomness that your operating system can provide.

The secrets module is broken into two sections, random numbers and generating tokens.

Random Numbers

  • class secrets.SystemRandom
  • secrets.choice(sequence)
  • secrets.randbelow(n)
  • secrets.randbits(k)

Generating Tokens

  • secrets.token_bytes([nbytes=None])
  • token_hex([nbytes=None])
  • token_urlsafe([nbytes=None])

I'm not going to go into the use of these as this is more to bring awareness of the availability of the module than a complete tutorial on cryptography.

I will talk about the two most common parts of the module that will be most interesting to most.

class secrets.SystemRandom

The class SystemRandom offers the most secure known way of generating random numbers for the platform you are using.

Using secrets.SystemRandom is similar to using the random module.

import secrets
rnd = secrets.SystemRandom()
rnd.random()

You want to make sure you call SystemRandom class from secrets module as the random module also has a SystemRandom.

secrets.choice(sequence)

The last part of the module I will go over is the choice method. This method securely returns a random element from a sequence. This is useful when automatically generating random alphanumeric passwords or any random sequence based on an existing sequence.

import secrets
import string
alphanumeric = string.ascii_letters + string.digits
secrets.choice(alphanumeric)

This will return a random letter/numeric from the sequence alphanumeric which consists of all US alphabet characters (upper and lower) and the numbers 0-9.

Keep in mind these class and functions exist in the random module so make sure you prefix all calls with secrets to assure you are using the correct module.

You can read more about the secrets module here

My Python Tips Series

X48EJ

Why you should vote me as witness

Witness & Administrator of four full nodes

themarkymark.png

My recent popular posts

STEEM, STEEM Power, Vests, and Steem Dollars. wtf is this shit?
The truth and lies about 25% curation, why what you know is FAKE NEWS
WTF is a hardware wallet, and why should you have one?
GINABOT - The Secret to your Sanity on Steemit
How to calculate post rewards
Use SSH all the time? Time for a big boy SSH Client
How to change your recovery account
How curation rewards work and how to be a kick ass curator
Markdown 101 - How to make kick ass posts on Steemit
Work ON your business, not in your business! - How to succeed as a small business
You are not entitled to an audience, you need to earn it!
How to properly setup SSH Key Authentication - If you are logging into your server with root, you are doing it wrong!
Building a Portable Game Console

H2
H3
H4
3 columns
2 columns
1 column
13 Comments
Ecency