$ show paragonie/random_compat
This repository is a PHP 5.x polyfill library that provides implementations of the `random_bytes()` and `random_int()` cryptographic functions introduced in PHP 7. The library implements a fallback chain of secure random sources: libsodium extension, /dev/urandom (Unix), mcrypt_create_iv(), and CAPICOM (Windows). It throws exceptions when no secure random source is available rather than falling back to insecure alternatives. The library features modular implementation with separate files for each random source, along with utility functions for mbstring.func_overload-safe string operations and safe integer casting. It includes comprehensive unit tests and maintains compatibility with PHP versions from 5.2 through current versions by disabling itself on PHP 7.0+.
The LLM identified 2 files worth highlighting for reuse:
Provides mbstring.func_overload-safe implementations of strlen() and substr() functions that treat strings as raw binary data. This component solves a specific PHP extension interference problem with non-obvious skill and can be extracted to any project needing binary-safe string operations.
Implements RandomCompat_intval() function that safely converts values to integers while handling edge cases with floating-point precision near integer boundaries. Demonstrates deep understanding of PHP's type juggling behaviors and can be reused in any project requiring safe integer conversion.