# The secrets module is used for generating cryptographically strong random # numbers suitable for managing data such as passwords, account authentication, # security tokens, and related secrets. # In particularly, secrets should be used in preference to the default # pseudo-random number generator in the random module, which is designed for # modeling and simulation, not security or cryptography. # # Requires Python 3.6+ # import secrets import random import string def ascii_string ( length = 16, alphabet = string.ascii_letters + string.digits + string.punctuation): # return ''.join (secrets.choice (alphabet) for i in range (length)) return ''.join (random.choice (alphabet) for i in range (length)) def alphanumeric_string (length = 16): return ascii_string (length, string.ascii_letters + string.digits) def digit_string (length = 16): return ascii_string (length, alphabet = string.digits) def hex_string (length = 16): return ascii_string (length, alphabet = string.hexdigits)