Python uses UUID library to generate unique ID

Source: Internet
Author: User
Tags md5 hash

Python official Doc: 20.15. uuid-UUID objects according to RFC 4122 UUID algorithm Introduction: A Universally Unique IDentifier (UUID) URN Namespace Overview: UUID is A 128-bit globally Unique IDentifier, it is usually represented by a 32-byte string. It can ensure the uniqueness of time and space, also known as GUID, all called: UUID -- Universally Unique IDentifier in Python uuid guid -- Globally Unique IDentifier C # is called GUID. It guarantees the uniqueness of the generated ID through MAC address, timestamp, namespace, random number, and pseudo-random number.. UUID consists of five algorithms: 1. uuid1 (), which is generated by MAC address, current timestamp, and random number based on the timestamp. Global uniqueness can be guaranteed. However, MAC security issues are also caused by the use of MAC. IP addresses can be used in LAN to replace MAC. 2. uuid2 ()-based on the distributed computing environment DCE (this function is not available in Python), the algorithm is the same as uid1. The difference is to replace the first four positions of the timestamp with the posix uid. This method is rarely used in practice. 3. uuid3 () -- name-based MD5 hash value is obtained by calculating the name and namespace MD5 hash value, ensuring the uniqueness of different names in the same namespace, and the uniqueness of different namespaces, but the same name of the same namespace generates the same uuid. 4. uuid4 ()-a random number is obtained from a pseudo-random number with a certain repetition probability. This probability can be calculated. 5. uuid5 () -- the name-based SHA-1 Hash value Algorithm is the same as that of uuid3. The difference is that the Secure Hash Algorithm 1 Algorithm is used. First, Python does not have a DCE-based Algorithm, therefore, uuid2 can be ignored. Second, uuid4 is probabilistic and repetitive. It is recommended that uuid1 be used in a Global distributed computing environment, uuid3 or uuid5. Encoding method: #-*-coding: UTF-8-*-import uuid name = "test_name" namespace = "test_namespace" print uuid. uuid1 () # For methods with parameters, see Python Doc print uuid. uuid3 (namespace, name) print uuid. uuid4 () print uuid. uuid5 (namespace, name)

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.