Final Class Yiisoft\Security\Random
| Inheritance | Yiisoft\Security\Random |
|---|
Random allows generating random values.
Currently, it has a single method "string". The following extras are available via PHP directly:
random_bytes()for bytes. Note that output may not be ASCII.random_int()for integers.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| string() | Generates a random string of specified length. | Yiisoft\Security\Random |
Method Details
Generates a random string of specified length.
The string generated matches [A-Za-z0-9_-]+ and is transparent to URL-encoding.
| public static string( integer $length = 32 ): string | ||
| $length | integer |
The length of the key in characters. |
| return | string |
The generated random key. |
|---|---|---|
| throws | Exception |
On failure. |
public static function string(int $length = 32): string
{
if ($length < 1) {
throw new InvalidArgumentException('First parameter ($length) must be greater than 0.');
}
/**
* Optimization: we can generate a quarter fewer bits to completely cover the desired length in base64
* @psalm-suppress ArgumentTypeCoercion
*/
$bytes = random_bytes((int) ceil($length * 0.75));
/** @var non-empty-string */
return substr(StringHelper::base64UrlEncode($bytes), 0, $length);
}
Signup or Login in order to comment.