visus/cuid2
PHP CUID2 generator for collision-resistant, secure, URL-safe IDs built for distributed systems. Uses SHA3-512 with time and entropy for scalable uniqueness. Supports instance or static usage plus identifier validation; GMP can improve performance.
A PHP implementation of collision-resistant identifiers that are secure, URL-safe, and horizontally scalable. Perfect for distributed systems where unique identifiers need to be generated across multiple machines without coordination.
CUID2 (Collision-resistant Unique Identifier, version 2) is a modern approach to generating unique identifiers that addresses common issues with UUIDs and other identification systems. This PHP implementation provides:
You can read more about CUIDs from the official project website.
Each CUID2 identifier is composed of several components that are combined and hashed to ensure uniqueness:
[prefix][hash]
Example output: p6p168tx2rxtgyehd3p2wz04
This architecture ensures that even if multiple processes generate CUIDs simultaneously, the combination of timestamp, counter, fingerprint, and random data prevents collisions.
Install visus/cuid2 via Composer:
composer require visus/cuid2
Requirements:
Recommended:
<?php
require_once 'vendor/autoload.php';
use Visus\Cuid2\Cuid2;
// Generate with default length of 24 characters
$cuid = new Cuid2();
// Implicit casting
echo $cuid; // p6p168tx2rxtgyehd3p2wz04
// Explicit casting
echo $cuid->toString(); // p6p168tx2rxtgyehd3p2wz04
// Generate with custom length (4-32 characters)
$shortCuid = new Cuid2(10);
echo $shortCuid; // a1ao2r0lve
<?php
require_once 'vendor/autoload.php';
use Visus\Cuid2\Cuid2;
// Generate with default length of 24 characters
$cuid = Cuid2::generate();
// Implicit casting
echo $cuid; // zbc8kp9qqoh3pvseey6m7nrq
// Explicit casting
echo $cuid->toString(); // zbc8kp9qqoh3pvseey6m7nrq
// Generate with custom length (4-32 characters)
$shortCuid = Cuid2::generate(10);
echo $shortCuid; // rywe9nkxrx
The isValid() method checks if a string follows the CUID2 format.
[!NOTE] This method validates the format only. It does not guarantee that the value was generated by this library or is globally unique.
<?php
require_once 'vendor/autoload.php';
use Visus\Cuid2\Cuid2;
// Validate format
Cuid2::isValid('p6p168tx2rxtgyehd3p2wz04'); // true
Cuid2::isValid('invalid-cuid'); // false
// Validate with expected length
Cuid2::isValid('a1ao2r0lve', expectedLength: 10); // true
Cuid2::isValid('a1ao2r0lve', expectedLength: 24); // false
This library uses base conversion (base16 to base36) as part of the CUID2 generation process. For optimal performance, it is strongly recommended to install or enable the GMP extension.
Performance Comparison (based on benchmarks):
| Hash Size | GMP Average | Pure PHP Average | Performance Gain |
|---|---|---|---|
| SHA3-512 (128 hex chars) | 0.81 μs | 247.77 μs | 306x faster |
| 64 hex chars | 0.58 μs | 78.51 μs | 135x faster |
| 32 hex chars | 0.47 μs | 28.08 μs | 60x faster |
| 16 hex chars | 0.43 μs | 11.94 μs | 28x faster |
Key Takeaways:
Installation:
# Ubuntu/Debian
sudo apt-get install php-gmp
# Fedora/RHEL/CentOS/AlmaLinux/Rocky Linux
sudo dnf install php-gmp
# Or on older systems: sudo yum install php-gmp
# FreeBSD
sudo pkg install php-gmp
# Or via ports: cd /usr/ports/math/php-gmp && make install clean
# NetBSD
sudo pkgin install php-gmp
# Or via pkgsrc: cd /usr/pkgsrc/math/php-gmp && make install
# OpenBSD
sudo pkg_add php-gmp
# Or via ports: cd /usr/ports/math/php-gmp && make install
# macOS (via Homebrew)
brew install gmp
pecl install gmp
# Windows
# GMP is often bundled with PHP installations
# Enable in php.ini by uncommenting:
extension=gmp
# If not bundled, download from PECL or use package manager:
# Via Chocolatey: choco install php-gmp
Run php -m | grep gmp (Unix) or php -m | findstr gmp (Windows) to verify the extension is loaded.
Contributions are welcome! Please see CONTRIBUTING.md for detailed guidelines on:
This project follows Conventional Commits and maintains 100% code coverage.
How can I help you explore Laravel packages today?