1 # Copyright (c) 2003-2016 CORE Security Technologies
3 # This software is provided under under a slightly modified version
4 # of the Apache Software License. See the accompanying LICENSE file
5 # for more information.
8 # Generate UUID compliant with http://www.webdav.org/specs/draft-leach-uuids-guids-01.txt.
9 # A different, much simpler (not necessarily better) algorithm is used.
12 # Javier Kohen (jkohen)
17 from random import randrange
18 from struct import pack, unpack
21 # UHm... crappy Python has an maximum integer of 2**31-1.
23 return pack("IIII", randrange(top), randrange(top), randrange(top), randrange(top))
25 def bin_to_string(uuid):
26 uuid1, uuid2, uuid3 = unpack('<LHH', uuid[:8])
27 uuid4, uuid5, uuid6 = unpack('>HHL', uuid[8:16])
28 return '%08X-%04X-%04X-%04X-%04X%08X' % (uuid1, uuid2, uuid3, uuid4, uuid5, uuid6)
30 def string_to_bin(uuid):
31 matches = re.match('([\dA-Fa-f]{8})-([\dA-Fa-f]{4})-([\dA-Fa-f]{4})-([\dA-Fa-f]{4})-([\dA-Fa-f]{4})([\dA-Fa-f]{8})', uuid)
32 (uuid1, uuid2, uuid3, uuid4, uuid5, uuid6) = map(lambda x: long(x, 16), matches.groups())
33 uuid = pack('<LHH', uuid1, uuid2, uuid3)
34 uuid += pack('>HHL', uuid4, uuid5, uuid6)
37 def stringver_to_bin(s):
38 (maj,min) = s.split('.')
39 return pack('<H',int(maj)) + pack('<H',int(min))
41 def uuidtup_to_bin(tup):
42 if len(tup) != 2: return
43 return string_to_bin(tup[0]) + stringver_to_bin(tup[1])
45 def bin_to_uuidtup(bin):
47 uuidstr = bin_to_string(bin[:16])
48 maj, min = unpack("<HH", bin[16:])
49 return uuidstr, "%d.%d" % (maj, min)
52 #output: tuple (uuid,version)
53 #if version is not found in the input string "1.0" is returned
55 # "00000000-0000-0000-0000-000000000000 3.0" returns ('00000000-0000-0000-0000-000000000000','3.0')
56 # "10000000-2000-3000-4000-500000000000 version 3.0" returns ('00000000-0000-0000-0000-000000000000','3.0')
57 # "10000000-2000-3000-4000-500000000000 v 3.0" returns ('00000000-0000-0000-0000-000000000000','3.0')
58 # "10000000-2000-3000-4000-500000000000" returns ('00000000-0000-0000-0000-000000000000','1.0')
59 def string_to_uuidtup(s):
60 g = re.search("([A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}).*?([0-9]{1,5}\.[0-9]{1,5})",s+" 1.0")
66 def uuidtup_to_string(tup):
67 uuid, (maj, min) = tup
68 return "%s v%d.%d" % (uuid, maj, min)