Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

u58

u58 prints UUID values encoded with the Bitcoin base58 alphabet. UUIDv4 is the default:

u58

Generate more than one ID:

u58 -n 10

Generate UUIDv4 values:

u58 -4
u58 -4 -n 10

Generate UUIDv7 values:

u58 -7
u58 -7 -n 10

Build

Supported platforms: Unix-like systems with either arc4random_buf, Linux getrandom, or /dev/urandom.

With a C compiler:

make
./u58

With Zig compiling the C sources:

zig build
./zig-out/bin/u58

The reusable library surface is intentionally tiny: src/u58.c contains the generator and encoder, and include/u58/u58.h exposes u58_generate, u58_generate_v4, and u58_generate_v7. u58_generate is an alias for UUIDv4.

Library Use

Embed src/u58.c and include include/u58/u58.h. There are no third-party runtime dependencies.

int u58_generate(char out[U58_BUFSZ]);     /* UUIDv4 default */
int u58_generate_v4(char out[U58_BUFSZ]);  /* UUIDv4 */
int u58_generate_v7(char out[U58_BUFSZ]);  /* UUIDv7 */

The caller provides a U58_BUFSZ byte buffer. On success, the functions return 0 and write a NUL-terminated base58 string. On failure, they return -1.

From C

#include <stdio.h>
#include "u58/u58.h"

int main(void) {
    char id[U58_BUFSZ];

    if (u58_generate(id) != 0) {
        return 1;
    }

    puts(id);
    return 0;
}

Compile it with:

cc -I/path/to/u58/include your_program.c /path/to/u58/src/u58.c -o your_program

From Zig

For Zig 0.16, add the C source and include path in your build.zig:

const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const exe = b.addExecutable(.{
        .name = "your_program",
        .root_module = b.createModule(.{
            .root_source_file = b.path("src/main.zig"),
            .target = target,
            .optimize = optimize,
            .link_libc = true,
        }),
    });

    exe.root_module.addIncludePath(b.path("path/to/u58/include"));
    exe.root_module.addCSourceFile(.{
        .file = b.path("path/to/u58/src/u58.c"),
        .flags = &.{"-std=c11"},
    });

    b.installArtifact(exe);
}

Then call it with @cImport and Zig 0.16's std.Io stdout pattern:

const std = @import("std");
const Io = std.Io;
const c = @cImport({
    @cInclude("u58/u58.h");
});

pub fn main(init: std.process.Init) !void {
    var id: [c.U58_BUFSZ]u8 = undefined;

    if (c.u58_generate(&id) != 0) {
        return error.U58GenerateFailed;
    }

    var stdout_buffer: [128]u8 = undefined;
    var stdout_writer = Io.File.stdout().writer(init.io, &stdout_buffer);
    const stdout = &stdout_writer.interface;

    try stdout.print("{s}\n", .{std.mem.sliceTo(&id, 0)});
    try stdout.flush();
}

License

The pruned UUID helper code is derived from stateless-me/uuidv47, MIT licensed. The base58 encoder is adapted from Bitcoin Core's MIT licensed base58 encoder. See LICENSES.md.

About

`u58` is a CLI utility that prints UUIDv4 or UUIDv7 values encoded with the Bitcoin base58 alphabet

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages