Python uses the UUID library to generate a unique ID.

Source: Internet
Author: User
Tags md5 hash

Python uses the UUID library to generate a unique ID.

UUID Introduction

UUID is a 128-bit globally unique identifier, usually represented by a 32-byte string. It can ensure the uniqueness of time and space, also known as GUID. It is called UUID -- Universally Unique IDentifier, which is called UUID in Python.

It ensures the uniqueness of the generated ID through MAC address, timestamp, namespace, random number, and pseudo-random number.

UUID mainly consists of five algorithms, namely, five methods.

  1. uuid1()-- Based on the timestamp. Generated by MAC address, current timestamp, and random number. 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 uuid1. The difference is to replace the first four positions of the timestamp with the UID of POSIX. This method is rarely used in practice.
  3. uuid3()-- Name-based MD5 hash value. By calculating the MD5 Hash Value of the name and namespace, the uniqueness of different names in the same namespace and those of different namespaces are ensured, however, the same name in the same namespace generates the same uuid.
  4. uuid4()-- Based on random number. A pseudo-random number has a certain recurrence probability, which can be calculated.
  5. uuid5()-- Name-based hash value of SHA-1. The Algorithm is the same as that of uuid3. The difference is that the Secure Hash Algorithm 1 Algorithm is used.

Example

#! Coding: utf-8import uuidprint u "uuid1 generates UUID" print uuid Based on the host ID and current time. uuid1 () # UUID ('a8098c1a-f86e-11da-bd1a-00112444be1e ') print u "\ nuuid3 Based on the namespace and a character of the MD5 encrypted UUID" print uuid. uuid3 (uuid. NAMESPACE_DNS, 'python. org ') # UUID ('6fa459ea-ee8a-3ca4-894e-db77e160355e') print u "\ nuuid4 randomly generates a UUID" print uuid. uuid4 () #'16fd2706-8baf-433b-82eb-8c7fada847da 'print u "\ nuuid5 UUID Based on namespace and SHA-1 character encryption. uuid5 (uuid. NAMESPACE_DNS, 'python. org ') # UUID ('886313e1-3b8a-5372-9b90-0c9aee199e5d') print u "\ n generate UUID" x = uuid Based on hexadecimal characters. UUID ('{00010203-0405-0607-0809-0a0b0c0d0e0f}') print u "to hexadecimal UUID representation character" print str (x) #'2017-0405-0607-0809-0a0b0c0d0e0f'

Result

Uuid1 generate a UUID31a936a1-2339-11e6-8542-9cb70ded607fuuid3 based on the computer host ID and current time based on namespace and a character-based MD5-encrypted UUID6fa459ea-ee8a-3ca4-894e-db77e160355euuid4 randomly generate a UUID67e6497c-8aec-4413-9955-da86f38ff2d6uuid5 Based on namespace and a character-based SHA-1-encrypted UUID according to the hexadecimal UUID generated by character conversion to hexadecimal UUID representation character 00010203-0405-0607-0809-0a0b0c0d0e0f

Summary

The above is all about Python's use of the UUID library to generate a unique ID. I hope this article will be helpful for you to learn or use python. If you have any questions, please leave a message.

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.