summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosh Hoyt <josh@janrain.com>2006-01-23 21:42:04 +0000
committerJosh Hoyt <josh@janrain.com>2006-01-23 21:42:04 +0000
commite450068102aca0e324af68924ad7cb6f0b8bb80b (patch)
treea4dff8a10689078bfa1cdaa898daf45b20468e95
parentf944d0cbf55332bae7f02611ee11b7353e15e526 (diff)
downloadphp-openid-e450068102aca0e324af68924ad7cb6f0b8bb80b.zip
php-openid-e450068102aca0e324af68924ad7cb6f0b8bb80b.tar.gz
php-openid-e450068102aca0e324af68924ad7cb6f0b8bb80b.tar.bz2
[project @ Move randrange out of Auth_OpenID_CryptUtil]
-rw-r--r--Auth/OpenID/CryptUtil.php114
-rw-r--r--Auth/OpenID/DiffieHellman.php2
-rw-r--r--Tests/Auth/OpenID/CryptUtil.php8
3 files changed, 62 insertions, 62 deletions
diff --git a/Auth/OpenID/CryptUtil.php b/Auth/OpenID/CryptUtil.php
index 687d38d..0fbb317 100644
--- a/Auth/OpenID/CryptUtil.php
+++ b/Auth/OpenID/CryptUtil.php
@@ -172,6 +172,63 @@ function Auth_OpenID_binaryToLong($str)
}
/**
+ * Returns a random number in the specified range. This function
+ * accepts $start, $stop, and $step values of arbitrary magnitude
+ * and will utilize the local large-number math library when
+ * available.
+ *
+ * @param integer $start The start of the range, or the minimum
+ * random number to return
+ * @param integer $stop The end of the range, or the maximum
+ * random number to return
+ * @param integer $step The step size, such that $result - ($step
+ * * N) = $start for some N
+ * @return integer $result The resulting randomly-generated number
+ */
+function Auth_OpenID_randrange($stop)
+{
+ static $duplicate_cache = array();
+ $lib =& Auth_OpenID_getMathLib();
+
+ // DO NOT MODIFY THIS VALUE.
+ $rbytes = Auth_OpenID_longToBinary($stop);
+
+ if (array_key_exists($rbytes, $duplicate_cache)) {
+ list($duplicate, $nbytes) = $duplicate_cache[$rbytes];
+ } else {
+ if ($rbytes[0] == "\x00") {
+ $nbytes = strlen($rbytes) - 1;
+ } else {
+ $nbytes = strlen($rbytes);
+ }
+
+ $mxrand = $lib->pow(256, $nbytes);
+
+ // If we get a number less than this, then it is in the
+ // duplicated range.
+ $duplicate = $lib->mod($mxrand, $stop);
+
+ if (count($duplicate_cache) > 10) {
+ $duplicate_cache = array();
+ }
+
+ $duplicate_cache[$rbytes] = array($duplicate, $nbytes);
+ }
+
+ while (1) {
+ $bytes = "\x00" . Auth_OpenID_CryptUtil::getBytes($nbytes);
+ $n = Auth_OpenID_binaryToLong($bytes);
+ // Keep looping if this value is in the low duplicated
+ // range
+ if ($lib->cmp($n, $duplicate) >= 0) {
+ break;
+ }
+ }
+
+ return $lib->mod($n, $stop);
+}
+
+/**
* Auth_OpenID_CryptUtil houses static utility functions.
*
* @package OpenID
@@ -236,63 +293,6 @@ class Auth_OpenID_CryptUtil {
return $str;
}
- /**
- * Returns a random number in the specified range. This function
- * accepts $start, $stop, and $step values of arbitrary magnitude
- * and will utilize the local large-number math library when
- * available.
- *
- * @param integer $start The start of the range, or the minimum
- * random number to return
- * @param integer $stop The end of the range, or the maximum
- * random number to return
- * @param integer $step The step size, such that $result - ($step
- * * N) = $start for some N
- * @return integer $result The resulting randomly-generated number
- */
- function randrange($stop)
- {
- static $duplicate_cache = array();
- $lib =& Auth_OpenID_getMathLib();
-
- // DO NOT MODIFY THIS VALUE.
- $rbytes = Auth_OpenID_longToBinary($stop);
-
- if (array_key_exists($rbytes, $duplicate_cache)) {
- list($duplicate, $nbytes) = $duplicate_cache[$rbytes];
- } else {
- if ($rbytes[0] == "\x00") {
- $nbytes = strlen($rbytes) - 1;
- } else {
- $nbytes = strlen($rbytes);
- }
-
- $mxrand = $lib->pow(256, $nbytes);
-
- // If we get a number less than this, then it is in the
- // duplicated range.
- $duplicate = $lib->mod($mxrand, $stop);
-
- if (count($duplicate_cache) > 10) {
- $duplicate_cache = array();
- }
-
- $duplicate_cache[$rbytes] = array($duplicate, $nbytes);
- }
-
- while (1) {
- $bytes = "\x00" . Auth_OpenID_CryptUtil::getBytes($nbytes);
- $n = Auth_OpenID_binaryToLong($bytes);
- // Keep looping if this value is in the low duplicated
- // range
- if ($lib->cmp($n, $duplicate) >= 0) {
- break;
- }
- }
-
- return $lib->mod($n, $stop);
- }
-
}
/**
diff --git a/Auth/OpenID/DiffieHellman.php b/Auth/OpenID/DiffieHellman.php
index cf4a591..3e12c21 100644
--- a/Auth/OpenID/DiffieHellman.php
+++ b/Auth/OpenID/DiffieHellman.php
@@ -74,7 +74,7 @@ class Auth_OpenID_DiffieHellman {
}
if ($private === null) {
- $r = Auth_OpenID_CryptUtil::randrange($this->mod);
+ $r = Auth_OpenID_randrange($this->mod);
$this->private = $this->lib->add($r, 1);
} else {
$this->private = $private;
diff --git a/Tests/Auth/OpenID/CryptUtil.php b/Tests/Auth/OpenID/CryptUtil.php
index b42016d..6d7b07d 100644
--- a/Tests/Auth/OpenID/CryptUtil.php
+++ b/Tests/Auth/OpenID/CryptUtil.php
@@ -127,7 +127,7 @@ class Tests_Auth_OpenID_BinLongConvertRnd extends PHPUnit_TestCase {
{
$n = $this->lib->init(0);
foreach (range(0, 9) as $i) {
- $rnd = Auth_OpenID_CryptUtil::randrange($this->max);
+ $rnd = Auth_OpenID_randrange($this->max);
$n = $this->lib->add($n, $rnd);
}
$s = Auth_OpenID_longToBinary($n);
@@ -201,8 +201,8 @@ class Tests_Auth_OpenID_RandRange extends PHPUnit_TestCase {
function runTest()
{
$stop = $this->lib->pow(2, 128);
- $a = Auth_OpenID_CryptUtil::randrange($stop);
- $b = Auth_OpenID_CryptUtil::randrange($stop);
+ $a = Auth_OpenID_randrange($stop);
+ $b = Auth_OpenID_randrange($stop);
$this->assertFalse($this->lib->cmp($b, $a) == 0, "Same: $a $b");
@@ -211,7 +211,7 @@ class Tests_Auth_OpenID_RandRange extends PHPUnit_TestCase {
// Make sure that we can generate random numbers that are
// larger than platform int size
- $result = Auth_OpenID_CryptUtil::randrange($n);
+ $result = Auth_OpenID_randrange($n);
// What can we say about the result?
}