nx_nanoid.nx
buildroot/runtime/nx_nanoid.nx
about
nanoid.nx -- modern URL-safe ID generator.
Andrey Sitnik 2017, widely adopted in the JS ecosystem (Next.js,
Prisma, PlanetScale, tRPC). Alternative to UUID + ULID with
different trade-offs:
- Default 21 chars ~= 126 bits of entropy (UUID: 128)
- URL-safe alphabet: A-Z a-z 0-9 _ - (64 chars, 6 bits each)
- No structure: pure random, no embedded timestamp
- No lexicographic sort: use ULID if sorting matters
Use cases: URL slugs that need collision-resistance, short
share codes, DB primary keys where you don't need time-sort.
Algorithm:
Request N random bytes from /dev/urandom via rand.nx.
Mask each byte to 6 bits + retry on overflow (standard
rejection sampling -- keeps the output alphabet uniform).
Composes rand.nx.
Invariants:
N1 Output is fixed-length caller-chosen (default 21).
N2 Every byte is from the 64-char URL-safe alphabet.
N3 Rejection sampling ensures uniform distribution -- we
don't use mod-256 which would bias bytes 0..31.
dependencies 2 imports · 0 importers
imports: nx_syscalls.nxnx_rand.nx
imported by: nobody (leaf or entry point)
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| none |
consts
| 37 | const NANOID_DEFAULT_LEN: i64 = 21 |
functions
| 41 | func nano_char(idx: i64) -> i64 called by 1: nanoid |
| 54 | func nanoid(out: *u8, n: i64) -> i64 |
| 68 | func nanoid_default(out: *u8) -> i64 |
| 73 | func main() -> i64 |