import subprocess def uuid_str_to_int(uuid_str: str) -> int: """ >>> uuid_str_to_int('613ea4ac-a4cf-4026-8e99-1904b2bb5cd0') 129260377989791042510121038559452028112 >>> uuid_str_to_int('c064bf40-4fd1-50e1-a2f7-2aefa4593f67') 255734883912096391966117250963728842599 """ return int(uuid_str.replace("-", ""), 16) def uuid_int_to_str(uuid_int: str) -> str: """ >>> uuid_int_to_str(129260377989791042510121038559452028112) '613ea4ac-a4cf-4026-8e99-1904b2bb5cd0' >>> uuid_int_to_str(255734883912096391966117250963728842599) 'c064bf40-4fd1-50e1-a2f7-2aefa4593f67' """ uuid_hex = hex(uuid_int)[2:] return "-".join( (uuid_hex[0:8], uuid_hex[8:12], uuid_hex[12:16], uuid_hex[16:20], uuid_hex[20:]) ) def uuid_int_to_bytes(uuid_siv: int) -> bytes: """ >>> uuid_int_to_bytes(129260377989791042510121038559452028112) b'a>\\xa4\\xac\\xa4\\xcf@&\\x8e\\x99\\x19\\x04\\xb2\\xbb\\\\\\xd0' >>> uuid_int_to_bytes(255734883912096391966117250963728842599) b'\\xc0d\\xbf@O\\xd1P\\xe1\\xa2\\xf7*\\xef\\xa4Y?g' """ return uuid_siv.to_bytes(16, byteorder="big") def uuid_bytes_to_int(uuid_bytes: bytes) -> int: """ >>> uuid_bytes_to_int(b'a>\\xa4\\xac\\xa4\\xcf@&\\x8e\\x99\\x19\\x04\\xb2\\xbb\\\\\\xd0') 129260377989791042510121038559452028112 >>> uuid_bytes_to_int(b'\\xc0d\\xbf@O\\xd1P\\xe1\\xa2\\xf7*\\xef\\xa4Y?g') 255734883912096391966117250963728842599 """ return int.from_bytes(uuid_bytes, byteorder="big", signed=False) def uuid_str_to_bytes(uuid_str: str) -> bytes: """ >>> uuid_str_to_bytes('613ea4ac-a4cf-4026-8e99-1904b2bb5cd0') b'a>\\xa4\\xac\\xa4\\xcf@&\\x8e\\x99\\x19\\x04\\xb2\\xbb\\\\\\xd0' >>> uuid_str_to_bytes('c064bf40-4fd1-50e1-a2f7-2aefa4593f67') b'\\xc0d\\xbf@O\\xd1P\\xe1\\xa2\\xf7*\\xef\\xa4Y?g' """ return uuid_int_to_bytes(uuid_str_to_int(uuid_str)) def uuid_bytes_to_str(uuid_bytes: bytes) -> str: """ >>> uuid_bytes_to_str(b'a>\\xa4\\xac\\xa4\\xcf@&\\x8e\\x99\\x19\\x04\\xb2\\xbb\\\\\\xd0') '613ea4ac-a4cf-4026-8e99-1904b2bb5cd0' >>> uuid_bytes_to_str(b'\\xc0d\\xbf@O\\xd1P\\xe1\\xa2\\xf7*\\xef\\xa4Y?g') 'c064bf40-4fd1-50e1-a2f7-2aefa4593f67' """ return uuid_int_to_str(uuid_bytes_to_int(uuid_bytes)) def generate_uuid4_bytes() -> bytes: return subprocess.check_output(["uuid", "-v", "4", "-F", "BIN"]).strip()