A detailed example of how Python generates a unique ID using the UUID library

Source: Internet
Author: User
Tags md5 hash
UUID Introduction
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 the GUID, called the UUID in the uuid――universally unique Identifier,python.
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.

UUID1 ()

--based on time stamp. 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.

Uuid2 ()

--based on the Distributed computing environment DCE (this function is not 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.

UUID3 ()

--MD5 hash value based on the 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.

Uuid4 ()

– based on random numbers. obtained by pseudo-random number, there is a certain repetition probability, the probability can be calculated.

UUID5 ()

--SHA-1 hash value based on the name. The algorithm is the same as UUID3, and the other is using the Secure Hash algorithm 1 algorithm.
Using the example

#! Coding:utf-8import uuidprint u "UUID1 generates UUID based on computer host ID and current time" Print uuid.uuid1 () # UUID (' A8098C1A-F86E-11DA-BD1A-00112444BE1E ') print U "\nuuid3 based on a namespace and a character of MD5 encrypted uuid" Print uuid.uuid3 (UUID. Namespace_dns, ' python.org ') #UUID (' 6fa459ea-ee8a-3ca4-894e-db77e160355e ') print U "\nuuid4 randomly generate a UUID" print Uuid.uuid4 () # ' 16fd2706-8baf-433b-82eb-8c7fada847da ' Print U "\NUUID5 based on the namespace and a character of SHA-1 encrypted UUID" UUID.UUID5 (UUID. Namespace_dns, ' python.org ') #UUID (' 886313e1-3b8a-5372-9b90-0c9aee199e5d ') print U "\ n generate UUID based on hexadecimal characters" x = uuid. UUID (' {00010203-0405-0607-0809-0a0b0c0d0e0f} ') print U "translates to 16-binary UUID representation character" Print str (x) # ' 00010203-0405-0607-0809-0a0b0c0d0e0f '


Results

UUID1 generates a UUID31A936A1-2339-11E6-8542-9CB70DED607FUUID3 based on the computer host ID and the current time MD5 encrypted UUID6FA459EA-EE8A-3CA4-894E-DB77E160355EUUID4 based on namespaces and one character Randomly generate a UUID67E6497C-8AEC-4413-9955-DA86F38FF2D6UUID5 A SHA-1 encrypted UUID based on a namespace and a character generates a UUID based on the hexadecimal character to convert to a 16-binary UUID representation character 00010203-0405-0607-0809-0a0b0c0d0e0f


Summarize
The above is about Python using the UUID library to generate a unique ID of the entire content, I hope the content of this article for you to learn or use Python can help, if there are questions you can message exchange.


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.