PHP - random_bytes
Trieda
Metóda - random_bytes
(PHP 7)
random_bytes() is used to generate a pseudo-random string of
bytes, that can be surely used for cryptographic operations.
As a source of entropy is used "getrandom(2)":https://manpages.debian.org/…om.2.en.html
(on Linux systems) or "CryptGetRandom":https://msdn.microsoft.com/…=vs.85).aspx
(on Windows systems). On other systems (or if none of the previous is
available), /dev/urandom is used.
If none of the above sources can be used, an exception is thrown.
Procedurálne
- function random_bytes (int $length) : string
Parametre
| Názov | Dátový typ | Predvolená hodnota | Popis |
|---|---|---|---|
| $length | int | The length of the requested string in bytes. |
Mávratovej hodnoty
Vracia: string
Returns a string of the requested length containing pseudo-randomly generated bytes.
Príklady
Generating a pseudo-random token (encoded to base64):
<?php
$randomBytes = random_bytes(64);
$token = base64_encode($randomBytes);
echo $token;
?>
Súvisiace manuály
- function bin2hex (string $str) : string
- function random_int () : int
