25 lines
549 B
Python
25 lines
549 B
Python
"""This module is a workaround module for python standard library secrets, which
|
|
is available since 3.6.
|
|
|
|
we copy the function source code from standard library.
|
|
"""
|
|
|
|
import base64
|
|
import binascii
|
|
import os
|
|
|
|
__all__ = ['token_bytes', 'token_hex', 'token_urlsafe']
|
|
|
|
|
|
def token_bytes(nbytes=32):
|
|
return os.urandom(nbytes)
|
|
|
|
|
|
def token_hex(nbytes=32):
|
|
return binascii.hexlify(token_bytes(nbytes)).decode('ascii')
|
|
|
|
|
|
def token_urlsafe(nbytes=32):
|
|
tok = token_bytes(nbytes)
|
|
return base64.urlsafe_b64encode(tok).rstrip(b'=').decode('ascii')
|