Generating hash ids using uuid3() and uuid5() in Python

PythonServer Side ProgrammingProgramming

The universally unique identifier is a 32 bit hexadecimal number that can guarantee a unique value in a given namespace. This helps in tracking down objects created by a program or where ever python needs to handle object or data that needs large value of identifier. The UUID class defines functions that can create these values.

Syntax

uuid3(namespace, string)
uuid3 usesMD5 hash value to create the identifier.

Uuid5(namespace, string)
Uuid5 uses SHA-1 hash value to create the identifier.
The namespace can be –
NAMESPACE_DNS : Used when name string is fully qualified domain name.
NAMESPACE_URL : Used when name string is a URL.

In the below example we see that we can choose an initial string which can be further used to create the uuids..

Example

 Live Demo

import uuid
# A given string
str1 = "www.tutorialspoint.com"
str2 = "http://www.Tutorialspoint.com"
print("Using uuid3, the generated ID is :\n",
   uuid.uuid3(uuid.NAMESPACE_URL, str1))
print("Using uuid3, the generated ID is :\n",
   uuid.uuid3(uuid.NAMESPACE_DNS, str2))
print("Using uuid5, the generated ID is :\n ",
   uuid.uuid5(uuid.NAMESPACE_URL, str1))
print("Using uuid5, the generated ID is :\n",
   uuid.uuid5(uuid.NAMESPACE_DNS, str2))

Running the above code gives us the following result:

Output

Using uuid3, the generated ID is :
e5051d13-d1a5-381a-bc21-5017b275a7f2
Using uuid3, the generated ID is :
de365612-734a-38e3-abc4-6e3ffc7d61db
Using uuid5, the generated ID is :
a064f94e-5ff6-51e4-88e2-e2163a79abce
Using uuid5, the generated ID is :
b9761e0a-0ef3-5fd3-9ec4-86b6e073e61b
raja
Updated on 20-Dec-2019 10:40:51

Advertisements