php-standard-library/secure-random
Generate cryptographically secure random tokens, passwords, nonces, and bytes in PHP. SecureRandom provides simple, reliable APIs built on native CSPRNG sources, suitable for authentication, CSRF protection, and other security-sensitive identifiers.
Str::random() while enforcing cryptographic guarantees. The package’s API mirrors Laravel’s conventions (e.g., hex(), base64()), reducing cognitive load for developers.Str::random() calls with a vetted source.openssl_random_pseudo_bytes() alternative).SecureRandom directly or bind it to Laravel’s container.SecureRandom::hex(32) are intuitive for Laravel devs familiar with Str::random(32). The package’s design avoids Laravel magic, making it portable.Str::random() calls remain unchanged. The package is additive, enabling a gradual migration.random_bytes() stubs), but production use requires real CSPRNG.random_bytes()/random_int(), which may fall back to weaker sources on low-entropy systems (e.g., Docker containers, CI environments). Risk mitigated by monitoring /proc/sys/kernel/random/entropy_avail (Linux) or equivalent.mt_rand(). Benchmark critical paths (e.g., auth token generation) to ensure latency stays under SLA thresholds (e.g., <50ms for 99th percentile).Hash or Encryption facades (manual wiring required).SecureRandom::uuid() (collision probability: ~1 in 2¹²²).SecureRandom via code linting (e.g., PHPStan rules) or documentation-only guidelines?mt_rand() or uniqid()? Deprecate or grandfather?SecureRandom::hex(128).)dieharder, ent, or custom statistical tests.)random_bytes() falls back to a weaker source?SecureRandom::generate()) to standardize usage?Ramsey\Uuid or use the package’s SecureRandom::uuid()?random_bytes() fails in production? (E.g., retry with exponential backoff.)random_bytes() calls, Str::random() for security-critical use cases, and third-party libraries like ramsey/uuid (for random UUIDs).Hash facade (for generating secure secrets).Str::uuid() (for non-security-sensitive UUIDs).paragonie/random_compat (for PHP < 7.0, but not needed here).web-token/jwt-framework (includes its own randomness; avoid duplication).mt_rand(), rand(), uniqid(), or custom PRNG logic.Str::random() calls in security-sensitive paths (auth, CSRF, encryption).git grep, PHPStan, or custom regex patterns.SecureRandom.Str::random().AB test auth flow latency).// Before
$token = Str::random(32);
// After
$token = SecureRandom::hex(32);
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton('secureRandom', function () {
return new \SecureRandom\SecureRandom();
});
}
if (function_exists('legacy_random_token')) {
trigger_deprecation('laravel', '1.0', 'Use SecureRandom instead.');
}
Auth::login() token generation:
// config/auth.php
'password_reset_token' => SecureRandom::hex(64),
AppServiceProvider::boot():
public function boot()
{
view()->composer('*', function ($view) {
$view->with('csrf_token', SecureRandom::hex(32));
});
}
$iv = SecureRandom::hex(16);
$encrypted = openssl_encrypt($data, 'AES-128-CBC', $key, 0, $iv);
SecureRandom::uuid() instead of Ramsey\Uuid:
$uuid = SecureRandom::uuid(); // e.g., "1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed"
feature_abc123).hex(), base64()).How can I help you explore Laravel packages today?