Python generates a unique ID using the UUID library

Source: Internet
Author: User
Tags md5 hash repetition unique id

Information:

Official Python doc: "20.15." Uuid-uuid objects according to RFC 4122
UUID Algorithm Introduction: "A universally Unique IDentifier (UUID) URN Namespace"

Overview:

A UUID is a globally unique identifier of 128 bits, typically represented by a 32-byte string. 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, current timestamp, random number. can guarantee uniqueness globally, but the use of Mac also brings security problems, LAN can use IP instead of Mac.

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 ensure 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

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.

Coding method:

 #  -*-coding:utf-8-*- import   uuidname  =  "  Test_name   " namespace  =  " test_namespace  "  print  uuid.uuid1 () #   see python Doc  print   print   Uuid.uuid4 ()  print  uuid.uuid5 (namespace, name)  

Python generates a unique ID using the UUID library

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.