URN - UUIDs (Universally Unique IDentifier) or GUIDs (Globally Unique IDentifier)
Table of Contents
1 - About
UUID (Universally Unique IDentifier) are identifier
An UUID is also known as GUIDs (Globally Unique IDentifier).
A UUID is an identifier that is unique across both :
- time (Every UUID has a timestamp)
- space (where the uuid is generated)
Since UUIDs are unique and persistent, you can use them as URNs (The string representation of a UUID is fully compatible with the URN syntax. ) See the example for an URN example
2 - Articles Related
3 - Example
- version 1 with the urn prefix (the 1 in 11d0)
urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6
- version 4 (the 4 in 4e2b)
27701bfc-78d0-4e2b-92ca-193cea53fa30
4 - Usage
- A UUID can be used for multiple purposes, from tagging objects with an extremely short lifetime, to reliably identifying very persistent objects across a network
- A UUID is generally used to uniquely identify some object or entity on the Internet.
5 - Pro/Cons
- Pro:
- An UUID does not need an central authority to be created
- A UUID is reasonably small. This lends itself well to sorting, ordering, and hashing of all sorts, storing in databases, simple allocation, and ease of programming in general.
- Cons:
- Since a UUID is a fixed size and contains a time field, it is possible for values to rollover (around A.D. 3400, depending on the specific algorithm used).
6 - Algorithm
Three algorithms are used to generate UUIDs and be sure that they are unique:
- from the unique values of 802 MAC addresses
- from pseudo-random number
- from cryptographic hashing and application-provided text strings
7 - Format
UUID is a fixed size of 16 octets and contains a time field.
To guarantee uniqueness, it's using
- a clock sequence to help avoid duplicates that could arise when the clock is set backwards in time or if the node ID changes.
- a node field that consists of an IEEE 802 MAC address, usually the host address. (it will change if the network card is replaced)
7.1 - EBNF
The format of an UUID is in EBNF syntax for bit.
UUID = time-low "-" time-mid "-" time-high-and-version "-" clock-seq-and-reserved clock-seq-low "-" node
time-low = 4hexOctet
time-mid = 2hexOctet
time-high-and-version = 2hexOctet
clock-seq-and-reserved = hexOctet
clock-seq-low = hexOctet
node = 6hexOctet
hexOctet = hexDigit hexDigit
hexDigit = "0" / "1" / "2" / "3" / "4" / "5" / "6" / "7" / "8" / "9" / "a" / "b" / "c" / "d" / "e" / "f" / "A" / "B" / "C" / "D" / "E" / "F"
7.2 - Variant and version (Type)
The real version of a UUID is given by:
- its variant field (a type) - The variant field contains a value which identifies the layout
- and its version (a subtype) - The version field holds a value that describes the type
where
- The variant field (type) consists of a variable number of the most significant bits of octet 8 of the UUID.
- The version number (sub-type) is in the most significant 4 bits of the time stamp (bits 4 through 7 of the time_hi_and_version field). It has the following meaning
- 1 - The time-based version
- 2 - DCE Security version, with embedded POSIX UIDs.
- 3 - The name-based version that uses MD5 hashing.
- 4 - The randomly or pseudo-randomly generated
- 5 - The name-based version that uses SHA-1 hashing.
7.3 - Version 4 (Random)
The version 4 UUID is the most used and is meant for generating UUIDs from truly-random or pseudo-random numbers. ref doc
Because it's not device scoped, it provides better privacy properties.
The chance of collision are so small that it can be ignored.
Furthermore, Google Analytics use it to generate the Anonymous Id on the mobile platform.
- the timestamp is a randomly or pseudo-randomly generated 60-bit value
- clock sequence is a randomly or pseudo-randomly generated 14-bit value
- the node field is a randomly or pseudo-randomly generated 48-bit
8 - Specification
9 - Implementation
9.1 - Javascript Code
From stackoverflow
function uuidv4() {
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
);
}
console.log(uuidv4());
9.2 - Uuid package
https://www.npmjs.com/package/uuid
Example:
<script src="https://wzrd.in/standalone/uuid%[email protected]"></script>
console.log(uuidv4());