Python third party Library series of eight--uuid library

Source: Internet
Author: User
Tags md5 md5 hash uuid

You are the only one for me. A unique identifier is also required in the program, and the UUID is born.

I. Overview

The UUID is a 128-bit globally unique identifier, typically represented by a 32-byte string.
It guarantees the uniqueness of time and space, also known as GUIDs, which are all called:
uuid--universally Unique IDentifier Python called UUID
guid--globally Unique IDentifier C # is called the GUID
It guarantees the uniqueness of the generation ID by MAC address, timestamp, namespace, random number, pseudo-random number.

Two, the algorithm of the UUID
The UUID has five main algorithms, namely five methods to achieve:
1, UUID1 ()--based on time stamp
Generated by MAC address, current timestamp, random number. can guarantee the uniqueness of the world,
But the use of Mac also brings security problems, the LAN can use IP to replace the Mac.
2, Uuid2 ()--DCE (without this function in Python) based on distributed computing environment
The algorithm is the same as UUID1, which replaces the first 4 bits of the timestamp with the POSIX uid.
This method is rarely used in practice.
3, UUID3 ()--MD5 hash value based on name
The MD5 hash of the name and namespace is worthwhile, guaranteeing the uniqueness of different names in the same namespace,
and uniqueness of different namespaces, but the same name of the same namespace generates the same UUID.
4, Uuid4 ()--based on random number
by pseudo random number, there is a certain probability of repetition, which can be calculated.
5, UUID5 ()--SHA-1 hash value based on name
The algorithm is the same as UUID3, and the Secure Hash algorithm 1 algorithm is used differently
In terms of usage:
First of all, there is no DCE based in Python, so uuid2 can be ignored;
Secondly, there is a probability of duplication, by uuid4, preferably not;
Thirdly, if the global distributed computing environment, it is best to use UUID1;
Finally, it is best to use UUID3 or UUID5 if the uniqueness of the name is required.

Third, coding practice

The import uuid

name = "Test_name"

print Uuid.uuid1 ()  # method with parameters see Python Doc
print uuid.uuid3 (uuid. Namespace_dns, name)
print uuid.uuid4 ()
print uuid.uuid5 (uuid. Namespace_dns, name)

#271195ee -74b1-11e4-88b1-600308a572a2
#7e9fc176 -3b19-394a-9530-83391161f8e9
# f38e76ba-4d54-43e9-81b5-6d96be63b878
#ad85ae8c-f638-56e0-b9fc-5d7a58009f62
So easy~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.