checkthiscloud / crockford-random
A PHP library for generating Crockford Base32 random strings
v1.0.4
2025-10-01 14:25 UTC
Requires
- php: ^8.3
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^12.3
Suggests
- brick/math: ^0.8 || ^0.9 || ^0.10 || ^0.11 || ^0.12 - Required for UniqueCrockfordPool with length > 12
- dev-main
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- dev-copilot/fix-d855fff3-d8e5-4bf3-8e28-1a4ebabe6d8b
- dev-michal/unique-pooling
- dev-copilot/fix-b2a6e3b0-71c2-4b11-a8c8-1fefa7903058
- dev-copilot/fix-59998018-3e4d-4d5d-9f93-5007e433c5ad
- dev-hotfix/wrong-name
- dev-copilot/fix-5e29a663-0980-47a0-97e1-8e48aca2c3d6
This package is auto-updated.
Last update: 2025-10-01 14:26:10 UTC
README
A PHP library for generating random strings using Crockford Base32 encoding alphabet.
Features
- Generates cryptographically secure random strings using PHP 8.3+'s
Random\Randomizer
- Uses Crockford Base32 alphabet:
0123456789ABCDEFGHJKMNPQRSTVWXYZ
- Excludes ambiguous characters (I, L, O, U) for better readability
- Type-safe with strict typing enabled
- Comprehensive error handling
- Optional
brick/math
dependency for large unique pools (> 1.15 quintillion codes)
Requirements
- PHP 8.3 or higher
Random\Randomizer
extension (included in PHP 8.3+)
Optional Dependencies
brick/math
(^0.8 || ^0.9 || ^0.10 || ^0.11 || ^0.12) - Required only forUniqueCrockfordPool
with lengths > 12. For most use cases (pool lengths 1-12, which support up to 1.15 quintillion unique codes), native PHP integers are sufficient.
Installation
composer require checkthiscloud/crockford-random
Usage
Basic Random String Generation
<?php use CheckThisCloud\CrockfordRandom\CrockfordRandom; // Generate a random string of specified length $randomString = CrockfordRandom::generate(10); echo $randomString; // Example: "4G2KPQRST3" // Generate empty string $empty = CrockfordRandom::generate(0); echo $empty; // "" // Generate longer strings $longString = CrockfordRandom::generate(32); echo $longString; // Example: "8N2KPQRST34G2KPQRST34G2KPQRST3W"
Unique Code Pool (Experimental)
The UniqueCrockfordPool
class generates unique Crockford Base32 codes of a fixed length, ensuring no duplicates within a pool instance:
<?php use CheckThisCloud\CrockfordRandom\UniqueCrockfordPool; // Create a pool for 6-character codes (capacity: 1,073,741,824 unique codes) $pool = new UniqueCrockfordPool(6); // Get the next unique code $code1 = $pool->next(); // Example: "A3F2K9" $code2 = $pool->next(); // Example: "7Y4MXP" (guaranteed different from code1) // Reserve multiple codes at once $codes = $pool->reserve(100); // Returns array of 100 unique codes // Check pool status echo $pool->issuedCount(); // 102 (2 + 100) echo $pool->capacityInt(); // 1073741824 echo $pool->remaining(); // 1073741722 // Check if a code was issued $pool->hasIssued($code1); // true $pool->hasIssued('ZZZZZZ'); // false // Reset the pool (useful for testing) $pool->reset(); echo $pool->issuedCount(); // 0
Pool Capacity Limits
The pool capacity is 32^length
:
- Length 1-12: Supported without
brick/math
(up to 1.15 quintillion codes at length 12) - Length 13+: Requires
brick/math
library
# Install brick/math for large pools (optional)
composer require brick/math
If you try to create a pool with length > 12 without brick/math
, you'll get a clear error message:
// Without brick/math installed $pool = new UniqueCrockfordPool(13); // Throws: InvalidLength: "Length 13 requires brick/math library. // Install it with: composer require brick/math. // For lengths up to 12, brick/math is not required."
Error Handling
The library throws ValueError
for invalid input:
try { CrockfordRandom::generate(-1); } catch (ValueError $e) { echo $e->getMessage(); // "Length must be positive" }
Testing
Run the tests with PHPUnit:
composer test # or ./vendor/bin/phpunit
Character Set
The library uses the Crockford Base32 alphabet which excludes ambiguous characters:
- Included:
0123456789ABCDEFGHJKMNPQRSTVWXYZ
- Excluded:
I
,L
,O
,U
(to avoid confusion with1
,1
,0
,V
)
License
MIT License