NanoID Generator

कस्टमाइज़ेबल अल्फाबेट के साथ छोटे URL-सेफ यूनिक IDs जनरेट करें

अल्फाबेट

साइज़

संख्या

NanoIDs बनाने के लिए जनरेट पर क्लिक करें

NanoID क्या है?

NanoID एक tiny, fast, URL-safe random ID generator है। Default में यह 64-character alphabet (A-Za-z0-9_-) use करके 21-character strings produce करता है, approximately 126 bits of entropy provide करता है — UUID v4 के 122 bits के comparable लेकिन shorter string में।

NanoID timestamp या कोई structured data embed नहीं करता। हर ID purely random है, operating system के cryptographically secure random number generator से generate होता है।

NanoID vs UUID v4

NanoID और UUID v4 दोनों CSPRNG-backed random ID generators हैं। वे format, length, और ecosystem support में differ करते हैं:

PropertyNanoID (default)UUID v4
FormatURL-safe alphanumeric + _-Hyphenated hexadecimal
Length21 characters (default)36 characters
Entropy~126 bits122 bits
URL-safeहाँ — कोई encoding needed नहींहाँ (with hyphens)
Alphabet64 characters (A-Za-z0-9_-)16 characters (0-9a-f)
Dependenciesnpm package requiredNative (crypto.randomUUID)
Customizableहाँ — length और alphabetनहीं
Standardकोई नहीं (community library)RFC 4122 / RFC 9562

UUID v4 चुनें जब external systems के साथ interoperability matter करे — native UUID columns वाले databases, UUID format expect करने वाले APIs, या UUIDs parse करने वाला logging infrastructure। NanoID चुनें जब shorter IDs चाहिए और full stack control में है।

Size द्वारा Collision Probability

NanoID की collision probability ID length और generation rate पर depend करती है। निम्न table default 64-character alphabet use करती है:

Size (chars)Possible IDsCollision safety
664~1 in 4.5B — कुछ thousand IDs के लिए safe
864~1 in 4.5T — millions of IDs के लिए safe
1164~1 in 2.8 quadrillion — billions of IDs के लिए safe
1664~1 in 1.2 × 10^19 — trillions of IDs के लिए safe
2164~1 in 10^30 — centuries के लिए per day 100 billion IDs के लिए safe
3264UUID v4 (122 bits) के comparable
3636UUID v4 से exceeds

Default 21-character size UUID v4 की collision resistance (~126 bits) match करने के लिए chosen है जबकि 41% shorter है। ज़्यादातर applications के लिए, 21 characters right choice है।

Custom Alphabets

NanoID का alphabet fully customizable है। Library किसी भी unique characters की string accept करती है alphabet के रूप में:

Numeric onlyA-Za-z0-9_-
SMS codes या PIN-style identifiers के लिए सभी-digit IDs के लिए '0123456789' use करें।
Lowercase hexA-Za-z0-9
UUID hyphen format के बिना compact hex strings के लिए '0123456789abcdef' use करें।
Human-readable0-9a-f
Visually ambiguous characters (0, O, 1, I, l) exclude करें उन IDs के लिए जो users manually type करें।
Custom domain0-9
अपने application के लिए appropriate characters का कोई भी set use करें।

Important: nanoid/non-secure केवल non-security-sensitive applications के लिए use करें। किसी भी ID के लिए जो unguessable होनी चाहिए, हमेशा default secure import use करें।

NanoID Randomness कैसे Generate करता है

NanoID operating system के cryptographically secure pseudo-random number generator (CSPRNG) use करता है। Browsers में यह crypto.getRandomValues() है; Node.js में यह crypto.randomFillSync() है। यह TLS session keys के लिए use किया जाने वाला same entropy source है — Math.random() से far stronger।

Rejection Sampling (Modulo Bias से बचना)

Random characters generate करने का naive approach: random byte (0–255) लें और byte % alphabetSize compute करें। यह modulo bias introduce करता है — कुछ characters दूसरों से slightly अधिक often appear होते हैं।

NanoID rejection sampling use करके इस bias को eliminate करता है:

  1. Alphabet size cover करने वाला smallest power-of-two mask determine करें (जैसे 64-char alphabet के लिए, mask 63 = 0b00111111 है)
  2. Random bytes generate करें और mask apply करें: byte & mask
  3. यदि masked value alphabet range में है, use करें। नहीं तो, discard और फिर try करें।

इसका मतलब कुछ random bytes discarded होते हैं, लेकिन result alphabet पर perfectly uniform distribution है — कोई character दूसरे से अधिक likely नहीं।

How the algorithm works — step by step
// Pure browser — no npm package needed
function generateNanoid(alphabet, size) {
  const mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
  const step = Math.ceil((1.6 * mask * size) / alphabet.length)
  let id = ''
  while (id.length < size) {
    const bytes = crypto.getRandomValues(new Uint8Array(step))
    for (const byte of bytes) {
      const idx = byte & mask
      if (idx < alphabet.length) {
        id += alphabet[idx]
        if (id.length === size) break
      }
    }
  }
  return id
}

const URL_SAFE = 'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'
generateNanoid(URL_SAFE, 21)  // → "V1StGXR8_Z5jdHi6B-myT"

Environment Support

Browser
Modern browsers (Chrome 37+, Firefox 34+, Safari 7+) — crypto.getRandomValues() use करता है
Node.js 14+
Node.js 14.18+ — crypto.randomFillSync() use करता है
Deno
Deno — crypto.getRandomValues() use करता है
Bun
React Native — expo-crypto या react-native-get-random-values polyfill use करता है
Edge / Cloudflare Workers
Edge runtimes (Cloudflare Workers, Vercel Edge) — Web Crypto API available
React Native
Bun — native crypto support

Code Examples

JavaScript / TypeScript

JavaScript
// npm i nanoid
import { nanoid } from 'nanoid'
nanoid()          // → "V1StGXR8_Z5jdHi6B-myT" (21 chars, URL-safe)
nanoid(8)         // → "Uakgb_J5" (custom size)

// Custom alphabet
import { customAlphabet } from 'nanoid'
const hexId  = customAlphabet('0123456789abcdef', 16)
hexId()       // → "4f3a1b8c9d2e0f7a"

const numId  = customAlphabet('0123456789', 8)
numId()       // → "30812894"

Browser (CDN)

NanoID को CDN import के माध्यम से browser में directly use किया जा सकता है। Quick prototyping के लिए कोई build step required नहीं।

JavaScript
// Pure browser — no npm package needed
function generateNanoid(alphabet, size) {
  const mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
  const step = Math.ceil((1.6 * mask * size) / alphabet.length)
  let id = ''
  while (id.length < size) {
    const bytes = crypto.getRandomValues(new Uint8Array(step))
    for (const byte of bytes) {
      const idx = byte & mask
      if (idx < alphabet.length) {
        id += alphabet[idx]
        if (id.length === size) break
      }
    }
  }
  return id
}

const URL_SAFE = 'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'
generateNanoid(URL_SAFE, 21)  // → "V1StGXR8_Z5jdHi6B-myT"

Python

Python
# pip install nanoid
from nanoid import generate

generate()              # → "V1StGXR8_Z5jdHi6B-myT"
generate(size=8)        # → "Uakgb_J5"
generate('0123456789abcdef', 16)  # custom alphabet + size

Node.js (CommonJS)

JavaScript
// Node.js — stdlib only, no npm needed
const { randomFillSync } = require('crypto')

function nanoid(alphabet, size) {
  const mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
  const step = Math.ceil((1.6 * mask * size) / alphabet.length)
  let id = ''
  while (id.length < size) {
    const bytes = randomFillSync(Buffer.alloc(step))
    for (const byte of bytes) {
      const idx = byte & mask
      if (idx < alphabet.length) { id += alphabet[idx]; if (id.length === size) break }
    }
  }
  return id
}

अक्सर पूछे जाने वाले प्रश्न

क्या NanoID cryptographically secure है?
हाँ — default import use करने पर, NanoID OS-level CSPRNG use करके IDs generate करता है। NanoID values session tokens, API keys, और अन्य security-sensitive identifiers के लिए suitable हैं।
क्या मैं NanoID को database primary key के रूप में use कर सकता हूँ?
हाँ। NanoID strings URL-safe हैं और CHAR या VARCHAR columns के रूप में store की जा सकती हैं। हालाँकि, NanoID में कोई timestamp component नहीं है, इसलिए IDs generation order के अनुसार sortable नहीं हैं — यह B-tree index fragmentation cause करेगा।
NanoID crypto.randomUUID() से कैसे compare करता है?
दोनों CSPRNG use करते हैं और strong randomness provide करते हैं। crypto.randomUUID() native है (no dependency), 36-character hyphenated UUID v4 strings produce करता है, और universally recognized है। NanoID npm package require करता है लेकिन shorter strings produce करता है (21 chars default) customizable alphabet के साथ।
क्या होता है अगर मैं बहुत short NanoID use करूँ?
Short IDs (जैसे 6–8 characters) की significantly higher collision probability है। Appropriate size choose करने के लिए ऊपर collision probability table use करें।
क्या मैं npm के बिना browser में NanoID use कर सकता हूँ?
हाँ। NanoID CDN (जैसे jsDelivr या esm.sh) से ESM imports support करता है। Production के लिए specific version pin करें और script self-hosting consider करें।
क्या NanoID सभी environments में काम करता है?
NanoID सभी modern browsers, Node.js, Deno, Bun, Cloudflare Workers, और Vercel Edge Functions में काम करता है। React Native के लिए getRandomValues के लिए polyfill required है (react-native-get-random-values)।