[CalendarServer-changes] [760] CalendarServer/trunk/twistedcaldav/directory

source_changes at macosforge.org source_changes at macosforge.org
Fri Dec 8 00:01:20 PST 2006


Revision: 760
          http://trac.macosforge.org/projects/calendarserver/changeset/760
Author:   wsanchez at apple.com
Date:     2006-12-08 00:01:19 -0800 (Fri, 08 Dec 2006)

Log Message:
-----------
Add some UUID-making sauce.

Added Paths:
-----------
    CalendarServer/trunk/twistedcaldav/directory/test/test_util.py
    CalendarServer/trunk/twistedcaldav/directory/util.py

Added: CalendarServer/trunk/twistedcaldav/directory/test/test_util.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/directory/test/test_util.py	                        (rev 0)
+++ CalendarServer/trunk/twistedcaldav/directory/test/test_util.py	2006-12-08 08:01:19 UTC (rev 760)
@@ -0,0 +1,30 @@
+##
+# Copyright (c) 2005-2006 Apple Computer, Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# DRI: Wilfredo Sanchez, wsanchez at apple.com
+##
+
+from twistedcaldav.directory.util import *
+
+import twisted.trial.unittest
+
+uuid_namespace_dns = "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
+
+class UUID (twisted.trial.unittest.TestCase):
+    def test_uuidFromName(self):
+        self.assertEquals(
+            uuidFromName(uuid_namespace_dns, "python.org"),
+            "886313e1-3b8a-5372-9b90-0c9aee199e5d",
+        )

Added: CalendarServer/trunk/twistedcaldav/directory/util.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/directory/util.py	                        (rev 0)
+++ CalendarServer/trunk/twistedcaldav/directory/util.py	2006-12-08 08:01:19 UTC (rev 760)
@@ -0,0 +1,62 @@
+##
+# Copyright (c) 2006 Apple Computer, Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# DRI: Wilfredo Sanchez, wsanchez at apple.com
+##
+
+"""
+Utilities.
+"""
+
+__all__ = [
+    "uuidFromName",
+]
+
+from sha import sha
+
+def uuidFromName(namespace, name):
+    """
+    Generate a version 5 (SHA-1) UUID from a namespace UUID and a name.
+    See http://www.ietf.org/rfc/rfc4122.txt, section 4.3.
+    @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
+
+    # Start with a SHA-1 hash of the namespace and name
+    uuid = sha(namespace + name).digest()[:16]
+
+    # 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:])

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20061208/7305f994/attachment.html


More information about the calendarserver-changes mailing list