1. Basic
A UUID is a globally unique identifier of 128 bits, typically represented by a 32-byte string (hexadecimal).
It guarantees the uniqueness of time and space, also known as a GUID, all called:
- uuid--universally Unique IDentifier Python called UUID
- guid--globally Unique IDentifier C # called GUID
It guarantees the uniqueness of the generation ID by MAC address, timestamp, namespace, random number, pseudo-random number.
The UUID consists of five algorithms, which are five ways to achieve:
1, UUID1 ()--based on timestamp
Generated by MAC address (host Physical address), current timestamp, random number. can guarantee uniqueness worldwide,
But the use of Mac also brings security problems, LAN can use IP instead of Mac.
Note that UUID1 () returns a UUID object that is not a normal string, and contains rich member functions and variables.
2, Uuid2 ()--based on the Distributed computing environment DCE (without this function in Python)
The algorithm is the same as UUID1, the difference is to replace the first 4 bits of the timestamp with the POSIX UID.
This method is seldom used in practice.
3, UUID3 ()--MD5 hash value based on name
By calculating the MD5 hash of the name and namespace, it is worthwhile to guarantee the uniqueness of different names in the same namespace,
And the uniqueness of different namespaces, but the same name of the same namespace produces the same UUID.
4, Uuid4 ()--based on random numbers
obtained by pseudo-random number, there is a certain repetition probability, the probability can be calculated.
5, UUID5 ()--SHA-1 hash value based on name
The algorithm is the same as UUID3, and the other is using the Secure Hash algorithm 1 algorithm
2. Actual combat
Terms of Use:
First of all, Python is not based on DCE, so UUID2 can be ignored;
Secondly, there is a probabilistic repetition of uuid4, which is not mapped and preferably not.
Thirdly, if the global distributed computing environment, it is best to use UUID1;
Finally, if the uniqueness of the name requires, it is best to use UUID3 or UUID5.
1 ImportUUID2 3Name ="test_name"4namespace ="Test_namespace"5 6 PrintUUID.UUID1 ()#See the Python Doc for a method with the reference7 PrintUuid.uuid3 (namespace, name)8 PrintUuid.uuid4 ()9 PrintUuid.uuid5 (namespace, name)
Python Standard library--uuid (Generate unique ID)