UUID Module in Python
Hello everyone, In this tutorial, we’ll be learning about the UUID Module in Python which is used to generate immutable unique ids of 128 bits. These are Randomly generated using some hashing techniques, MAC Addresses, Time, Hardware, etc. Let us start this tutorial with a brief overview of the UUID Module.
Overview of UUID Module in Python
This module enables us to generate a Universal Unique Identifier(UUID) that can be used in various ways like Unique Keys in our database, Transaction ids, Encoding, Cryptography, etc. This module lets us generate 4 versions of UUID’s out of 5 versions that are specified in RFC 4122. because the remaining one version(UUID VERSION 2) is reserved for DCE security and it does not provide any information. Let us see the Read-Only attributes that are common to all the versions of UUIDs generated by this Module.
Attributes of Generated UUIDs
- bytes – Returns the UUID as a 16-byte string.
- hex – Returns the UUID as a 32-character hexadecimal string.
- int – Returns the UUID as a 128-bit integer.
- urn – Returns the UUID as a Uniform Resource Name.
- version – Returns the Version number(1 to 5) of the UUID.
- getnode() – Returns the Hardware address as a 48-bit integer. If this fails to get the address any random 48-bit integer is returned.
- fields – Returns a tuple containing values of 6 individual attributes and 2 derived attributes.
- time_low – First 32 bits of the UUID (0-31)
- time_mid – Next 16 bits of the UUID (32-47)
- time_hi_version – Next 16 bits of the UUID (48-63)
- clock_seq_hi_variant – Next 8 bits of the UUID (64-71)
- clock_seq_low – Next 8 bits of the UUID (72-79)
- node – Last 48 bits of the UUID (80-127)
- time – Returns the 60-bit timestamp
- clock_seq – Returns the 14-bit sequence number
UUID1() in Python
This function will return a 16 Byte randomly generated UUID that contains System’s host ID, sequence number, and the current time and due to which it is less secure than the other types of UUIDs.
import uuid print("1st generated UUID using UUID1() is: ",uuid.uuid1()) print("2nd generated UUID using UUID1() is: ",uuid.uuid1())
1st generated UUID using UUID1() is: 58b7085c-feed-11e9-b70a-28f10e2ffee1 2nd generated UUID using UUID1() is: 58b841ac-feed-11e9-87fb-28f10e2ffee1
We notice that both the UUIDs are very much similar to each other. This is why they are less secure than other Versions of UUIDs.
UUID4() in Python
We will look at UUID4() because it overcomes the drawbacks of using UUID1() and this generates a Fully Random UUID and therefore is secure. Let us generate a UUID using this function and try to convert them in different forms and apply other attributes that we have already discussed.
x = uuid.uuid4() print("Generated UUID using UUID4() is: ",x) print("\n-------Attributes of UUID-------\n") print("Byte form : ",x.bytes) print("Int form : ",x.int) print("Hex form : ",x.hex) print("URN form : ",x.urn) print("Version Number : ",x.version) print("Fields : ",x.fields)
Generated UUID using UUID4() is: 8effa93f-f9da-44ad-8366-300b39376d5c -------Attributes of UUID------- Byte form : b'\x8e\xff\xa9?\xf9\xdaD\xad\x83f0\x0b97m\\' Int form : 190077843896307328124437573854035406172 Hex form : 8effa93ff9da44ad8366300b39376d5c URN form : urn:uuid:8effa93f-f9da-44ad-8366-300b39376d5c Version Number : 4 Fields : (2399119679, 63962, 17581, 131, 102, 52824762707292)
Namespace Identifiers in UUID Module
Before using UUID3() and UUID5() functions we should know about these namespaces. They are necessary because UUID3() and UUID5() are based on MD-5 and SHA-1 hashing algorithms of a namespace identifier respectively. This Module provides several namespaces which are listed below.
- NAMESPACE_DNS – This string passed is a fully-qualified domain name.
- NAMESPACE_URL – This string passed is a Uniform Resource Locator(URL).
- NAMESPACE_OID – This string passed is an ISO OID(object identifier).
- NAMESPACE_X500 – This string passed is an X.500 DN in DER or a text output format.
UUID3() and UUID5() in Python
These will generate UUIDs that are based on MD-5 and SHA-1 hashing algorithms that are applied to the name argument and the specified UUID namespace identifier. These will take two mandatory arguments as:
- namespace – It is the namespace identifier that we have discussed above.
- name – The string which id related to the specified namespace.
y = uuid.uuid3(uuid.NAMESPACE_URL,"https://codespeedy.com/category/python/") print("Generated UUID using UUID3() is: ",y) z = uuid.uuid5(uuid.NAMESPACE_DNS,"codespeedy.com") print("\nGenerated UUID using UUID5() is: ",z)
Generated UUID using UUID3() is: 0daa5e1d-4d86-3bb1-82bd-1bbe4ed5f6f8 Generated UUID using UUID5() is: 3985abb4-b476-52ea-9eb3-1721ab86f61f
We hope you like this tutorial and if you have any doubts, feel free to ask in the comment tab below.
You may like to read.