cuid2-rs
A Rust implementation of CUID2 (Collision-resistant Unique IDentifiers) - secure, short, URL-friendly unique string IDs.
Features
- Secure: Cryptographically strong random generation using ChaCha20 and SHA3-512
- Collision-resistant: Designed to minimize the risk of ID collisions
- URL-friendly: Contains only lowercase letters and numbers
- Configurable: Customize ID length based on your requirements
- Thread-safe: Safe for concurrent use in multi-threaded applications
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Usage
Basic Usage
use generate;
Custom Length
use generate_cuid;
Validation
use ;
How It Works
CUID2 generates secure, collision-resistant IDs by combining several sources of entropy:
- A random lowercase letter at the beginning (for database indexing benefits)
- Current timestamp in milliseconds
- A counter to prevent collisions in rapid generation
- Cryptographically secure random values
- A system fingerprint hash to prevent collisions in distributed systems
The result is securely hashed with SHA3-512 and formatted to the desired length.
Configuration
The library provides several constants that you can use:
pub const DEFAULT_LENGTH: usize = 24; // Default CUID length
pub const MAX_LENGTH: usize = 32; // Maximum allowed length
pub const MIN_LENGTH: usize = 2; // Minimum allowed length
Error Handling
The generate() and generate_cuid() functions return a Result that can contain errors:
use ;
Performance
CUID2 is designed to be efficient while maintaining security. The implementation uses:
- ChaCha20 for secure random number generation
- SHA3-512 for cryptographic hashing
- Atomic operations for thread safety
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Acknowledgements
This is a Rust implementation of the CUID2 algorithm, originally developed by Eric Elliott.