# SECUREAUTH LABS. Copyright 2018 SecureAuth Corporation. All rights reserved. # # This software is provided under under a slightly modified version # of the Apache Software License. See the accompanying LICENSE file # for more information. # # Description: # Generate UUID compliant with http://www.webdav.org/specs/draft-leach-uuids-guids-01.txt. # A different, much simpler (not necessarily better) algorithm is used. # # Author: # Javier Kohen (jkohen) # from __future__ import absolute_import from __future__ import print_function import re import binascii from random import randrange from struct import pack, unpack EMPTY_UUID = b'\x00'*16 def generate(): # UHm... crappy Python has an maximum integer of 2**31-1. top = (1<<31)-1 return pack("IIII", randrange(top), randrange(top), randrange(top), randrange(top)) def bin_to_string(uuid): uuid1, uuid2, uuid3 = unpack('HHL', uuid[8:16]) return '%08X-%04X-%04X-%04X-%04X%08X' % (uuid1, uuid2, uuid3, uuid4, uuid5, uuid6) def string_to_bin(uuid): # If a UUID in the 00000000000000000000000000000000 format, let's return bytes as is if '-' not in uuid: return binascii.unhexlify(uuid) # If a UUID in the 00000000-0000-0000-0000-000000000000 format, parse it as Variant 2 UUID # The first three components of the UUID are little-endian, and the last two are big-endian 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) (uuid1, uuid2, uuid3, uuid4, uuid5, uuid6) = [int(x, 16) for x in matches.groups()] uuid = pack('HHL', uuid4, uuid5, uuid6) return uuid def stringver_to_bin(s): (maj,min) = s.split('.') return pack('