---
name: "uuid"
section: "3ossp"
platform: "linux"
package: "libossp-uuid-dev"
version: "1.6.4-1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/uuid/3ossp"
---

# uuid(3ossp)

Universally Unique IDentifier

## NAME

**uuid** – Universally Unique IDentifier

## SYNOPSIS

**#include \<uuid.h\>**

**cc** $(**pkg-config** **--libs** **--cflags** *ossp-uuid*) ...

## DESCRIPTION

OSSP **uuid** is a ISO-C:1999 application programming interface (API) and corresponding command line interface (CLI) for the generation of RFC 9562, ISO/IEC 11578:1996 and IETF RFC-4122 compliant *Universally* *Unique* *Identifier* (UUID). It supports DCE 1.1 variant UUIDs of version 1 (time and node based), version 3 (name based, MD5), version 4 (random number based) and version 5 (name based, SHA-1). Additional API bindings are provided for the Perl:5 language. Optional backward compatibility exists for the ISO-C DCE-1.1 and Perl Data::UUID APIs.

UUIDs are 128 bit numbers which are intended to have a high likelihood of uniqueness over space and time and are computationally difficult to guess. They are globally unique identifiers which can be locally generated without contacting a global registration authority. UUIDs are intended as unique identifiers for both mass tagging objects with an extremely short lifetime and to reliably identifying very persistent objects across a network.

This is the ISO-C application programming interface (API) of OSSP **uuid**.

### UUID Binary Representation

According to RFC 9562, ISO/IEC 11578:1996 and IETF RFC-4122 standards, a DCE 1.1 variant UUID is a 128 bit number defined out of 7 fields, each field a multiple of an octet in size and stored in network byte order:

```
                                                   [4]
                                                  version
                                                -->|  |<--
                                                   |  |
                                                   |  |  [16]
               [32]                      [16]      |  |time_hi
             time_low                  time_mid    | _and_version
   |<---------------------------->||<------------>||<------------>|
   | MSB                          ||              ||  |           |
   | /                            ||              ||  |           |
   |/                             ||              ||  |           |
   +------++------++------++------++------++------++------++------+~~
   |  15  ||  14  ||  13  ||  12  ||  11  ||  10  |####9  ||   8  |
   | MSO  ||      ||      ||      ||      ||      |####   ||      |
   +------++------++------++------++------++------++------++------+~~
   7654321076543210765432107654321076543210765432107654321076543210
 ~~+------++------++------++------++------++------++------++------+
   ##* 7  ||   6  ||   5  ||   4  ||   3  ||   2  ||   1  ||   0  |
   ##*    ||      ||      ||      ||      ||      ||      ||  LSO |
 ~~+------++------++------++------++------++------++------++------+
   7654321076543210765432107654321076543210765432107654321076543210
   | |    ||      ||                                             /|
   | |    ||      ||                                            / |
   | |    ||      ||                                          LSB |
   |<---->||<---->||<-------------------------------------------->|
   |clk_seq clk_seq                      node
   |_hi_res _low                         [48]
   |[5-6]    [8]
   | |
-->| |<--
 variant
  [2-3]
```

An example of a UUID binary representation is the octet stream 0xF8 0x1D 0x4F 0xAE 0x7D 0xEC 0x11 0xD0 0xA7 0x65 0x00 0xA0 0xC9 0x1E 0x6B 0xF6. The binary representation format is exactly what the OSSP **uuid** API functions **uuid\_import**() and **uuid\_export**() deal with under UUID\_FMT\_BIN.

### UUID ASCII String Representation

According to RFC 9562, ISO/IEC 11578:1996 and IETF RFC-4122 standards, a DCE 1.1 variant UUID is represented as an ASCII string consisting of 8 hexadecimal digits followed by a hyphen, then three groups of 4 hexadecimal digits each followed by a hyphen, then 12 hexadecimal digits. Formally, the string representation is defined by the following grammar:

```
uuid                        = <time_low> "-"
                              <time_mid> "-"
                              <time_high_and_version> "-"
                              <clock_seq_high_and_reserved>
                              <clock_seq_low> "-"
                              <node>
time_low                    = 4*<hex_octet>
time_mid                    = 2*<hex_octet>
time_high_and_version       = 2*<hex_octet>
clock_seq_high_and_reserved = <hex_octet>
clock_seq_low               = <hex_octet>
node                        = 6*<hex_octet>
hex_octet                   = <hex_digit> <hex_digit>
hex_digit                   = "0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9"
                            |"a"|"b"|"c"|"d"|"e"|"f"
                            |"A"|"B"|"C"|"D"|"E"|"F"
```

An example of a UUID string representation is the ASCII string "f81d4fae-7dec-11d0-a765-00a0c91e6bf6". The string representation format is exactly what the OSSP **uuid** API functions **uuid\_import**() and **uuid\_export**() deal with under UUID\_FMT\_STR.

Notice: a corresponding URL can be generated out of a ASCII string representation of an UUID by prefixing with **urn:uuid:** as in "urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6".

### UUID Single Integer Value Representation

According to the ISO/IEC 11578:1996 and ITU-T Rec. X.667 standards, a DCE 1.1 variant UUID can be also represented as a single integer value consisting of a decimal number with up to 39 digits.

An example of a UUID single integer value representation is the decimal number "329800735698586629295641978511506172918". The string representation format is exactly what the  OSSP **uuid** API functions **uuid\_import**() and **uuid\_export**() deal with under UUID\_FMT\_SIV.

Notice: a corresponding ISO OID can be generated under the "{joint-iso-itu-t(2) uuid(25)}" arc out of a single integer value representation of a UUID by prefixing with **2.25.**. An example OID is "2.25.329800735698586629295641978511506172918". Additionally, an URL can be generated by further prefixing with **urn:oid:** as in "urn:oid:2.25.329800735698586629295641978511506172918".

### UUID Variants and Versions

A UUID has a variant and version. The variant defines the layout of the UUID. The version defines the content of the UUID. The UUID variant supported in OSSP **uuid** is DCE 1.1 variant only. DCE 1.1 UUID variant versions supported in OSSP **uuid** are:

- Version **1** (time and node based)

  These are the classical UUIDs, created out of a 60-bit system time, a 14-bit local clock sequence and 48-bit system MAC address. The MAC address can be either the real one of a physical network interface card (NIC) or a random multi-cast MAC address. Version 1 UUIDs are usually used as one-time global unique identifiers.

- Version **3** (name based, MD5)

  These are UUIDs which are based on the 128-bit MD5 message digest of the concatenation of a 128-bit namespace UUID and a name string of arbitrary length. Version 3 UUIDs are usually used for non-unique but repeatable message digest identifiers.

- Version **4** (random data based)

  These are UUIDs which are based on just 128-bit of random data. Version 4 UUIDs are usually used as one-time local unique identifiers.

- Version **5** (name based, SHA-1)

  These are UUIDs which are based on the 160-bit SHA-1 message digest of the concatenation of a 128-bit namespace UUID and a name string of arbitrary length. Version 5 UUIDs are usually used for non-unique but repeatable message digest identifiers.

- Version **5** (name based, SHA-1)

  These are UUIDs which are based on the 160-bit SHA-1 message digest of the concatenation of a 128-bit namespace UUID and a name string of arbitrary length. Version 5 UUIDs are usually used for non-unique but repeatable message digest identifiers.

- Version **6** (time (reverse) and node based)

  These are compatible with and very similar to Version **1** UUIDs, except the time is stored from the most significant parts to the least significant parts. This improves locality in some database implementations.

- Version **7** (UNIX time based)

  These UUIDs consist of a 48-bit UNIX time (with 1ms precision) in big-endian, then 10 bytes of random data. RFC 9562 *recommends* these for new designs.

### UUID Uniqueness

Version **1**/**6** UUIDs are guaranteed to be unique through combinations of hardware addresses, time stamps and random seeds. There is a reference in the UUID to the hardware (MAC) address of the first network interface card (NIC) on the host which generated the UUID — this reference is intended to ensure the UUID will be unique in space as the MAC address of every network card is assigned by a single global authority (IEEE) and is guaranteed to be unique. The next component in a UUID is a timestamp which, as clock always (should) move forward, will be unique in time. Just in case some part of the above goes wrong (the hardware address cannot be determined or the clock moved steps backward), there is a random clock sequence component placed into the UUID as a "catch-all" for uniqueness.

Version **3**/**5** UUIDs are guaranteed to be inherently globally unique if the combination of namespace and name used to generate them is unique.

Version **4** UUIDs are not guaranteed to be globally unique, because they are generated out of locally gathered random numbers only. Nevertheless there is still a high likelihood of uniqueness over space and time and that they are computationally difficult to guess.

Version **7** UUIDs attempt to guarantee uniqueness by subdividing each millisecond of real time into 2 sup 80 ( 10 sup 24 ) 2^80 (10^24) possible values.

### Nil UUID

There is a special *Nil* UUID consisting of all octets set to zero in the binary representation. It can be used as a special UUID value which does not conflict with real UUIDs.

### Max UUID

Similarly to the *Nil* UUID, the *Max* UUID consists of all octets set to 0xFF (255, all bits set) in the binary representation. It can be used as a special UUID value which does not conflict with real UUIDs, but sorts after everything else.

## APPLICATION PROGRAMMING INTERFACE

The ISO-C Application Programming Interface (API) of OSSP **uuid** consists of the following components.

### CONSTANTS

The following constants are provided:

- UUID\_VERSION

  The hexadecimal encoded OSSP **uuid** version. This allows compile-time checking of the OSSP **uuid** version. For run-time checking use **uuid\_version**() instead.

  The current value 0x106204 encodes 1.6.4.

- UUID\_LEN\_BIN, UUID\_LEN\_STR, UUID\_LEN\_SIV

  The number of octets of the UUID binary and string representations. Notice that the lengths of the string representation (UUID\_LEN\_STR) and the lengths of the single integer value representation (UUID\_LEN\_SIV) does *not* include the necessary NUL termination character.

- UUID\_MAKE\_V1, UUID\_MAKE\_V3, UUID\_MAKE\_V4, UUID\_MAKE\_V5, UUID\_MAKE\_V6, UUID\_MAKE\_V7, UUID\_MAKE\_MC

  The *mode* bits for use with **uuid\_make**(). UUID\_MAKE\_V*n* specify which UUID version to generate. UUID\_MAKE\_MC forces the use of a random multi-cast MAC address instead of the real physical MAC address in version 1 and 6 UUIDs.

- UUID\_RC\_OK, UUID\_RC\_ARG, UUID\_RC\_MEM, UUID\_RC\_SYS, UUID\_RC\_INT, UUID\_RC\_IMP

  The possible numerical return-codes of API functions. The UUID\_RC\_OK indicates success, the others indicate errors. Use **uuid\_error**() to translate them into string versions.

- UUID\_FMT\_BIN, UID\_FMT\_STR, UID\_FMT\_SIV, UID\_FMT\_TXT

  The *fmt* formats for use with **uuid\_import**() and **uuid\_export**():

  - UUID\_FMT\_BIN

    indicates the UUID binary representation (of length UUID\_LEN\_BIN),

  - UUID\_FMT\_STR

    indicates the UUID string representation (of length UUID\_LEN\_STR),

  - UUID\_FMT\_SIV

    indicates the UUID single integer value representation (of maximum length UUID\_LEN\_SIV), and

  - UUID\_FMT\_TXT

    indicates the textual description (of arbitrary length) of a UUID (this powers [uuid(1)](/uuid/1.md) **-d**).

### FUNCTIONS

-

  *uuid\_rc\_t* **uuid\_create**(*uuid\_t \*\*uuid*) Create a new UUID object and store a pointer to it in *\*uuid*. A UUID object consists of an internal representation of a UUID, potential internal RNG context, and timestamp information. The initial UUID is the *Nil* UUID.

-

  *uuid\_rc\_t* **uuid\_destroy**(*uuid\_t \*uuid*) Destroy UUID object *uuid*.

-

  *uuid\_rc\_t* **uuid\_clone**(*const uuid\_t \*uuid*, *uuid\_t \*\*uuid\_clone*) Clone UUID object *uuid* and store new UUID object in *\*uuid\_clone*.

-

  *uuid\_rc\_t* **uuid\_isnil**(*const uuid\_t \*uuid*, *int \*result*) Checks whether the UUID in uuid is the *Nil* UUID. If this is the case, it returns **1** in *\*result*. Else it returns **false** in *\*result*.

-

  *uuid\_rc\_t* **uuid\_ismax**(*const uuid\_t \*uuid*, *int \*result*) Checks whether the UUID in uuid is the *Max* UUID. If this is the case, it returns **1** in *\*result*. Else it returns **false** in *\*result*.

-

  *uuid\_rc\_t* **uuid\_compare**(*const uuid\_t \*uuid*, *const uuid\_t \*uuid2*, *int \*result*) Compares the order of the two UUIDs and returns the result in *\*result*: **-1** if *uuid* \< *uuid2*, **0** if *uuid* = *uuid2*, and **+1** if *uuid* \> *uuid2*.

-

  *uuid\_rc\_t* **uuid\_import**(*uuid\_t \*uuid*, *uuid\_fmt\_t fmt*, *const void \*data\_ptr*, *size\_t data\_len*) Imports *uuid* from an external representation of format *fmt*. The data is read from the buffer at *data\_ptr* which contains at least *data\_len* bytes.

  The format of the external representation is specified by *fmt* and the minimum expected length in *data\_len* depends on it. Valid values for *fmt* are UUID\_FMT\_BIN, UUID\_FMT\_STR, and UUID\_FMT\_SIV.

-

  *uuid\_rc\_t* **uuid\_export**(*const uuid\_t \*uuid*, *uuid\_fmt\_t fmt*, *void \*data\_ptr*, *size\_t \*data\_len*) Exports *uuid* into an external representation of format *fmt*. Valid values for *fmt* are UUID\_FMT\_BIN, UUID\_FMT\_STR, UUID\_FMT\_SIV, and UUID\_FMT\_TXT.

  The data is written to the buffer located at *\*(type \*\*)data\_ptr*, where *type* is *uint8\_t* for UUID\_FMT\_BIN and *char* for other formats.

  The buffer has to have room for at least *\*data\_len* bytes. If *\*(type \*\*)data\_ptr* is NULL, *data\_len* is ignored as input and a new buffer is allocated with [malloc(3)](/malloc/3.md) and returned in *\*(type \*\*)data\_ptr*.

  If *data\_len* is not NULL, the number of available bytes in the buffer has to be provided in *\*data\_len*. The number of actually written bytes are returned in *\*data\_len* again. The minimum required buffer length depends on the external representation as specified by *fmt* (see above). For UUID\_FMT\_TXT, a buffer of unspecified length is required, and hence it is recommended to allow OSSP **uuid** to allocate the buffer as necessary.

-

  *uuid\_rc\_t* **uuid\_load**(*uuid\_t \*uuid*, *const char \*name*) Loads a pre-defined UUID value into *uuid*. The following *name* arguments are currently known:

  - **nil**

    00000000-0000-0000-0000-000000000000

  - **max**

    ffffffff-ffff-ffff-ffff-ffffffffffff

  - **ns:DNS**

    6ba7b810-9dad-11d1-80b4-00c04fd430c8

  - **ns:URL**

    6ba7b811-9dad-11d1-80b4-00c04fd430c8

  - **ns:OID**

    6ba7b812-9dad-11d1-80b4-00c04fd430c8

  - **ns:X500**

    6ba7b814-9dad-11d1-80b4-00c04fd430c8

    **ns:***XXX* are names of pre-defined name-space UUIDs for use in the generation of DCE 1.1 version 3 and version 5 UUIDs.

-

  *uuid\_rc\_t* **uuid\_make**(*uuid\_t \*uuid*, *unsigned int mode*, *...*) Generates a new UUID in *uuid* according to *mode* and optional arguments (dependent on *mode*).

  If *mode* contains the UUID\_MAKE\_V1 or UUID\_MAKE\_V6 bit, a DCE 1.1 variant UUID of version **1** or **6** is generated. Then optionally the bit UUID\_MAKE\_MC forces the use of random multi-cast MAC address instead of the real physical MAC address (the default). The UUID is generated out of the 60-bit current system time, a 12-bit clock sequence (initialised to random, then incremental in v**1**; always random in v**6**) and the 48-bit MAC address.

  If *mode* contains the UUID\_MAKE\_V3 or UUID\_MAKE\_V5 bit, a DCE 1.1 variant UUID of version **3** or **5** is generated and two additional arguments are expected: first, a namespace UUID object (*uuid\_t \**). Second, a name string of arbitrary length (*const char \**). The UUID is generated out of the 128-bit MD5 or 160-bit SHA-1 from the concatenated octet stream of namespace UUID and name string.

  If *mode* contains the UUID\_MAKE\_V4 bit, a DCE 1.1 variant UUID of version 4 is generated. The UUID is generated out of 128-bit random data.

  If *mode* contains the UUID\_MAKE\_V7 bit, a DCE 1.1 variant UUID of version 7 is generated. The UUID is generated out of 48 bits of time and 80 bits of random data.

-

  *char \** **uuid\_error**(*uuid\_rc\_t rc*) Returns a constant string representation corresponding to the return-code *rc* for use in displaying OSSP **uuid** errors.

-

  *unsigned* *long* **uuid\_version**() Returns the hexadecimal encoded OSSP **uuid** version as compiled into the library object files. This allows run-time checking of the OSSP **version**. For compile-time checking use UUID\_VERSION instead.

## EXAMPLES

The following shows an example usage of the API. Error handling is omitted for code simplification and has to be re-added for production code.

```
/* generate a RFC 9562 v1 UUID from system environment */
char *uuid_v1() {
	uuid_t *uuid;
	uuid_create(&uuid);
	uuid_make(uuid, UUID_MAKE_V1);
	char *str = NULL;
	uuid_export(uuid, UUID_FMT_STR, &str, NULL);
	uuid_destroy(uuid);
	return str;
}
/* generate a RFC 9562 v3 UUID from an URL */
char *uuid_v3(const char *url) {
	uuid_t *uuid_ns;
	uuid_create(&uuid_ns);
	uuid_load(uuid_ns, "ns:URL");
	uuid_t *uuid;
	uuid_create(&uuid);
	uuid_make(uuid, UUID_MAKE_V3, uuid_ns, url);
	char *str = NULL;
	uuid_export(uuid, UUID_FMT_STR, &str, NULL);
	uuid_destroy(uuid_ns);
	uuid_destroy(uuid);
	return str;
}
```

## SEE ALSO

[uuid(1)](/uuid/1.md), OSSP::uuid(3)

The following are references to UUID documentation and specifications: K. Davis, B. Peabody, and P. Leach, *Universally Unique IDentifiers (UUIDs)*, RFC-9562, ISSN 2070-1721, IETF, Cisco Systems, Uncloud, University of Washington, https://datatracker.ietf.org/doc/html/rfc9562. This is the current UUID standard. P. Leach, M. Mealling, and R. Salz, *A Universally Unique IDentifier (UUID) URN Namespace*, RFC-4122, IETF, July 2005, http://www.ietf.org/rfc/rfc4122.txt. *Information Technology – Open Systems Interconnection (OSI), Procedures for the operation of OSI Registration Authorities: Generation and Registration of Universally Unique Identifiers (UUIDs) and their Use as ASN.1 Object Identifier Components*, 9834-8:2004 / ITU-T Rec. X.667 2004, ISO/IEC, December 2004, http://www.itu.int/ITU-T/studygroups/com17/oid/X.667-E.pdf. “Universally Unique Identifier”, *DCE 1.1: Remote Procedure Call*, Document Number C706, Open Group Technical Standard, August 1997, http://www.opengroup.org/publications/catalog/c706.htm. “6.4.1 Node Field Generation Without the IEEE 802 Address”, *HTTP Extensions for Distributed Authoring (WebDAV)*, RFC-2518, IETF, February 1999, http://www.ietf.org/rfc/rfc2518.txt. “DCE 1.1 compliant UUID functions”, *FreeBSD manual pages [uuid(3)](/uuid/3.md) and [uuidgen(2)](/uuidgen/2.md)*, http://www.freebsd.org/cgi/man.cgi?query=uuid&manpath=FreeBSD+6.0-RELEASE.

## HISTORY

OSSP **uuid** was implemented in January 2004 by Ralf S. Engelschall ⟨rse@engelschall.com⟩. It was prompted by the use of UUIDs in the OSSP **as** and **OpenPKG** projects. It is a clean room implementation intended to be strictly standards compliant and maximally portable.
