RFC 9562 UUID implementation in Zig
  • Zig 98.5%
  • Nix 1.5%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Jeffrey C. Ollie e5c26080bc
All checks were successful
test / test (push) Successful in 2m24s
test / docs (push) Successful in 2m32s
add remaining RFC 9562 test vectors and the name space IDs
Cover the vectors from the RFC that the tests did not yet use: the
section 4 example UUID in its string, unsigned integer, and URN forms;
the four UUIDv4 variant examples; and the nil and max string forms.

Expose the well-known name space IDs from Table 3 (DNS, URL, OID, and
X.500) as constants, along with a toBytes method that returns the
network-byte-order bytes needed to hash a name space when creating v3
and v5 UUIDs. That allows the RFC's v3 and v5 examples to be tested end
to end: the tests now compute the actual MD5 and SHA-1 of the DNS name
space plus "www.example.com" and check that new() produces the RFC's
expected UUIDs, instead of starting from hardcoded digests.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01N46mD8544YMCkkJ5Bafrmw
2026-08-30 12:13:35 -05:00
.forgejo/workflows drop docs build from the test job 2026-08-30 10:51:52 -05:00
LICENSES update licensing, add tangled workflow 2026-05-19 22:20:08 -05:00
src add remaining RFC 9562 test vectors and the name space IDs 2026-08-30 12:13:35 -05:00
.gitignore update licensing, add tangled workflow 2026-05-19 22:20:08 -05:00
build.zig add benchmarks as a build step 2026-08-30 12:03:03 -05:00
build.zig.zon expand README and include it in the package 2026-08-30 10:19:51 -05:00
flake.lock rework flake: tarball nixpkgs channel, zon2nix input, all exposed systems 2026-08-30 10:35:27 -05:00
flake.nix publish API docs to ocj.page from CI 2026-08-30 10:42:35 -05:00
README.md add remaining RFC 9562 test vectors and the name space IDs 2026-08-30 12:13:35 -05:00
REUSE.toml update licensing, add tangled workflow 2026-05-19 22:20:08 -05:00

RFC 9562 UUIDs for Zig

A Zig library for generating, parsing, and formatting RFC 9562 UUIDs.

Features

  • Generate version 1, 2, 3, 4, 5, 6, 7, and 8 UUIDs
  • The special nil (all zeros) and max (all ones) UUIDs
  • The well-known name space IDs (DNS, URL, OID, X.500) for creating v3 and v5 UUIDs
  • Parse and format the standard string representation (c232ab00-9414-11ec-b3c8-9f6bdeced846) and the URN representation (urn:uuid:c232ab00-9414-11ec-b3c8-9f6bdeced846)
  • Implements format so UUIDs can be printed directly with {f}
  • UUID is a packed union that is exactly 128 bits — compare UUIDs with a.id == b.id, or access the version and variant fields via meta
  • No allocations, no dependencies

Requirements

Zig 0.16.0 or later.

Installation

Add the dependency to your project:

zig fetch --save git+https://git.ocjtech.us/jeff/zig-uuid

Then in your build.zig:

const uuid = b.dependency("uuid", .{
    .target = target,
    .optimize = optimize,
});
exe.root_module.addImport("uuid", uuid.module("uuid"));

Examples

Generate a random (version 4) UUID:

const std = @import("std");
const UUID = @import("uuid").UUID;

pub fn main(init: std.process.Init) void {
    const rng_impl: std.Random.IoSource = .{ .io = init.io };
    const rng = rng_impl.interface();

    const uuid: UUID = .new(.{
        .v4 = .{
            .rng = rng,
        },
    });

    std.debug.print("{f}\n", .{uuid});
}

Generate a timestamp-based, sortable (version 7) UUID:

const uuid: UUID = .new(.{
    .v7 = .{
        .unix_ts_ms = .{ .timestamp = .now(io, .real) },
        .rng = rng,
    },
});

Parse and format:

const uuid = try UUID.deserialize("c232ab00-9414-11ec-b3c8-9f6bdeced846");
const str: [36]u8 = uuid.serialize();
const urn: [45]u8 = uuid.serializeUrn();

Note that for version 3 and 5 UUIDs you compute the MD5 or SHA-1 hash yourself and pass the digest in via the hash field. Version 2 (DCE Security) UUIDs store only 28 bits of timestamp and 6 bits of clock sequence, so UUIDs generated within the same ~7 minute window for the same local ID collide easily — prefer another version unless you specifically need DCE semantics.

Standards

The test suite checks against the example values and test vectors from RFC 9562, with the name-based v8 example corrected per erratum 7929.

Development

zig build test   # run the tests
zig build run    # run the example UUID generator
zig build docs   # build the API docs into zig-out/docs
zig build bench  # run the benchmarks (always ReleaseFast)

License

MIT. This project follows the REUSE specification.