Move the random hex generator to helpers.

This commit is contained in:
voussoir 2018-02-24 18:54:59 -08:00
parent 2fceeedbbb
commit 0228fbebfd
2 changed files with 7 additions and 4 deletions

View file

@ -217,6 +217,12 @@ def now(timestamp=True):
return n.timestamp() return n.timestamp()
return n return n
def random_hex(length=12):
randbytes = os.urandom(math.ceil(length / 2))
token = ''.join('{:02x}'.format(x) for x in randbytes)
token = token[:length]
return token
def read_filebytes(filepath, range_min, range_max, chunk_size=2 ** 20): def read_filebytes(filepath, range_min, range_max, chunk_size=2 ** 20):
''' '''
Yield chunks of bytes from the file between the endpoints. Yield chunks of bytes from the file between the endpoints.

View file

@ -10,10 +10,7 @@ SESSION_MAX_AGE = 86400
REQUEST_TYPES = (flask.Request, werkzeug.wrappers.Request, werkzeug.local.LocalProxy) REQUEST_TYPES = (flask.Request, werkzeug.wrappers.Request, werkzeug.local.LocalProxy)
def _generate_token(length=32): def _generate_token(length=32):
randbytes = os.urandom(math.ceil(length / 2)) return etiquette.helpers.random_hex(length=length)
token = ''.join('{:02x}'.format(x) for x in randbytes)
token = token[:length]
return token
def _normalize_token(token): def _normalize_token(token):
if isinstance(token, REQUEST_TYPES): if isinstance(token, REQUEST_TYPES):