UUID (Universally Unique Identifier) is a 128-bit number used in computer systems to identify information, and while the probability of generating a UUID is not zero, it is infinitely close to zero, and therefore negligible, so that everyone can create UUIDs that don't conflict with anyone else.
UUID format composition
In the canonical text, the sixteen octet identification bits of the UUID, 32 hexadecimal (base 16) digits, are displayed in five groups divided by characters, 8-4-4-4-12 total grid of 36 characters (32 alphanumeric characters and 4 hyphens), for example:
123e4567-e89b-12d3-a456-426655440000
xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
where M stands for version, and from the following knowledge, this UUID may be generated by Python's uuid.uuid1(node, clock_seq)
The uuid Module in Python
Introduced after Python 2.5, the interface consists of the immutable object UUID (UUID class) and the functions uuid1(), uuid3(), uuid4(), uuid5(), the last four functions are used to generate the UUIDs specified in the specification of RFC 4122, versions 1, 3, 4 and 5. The specific algorithm is as follows:
UUID()
class ([hex[, bytes[, bytes_le[, fields[, int[, version]]]]]])
This class is used to instantiate a UUID object from the content given by the parameters (hex, bytes, bytes_le, fields, int must and can only be specified):
hex: Specify 32 characters to create a UUID object. When specifying a string of 32 characters to create a UUID object, parentheses, hyphens, URN prefixes, etc. are optional;
bytes: Specifies a big-endian byte-ordered byte string of 16 bytes in total length to create the UUID object;
bytes_le: Specifies a little-endian byte-ordered byte string of total length 16 bytes to create the UUID object;
fields: Specify 6 integer fields totaling 128 bits to create the UUID (32 bits for the time_low segment, 16 bits for the time_mid segment, 16 bits for the time_hi_version segment, 8 bits for the clock_seq_hi_variant segment, 8 bits for the clock_seq_low segment and (48 bits as node segment);
int: directly specifies an integer of length 128 binary bits to be used to create the UUID object;
version: (Optional) Specifies the version of the UUID, from 1 to 5. Once this parameter is specified, the generated UUID will have its own variant and version number, see RFC 4122 for details.
The following various methods create the same UUID object
u = UUID('{12345678-1234-5678-1234-567812345678}') u = UUID(hex = '12345678123456781234567812345678') u = UUID('urn:uuid:12345678-1234-5678-1234-567812345678') u = UUID(bytes='\x12\x34\x56\x78'*4) u = UUID(bytes_le='\x78\x56\x34\x12\x34\x12\x78\x56' + '\x12\x34\x56\x78\x12\x34\x56\x78') u = UUID(fields=(0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678)) u = UUID(int=0x12345678123456781234567812345678)
uuid1()
Generate a UUID from the host ID, sequence number and current time, if "node" is not given, use getnode() to get the hardware address. If "clock sequence" is given, it is used as the sequence number; otherwise, a random 14-bit sequence number is chosen.
# Source code reference
def uuid(node=None, clock_seq=None): ... return UUID(fields=(time_low, time_mid, time_hi_version, clock_seq_hi_variant, clock_seq_low, node), version=1)
fields The meaning of each parameter is shown below:
uuid3()
UUID generated based on an MD5 hash of a namespace identifier (essentially a UUID) and a name (essentially a string)
# Source code reference
def uuid3(namespace, name): """Generate a UUID from the MD5 hash of a namespace UUID and a name.""" from hashlib import md5 hash = md5( + bytes(name, "utf-8")).digest() return UUID(bytes=hash[:16], version=3)
uuid4()
UUID generation based on random numbers
# Source code reference
import os def uuid4(): """Generate a random UUID.""" return UUID(bytes=(16), version=4)
uuid5()
UUID generated based on a namespace identifier (essentially a UUID) and a SHA-1 hash of a name (essentially a string)
# Source code reference
def uuid5(namespace, name): """Generate a UUID from the SHA-1 hash of a namespace UUID and a name.""" from hashlib import sha1 hash = sha1( + bytes(name, "utf-8")).digest() return UUID(bytes=hash[:16], version=5)
From the source code, uuid1() uses the UUID (fields=(...))
uuid3(), uuid4(), uuid5() all use the UUID (bytes=...)
"RFC 4122" recommends using version 5 (SHA1) instead of version 3 (MD5).
getnode() in uuid1()
Gets the address of the hardware and returns it as a 48-bit binary integer, where the hardware address is the MAC address of the network interface, or any one of them if the machine has more than one network interface. If the fetch fails, a random 48-bit number is returned with bit 8 set to 1 (and its multicast bit (the least significant bit of the first octet) set to 1), as specified in "RFC 4122".
Regarding the namespace identifiers mentioned in uuid3() and uuid5().The uuid module defines the following options:
uuid.NAMESPACE_DNS
When this namespace is specified, the parameter name is a fully-qualified domain name.
uuid.NAMESPACE_URL
When this namespace is specified, the parameter name is a URL
uuid.NAMESPACE_OID
When this namespace is specified, the parameter name is an ISO OID.
uuid.NAMESPACE_X500
When this namespace is specified, the parameter name is an X.500 DN in DER or text output format.
These identifiers uniformly point to UUID('6ba7b810-9dad-11d1-80b4-00c04fd430c8') in the source code, so the namespace is used only as an identifier, defining the format of the name parameter
Practical application of UUID
uuid1 is adapted for use in distributed computing environments and is highly unique;
uuid3 and uuid5 are suitable for environments where a certain range of names are unique and where duplicate UUID generation is required or possible;
uuid4 is the simplest, but completely random and uncontrollable, it is suggested that it can be used as the user's SECRET_KEY when DRF generates and verifies JWT, which can be used to ensure that the user logs in every time, logs in at the same time on the same device, and changes the password, etc., and that the JWT fails.
Disadvantages of UUID
(36) Strings take up more space, but carry very little information and are not intuitive
2. This builds the index is very performance-consuming and slow
is unordered, but business systems often want to generate ordered, or roughly ordered, data.
References:
- uuid — UUID objects according to RFC 4122 /3/library/
- Python--uuid /Security-Darren/p/
- Python generates unique IDs using the UUID library /kaituorensheng/p/
- Universally unique identifier /wiki/Universally_unique_identifier
- Disadvantages of using UUIDs /woshiyexinjie/article/details/83351677
summarize
to this article on the Python standard library uuid module (generating unique identifiers) of the article is introduced to this, more related to the Python standard library uuid module content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!