php-standard-library/secure-random
SecureRandom provides cryptographically secure random bytes and strings in PHP for tokens, passwords, nonces, and IDs. Simple API built on secure system sources, suitable for authentication, session, and security-sensitive workflows.
mt_rand(), uniqid()) in Laravel, addressing OWASP risks (A03: Injection, A05: Security Misconfiguration). Its API (hex(), base64(), uuid()) aligns with Laravel’s Str::random() but enforces cryptographic guarantees, making it ideal for:
Str::random() with a vetted source.Encrypter).random_bytes() in unit tests). The package’s minimalism avoids Laravel-specific dependencies, ensuring portability.SecureRandom directly or bind it to Laravel’s container:
$this->app->singleton(SecureRandom::class, function () {
return new \SecureRandom\SecureRandom();
});
SecureRandom::hex(32) are intuitive for Laravel devs, with 1:1 parity to Str::random(32) for security-critical use cases.Str::random() calls remain unchanged; the package is additive, enabling a gradual migration.random_bytes()), but production use enforces real CSPRNG.random_bytes() may fall back to weaker sources on low-entropy systems (e.g., Docker, CI environments)./proc/sys/kernel/random/entropy_avail (Linux) or use random_int() as a fallback. Log warnings if entropy drops below thresholds.mt_rand(). Critical paths (e.g., auth token generation) may exceed latency SLAs.SecureRandom::hex(128) and optimize caching (e.g., pre-generate CSRF tokens).Hash or Encryption facades.app(SecureRandom::class)->hex(32)).mt_rand() or uniqid()? Deprecate with warnings or grandfather?SecureRandom::hex(128) under load.)dieharder, ent, or custom statistical tests.)random_bytes() falls back to a weaker source?SecureRandom::generate()) to standardize usage across the codebase?Ramsey\Uuid or use the package’s SecureRandom::uuid()?random_bytes() fails in production? (E.g., retry with exponential backoff or degrade to random_int().)random_bytes() calls, Str::random() for security-critical paths, and third-party libraries like ramsey/uuid (for random UUIDs).Hash facade (for generating secure secrets).Str::uuid() (for non-security-sensitive UUIDs).web-token/jwt-framework (which includes its own randomness).Phase 1: Audit (1–2 weeks)
git grep, PHPStan, or custom regex patterns to find:
mt_rand(), rand(), uniqid(), or custom PRNG logic.Str::random() calls in security-sensitive paths (auth, CSRF, encryption).Phase 2: Pilot (2–3 weeks)
Str::random() with SecureRandom::hex() for tokens.SecureRandom to Laravel’s container for DI.app(SecureRandom::class)->hex(32)) for consistency.random_bytes().Phase 3: Rollout (3–4 weeks)
Str::uuid()).mt_rand()/rand().SecureRandom.SecureRandom::base64(32) for CSRF tokens").Phase 4: Enforcement (Ongoing)
phpstan/extension-installer for custom rules.random_bytes() fallback events./proc/sys/kernel/random/entropy_avail < 1000).random_bytes()/random_int()).| Step | Dependency | Owner | Timeline |
|---|---|---|---|
| Audit insecure PRNGs | Codebase access | DevOps/Security | Week 1 |
| Pilot in auth/CSRF | SecureRandom package | Backend Team | Week 2–3 |
| Load/performance tests | Pilot results | QA/Performance | Week 3 |
| Rollout to encryption | Pilot success | Security Team | Week 4 |
| CI/CD enforcement | Static analysis tools | DevOps | Week 5 |
| Deprecate legacy PRNGs | Full adoption | Tech Lead | Ongoing |
How can I help you explore Laravel packages today?