From 8eda09bb20936d2ae2406a4908f6c3cc8d2108af Mon Sep 17 00:00:00 2001 From: lars Date: Mon, 26 Apr 2010 01:13:15 +0000 Subject: [PATCH] use hashes of approximately 80 bit and use all digits and characters (lower case) for the hash --- wortschlucker/src/wortschlucker.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/wortschlucker/src/wortschlucker.py b/wortschlucker/src/wortschlucker.py index c2f0236..96591f1 100755 --- a/wortschlucker/src/wortschlucker.py +++ b/wortschlucker/src/wortschlucker.py @@ -178,10 +178,22 @@ def get_poll_admin_id(hash_key): else: return None -def get_new_hash_key(): - hash_key = uuid.uuid4().get_hex() +def get_new_hash_key(length=16, charset=None): + """ returns a quite random hash key with the specified length """ + if charset is None: + charset = "0123456789abcdefghijklmnopqrstuvwxyz" + def get_hash_string(length): + base = uuid.uuid4().int + result = [] + while len(result) < length: + value = base % len(charset) + base //= len(charset) + result.append(charset[value]) + return "".join(result) + # repeat the hash generation until a new value is found + hash_key = get_hash_string(length) while (not get_poll_id(hash_key) is None) or (not get_poll_admin_id(hash_key) is None): - hash_key = uuid.uuid4().get_hex() + hash_key = get_hash_string(length) return hash_key @bobo.query('/polls/new')