26 lines
798 B
Python
26 lines
798 B
Python
import random
|
|
import hashlib
|
|
import time
|
|
|
|
|
|
try:
|
|
random = random.SystemRandom()
|
|
using_sysrandom = True
|
|
except NotImplementedError:
|
|
import warnings
|
|
warnings.warn('''A secure pseudo-random number generator is not available on your system. Falling back to Mersenne '
|
|
Twister.''')
|
|
using_sysrandom = False
|
|
|
|
|
|
async def get_random_string(length=12, allowed_chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'):
|
|
if not using_sysrandom:
|
|
random.seed(
|
|
hashlib.sha256(
|
|
("%s%s%s" % (
|
|
random.getstate(),
|
|
time.time(),
|
|
'0Fxo7$hYArCI1f78w12U6F6ja63nPlnU'))
|
|
).digest())
|
|
return ''.join([random.choice(allowed_chars) for i in range(length)])
|