function drupal_random_bytes

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.

Parameters

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

8 calls to drupal_random_bytes()
drupal_get_private_key in drupal/core/includes/common.inc
Ensures the private key variable used to generate tokens is set.
drupal_install_config_directories in drupal/core/includes/install.inc
Creates the config directory and ensures it is operational.
drupal_session_regenerate in drupal/core/includes/session.inc
Called when an anonymous user becomes authenticated or vice-versa.
install_settings_form_submit in drupal/core/includes/install.core.inc
Form submission handler for install_settings_form().
Php::generate in drupal/core/lib/Drupal/Component/Uuid/Php.php
Implements Drupal\Component\Uuid\UuidInterface::generate().

... See full list

File

drupal/core/includes/bootstrap.inc, line 1919
Functions that need to be loaded on every Drupal request.

Code

function drupal_random_bytes($count) {

  // $random_state does not use drupal_static as it stores random bytes.
  static $random_state, $bytes, $php_compatible;

  // Initialize on the first call. The contents of $_SERVER includes a mix of
  // user-specific and system information that varies a little with each page.
  if (!isset($random_state)) {
    $random_state = print_r($_SERVER, TRUE);
    if (function_exists('getmypid')) {

      // Further initialize with the somewhat random PHP process ID.
      $random_state .= getmypid();
    }
    $bytes = '';
  }
  if (strlen($bytes) < $count) {

    // PHP versions prior 5.3.4 experienced openssl_random_pseudo_bytes()
    // locking on Windows and rendered it unusable.
    if (!isset($php_compatible)) {
      $php_compatible = version_compare(PHP_VERSION, '5.3.4', '>=');
    }

    // /dev/urandom is available on many *nix systems and is considered the
    // best commonly available pseudo-random source.
    if ($fh = @fopen('/dev/urandom', 'rb')) {

      // PHP only performs buffered reads, so in reality it will always read
      // at least 4096 bytes. Thus, it costs nothing extra to read and store
      // that much so as to speed any additional invocations.
      $bytes .= fread($fh, max(4096, $count));
      fclose($fh);
    }
    elseif ($php_compatible && function_exists('openssl_random_pseudo_bytes')) {
      $bytes .= openssl_random_pseudo_bytes($count - strlen($bytes));
    }

    // If /dev/urandom is not available or returns no bytes, this loop will
    // generate a good set of pseudo-random bytes on any system.
    // Note that it may be important that our $random_state is passed
    // through hash() prior to being rolled into $output, that the two hash()
    // invocations are different, and that the extra input into the first one -
    // the microtime() - is prepended rather than appended. This is to avoid
    // directly leaking $random_state via the $output stream, which could
    // allow for trivial prediction of further "random" numbers.
    while (strlen($bytes) < $count) {
      $random_state = hash('sha256', microtime() . mt_rand() . $random_state);
      $bytes .= hash('sha256', mt_rand() . $random_state, TRUE);
    }
  }
  $output = substr($bytes, 0, $count);
  $bytes = substr($bytes, $count);
  return $output;
}