uuid
The UUID library, generates, parses and formats RFC 9562 UUIDs.
All generators return the canonical form: 36 lower-case characters, xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx, where M is the version digit and N is the variant digit (8, 9, a or b).
Which one to use:
uuid.v7- time-ordered, the default choice for record identifiers and anything stored or sorted.uuid.v4- fully random, when the creation time must not be visible.uuid.v5/uuid.v3- deterministic, the same namespace and name always give the same UUID.uuid.v8- custom layout, the caller provides the bytes.uuid.unique- not a UUID, the fastest process-unique identifier for cache files and temporary keys.
Random parts come from math.random, which is not cryptographically secure: the generated values are unique, but can be predicted, so never use them as secrets (session tokens, password reset links, API keys), use crypto.random.bytes for those.
Fields
| Name | Type | Description |
|---|---|---|
NAMESPACE_DNS | string | The RFC 9562 namespace for fully qualified domain names, for use with uuid.v3 and uuid.v5.
```lua
uuid.v5( uuid.NAMESPACE_DNS, "www.example.com" ) -- "2ed6657d-e927-568b-95e1-2665a8aea6a2"
``` = "6ba7b810-9dad-11d1-80b4-00c04fd430c8" |
NAMESPACE_URL | string | The RFC 9562 namespace for URLs, for use with uuid.v3 and uuid.v5. = "6ba7b811-9dad-11d1-80b4-00c04fd430c8" |
NAMESPACE_OID | string | The RFC 9562 namespace for ISO object identifiers (OIDs), for use with uuid.v3 and uuid.v5. = "6ba7b812-9dad-11d1-80b4-00c04fd430c8" |
NAMESPACE_X500 | string | The RFC 9562 namespace for X.500 distinguished names (DER or text), for use with uuid.v3 and uuid.v5. = "6ba7b814-9dad-11d1-80b4-00c04fd430c8" |
Functions
uuid.parse | Parses a UUID string into its 16 raw bytes (big-endian, as stored on the wire). |
uuid.toString | Formats 16 raw bytes as a canonical (8-4-4-4-12, lower-case) UUID string, the inverse of uuid.parse. |
uuid.unique | Returns a fast, process-unique identifier formatted as a UUID version 8. |
uuid.v3 | Generates a UUID version 3 (name-based, MD5). |
uuid.v4 | Generates a UUID version 4 (random). |
uuid.v5 | Generates a UUID version 5 (name-based, SHA-1). |
uuid.v7 | Generates a UUID version 7 (time-ordered). |
uuid.v8 | Returns a UUID version 8 (custom) string from the given bytes. |
#uuid.parse
function uuid.parse(uuid_str: string): string | nil
Parses a UUID string into its 16 raw bytes (big-endian, as stored on the wire).
Accepts the canonical (8-4-4-4-12) and the plain (32 hex digits) forms, in any letter case. Braces, URNs (urn:uuid:...) and surrounding whitespace are rejected.
Any version is accepted, so it also works as a validator.
uuid.parse( "6ba7b810-9dad-11d1-80b4-00c04fd430c8" ) -- "\x6B\xA7\xB8\x10..." (16 bytes)
uuid.parse( "6BA7B8109DAD11D180B400C04FD430C8" ) -- same bytes
uuid.parse( "not a uuid" ) -- nil| Name | Type | Description |
|---|---|---|
uuid_str | string | The UUID string to parse. |
| Type | Name | Description |
|---|---|---|
string | nil | uuid_bytes | The 16 raw bytes, or nil if the string is not a valid UUID. |
#uuid.toString
function uuid.toString(uuid_bytes: string): string
Formats 16 raw bytes as a canonical (8-4-4-4-12, lower-case) UUID string, the inverse of uuid.parse.
The bytes are used as-is, version and variant bits are not touched.
uuid.toString( uuid.parse( "6BA7B810-9DAD-11D1-80B4-00C04FD430C8" ) ) -- "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
uuid.toString( string.rep( "\0", 16 ) ) -- "00000000-0000-0000-0000-000000000000"| Name | Type | Description |
|---|---|---|
uuid_bytes | string | The 16 raw bytes, errors on any other length. |
| Type | Name | Description |
|---|---|---|
string | uuid_str | The UUID string. |
#uuid.unique
function uuid.unique(is_unique?: ( fun( uuid_str: string ): boolean )?): string
Returns a fast, process-unique identifier formatted as a UUID version 8.
Layout: ssssssss-rrrr-8rrr-Nrrr-rrrrcccccccc
s- UNIX time in seconds when the prefix was rolled (library load).N,r- variant and 58 random bits, rolled together withs.c- 32-bit counter, increments on every call, the prefix is re-rolled when it overflows.
Identifiers never repeat within the process, sort in creation order and practically never collide between sessions or machines. They are trivially predictable, never use them as secrets.
Use it for cache file names, temporary keys and similar, prefer uuid.v7 for record identifiers.
When is_unique is given, identifiers are generated until it returns true, e.g. to skip names already taken by files from previous sessions. A callback that never returns true loops forever.
uuid.unique() -- e.g. "6902aa80-6ae3-8ad8-9f21-4c7b00000000"
uuid.unique() -- e.g. "6902aa80-6ae3-8ad8-9f21-4c7b00000001"
uuid.unique( function( uuid_str )
return not File.exists( "/garrysmod/data/cache/" .. uuid_str .. ".dat" )
end )| Name | Type | Description |
|---|---|---|
is_uniqueoptional | ( fun( uuid_str: string ): boolean )? | Optional check, called with each candidate until it returns true. |
| Type | Name | Description |
|---|---|---|
string | uuid_str | A UUID v8 string. |
#uuid.v3
function uuid.v3(namespace: string, name: string): string
Generates a UUID version 3 (name-based, MD5).
The UUID is the MD5 hash of the namespace bytes followed by the name, so the same namespace and name always give the same UUID, on any machine.
Prefer uuid.v5 for new identifiers, v3 exists for compatibility with systems that already use it.
uuid.v3( uuid.NAMESPACE_DNS, "www.example.com" ) -- "5df41881-3aed-3515-88a7-2f4a814cf09e"| Name | Type | Description |
|---|---|---|
namespace | string | The namespace UUID string (see uuid.NAMESPACE_*), or its 16 raw bytes from uuid.parse, errors if invalid. |
name | string | The name to hash within the namespace. |
| Type | Name | Description |
|---|---|---|
string | uuid_str | A UUID v3 string. |
#uuid.v4
function uuid.v4(): string
Generates a UUID version 4 (random).
122 of the 128 bits are random, the remaining 6 hold the version and the variant. Unlike uuid.v7, it reveals nothing about when it was created, but it does not sort by creation time and scatters database index writes.
The bits come from math.random, unique in practice, but predictable, never use it as a secret, use crypto.random.bytes for secrets.
uuid.v4() -- e.g. "3f2b8c1e-9a4d-4e7f-b215-0c6d8e9fa123"| Type | Name | Description |
|---|---|---|
string | uuid_str | A UUID v4 string. |
#uuid.v5
function uuid.v5(namespace: string, name: string): string
Generates a UUID version 5 (name-based, SHA-1).
The UUID is the first 16 bytes of the SHA-1 hash of the namespace bytes followed by the name, so the same namespace and name always give the same UUID, on any machine.
Useful for stable identifiers derived from existing keys, e.g. a UUID per SteamID or per file path. Anyone who knows the namespace and the name can compute the UUID, so it is not a secret.
local NAMESPACE_PLAYERS = uuid.parse( "a6c3c5f0-2b1e-4c7d-9f00-6d3e5b2a1c90" ) -- your own namespace, parsed once
uuid.v5( uuid.NAMESPACE_DNS, "www.example.com" ) -- "2ed6657d-e927-568b-95e1-2665a8aea6a2"
uuid.v5( NAMESPACE_PLAYERS, "STEAM_0:1:12345" ) -- same result every time| Name | Type | Description |
|---|---|---|
namespace | string | The namespace UUID string (see uuid.NAMESPACE_*), or its 16 raw bytes from uuid.parse, errors if invalid. |
name | string | The name to hash within the namespace. |
| Type | Name | Description |
|---|---|---|
string | uuid_str | A UUID v5 string. |
#uuid.v7
function uuid.v7(timestamp?: integer): string
Generates a UUID version 7 (time-ordered).
The first 48 bits are the UNIX time in milliseconds, so UUIDs sort by creation time both as strings and as bytes, which keeps database indexes compact. The default choice for record identifiers.
Layout: tttttttt-tttt-7sss-Nrrr-rrrrrrrrrrrr
t- 48-bit timestamp in milliseconds.s- 12 bits, without atimestampthe sub-millisecond fraction of the current time (RFC 9562, section 6.2, method 3), bumped when needed, so UUIDs generated by this process are strictly increasing, even within the same millisecond or when the clock goes backwards. With atimestampthey are random.N,r- variant and 62 random bits frommath.random.
The creation time can be read back from the UUID, use uuid.v4 when that must stay hidden.
uuid.v7() -- e.g. "019a326a-047b-7801-9c4f-5a6e0b1d2c3f"
uuid.v7( 1761782400123 ) -- "019a326a-047b-7xxx-..." (random tail)| Name | Type | Description |
|---|---|---|
timestampoptional | integer | The UNIX timestamp in milliseconds (0..2^48 - 1), defaults to the current time. |
| Type | Name | Description |
|---|---|---|
string | uuid_str | A UUID v7 string. |
#uuid.v8
function uuid.v8(uint8_1?: integer, uint8_2?: integer, uint8_3?: integer, uint8_4?: integer, uint8_5?: integer, uint8_6?: integer, uint8_7?: integer, uint8_8?: integer, uint8_9?: integer, uint8_10?: integer, uint8_11?: integer, uint8_12?: integer, uint8_13?: integer, uint8_14?: integer, uint8_15?: integer, uint8_16?: integer): string
Returns a UUID version 8 (custom) string from the given bytes.
UUID v8 carries application-defined data, only the version and variant bits are fixed, so 122 bits are free: the low 4 bits of uint8_7, the low 6 bits of uint8_9 and all other bytes.
Missing bytes default to 0, out of range values are wrapped to their allowed bits.
uuid.v8() -- "00000000-0000-8000-8000-000000000000"
uuid.v8( 0xDE, 0xAD, 0xBE, 0xEF ) -- "deadbeef-0000-8000-8000-000000000000"| Name | Type | Description |
|---|---|---|
uint8_1optional | integer | Unsigned byte (0..255). |
uint8_2optional | integer | Unsigned byte (0..255). |
uint8_3optional | integer | Unsigned byte (0..255). |
uint8_4optional | integer | Unsigned byte (0..255). |
uint8_5optional | integer | Unsigned byte (0..255). |
uint8_6optional | integer | Unsigned byte (0..255). |
uint8_7optional | integer | Low 4 bits (0..15), the high 4 bits hold the version. |
uint8_8optional | integer | Unsigned byte (0..255). |
uint8_9optional | integer | Low 6 bits (0..63), the high 2 bits hold the variant. |
uint8_10optional | integer | Unsigned byte (0..255). |
uint8_11optional | integer | Unsigned byte (0..255). |
uint8_12optional | integer | Unsigned byte (0..255). |
uint8_13optional | integer | Unsigned byte (0..255). |
uint8_14optional | integer | Unsigned byte (0..255). |
uint8_15optional | integer | Unsigned byte (0..255). |
uint8_16optional | integer | Unsigned byte (0..255). |
| Type | Name | Description |
|---|---|---|
string | uuid_str | A UUID v8 string. |