W3cubDocs

/Drupal 8

public static function Crypt::randomBytes

public static Crypt::randomBytes($count)

Returns a string of highly randomized bytes (over the full 8-bit range).

This function is better than simply calling mt_rand() or any other built-in PHP function because it can return a long string of bytes (compared to < 4 bytes normally from mt_rand()) and uses the best available pseudo-random source.

In PHP 7 and up, this uses the built-in PHP function random_bytes(). In older PHP versions, this uses the random_bytes() function provided by the random_compat library.

Parameters

int $count: The number of characters (bytes) to return in the string.

Return value

string A randomly generated string.

File

core/lib/Drupal/Component/Utility/Crypt.php, line 30

Class

Crypt
Utility class for cryptographically-secure string handling routines.

Namespace

Drupal\Component\Utility

Code

public static function randomBytes($count) {
  return random_bytes($count);
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Component!Utility!Crypt.php/function/Crypt::randomBytes/8.1.x