From 6174e720360b85ad96658c5c4067810db5fc5093 Mon Sep 17 00:00:00 2001 From: Ole Laursen Date: Fri, 15 Nov 2013 16:07:10 +0000 Subject: [PATCH] Rename unique key to random key as it is not really unique, add function for generating an access token from the key - Legacy-Id: 6716 --- ietf/utils/accesstoken.py | 15 +++++++++++++++ ietf/utils/uniquekey.py | 7 ------- 2 files changed, 15 insertions(+), 7 deletions(-) create mode 100644 ietf/utils/accesstoken.py delete mode 100644 ietf/utils/uniquekey.py diff --git a/ietf/utils/accesstoken.py b/ietf/utils/accesstoken.py new file mode 100644 index 000000000..da07c9a3a --- /dev/null +++ b/ietf/utils/accesstoken.py @@ -0,0 +1,15 @@ +import time, random, hashlib + +from django.conf import settings + +def generate_random_key(max_length=32): + """Generate a random access token.""" + return hashlib.sha256(settings.SECRET_KEY + ("%.16f" % time.time()) + ("%.16f" % random.random())).hexdigest()[:max_length] + +def generate_access_token(key, max_length=32): + """Make an access token out of key.""" + assert key, "key must not be empty" + # we hash it with the private key to make sure only we can + # generate and use the final token - so storing the key in the + # database is safe + return hashlib.sha256(settings.SECRET_KEY + key).hexdigest()[:max_length] diff --git a/ietf/utils/uniquekey.py b/ietf/utils/uniquekey.py deleted file mode 100644 index 9adcf0db6..000000000 --- a/ietf/utils/uniquekey.py +++ /dev/null @@ -1,7 +0,0 @@ -import time, random, hashlib - -from django.conf import settings - -def generate_unique_key(max_length=32): - return hashlib.sha256(settings.SECRET_KEY + ("%.16f" % time.time()) + ("%.16f" % random.random())).hexdigest()[:max_length] -