4. Specification
4.1. Format
The UUID format is 16 octets; some bits of the eight octet variant field specified below determine finer structure.
4.1.1. Variant
The variant field determines the layout of the UUID. That is, the interpretation of all other bits in the UUID depends on the setting of the bits in the variant field. The variant field consists of a variable number of the most significant bits of octet 8 of the UUID.
The following table lists the contents of the variant field, where the letter "x" indicates a "don't-care" value.
| Msb0 | Msb1 | Msb2 | Description |
|---|---|---|---|
| 0 | x | x | Reserved, NCS backward compatibility |
| 1 | 0 | x | The variant specified in this document |
| 1 | 1 | 0 | Reserved, Microsoft Corporation backward compatibility |
| 1 | 1 | 1 | Reserved for future definition |
4.1.2. Layout and Byte Order
To minimize confusion about bit assignments within octets, the UUID record definition is defined only in terms of fields that are integral numbers of octets. The fields are presented with the most significant one first.
| Field | Data Type | Octet # | Note |
|---|---|---|---|
| time_low | unsigned 32 bit integer | 0-3 | The low field of the timestamp |
| time_mid | unsigned 16 bit integer | 4-5 | The middle field of the timestamp |
| time_hi_and_version | unsigned 16 bit integer | 6-7 | The high field of the timestamp multiplexed with the version number |
| clock_seq_hi_and_reserved | unsigned 8 bit integer | 8 | The high field of the clock sequence multiplexed with the variant |
| clock_seq_low | unsigned 8 bit integer | 9 | The low field of the clock sequence |
| node | unsigned 48 bit integer | 10-15 | The spatially unique node identifier |
UUID Structure (128 bits):
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| time_low |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| time_mid | time_hi_and_version |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|clk_seq_hi_res | clk_seq_low | node (0-1) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| node (2-5) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
4.1.3. Version
The version number is in the most significant 4 bits of the time stamp (bits 4 through 7 of the time_hi_and_version field).
The following table lists the currently-defined versions for this UUID variant:
| Msb0-3 | Version | Description |
|---|---|---|
| 0001 | 1 | The time-based version specified in this document |
| 0010 | 2 | DCE Security version, with embedded POSIX UIDs |
| 0011 | 3 | The name-based version specified in this document that uses MD5 hashing |
| 0100 | 4 | The randomly or pseudo-randomly generated version specified in this document |
| 0101 | 5 | The name-based version specified in this document that uses SHA-1 hashing |
4.1.4. Timestamp
The timestamp is a 60-bit value. For UUID version 1, this is represented by Coordinated Universal Time (UTC) as a count of 100-nanosecond intervals since 00:00:00.00, 15 October 1582 (the date of Gregorian reform to the Christian calendar).
For UUID version 3 or 5, the timestamp is a 60-bit value constructed from a name as described in Section 4.3.
For UUID version 4, the timestamp is a randomly or pseudo-randomly generated 60-bit value, as described in Section 4.4.
4.1.5. Clock Sequence
For UUID version 1, the clock sequence is used to help avoid duplicates that could arise when the clock is set backwards in time or if the node ID changes.
The clock sequence MUST be originally (i.e., once in the lifetime of a system) initialized to a random number to minimize the correlation across systems.
For UUID version 3 or 5, the clock sequence is a 14-bit value constructed from a name as described in Section 4.3.
For UUID version 4, clock sequence is a randomly or pseudo-randomly generated 14-bit value.
4.1.6. Node
For UUID version 1, the node field consists of an IEEE 802 MAC address, usually the host address. For systems with multiple IEEE 802 addresses, any available one can be used.
For UUID version 3 or 5, the node field is a 48-bit value constructed from a name as described in Section 4.3.
For UUID version 4, the node field is a randomly or pseudo-randomly generated 48-bit value.
4.1.7. Nil UUID
The nil UUID is special form of UUID that is specified to have all 128 bits set to zero:
00000000-0000-0000-0000-000000000000
4.2. Algorithms for Creating a Time-Based UUID
4.2.1. Basic Algorithm
The basic algorithm for generating a time-based UUID:
- Obtain a system-wide global lock
- Read the current time
- Get the current node ID
- If the node ID has changed since the last UUID was generated, set the clock sequence to a random value
- If the current time is before the last timestamp, increment the clock sequence
- Save the timestamp and node ID
- Release the global lock
- Format the UUID using the timestamp, clock sequence, and node ID
4.2.2. Generation Details
Timestamp: The timestamp is a 60-bit value constructed from the system time. The timestamp resolution is 100 nanoseconds.
Clock Sequence: The clock sequence is initially set to a random value. It is incremented each time the clock moves backward or the node ID changes.
Node ID: The node ID is typically an IEEE 802 MAC address.
4.3. Algorithm for Creating a Name-Based UUID
The algorithm for creating a name-based UUID (version 3 or version 5):
- Allocate a UUID to use as a "name space ID" for all UUIDs generated from names in that name space
- Choose either MD5 (version 3) or SHA-1 (version 5) as the hash algorithm
- Convert the name to a canonical sequence of octets
- Compute the hash of the name space ID concatenated with the name
- Set octets zero through 15 to octets zero through fifteen of the hash
- Set the four most significant bits (bits 12 through 15) of octet 6 to the appropriate version number
- Set the two most significant bits (bits 6 and 7) of octet 8 to zero and one, respectively
4.4. Algorithms for Creating a UUID from Truly Random or Pseudo-Random Numbers
The version 4 UUID is meant for generating UUIDs from truly random or pseudo-random numbers.
The algorithm is as follows:
- Set all the bits to randomly (or pseudo-randomly) chosen values
- Set the four most significant bits (bits 12 through 15) of octet 6 to the 4-bit version number (0100 binary)
- Set the two most significant bits (bits 6 and 7) of octet 8 to zero and one, respectively
4.5. Node IDs that Do Not Identify the Host
This section describes alternatives for generating node IDs that do not identify the host:
-
Random Node ID: Set the least significant bit of the first octet of the node ID to one (multicast bit). This marks the UUID as not based on a real MAC address.
-
Persistent Random Node ID: Generate a random node ID once and store it for reuse. This provides consistency across reboots while maintaining privacy.
Note: For detailed implementation examples and complete algorithms, please refer to the official RFC 4122 specification and Appendix A.