Same name and namespace in other branches
  1. 6.x modules/openid/openid.inc \_openid_dh_rand()
2 calls to _openid_dh_rand()
openid_association in modules/openid/openid.module
Attempt to create a shared secret with the OpenID Provider.
_openid_test_endpoint_associate in modules/openid/tests/openid_test.module
OpenID endpoint; handle "associate" requests (see OpenID Authentication 2.0, section 8).

File

modules/openid/openid.inc, line 530
OpenID utility functions.

Code

function _openid_dh_rand($stop) {
  $duplicate_cache =& drupal_static(__FUNCTION__, array());

  // Used as the key for the duplicate cache
  $rbytes = _openid_dh_long_to_binary($stop);
  if (isset($duplicate_cache[$rbytes])) {
    list($duplicate, $nbytes) = $duplicate_cache[$rbytes];
  }
  else {
    if ($rbytes[0] == "\0") {
      $nbytes = strlen($rbytes) - 1;
    }
    else {
      $nbytes = strlen($rbytes);
    }
    $mxrand = _openid_math_pow(256, $nbytes);

    // If we get a number less than this, then it is in the
    // duplicated range.
    $duplicate = _openid_math_mod($mxrand, $stop);
    if (count($duplicate_cache) > 10) {
      $duplicate_cache = array();
    }
    $duplicate_cache[$rbytes] = array(
      $duplicate,
      $nbytes,
    );
  }
  do {
    $bytes = "\0" . drupal_random_bytes($nbytes);
    $n = _openid_dh_binary_to_long($bytes);

    // Keep looping if this value is in the low duplicated range.
  } while (_openid_math_cmp($n, $duplicate) < 0);
  return _openid_math_mod($n, $stop);
}