CUID2 Generator

सुरक्षित नेक्स्ट-जेनरेशन CUID2 आइडेंटिफायर जनरेट करें

संख्या
लंबाई

जनरेट किए गए CUID2s

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

CUID2 क्या है?

CUID2 CUID की second generation है, जो cryptographic security और privacy पर focus करते हुए scratch से rewrite किया गया है।

CUID v1 के विपरीत जो timestamp visibly embed करता है, CUID2 सभी components को SHA3 hash के अंदर hide करता है, जो practically unpredictable strings produce करता है।

IDs एक customizable alphabetic prefix (default c) से शुरू होते हैं जिसके बाद SHA3 hash का base36 string होता है।

CUID2 क्यों?

CUID2 CUID v1 में specific security concerns के response में आया:

  • Exposed timestamp: CUID v1 हर ID के शुरू में creation time reveal करता था।
  • Predictability: CUID v1 structure knowledge के साथ, internal state का हिस्सा infer किया जा सकता था।
  • Cryptographic security की कमी: CUID v1 explicitly cryptographically secure random number generator use नहीं करता था।

CUID2 SHA3, CSPRNG, और timestamp hiding use करके इन concerns को address करता है।

Design Principles

Cryptographic Security
CSPRNG और SHA3 use करता है ताकि IDs predictable न हों, यहाँ तक कि time window जानने पर भी।
Timestamp Privacy
Timestamp hash में mix होता है और directly appear नहीं करता, timing privacy protect करता है।
Customizable Prefix
Different entity types के IDs distinguish करने के लिए custom prefix specify करें (जैसे usr_, prd_)।
Customizable Length
Default 24 characters है लेकिन application requirements के अनुसार adjust हो सकता है।
Shared Fingerprint
Better entropy distribution के लिए function calls के across random fingerprint value share करें।
Database Compatible
हमेशा alphabetic letter से शुरू होता है, variable names और database constraints के साथ compatible।

CUID2 vs CUID v1

दोनों generations के key characteristics की comparison:

AttributeCUID2CUID v1
SecurityCryptographic (SHA3+CSPRNG)Non-cryptographic
PredictabilityVery lowLow (exposed timestamp)
LengthCustomizable (default 24)Fixed 25 characters
PrefixCustomizableFixed 'c'
DistributionEvenly distributedTimestamp-biased
StatusActive & recommendedDeprecated

CUID2 vs UUID v4

Common UUID standard के साथ comparison:

AttributeCUID2UUID v4
Default Length24 characters36 characters
URL-safeहाँहाँ (hyphens के साथ)
Custom Lengthहाँनहीं
Sortableनहींनहीं
EntropyHigh (SHA3)High (CSPRNG)
Character SetLowercase alphanumericHex + hyphens

CUID2 UUID v4 के equivalent security के साथ guaranteed alphabetic prefix और flexible length provide करता है।

CUID2 कौन Use करता है?

CUID2 को prominent communities और frameworks ने adopt किया है:

  • Prisma @id field में @default(cuid()) के through ID type के रूप में CUID2 support करता है।
  • Drizzle ORM community default IDs के लिए CUID2 adopt करती है।
  • SvelteKit और Remix projects session IDs के लिए इसे use करते हैं।
  • High-volume API servers जिन्हें logs में traceable IDs चाहिए।
  • Applications जो multiple databases या regions में sync करती हैं।

Code Examples

Installation और usage: pnpm add @paralleldrive/cuid2

JavaScript (npm — @paralleldrive/cuid2)
import { createId } from '@paralleldrive/cuid2'

// Generate a single CUID2 (default length: 24)
const id = createId()
console.log(id) // e.g. "tz4a98xxat96iws9zmbrgj3a"

// Custom length
import { init } from '@paralleldrive/cuid2'
const createShortId = init({ length: 16 })
const shortId = createShortId()
console.log(shortId) // e.g. "tz4a98xxat96iws9"

Advanced configuration:

Prisma Schema
model User {
  id        String   @id @default(cuid())
  email     String   @unique
  name      String?
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

Database integration:

Node.js (Web Crypto — no dependencies)
async function generateCuid2(length = 24) {
  const alphabet = 'abcdefghijklmnopqrstuvwxyz'

  // Random prefix letter
  const firstByte = crypto.getRandomValues(new Uint8Array(1))[0]
  const firstChar = alphabet[firstByte % 26]

  // Random 32-byte salt
  const salt = crypto.getRandomValues(new Uint8Array(32))
  const saltHex = [...salt].map(b => b.toString(16).padStart(2, '0')).join('')

  // SHA-512 of timestamp + salt
  const input = Date.now().toString(36) + saltHex
  const hashBuffer = await crypto.subtle.digest(
    'SHA-512',
    new TextEncoder().encode(input)
  )

  // Encode hash bytes as base-36 string
  const bytes = new Uint8Array(hashBuffer)
  let hash = ''
  for (let i = 0; i < bytes.length; i += 8) {
    let chunk = 0n
    for (let j = 0; j < 8 && i + j < bytes.length; j++) {
      chunk = (chunk << 8n) | BigInt(bytes[i + j])
    }
    hash += chunk.toString(36)
  }

  return (firstChar + hash).slice(0, length)
}

// Usage
const id = await generateCuid2()
console.log(id) // e.g. "m7k3r9p2nxq8zt5a6cwj4bvd"

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

क्या CUID2 SHA3 की वजह से CUID v1 से slower है?
थोड़ा — SHA3 CUID v1 के custom algorithm से slower है, लेकिन most applications में difference imperceptible है। CUID2 per second tens of thousands of IDs generate करता है, जो typical database round-trip से far faster है।
क्या मैं data migration के बिना CUID v1 से CUID2 upgrade कर सकता हूँ?
आप CUID2 column add कर सकते हैं और new records के लिए gradually transition कर सकते हैं। अगर CUID primary key के रूप में use है, तो migration plan की जरूरत है क्योंकि format differ करता है।
CUID2 में कितना entropy है?
Default 24-character length पर, CUID2 approximately 118 bits of entropy provide करता है — UUID v4 (122 random bits, लेकिन कुछ version के लिए determined) से अधिक। Custom length entropy को linearly adjust करता है।
क्या CUID2 privacy-oriented IDs के लिए suitable है?
हाँ — CUID v1 के विपरीत, CUID2 creation time या host information leak नहीं करता। Timestamp hash में mixed है और ID alone से extract नहीं किया जा सकता।
CUID2 के लिए optimal length क्या है?
Length 24 (default) most applications के लिए sufficient है। अगर per day millions of IDs generate कर रहे हैं, तो 32 तक increase consider करें। Adequate collision resistance maintain करने के लिए recommended minimum 16 characters है।
क्या मैं browser में CUID2 use कर सकता हूँ?
हाँ — CUID2 library Web Crypto API के through browser में, और crypto module के through Node.js में run करती है। Client-side और server applications दोनों के लिए suitable।