Python Standard library--uuid (Generate unique ID)

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

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)

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.