What is a UUID?
UUID (Universally Unique Identifier) represents a very long string of a 128-bit long unique identification value. The UUID is expressed as a string consisting of 8 hexadecimal digits, each followed by a hyphen, then three groups of 4 hexadecimal digits, each followed by a hyphen, then 12 hexadecimal digits. Due to their extremely low probability of duplication, UUIDs are a widely adopted tool for giving persistent and unique identities to practically every type of all practical purpose.
How Unique is UUID?
From Wikipedia, the number of UUIDs generated to have at least 1 collision is 2.71 quintillion. At a rate of one per second, would take a billion years and is equivalent to generating around 1 billion UUIDs per second for about 85 years.
A collision is possible but the total number of unique keys generated is so large that the possibility of a collision is almost zero. So they are unique enough for practical purposes, taking into account the natural limitations of human and systems lifespans is low enough to be ignored.
UUID Versions Explained?
There are five different versions of UUID. Each version has different strengths and ways of creating and suiting different use cases.
- UUID Version 1 is sometimes referred to as “time-based” since they incorporate the DateTime at which they were generated. Simply speaking, it's a combination of the MAC address of the machine and the current timestamp in nanoseconds.
- UUID Version 2 is similar to Version 1 UUID but the part of DateTime information that would be in a v1 UUID is replaced by a local domain number. While this can be beneficial in some situations, it limits the uniqueness of the UUID and raises certain privacy concerns which result in version 2 UUID is not being widely used.
- UUID Version 3 is created based on “namespace” and “name” data rather than time-based and host-related values using message-digest algorithm (MD5) as the hashing algorithm to generate a UUID.
- UUID Version 4 is generated randomly. Its value is randomly generated from a cryptographically secure generator that does not contain any namespace, device, or time-based information.
- UUID Version 5 is generated the same way as version 3. However, it is generated using a Secure Hash Algorithm (SHA-1), which is a stronger hashing algorithm as opposed to MD5 which version 3 uses for hashing. Nowadays the MD5 hashing algorithm is considered vulnerable and easier to crack. However, SHA-1 is also facing the same security issue as MD5. So it's not recommended to use both UUID versions anymore.
Which UUID Version Should You Use?
The short answer is:
- If you want to generate a truly >random and unique UUID, then Version 4 is recommended over Version 1
- If you need a reproducible version of UUID, then Version 5 is recommended because Version 5 is more secure as it uses SHA-1 Hash while Version 3 uses MD5
What is the difference between UUID and GUID?
Universally Unique Identifier (UUID) and Globally Unique Identifier (GUID) is a 16 bytes (128bits) value that looks and serves a similar purpose of uniqueness. The only difference between the two is that, UUID is defined by RFC4122 while GUID is Microsoft's implementation of a the UUID.