import random import hashlib import time def get_random_string(length=12, allowed_chars='abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'): """ See also: https://github.com/rs/xid (globally unique id generator) https://stackoverflow.com/questions/41354205/how-to-generate-a-unique-auth-token-in-python """ random.seed( hashlib.sha256( ("%s%s%s" % ( random.getstate(), time.time(), 'O_O-SECRET_KEY')).encode('utf-8') ).digest()) return ''.join(random.choice(allowed_chars) for i in range(length))