Python USES the UUID library to generate an example of a unique ID method

  • 2020-05-17 05:52:41
  • OfStack

UUID introduction

UUID is a 128-bit global unique identifier, typically represented by a 32-byte string. It guarantees the uniqueness of time and space, also known as GUID, UUID -- Universally Unique IDentifier, UUID in Python.

It USES the MAC address, timestamp, namespace, random number, and pseudorandom Numbers to guarantee the generation of ID's uniqueness.

UUID mainly has five algorithms, that is, five methods to implement it.

uuid1() -- based on timestamps. Generated by MAC address, current timestamp, random number. Global exclusiveness can be guaranteed, but the use of MAC also brings security problems. IP can be used instead of MAC in LAN.
uuid2() -- based on distributed computing environment DCE (Python does not have this function). The algorithm is the same as uuid1 except that the first four positions of the timestamp are changed to UID of POSIX. This method is rarely used in practice.
uuid3() -- name based MD5 hash value. By calculating the MD5 hash of names and namespaces, the singleness of different names in the same namespace and the singleness of different namespaces are guaranteed, but the singleness of names in the same namespace generates the same uuid.
uuid4() -- based on random Numbers. Given a pseudorandom number, there is a definite repeat probability, which can be calculated.
uuid5() -- name based SHA-1 hash value. The algorithm is the same as uuid3, except that Secure Hash Algorithm 1 is used.

Use the sample


#! coding:utf-8
import uuid
print u"uuid1  Generation is based on the computer host ID And the current time UUID"
print uuid.uuid1() # UUID('a8098c1a-f86e-11da-bd1a-00112444be1e')

print u"\nuuid3  Based on namespaces and 1 A character MD5 encrypted UUID"
print uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org') #UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e')

print u"\nuuid4  Randomly generated 1 a UUID"
print uuid.uuid4() #'16fd2706-8baf-433b-82eb-8c7fada847da'

print u"\nuuid5  Based on namespaces and 1 A character SHA-1 encrypted UUID"
uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org') #UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d')

print u"\n According to the 106 Base character generation UUID"
x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}')
print u" Converted to 106 Into the system UUID Performance characters "
print str(x) # '00010203-0405-0607-0809-0a0b0c0d0e0f'

The results of


uuid1  Generation is based on the computer host ID And the current time UUID
31a936a1-2339-11e6-8542-9cb70ded607f

uuid3  Based on namespaces and 1 A character MD5 encrypted UUID
6fa459ea-ee8a-3ca4-894e-db77e160355e

uuid4  Randomly generated 1 a UUID
67e6497c-8aec-4413-9955-da86f38ff2d6

uuid5  Based on namespaces and 1 A character SHA-1 encrypted UUID

 According to the 106 Base character generation UUID
 Converted to 106 Into the system UUID Performance characters 
00010203-0405-0607-0809-0a0b0c0d0e0f

conclusion

The above is all about Python using UUID library to generate only 1ID. I hope the content of this article can help you to learn or use python. If you have any questions, please leave a message to communicate.


Related articles: