Universal Unique Identifier-UUID (Python)

1 Overview

UUID (Universally Unique Identity) abbreviation is a software construction standard, usually represented by a 32-byte hexadecimal number (128 bits), which can guarantee the uniqueness of time and space. Currently, the most widely used UUID is Microsoft's GUIDs.

2 role

UUID allows all elements of a distributed system to have unique identification information, so that everyone can create a UUID that is different from others, without having to consider the problem of name duplication during database creation. Randomly generate a string and use it as a token, user account, order, etc.

3 Principle

UUID refers to a number generated on a machine, and it guarantees that all machines in the same time and space are unique.

UUID consists of the following parts:

  1. Timestamp: Generate a string according to the current time or clock sequence
  2. The unique machine identification number of the whole play is obtained according to the MAC address or IP of the network card. If there is no network card, it will be obtained by other means.
  3. Random number: the machine automatically randomizes a set of sequences

and many more

4 Algorithm

There are 5 generating algorithms for uuid, namely uuid1(), uuid2(), uuid3(), uuid4(), uuid5().

1. uuid1() is based on timestamp

It consists of MAC address, current timestamp, and random number. Guarantee uniqueness on a global scale. However, since the use of MAC addresses will bring security problems, use IP instead of MAC in the LAN

2. uuid2() is based on distributed environment DCE

The algorithm is the same as uuid1, except that the first four digits of the timestamp are replaced with the UID of POIX, which is rarely used in practice.
Note: there is no such function in python

3. uuid3() is based on the name and MD5 hash value

It is obtained by calculating the MD5 hash value of the name and the namespace, which ensures the uniqueness of different names in the same namespace and the uniqueness of different namespaces. But the same name space and the same name generate the same uuid.

4. uuid4() is based on random numbers

Obtained from pseudo-random numbers, there is a certain probability of repetition, this probability can be calculated

5. uuid5() is based on name and SAHI value

The algorithm is the same as uuid3, the difference is that the SAHI algorithm is used

5 Use experience

  1. There is no DCE in python, so uuid2 can be ignored
  2. uuid4 has probabilistic repeatability, it is best not to use it due to the lack of mapping
  3. If you are in a global distributed environment, it is best to use uuid1
  4. If the uniqueness of the name is required, it is best to use uuid3 or uuid5

6 Use

import uuid
print(uuid.uuid1())
print(uuid.uuid4())
print(uuid.uuid3(uuid.NAMESPACE_DNS, "myname"))
print(uuid.uuid5(uuid.NAMESPACE_DNS, "myname"))

operation result:

a8f5372c-2bb8-11eb-9da2-000c292b487b
17893888-3c02-434e-b207-a64ed87b8d15
86ade465-d446-3e2e-b52b-fe5589f9387f
3d30fd12-b931-53b1-98ed-25e755531898

Guess you like

Origin blog.csdn.net/happyjacob/article/details/109705616