FastSitePHP\Security\Crypto\Random

Generates cryptographically secure pseudo-random bytes.

源代码

GitHub

示例代码

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);

方法

bytes($length)

静态功能

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.

返回类型: string