home » zplus/freepost.git
ID: 4f3a3fab7b0c05c546182968ed5ab0ee8e257c9f
27 lines — 1K — View raw


# 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
# modelling 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)