r5400 - trunk/wooly/python/wooly

tmckay at fedoraproject.org tmckay at fedoraproject.org
Mon Jun 4 16:08:16 UTC 2012


Author: tmckay
Date: 2012-06-04 16:08:16 +0000 (Mon, 04 Jun 2012)
New Revision: 5400

Modified:
   trunk/wooly/python/wooly/util.py
Log:
Use os.urandom for unique_id generation if available
BZ827558


Modified: trunk/wooly/python/wooly/util.py
===================================================================
--- trunk/wooly/python/wooly/util.py	2012-06-04 14:02:43 UTC (rev 5399)
+++ trunk/wooly/python/wooly/util.py	2012-06-04 16:08:16 UTC (rev 5400)
@@ -6,6 +6,7 @@
 import random
 import sys
 import time
+import struct
 
 from copy import copy
 from cStringIO import StringIO
@@ -28,13 +29,18 @@
     return string
 
 def unique_id():
-    bits0 = random.getrandbits(32)
-    bits1 = random.getrandbits(32)
-    bits2 = random.getrandbits(32)
-    bits3 = random.getrandbits(32)
 
-    return "%08x-%08x-%08x-%08x" % (bits0, bits1, bits2, bits3)
+    # Generate 4 unsigned integers.
+    # Take 4 bytes from urandom and interpret
+    # them as a standard-sized ('=') unsigned long ('L').
+    # Unpack returns a tuple, hence the [0].
+    try:
+        bits = [struct.unpack("=L", os.urandom(4))[0] for x in range(4)]
+    except NotImplementedError:
+        bits = [random.getrandbits(32) for x in range(4)]
 
+    return "%08x-%08x-%08x-%08x" % (bits[0], bits[1], bits[2], bits[3])
+
 def escape_amp(string):
     return str(string).replace("&", "&")
 



More information about the cumin-developers mailing list