[CalendarServer-changes] [3261] CalendarServer/trunk/twistedcaldav/directory/util.py

source_changes at macosforge.org source_changes at macosforge.org
Wed Oct 29 11:57:04 PDT 2008


Revision: 3261
          http://trac.macosforge.org/projects/calendarserver/changeset/3261
Author:   wsanchez at apple.com
Date:     2008-10-29 11:57:02 -0700 (Wed, 29 Oct 2008)
Log Message:
-----------
Use uuid module

Modified Paths:
--------------
    CalendarServer/trunk/twistedcaldav/directory/util.py

Modified: CalendarServer/trunk/twistedcaldav/directory/util.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/directory/util.py	2008-10-29 18:28:15 UTC (rev 3260)
+++ CalendarServer/trunk/twistedcaldav/directory/util.py	2008-10-29 18:57:02 UTC (rev 3261)
@@ -23,7 +23,7 @@
     "NotFilePath",
 ]
 
-from hashlib import sha1
+from uuid import UUID, uuid5
 
 def uuidFromName(namespace, name):
     """
@@ -32,38 +32,12 @@
     @param namespace: a UUID denoting the namespace of the generated UUID.
     @param name: a byte string to generate the UUID from.
     """
-    # Logic distilled from http://zesty.ca/python/uuid.py
-    # by Ka-Ping Yee <ping at zesty.ca>
-    
-    # Convert from string representation to 16 bytes
-    namespace = long(namespace.replace("-", ""), 16)
-    bytes = ""
-    for shift in xrange(0, 128, 8):
-        bytes = chr((namespace >> shift) & 0xff) + bytes
-    namespace = bytes
-
     # We don't want Unicode here; convert to UTF-8
     if type(name) is unicode:
         name = name.encode("utf-8")
 
-    # Start with a SHA-1 hash of the namespace and name
-    uuid = sha1(namespace + name).digest()[:16]
+    return str(uuid5(UUID(namespace), name))
 
-    # Convert from hexadecimal to long integer
-    uuid = long("%02x"*16 % tuple(map(ord, uuid)), 16)
-
-    # Set the variant to RFC 4122.
-    uuid &= ~(0xc000 << 48L)
-    uuid |= 0x8000 << 48L
-    
-    # Set to version 5.
-    uuid &= ~(0xf000 << 64L)
-    uuid |= 5 << 76L
-
-    # Convert from long integer to string representation
-    uuid = "%032x" % (uuid,)
-    return "%s-%s-%s-%s-%s" % (uuid[:8], uuid[8:12], uuid[12:16], uuid[16:20], uuid[20:])
-
 import errno
 import time
 from twisted.python.filepath import FilePath
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20081029/ad5d1277/attachment-0001.html>


More information about the calendarserver-changes mailing list