Python’s UUID class defines four functions and each generates different version of UUIDs. Let’s see how to generate UUID based on MD5 and SHA-1 hash using
Cryptographic hashes can be used to generate different ID’s taking
NAMESPACE identifier and a
string as input. The functions that support cryptographic hash generation are :
uuid3(namespace, string): This function uses MD5 hash value of namespaces mentioned with a string to generate a random id of that particular string.
uuid5(namespace, string): This function uses SHA-1 hash value of namespaces mentioned with a string to generate a random id of that particular string.
uuid module defines the following namespace identifiers for use with uuid3() or uuid5() :
NAMESPACE_DNS: Used when name string is fully qualified domain name.
NAMESPACE_URL: Uused when name string is a URL.
NAMESPACE_OID: Used when name string is an ISO OID.
NAMESPACE_X500: Used when name string is an X.500 DN in DER or a text output format.
Code #1 :
The SHA1 hash value generated ID is : e13a319e-16d9-3ff5-a83c-96564007998e The MD5 hash value generated ID is : dbe3178d-4b83-5024-9b26-9b8e1b280514
Code #2 :
The SHA1 hash value generated ID is : adbed9f7-bbe3-376f-b88d-2018b8f6db07 The MD5 hash value generated ID is : f72cdf8a-b361-50b2-9451-37a997f8675d
Note : The ID generation is 2 step process. First, the concatenation of string and namespaces takes place, then given as input to the respective function to return a 128 UUID generated. If the same NAMESPACE value with a similar string is chosen again, ID generated will be same as well.
Reference : https://docs.python.org/2/library/uuid.html
- Generating random Id's in Python
- Generating Strong Password using Python
- Generating Word Cloud in Python
- Generating Word Cloud in Python | Set 2
- Generating Random id's using UUID in Python
- Generating random number list in Python
- Reading and Generating QR codes in Python using QRtools
- MD5 hash in Python
- Python | hash() method
- Generating all possible Subsequences using Recursion
- Generating subarrays using recursion
- Generating numbers that are divisor of their right-rotations
- Generating Lyndon words of length n
- Generating random strings until a given string is generated
- Hash Function for String data in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.