faster web2py_uuid() thanks Michele

This commit is contained in:
mdipierro
2012-09-28 09:21:03 -05:00
parent 8e6e7ed4cc
commit 8bee93c236
2 changed files with 6 additions and 9 deletions
+1 -1
View File
@@ -1 +1 @@
Version 2.0.9 (2012-09-28 08:51:08) dev Version 2.0.9 (2012-09-28 09:20:55) dev
+5 -8
View File
@@ -9,6 +9,7 @@ License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html)
This file specifically includes utilities for security. This file specifically includes utilities for security.
""" """
import string
import hashlib import hashlib
import hmac import hmac
import uuid import uuid
@@ -88,7 +89,7 @@ DIGEST_ALG_BY_SIZE = {
} }
### compute constant ctokens ### compute constant CTOKENS
def initialize_urandom(): def initialize_urandom():
""" """
This function and the web2py_uuid follow from the following discussion: This function and the web2py_uuid follow from the following discussion:
@@ -124,7 +125,7 @@ def initialize_urandom():
your system does not provide a cryptographically secure entropy source. your system does not provide a cryptographically secure entropy source.
This is not specific to web2py; consider deploying on a different operating system.""") This is not specific to web2py; consider deploying on a different operating system.""")
return ctokens return ctokens
ctokens = initialize_urandom() CTOKENS = initialize_urandom()
def web2py_uuid(): def web2py_uuid():
""" """
@@ -134,14 +135,10 @@ def web2py_uuid():
It works like uuid.uuid4 except that tries to use os.urandom() if possible It works like uuid.uuid4 except that tries to use os.urandom() if possible
and it XORs the output with the tokens uniquely associated with this machine. and it XORs the output with the tokens uniquely associated with this machine.
""" """
bytes = [random.randrange(256) for i in range(16)]
try: try:
ubytes = [ord(c) for c in os.urandom(16)] # use /dev/urandom if possible bytes = string.join(map(lambda ur, ctoken: chr(random.randrange(256)^ord(ur)^ctoken), os.urandom(16), CTOKENS),'')
bytes = [bytes[i] ^ ubytes[i] for i in range(16)]
except NotImplementedError: except NotImplementedError:
pass bytes = string.join(map(lambda ctoken: chr(random.randrange(256)^ctoken), CTOKENS),'')
## xor bytes with constant ctokens
bytes = ''.join(chr(c ^ ctokens[i]) for i,c in enumerate(bytes))
return str(uuid.UUID(bytes=bytes, version=4)) return str(uuid.UUID(bytes=bytes, version=4))
REGEX_IPv4 = re.compile('(\d+)\.(\d+)\.(\d+)\.(\d+)') REGEX_IPv4 = re.compile('(\d+)\.(\d+)\.(\d+)\.(\d+)')