Note: This library was machine-ported with Gemini 4 Neon.
A tiny, secure, URL-friendly, unique string ID generator for the D programming language.
This library is a native, D-idiomatic port of the original JavaScript Nano ID library by Andrey Sitnik.
- Zero dependencies. Uses only Phobos, druntime, and native OS cryptographic APIs.
- Cryptographically secure. Uses the operating system's CSPRNG with a lock-free, fork-safe thread-local entropy pool.
- Unbiased distribution. Uses bitmask and cutoff rejection sampling instead of naive modulo indexing, avoiding modulo bias across all alphabet sizes.
- Compact & URL-safe. Uses a 64-symbol alphabet (
A-Za-z0-9_-), packing 126 bits of entropy into 21 characters (comparable to UUID v4's 122 bits in 36 characters). - Idiomatic D.
@safe,-preview=dip1000-compatible, CTFE-capablecustomAlphabet, full UTF-8/Unicode alphabet support, and@nogc nothrowin-place buffer primitives.
import nanoid : nanoid;
string id = nanoid(); // e.g. "V1StGXR8_Z5jdHi6B-myT"- Comparison with UUID v4
- Security & Correctness
- Installation
- API
- Building & Testing
- Tools & References
- License
Nano ID provides collision resistance comparable to random-based UUID v4:
- UUID v4: 122 random bits across 36 characters (
0-9a-fplus 4 hyphens). - Nano ID: 126 random bits across 21 characters (
A-Za-z0-9_-).
You can estimate collision probabilities for custom lengths and alphabets with the Nano ID Collision Calculator.
- OS CSPRNG backends. Instead of Phobos's non-cryptographic
std.random,nanoidreads entropy directly from the operating system:- Windows:
BCryptGenRandom(BCRYPT_USE_SYSTEM_PREFERRED_RNG) - macOS / iOS / FreeBSD / OpenBSD / NetBSD / DragonFlyBSD:
arc4random_buf - Linux:
getrandom(2)(with automatic/dev/urandomfallback onENOSYS) - Other POSIX:
/dev/urandom(O_CLOEXEC)
- Windows:
- Thread-safe & fork-safe pooling. Random bytes are buffered in a thread-local (
TLS) 4 KiB pool, giving lock-free multi-threaded performance. Consumed bytes are scrubbed immediately after use, large requests bypass the pool, and POSIX processes register apthread_atforkchild handler to invalidate inherited pool bytes afterfork(). - Uniformity. Computing
randomByte % alphabet.lengthintroduces bias wheneveralphabet.lengthdoes not divide256. This library uses power-of-two bitmasking whenalphabet.lengthis a power of two and unbiased rejection sampling otherwise. - Unicode correctness. ASCII alphabets take a zero-decoding fast path (
1 char == 1 symbol). Alphabets containing non-ASCII characters (Cyrillic, CJK, emoji, etc.) are decoded as Unicode code points (dchar), sosizealways counts symbols rather than UTF-8 bytes and every generated string is guaranteed to be valid UTF-8.
Add nanoid to your Dub project:
dub.sdl
dependency "nanoid" version="~>0.9.0"
dub.json
"dependencies": {
"nanoid": "~>0.9.0"
}Or add it from the command line:
dub add nanoidBy default, nanoid() returns a 21-character string drawn from urlAlphabet (A-Za-z0-9_-).
Because the package is also named nanoid, prefer a selective import when calling nanoid() directly, or use the generate alias with a whole-module import:
import nanoid : nanoid;
string id = nanoid(); // 21 characters (defaultSize)
string shortId = nanoid(10); // 10 characters
string empty = nanoid(0); // ""import nanoid;
string id = generate(); // 21 characters
string qualified = nanoid.nanoid(); // 21 charactersWhen you want to avoid GC allocations, you can generate into a fixed-size stack array or fill an existing buffer in place:
import nanoid : nanoid, nanoidInto, tryNanoidInto;
// Stack-allocated fixed-length ID (zero heap allocations on success)
char[21] fixed = nanoid!21();
char[10] shortFixed = nanoid!10();
// Fill an existing slice in place (throws NanoIdException on OS RNG failure)
char[21] buf;
nanoidInto(buf[]);
// Strict @safe nothrow @nogc variant (returns false on OS RNG failure)
void makeIdNoGc(scope char[] outBuf) @safe nothrow @nogc
{
bool ok = tryNanoidInto(outBuf);
assert(ok);
}customAlphabet(alphabet, defaultSize = 21) validates the alphabet and returns a lightweight, immutable-friendly NanoIdGenerator struct.
- Alphabets must contain between 1 and 256 Unicode code points (symbols) and valid UTF-8.
customAlphabetispure @safeand CTFE-compatible, so generators can be declaredstatic immutableorenumand shared across threads with zero synchronization.
import nanoid : customAlphabet;
// Compile-time validated, thread-safe generator
static immutable hexId = customAlphabet("0123456789abcdef", 10);
string a = hexId(); // e.g. "4f90d13a42" (10 chars)
string b = hexId(5); // e.g. "f01a2" (override size per call)Unicode alphabets work seamlessly β size counts Unicode symbols, not UTF-8 bytes:
import nanoid : customAlphabet;
auto cyrillicId = customAlphabet("0123456789Π°Π±Π²Π³Π΄Π΅Ρ", 5);
string id = cyrillicId(); // e.g. "8Ρ56Π°" (5 Unicode symbols)In-place buffer filling is also available on NanoIdGenerator:
import nanoid : customAlphabet;
static immutable digits = customAlphabet("0123456789", 6);
char[6] asciiBuf;
digits.into(asciiBuf[]); // throws NanoIdException on failure
bool ok = digits.tryInto(asciiBuf[]); // @safe nothrow @nogc (ASCII alphabets only)
// For any alphabet (ASCII or Unicode), fill a dchar[] buffer:
static immutable emojis = customAlphabet("πππ₯β¨", 4);
dchar[4] codePoints;
emojis.into(codePoints[]);
bool okUni = emojis.tryInto(codePoints[]); // @safe nothrow @nogcNote:
into(char[])requires an ASCII alphabet (throwsNanoIdExceptionotherwise, andtryInto(char[])returnsfalse), because multi-byte UTF-8 symbols do not fit 1-to-1 intochar[]. UseopCall()orinto(dchar[])for Unicode alphabets.
customRandom(alphabet, defaultSize, rng) (or customRandom(alphabet, rng) using defaultSize = 21) lets you supply your own random generator. Three styles are supported automatically:
- Any
std.randomuniform RNG (isUniformRNG) β great for deterministic, seeded tests:
import std.random : Mt19937;
import nanoid : customRandom;
auto gen = customRandom("abcdef", 10, Mt19937(42));
string id = gen(); // Deterministic sequence for seed 42- A buffer-filling callable
void(ubyte[])β zero-allocation custom byte source:
import nanoid : customRandom, urlAlphabet;
auto gen = customRandom(urlAlphabet, 12, (ubyte[] buf) {
// Fill buf with random bytes from your custom source
foreach (i, ref b; buf)
b = cast(ubyte)(i * 31);
});
string id = gen();- A byte-returning callable
const(ubyte)[](size_t)β matches JavaScriptcustomRandom:
import nanoid : customRandom, random, urlAlphabet;
auto gen = customRandom(urlAlphabet, 10, &random);
string id = gen();Note: The internal sequence of random calls may change across library versions. Do not rely on seeded
customRandomproducing identical IDs across major/minor upgrades.
The underlying OS CSPRNG helpers are re-exported directly from nanoid (and defined in nanoid.random):
import nanoid : random, fillRandom, tryFillRandom;
ubyte[] bytes = random(32); // Allocates 32 secure random bytes
ubyte[16] buf;
fillRandom(buf[]); // Fills in place; throws NanoIdException on failure
bool ok = tryFillRandom(buf[]); // @safe nothrow @nogcIf you do not need cryptographic unpredictability, nanoid.nonsecure provides a non-secure generator backed by Phobos's thread-local std.random.rndGen:
import nanoid.nonsecure : nanoid, customAlphabet;
string id = nanoid();
auto gen = customAlphabet("0123456789abcdef", 10);
string token = gen();Security Warning: IDs generated by
nanoid.nonsecureare predictable and must not be used for session tokens, secrets, password resets, or any security-sensitive identifier.
Invalid configuration or OS CSPRNG failures throw NanoIdException (a subclass of Exception):
- Empty alphabet (
"") - Alphabet exceeding 256 Unicode symbols (in secure
customAlphabet/customRandom) - Invalid UTF-8 in an alphabet string
- Calling
into(char[])on a generator with a non-ASCII alphabet - Custom byte-returning callback returning an empty slice
- Operating system CSPRNG failure
Run the full unit test suite (including uniformity, edge-case, Unicode, error-recovery, and multi-threaded collision tests):
dub testTest with LDC:
dub test --compiler=ldc2Build the release library:
dub build --build=release- Original JavaScript Nano ID
- Nano ID Collision Probability Calculator
nanoid-dictionaryβ collection of popular custom alphabets
MIT License β see LICENSE.