UUID stands for Universal Unique Identifier is a Python library that helps in generating random objects of 128 bits as ids. It provides uniqueness as it generates ids based on time, Computer hardware (MAC, etc.). In this post, we will see how to use the Python UUID module to generate the universally unique identifiers.
Understanding UUID in detail
A UUID is 128 bits long number or ID to uniquely identify the sensitive information or insensitive like files, users, resources, or information in computer systems.
- UUID can ensure the uniqueness of Identifiers across all the space and time. When we talk about space and time, that means when UUID generated according to the standard. Also, the identifier does not duplicate one that has already been created or will be created to identify something else.
- Therefore UUID is beneficial where unique value is required.
The UUID module in Python implemented as per RFC 4122. RFC 4122 is the standard and Copyright (C) of The Internet Society. RFC 4122 specification encompasses all the details and algorithms to generate the Unique Identifiers of all the versions. RFC 4122 document defines three algorithms to create UUIDs.
Hence using the Python UUID module, you can generate versions 1, 3, 4, and 5 UUIDs. UUID generated using the uuid module is immutable.
Python UUID module provides the following versions of UUIDs.
- UUID1 generates UUID using a Host MAC address, serial(sequence) number, and the current time. The uuid1 version uses the IEEE 802 MAC addresses.
- UUID3 and UUID5 use cryptographic hashing and application-provided text strings to generate UUID. The UUID 3 uses MD5 hashing, and UUID 5 uses the SHA-1 hashing.
- UUID4 uses the pseudo-random number generators to generate UUID. It is more secure.
Representations of uuid1()
- bytes: Returns id in the form of 16-byte string.
- int: Returns id in the form of a 128-bit integer.
- hex: Returns the random id as 32 character hexadecimal string.
Components of uuid1()
- version: It is the version number of UUID.
- variant: It is a variant determining the internal layout of UUID.
Fields of uuid1()
- time_low: The first 32 bits of id.
- time_mid: The next 16 bits of id.
- time_hi_version: The next 16 bits of id.
- clock_seq_hi_variant : Next 8 bits of id.
- clock_seq_low: Next 8 bits of id.
- node: Last 48 bits of id.
- time: Time component field of id.
- clock_seq : 14 bit sequence number.
Let’s extract and print the above information for uuid.uuid1().
import uuid UUID = uuid.uuid1() print("UUID is ", UUID) print("UUID Type is ", type(UUID)) print('UUID.bytes :', UUID.bytes) print('UUID.bytes_le :', UUID.bytes_le) print('UUID.hex :', UUID.hex) print('UUID.int :', UUID.int) print('UUID.urn :', UUID.urn) print('UUID.variant :', UUID.variant) print('UUID.version :', UUID.version) print('UUID.fields :', UUID.fields) print("Prining each field seperately") print('UUID.time_low : ', UUID.time_low) print('UUID.time_mid : ', UUID.time_mid) print('UUID.time_hi_version : ', UUID.time_hi_version) print('UUID.clock_seq_hi_variant: ', UUID.clock_seq_hi_variant) print('UUID.clock_seq_low : ', UUID.clock_seq_low) print('UUID.node : ', UUID.node) print('UUID.time : ', UUID.time) print('UUID.clock_seq : ', UUID.clock_seq) print('UUID.SafeUUID : ', UUID.is_safe)
UUID is a1bd0546-9b5f-11ea-899c-d0817acce8e4 UUID Type is <class 'uuid.UUID'> UUID.bytes : b'\xa1\xbd\x05F\x9b_\x11\xea\x89\x9c\xd0\x81z\xcc\xe8\xe4' UUID.bytes_le : b'F\x05\xbd\xa1_\x9b\xea\x11\x89\x9c\xd0\x81z\xcc\xe8\xe4' UUID.hex : a1bd05469b5f11ea899cd0817acce8e4 UUID.int : 214987158433739041298017178985725487332 UUID.urn : urn:uuid:a1bd0546-9b5f-11ea-899c-d0817acce8e4 UUID.variant : specified in RFC 4122 UUID.version : 1 UUID.fields : (2713519430, 39775, 4586, 137, 156, 229254529607908) Prining each field seperately UUID.time_low : 2713519430 UUID.time_mid : 39775 UUID.time_hi_version : 4586 UUID.clock_seq_hi_variant: 137 UUID.clock_seq_low : 156 UUID.node : 229254529607908 UUID.time : 138093573625939270 UUID.clock_seq : 2460 UUID.SafeUUID : SafeUUID.unknown
Generate Random IDs using UUID.uuid1()
Python UUID module provides uuid1() method that generates the random id using the MAC address and time component.
- The node and clock_seq are optional arguments.
- The node is the hardware address, which is the 48-bit positive integer. If a node not given, then the uuid.getnode() function is used to obtain the Universally administered MAC addresses of a current host.
- If clock_seq is given, it used as the sequence number. Otherwise, a random 14-bit sequence number is chosen.
import uuid print("The random id using uuid1() is : ") print(uuid.uuid1())
The random id using uuid1() is : d8ed6420-9b50-11ea-9dad-d0817acce8e4
You can see that we have generated the random id. Now, we can use this id anywhere in our application or use in cryptography to further strengthen the encryption.
Generate unique ID for Host using node and clock sequence
We can apply the node and clock sequence to function explicitly.
import uuid print("Use of node and clock sequence") clock_seq = 1111 node = 0xcff7894d4a0 print(uuid.uuid1(node, clock_seq))
Use of node and clock sequence 9569ef86-9b58-11ea-8457-0cff7894d4a0
The uuid.getnode() method is used to get the MAC address of the network interface. If a machine has more than one network universally administered, then the MAC addresses are returned instead of over locally administered MAC addresses.
The administered MAC addresses guaranteed to be globally unique if the getnode() function fails to get the MAC address; it returns the random 48-bit number with a multicast bit as recommended in the RFC 4122.
Disadvantage of uuid1()
The major drawback of the uuid1() method is that this way includes the use of the MAC address of the computer, and hence can compromise privacy, even though it provides uniqueness. So the uuid1() method is not useful in data-sensitive applications.
Generate random ids using UUID.uuid4()
Python uuid.uuid4() function returns the random no. And doesn’t compromise with privacy.
import uuid print("The random id using uuid4() is : ") print(uuid.uuid4())
The random id using uuid4() is : 2f98f4b1-81ff-4876-b0f5-a48979b04219
From the output, UUID is built on the five components, and each component has a fixed length. A hyphen symbol separates each component. UUID’s presented in the format of “8-4-4-4-12”. The first component has 8 bits then, second has 4, third has 4, fourth has 4, and fifth has 12.
The formal definition of a UUID string representation is as follows.
UUID = time_low “-” time_mid “-“time_high_and_version ” “clock_seq_and_reserved_And_clock_seq_low“-” Node.
The uuid4() creates the random UUID. The chance of a clash is small. When UUIDs need to create on separate machines, or you need to generate a secure UUIDs use UUID4().
Generate random ids using uuid3()
The uuid.uuid3(namespace, name) function generates a UUID based on the MD5 hash of the namespace identifier (which is a UUID) and a string.
The UUID module defines the below namespace identifiers to use with uuid3() or uuid5().
- UUID.NAMESPACE_DNS: It means a fully qualified domain name. For example, google.com.
- UUID.NAMESPACE_URL When this namespace is defined, It means it is a URL.
- UUID.NAMESPACE_OID When this namespace is defined, the name string is an ISO OID.
- UUID.NAMESPACE_X500 When this namespace is defined, the name string is the X.500 DN in DER or a text output format.
Let’s generate random ids using uuid3().
import uuid hosts = ['google.com', 'facebook.com', 'amazon.com'] for host in hosts: print('Generate uuid3') print(uuid.uuid3(uuid.NAMESPACE_DNS, host))
Generate uuid3 9a74c83e-2c09-3513-a74b-91d679be82b8 Generate uuid3 1e93767f-9f44-31f4-aa41-08c8da88a58c Generate uuid3 c9485c47-6e77-30b4-88da-bbac9aa4ebf2
The UUIDs generated at different times using the same namespace and same name are equal. The unique ids generated from two different names in the same namespace are different. The UUIDs generated from the same name in two different namespaces are different.
Generate random ids using uuid5()
We can apply the same formula from the above function, but the difference is this time we use the uuid5() function.
import uuid hosts = ['google.com', 'facebook.com', 'amazon.com'] for host in hosts: print('Generate uuid5') print(uuid.uuid5(uuid.NAMESPACE_DNS, host))
Generate uuid5 64ee70a4-8cc1-5d25-abf2-dea6c79a09c8 Generate uuid5 03d4c789-f11c-533a-a48c-31ba2319170b Generate uuid5 a7846505-8bcc-57b5-a555-e1f871002f46
When and why using UUID
One of the most used applications right now is in cryptography and cybersecurity. If you are going to the bank, then when you deposit the money, the transaction is created, and then the transaction id is generated. Now, we can use here to create a transaction id. We can use the UUID to create a transaction ID. So in financial applications, this can be helpful.
UUID’s are also helpful for generating the unique session id to help state management. If you are working with Laravel or other PHP Framework, then you have seen that we are creating the user id using an autoincrement technic. In which, each time a user is created, we will increment its id by one. In this case, we can easily guess the user id.
When you use UUID, it is difficult to guess the ID because UUID not created in any serial format, so it is tough to guess it’s serial or sequence. So, it can be helpful in web development as well. UUID has a significant advantage because UUID is environmentally independent. i.e., UUID generated on any machine using any application is universally unique.
In this tutorial, first, we have understood what the uuid module is and what are its functions that can help us to create random ids for different scenarios. Then we have seen why and when we can use the uuid in real-world projects.
Finally, Generate Random IDs using UUID in Python example is over.