From a7b6cb5c23deea976783e3d16655992d61dc2144 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Thu, 26 Mar 2015 17:45:43 -0400 Subject: [PATCH] Fix handling of byte strings and large ints --- data/users.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/data/users.py b/data/users.py index 10c909cb8..3d763c9b6 100644 --- a/data/users.py +++ b/data/users.py @@ -148,19 +148,24 @@ class UserAuthentication(object): """ Returns the secret key to use for encrypting and decrypting. """ from app import app app_secret_key = app.config['SECRET_KEY'] + secret_key = None # First try parsing the key as an int. try: big_int = int(app_secret_key) - secret_key = bytearray.fromhex('{:02x}'.format(big_int)) + secret_key = str(bytearray.fromhex('{:02x}'.format(big_int))) except ValueError: - secret_key = app_secret_key + pass # Next try parsing it as an UUID. - try: - secret_key = uuid.UUID(app_secret_key).bytes - except ValueError: - secret_key = app_secret_key + if secret_key is None: + try: + secret_key = uuid.UUID(app_secret_key).bytes + except ValueError: + pass + + if secret_key is None: + secret_key = str(bytearray(map(ord, app_secret_key))) # Otherwise, use the bytes directly. return ''.join(itertools.islice(itertools.cycle(secret_key), 32))