Python generates a unique ID using the UUID library

Source: Internet
Author: User
Tags md5 hash repetition

Information:     python official doc: "20.15. uuid - uuid objects according to  rfc 4122 "    uuid's Algorithm Introduction:" a universally unique identifier  ( UUID)  urn namespace "Overview:     uuid is a 128-bit globally unique identifier, typically represented by a 32-byte string.      it can guarantee the uniqueness of time and space, also known as the GUID, all called:             UUID -- Universally Unique IDentifier       Python  called  uuid            guid.  -- Globally Unique IDentifier           c#    GUID     It guarantees the uniqueness of the generation ID by MAC address, timestamp, namespace, random number, pseudo-random number.     uuid mainly has five algorithms, that is, five ways to achieve:        1, UUID1 ()--based on timestamp             &nbThe sp;   is generated by the MAC address, the current timestamp, and the random number. Global uniqueness is guaranteed,                But the use of Mac also brings security problems, LAN can use IP instead of Mac.        2, Uuid2 ()--based on Distributed computing environment DCE (without this function in Python)       The             algorithm is the same as UUID1, and the difference is that the first 4 bits of the timestamp are replaced 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 out.        5, UUID5 ()--SHA-1 hash value based on name         The           algorithm is the same as UUID3, the difference is the use of  secure hash algorithm  1  algorithm use aspects:     first of all, Python is not based on DCE, so UUID2 can ignore;     second, Uuid4 existence of probabilistic repetition, by no mapping, it is best not to;     again, if in the global distributed computing environment, preferably with uuid1;     Finally, If the uniqueness of the name requires, it is best to use UUID3 or UUID5. Coding Method:    # -*- coding: utf-8 -*-    import  uuid    name =  "Test_name"     namespace =  "Test_ Namespace "    print uuid.uuid1 ()   #  method with parameters see python doc     PRINT UUID.UUID3 (Namespace, name)     print uuid.uuid4 ()      PRINT UUID.UUID5 (Namespace, name)


Python generates a unique ID using the UUID library

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.