CUID2
CUID2 is a secure, collision-resistant identifier that hashes multiple entropy sources using SHA3-512. Unlike time-ordered IDs (ULID, UUID v7), CUID2 prevents enumeration attacks by making IDs non-predictable.When to Use
Use CUID2 when you need:- Security against enumeration - IDs are not predictable
- Collision resistance - Uses multiple entropy sources + hashing
- Customizable length - 2-32 characters (default 24)
- Non-sequential IDs - Prevents guessing next ID
Basic Usage
API Reference
Main Function
Generate a 24-character CUID2 string with default settings.
Generate a CUID2 string with custom options.Options:
length?: number- Length of the generated ID (2-32 characters, default: 24)random?: Uint8Array- Custom random bytes for deterministic testing (must be at least 1 byte; 16+ bytes recommended for adequate entropy)
The fingerprint always uses cryptographically secure random bytes, regardless of the
random option.Static Methods
Validate that a value is a properly formatted CUID2 string. TypeScript type guard.
No Binary Conversion: Unlike UUID and ULID, CUID2 does not provide
toBytes/fromBytes methods because it is a string-native format with no canonical binary representation. The ID is the result of SHA3-512 hashing and Base36 encoding.Security Features
CUID2 provides multiple layers of security:- SHA3-512 Hashing - Cryptographically secure hash function
- Multiple Entropy Sources:
- Current timestamp (milliseconds)
- Cryptographically secure random salt
- Monotonic counter (initialized randomly)
- Host fingerprint (derived from global environment)
- Non-Predictable - Hash output prevents ID enumeration attacks
- Collision Resistant - Combination of time, random, and counter ensures uniqueness
Real-World Examples
API Tokens
Session IDs
Public Resource IDs
Invite Codes
Validation Example
Testing with Deterministic Output
Type Definitions
Length Recommendations
Short (8-12 chars)
Invite codes, short URLs, temporary tokensLower entropy - only use for non-critical IDs
Default (24 chars)
Database IDs, resource identifiers, session IDsGood balance of security and readability
Long (28-32 chars)
API secrets, encryption keys, high-security tokensMaximum entropy - use for security-critical IDs
Minimum (2-7 chars)
Not recommended - very high collision riskOnly for testing or non-production use
Performance Characteristics
Generation Speed
8× faster than
@paralleldrive/cuid2 npm packageSecurity
SHA3-512 hashing prevents enumeration attacks
Collision Resistance
Multiple entropy sources + hash function
Size
2-32 characters (configurable, default 24)
Bundle Size: ~1.1 KB minified + gzipped (excluding SHA3-512 dependency)
Validation Pattern
CUID2 must match this pattern:- First character is always
a-z(lowercase letter) - Remaining characters are
0-9ora-z - Length between 2 and 32 characters
- Uses Base36 encoding (0-9, a-z)
Migration Guide
From @paralleldrive/cuid2
uniku/cuid2is a direct function call vs.createId()factory pattern- No need to initialize - just import and call
cuid2() - 8× faster performance
