FastSitePHP\Security\Crypto\Random

Generates cryptographically secure pseudo-random bytes.

Source Code

GitHub

Example Code

Generate a string of random bytes

// Generate cryptographically secure pseudo-random bytes that
// are suitable for cryptographic use and secure applications.
$bytes = \FastSitePHP\Security\Crypto\Random::bytes(32);

// Convert the bytes to another format:
$hex_bytes = bin2hex($bytes);
$base64_bytes = base64_encode($bytes);

// When using PHP 7 or newer you can simply call [random_bytes()]
$bytes = random_bytes(32);

Methods

bytes($length)

Static Function

This function calls [random_bytes()] for newer versions of PHP and if using any version of PHP 5 then the function is first polyfilled using compatibility functions from the [paragonie/random_compat] library. [paragonie/random_compat] is widely used and known to be secure. It is used in WordPress and many other projects.

Returns: string