$bits = String::random(8); // 64 bits
$hex = bin2hex($bits); // [0-9a-f]+
Optionally base64-encodes the resulting random string per the following. The
alphabet used by base64_encode() is different than the one we should be using.
When considering the meaty part of the resulting string, however, a bijection
allows to go the from one to another. Given that we're working on random bytes, we
can use safely use base64_encode() without losing any entropy.
public static random ( integer $bytes, array $options = [] ) : string | ||
$bytes | integer | The number of random bytes to generate. |
$options | array | The options used when generating random bytes: - `'encode'` _integer_: If specified, and set to `String::ENCODE_BASE_64`, the resulting value will be base64-encoded, per the notes above. |
return | string | Returns a string of random bytes. |