A UUID (Universally Unique Identifier) is a 128-bit label used to identify information in computer systems. Defined by RFC 9562, UUIDs are designed to be globally unique without any central coordination authority. A UUID is represented as 32 hexadecimal digits grouped in the pattern 8-4-4-4-12, totalling 36 characters including hyphens.
UUID v4 is the simplest and most widely used UUID version. It uses a Cryptographically Secure Pseudo-Random Number Generator (CSPRNG) to fill 122 bits with random data. The remaining 6 bits are used for the version and variant markers.
Generate 128 random bits
The Web Crypto API (crypto.getRandomValues) generates 16 bytes of cryptographically secure random data. This is fundamentally different from Math.random() — it uses the operating system entropy pool.
Set version bits (position 49–52)
Bits 49–52 are set to 0100 (binary for 4). This sets the 13th hex character to 4 and is how you identify a v4 UUID.
Set variant bits (position 65–66)
Bits 65–66 are set to 10 (binary). This sets the 17th hex character to 8, 9, a, or b — the RFC 9562 variant marker.
Format as hexadecimal string
The 128 bits are converted to 32 lowercase hex characters and formatted with hyphens in the 8-4-4-4-12 pattern, producing the final UUID string.
UUID v7 is the recommended UUID version for database primary keys. It encodes a Unix millisecond timestamp in the first 48 bits, making UUIDs sortable by creation time.
Get Unix timestamp in milliseconds
Date.now() returns the current time as milliseconds since January 1, 1970. This is converted to a 48-bit integer and placed in the first 48 bits of the UUID.
Set version bits
Bits 49–52 are set to 0111 (7), making the 13th hex character 7.
Fill remaining bits with random data
The remaining 74 bits are filled with cryptographically secure random data, providing randomness within the same millisecond.
Set variant bits and format
Bits 65–66 are set to 10 (RFC 9562 variant). The result is formatted as a standard UUID string — but one that sorts lexicographically in time order.
UUID v5 is fundamentally different from v4 and v7: it is not random. Given the same inputs, it always produces the same output.
Choose a namespace
RFC 9562 defines four standard namespaces: DNS, URL, OID, and X500. Each namespace is itself a UUID. You can also define custom namespaces for your application's own naming hierarchy.
Concatenate namespace bytes + name bytes
The namespace UUID (16 bytes) and the name string (UTF-8 encoded) are concatenated into a single byte sequence. The name can be any string — a domain name, URL, email address, or any identifier.
Compute SHA-1 hash
SHA-1 is computed on the concatenated bytes, producing a 160-bit (20 byte) hash. The first 128 bits of this hash form the base of the UUID.
Set version and variant bits, format
Bits 49–52 are set to 0101 (5) and bits 65–66 are set to 10. The result is formatted as a UUID string — identical every time for the same namespace and name.
UUID v1 is the original time-based UUID, using a complex timestamp format and the MAC address of the generating machine.
Get timestamp in 100ns intervals since Oct 15, 1582
The UUID v1 timestamp counts 100-nanosecond intervals since the start of the Gregorian calendar reform. This is split across three fields: time-low (32 bits), time-mid (16 bits), and time-high (12 bits).
Generate clock sequence
A 14-bit clock sequence is maintained to prevent duplicates when the system clock is adjusted backwards. It is initialised randomly and incremented on each UUID generation within the same timestamp.
Add node (MAC address or random)
The original spec uses the MAC address of the network card as a 48-bit node identifier. UUIDCore uses a random node for privacy — exposing real MAC addresses is a security risk.
| Feature | v4 Random | v7 Sortable | v5 Name-based | v1 Time-based |
|---|---|---|---|---|
| Generation method | Random | Timestamp + random | SHA-1 hash | Timestamp + MAC |
| Time-ordered | ✗ | ✓ | ✗ | ~ |
| Deterministic | ✗ | ✗ | ✓ | ✗ |
| DB index friendly | ✗ | ✓ | ✗ | ~ |
| Timestamp extractable | ✗ | ✓ | ✗ | ✓ |
| Privacy safe | ✓ | ✓ | ✓ | ~ |
| RFC 9562 recommended | ✓ | ✓ New systems | ✓ | Legacy only |
| Best use case | General purpose | DB primary keys | Content addressing | Legacy/Cassandra |