UUID Generator — Zig source
Generate RFC 4122 v4 UUIDs with crypto-grade randomness. Bulk generate, copy all, toggle format (uppercase / no hyphens).
This is the Zig implementation — the same logic the interactive tool runs, in a shareable, citable form.
// RFC 4122 v4 UUID generator (cryptographically secure).
//
// Language: Zig (0.13+)
// CosmoDev polyglot showcase port of the `uuid` tool.
// Ported from src/lib/uuid.ts — display source, part of CosmoDev's
// polyglot tool pages.
//
// Produces a random (v4) UUID: 36 characters, lowercase, hyphenated as
// 8-4-4-4-12 hex digits, with the RFC version (4) and variant (10xx)
// bits set. std.crypto.random is Zig's bridge to the host CSPRNG, so
// output is unpredictable and collision-safe.
const std = @import("std");
/// Write a fresh RFC 4122 version-4 UUID into `buf` (36 bytes).
fn uuidV4(buf: *[36]u8) void {
var b: [16]u8 = undefined;
std.crypto.random.bytes(&b);
b[6] = (b[6] & 0x0f) | 0x40; // version 4: high nibble of byte 6
b[8] = (b[8] & 0x3f) | 0x80; // variant : high bits of byte 8
// Lowercase hex, hyphens at the 8-4-4-4-12 boundaries.
const hex = "0123456789abcdef";
var p: usize = 0;
for (b, 0..) |byte, i| {
if (i == 4 or i == 6 or i == 8 or i == 10) {
buf[p] = '-';
p += 1;
}
buf[p] = hex[byte >> 4];
buf[p + 1] = hex[byte & 0x0f];
p += 2;
}
}
pub fn main() !void {
var id: [36]u8 = undefined;
uuidV4(&id);
const stdout = std.io.getStdOut().writer();
try stdout.print("{s}\n", .{id});
}
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →