Example of how Python generates a unique ID with a UUID library _python

Source: Internet
Author: User
Tags current time md5 md5 hash unique id uuid in python

Introduction to UUID

The UUID is a 128-bit globally unique identifier, typically represented by a 32-byte string. It guarantees the uniqueness of time and space, also known as GUIDs, which are all called: uuid--universally unique Identifier,python called UUID.

It guarantees the uniqueness of the generation ID by MAC address, timestamp, namespace, random number, pseudo-random number.

The UUID mainly has five algorithms, namely five kinds of methods to implement.

    1. uuid1()--based on time stamp. Generated by MAC address, current timestamp, random number. can guarantee a global scope of uniqueness, but the use of Mac also brings security problems, LAN can use IP to replace the Mac.
    2. uuid2()--DCE (without this function in Python) based on distributed computing environment. The algorithm is the same as UUID1, which replaces the first 4 bits of the timestamp with the POSIX uid. This method is rarely used in practice.
    3. uuid3()--MD5 hash value based on the name. The MD5 hash of the name and namespace is worthwhile by calculating 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()--based on random numbers. by pseudo random number, there is a certain probability of repetition, which can be calculated.
    5. uuid5()--SHA-1 hash value based on the name. The algorithm is the same as UUID3, and the Secure Hash algorithm 1 algorithm is used.

Using the sample

#! Coding:utf-8
import uuid
print U "UUID1 generates a UUID"
print uuid.uuid1 () # UUID based on the computer host ID and current time (' A8098C1A-F86E-11DA-BD1A-00112444BE1E ')

print U "\nuuid3 a MD5 encrypted UUID based on namespaces and one character"
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 a SHA-1 encrypted UUID
based on namespaces and one character UUID.UUID5 (UUID. Namespace_dns, ' python.org ') #UUID (' 886313e1-3b8a-5372-9b90-0c9aee199e5d ')

print U "\ n generate UUID based on hexadecimal character"
x = Uuid. UUID (' {00010203-0405-0607-0809-0a0b0c0d0e0f} ')
print U "convert to 16-binary UUID expression character"
print str (x) # ' 00010203-0405-0607-0809-0a0b0c0d0e0f '

Results

UUID1 generates a UUID 31a936a1-2339-11e6-8542-9cb70ded607f based on the computer host ID and the current time

uuid3 a MD5 encrypted UUID
based on namespace and one character 6FA459EA-EE8A-3CA4-894E-DB77E160355E

uuid4 randomly generates a UUID
67e6497c-8aec-4413-9955-da86f38ff2d6

UUID5 A SHA-1-encrypted UUID based on namespaces and one character converts a UUID based on a

hexadecimal character to a
16-in-representation character
00010203-0405-0607-0809-0a0b0c0d0e0f

Summarize

This is all about Python using the UUID library to generate a unique ID, and hopefully the content of this article will help you learn or use Python, and if you have any questions you can communicate.

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.