Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/lukateras/nanoid.h

Sitnik's Nano ID as a 199-byte C header
https://github.com/lukateras/nanoid.h

c c-library cli id-generator meson nanoid

Last synced: 28 days ago
JSON representation

Sitnik's Nano ID as a 199-byte C header

Awesome Lists containing this project

README

        

# Nano ID for C

Logo

A tiny, secure, URL-friendly unique string ID generator for C.

- **Small.** 199 bytes. Header-only. No dependencies other than libc.
- **Safe.** Uses [`getentropy(3)`][], a modern, reliable, secure randomness source.
- **Short IDs.** A default Nano ID is 15 characters shorter than a UUIDv4 while
encoding 4 bits more entropy.
- **Portable.** POSIX-compliant. Available in [29 programming languages][ports].

```c
#include
#include
#include

int main() {
char id[NANOIDLEN + 1] = "";

if (nanoidgen(id, NANOIDLEN))
return EXIT_FAILURE;

puts(id);
}
```

```
$ cc main.c -o example
$ ./example
V1StGXR8_Z5jdHi6B-myT
```

[ports]: https://github.com/ai/nanoid#other-programming-languages

## Overview

Nano IDs are unique string IDs where each character is an alphanumeric, a
hyphen, or an underscore. Think of them as random unpadded [Base64url][]
strings. You've already seen this format elsewhere: on YouTube!

A default Nano ID is 21 characters long, encoding 126 bits of entropy at 6 bits
per character.

[`nanoid.h`][] is a self-contained Nano ID generator built on top of the
[`getentropy(3)`][] randomness source. It's as portable as [`getentropy(3)`][]
itself, which is a part of the POSIX.1-2024 standard. Just grab the header and
use it wherever you wish!

[Base64url]: https://datatracker.ietf.org/doc/html/rfc4648#section-5

## Usage

Works out of the box on NetBSD, OpenBSD, Linux, Android, macOS, illumos,
Solaris, FreeBSD, DragonFly, Haiku, GNU Hurd, Fuchsia, Emscripten, and WASI.
Windows support requires a [`getentropy(3)`][] shim.

To include [`nanoid(3)`][] copy [`nanoid.h`][] and [`LICENSE.txt`][] to your
project.

To build [`nanoidgen(1)`][] run `make nanoidgen`.

To build and install the project use [Meson][]:

```sh
meson setup ../nanoid && \
ninja -C ../nanoid && \
sudo ninja -C ../nanoid install
```

[`nanoid.h`]: nanoid.h
[`LICENSE.txt`]: LICENSE.txt

[Meson]: https://meson.build

## Documentation

See the PDF manual at .

### [`nanoid(3)`][]

```c
#include
```

- macro `NANOIDLEN`

The default Nano ID length of 21.

- static inline function `int *nanoidgen(char *buffer, size_t length)`

Fills the buffer with a Nano ID of the specified length up to
`GETENTROPY_MAX` (256).

Returns the return value of [`getentropy(3)`][].

### [`nanoidgen(1)`][]

```
$ nanoidgen [length]
```

Generates a Nano ID of the default length (21), or the specified length within
1 and `GETENTROPY_MAX` (256), and prints it to the standard output.

[`getentropy(3)`]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/getentropy.html
[`nanoid(3)`]: https://lukateras.github.io/nanoid.h/man/nanoid.3.html
[`nanoidgen(1)`]: https://lukateras.github.io/nanoid.h/man/nanoidgen.1.html

## Attribution

Port of [Nano ID](https://github.com/ai/nanoid) by [Andrey Sitnik](https://sitnik.ru).

Original logo by [Anton Lovchikov](https://github.com/antiflasher).

Social preview background texture by [Tuomo](https://x.com/tuomodesign).

Social preview logo assistance by [Tanya Nevskaya](https://github.com/unparalloser).