You are here

function acquia_agent_random_bytes in Acquia Connector 6

Same name and namespace in other branches
  1. 6.2 acquia_agent/acquia_agent.module \acquia_agent_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.

2 calls to acquia_agent_random_bytes()
_acquia_agent_authenticator in acquia_agent/acquia_agent_streams.inc
Creates an authenticator based on xmlrpc params and a HMAC-SHA1.
_acquia_agent_create_authenticator in acquia_agent/acquia_agent.pages.inc
Helper function. Creates an authenticator for xmlrpc calls

File

acquia_agent/acquia_agent.module, line 492
Acquia Agent securely sends information to Acquia Network.

Code

function acquia_agent_random_bytes($count) {
  static $random_state;

  // We initialize with the somewhat random PHP process ID on the first call.
  if (empty($random_state)) {
    $random_state = getmypid();
  }
  $output = '';

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

  // If /dev/urandom is not available or returns no bytes, this loop will
  // generate a good set of pseudo-random bytes on any system.
  while (strlen($output) < $count) {
    $random_state = md5(microtime() . mt_rand() . $random_state);
    $output .= pack('H*', md5(mt_rand() . $random_state));
  }
  return substr($output, 0, $count);
}