changeset 6497:aa6ac7ab93d1

model: greatly simplify generation of gist IDs Holy overcomplication, Batman!
author Søren Løvborg <sorenl@unity3d.com>
date Tue, 07 Feb 2017 21:53:56 +0100
parents 89eb2b2da3c5
children 137ea21dfc10
files kallithea/lib/utils2.py kallithea/model/gist.py
diffstat 2 files changed, 11 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/lib/utils2.py	Tue Feb 07 21:16:13 2017 +0100
+++ b/kallithea/lib/utils2.py	Tue Feb 07 21:53:56 2017 +0100
@@ -618,42 +618,6 @@
     os.environ['KALLITHEA_EXTRAS'] = os.environ['RC_SCM_DATA'] = json.dumps(extras)
 
 
-def unique_id(hexlen=32):
-    alphabet = "23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjklmnpqrstuvwxyz"
-    return suuid(truncate_to=hexlen, alphabet=alphabet)
-
-
-def suuid(url=None, truncate_to=22, alphabet=None):
-    """
-    Generate and return a short URL safe UUID.
-
-    If the url parameter is provided, set the namespace to the provided
-    URL and generate a UUID.
-
-    :param url to get the uuid for
-    :truncate_to: truncate the basic 22 UUID to shorter version
-
-    The IDs won't be universally unique any longer, but the probability of
-    a collision will still be very low.
-    """
-    # Define our alphabet.
-    _ALPHABET = alphabet or "23456789ABCDEFGHJKLMNPQRSTUVWXYZ"
-
-    # If no URL is given, generate a random UUID.
-    if url is None:
-        unique_id = uuid.uuid4().int
-    else:
-        unique_id = uuid.uuid3(uuid.NAMESPACE_URL, url).int
-
-    alphabet_length = len(_ALPHABET)
-    output = []
-    while unique_id > 0:
-        digit = unique_id % alphabet_length
-        output.append(_ALPHABET[digit])
-        unique_id = int(unique_id / alphabet_length)
-    return "".join(output)[:truncate_to]
-
-
 def get_current_authuser():
     """
     Gets kallithea user from threadlocal tmpl_context variable if it's
--- a/kallithea/model/gist.py	Tue Feb 07 21:16:13 2017 +0100
+++ b/kallithea/model/gist.py	Tue Feb 07 21:53:56 2017 +0100
@@ -26,12 +26,13 @@
 """
 
 import os
+import random
 import time
 import logging
 import traceback
 import shutil
 
-from kallithea.lib.utils2 import safe_unicode, unique_id, safe_int, \
+from kallithea.lib.utils2 import safe_unicode, safe_int, \
     time_to_datetime, AttributeDict
 from kallithea.lib.compat import json
 from kallithea.model.base import BaseModel
@@ -45,6 +46,14 @@
 GIST_METADATA_FILE = '.rc_gist_metadata'
 
 
+def make_gist_id():
+    """Generate a random, URL safe, almost certainly unique gist identifier."""
+    rnd = random.SystemRandom() # use cryptographically secure system PRNG
+    alphabet = '23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjklmnpqrstuvwxyz'
+    length = 20
+    return u''.join(rnd.choice(alphabet) for _ in xrange(length))
+
+
 class GistModel(BaseModel):
 
     def __delete_gist(self, gist):
@@ -100,7 +109,7 @@
         :param lifetime: in minutes, -1 == forever
         """
         owner = User.guess_instance(owner)
-        gist_id = safe_unicode(unique_id(20))
+        gist_id = make_gist_id()
         lifetime = safe_int(lifetime, -1)
         gist_expires = time.time() + (lifetime * 60) if lifetime != -1 else -1
         log.debug('set GIST expiration date to: %s',