ILIAS  Release_4_1_x_branch Revision 61804
 All Data Structures Namespaces Files Functions Variables Groups Pages
Auth_OpenID_MathLibrary Class Reference
+ Inheritance diagram for Auth_OpenID_MathLibrary:
+ Collaboration diagram for Auth_OpenID_MathLibrary:

Public Member Functions

 longToBinary ($long)
 Given a long integer, returns the number converted to a binary string.
 binaryToLong ($str)
 Given a binary string, returns the binary string converted to a long number.
 base64ToLong ($str)
 longToBase64 ($str)
 rand ($stop)
 Returns a random number in the specified range.

Detailed Description

Definition at line 33 of file BigMath.php.

Member Function Documentation

Auth_OpenID_MathLibrary::base64ToLong (   $str)

Definition at line 112 of file BigMath.php.

References binaryToLong().

{
$b64 = base64_decode($str);
if ($b64 === false) {
return false;
}
return $this->binaryToLong($b64);
}

+ Here is the call graph for this function:

Auth_OpenID_MathLibrary::binaryToLong (   $str)

Given a binary string, returns the binary string converted to a long number.

Parameters
string$binaryThe binary version of a long number, probably as a result of calling longToBinary
Returns
integer $long The long number equivalent of the binary string $str

Definition at line 86 of file BigMath.php.

References $n.

Referenced by base64ToLong(), and rand().

{
if ($str === null) {
return null;
}
// Use array_merge to return a zero-indexed array instead of a
// one-indexed array.
$bytes = array_merge(unpack('C*', $str));
$n = $this->init(0);
if ($bytes && ($bytes[0] > 127)) {
trigger_error("bytesToNum works only for positive integers.",
E_USER_WARNING);
return null;
}
foreach ($bytes as $byte) {
$n = $this->mul($n, pow(2, 8));
$n = $this->add($n, $byte);
}
return $n;
}

+ Here is the caller graph for this function:

Auth_OpenID_MathLibrary::longToBase64 (   $str)

Definition at line 123 of file BigMath.php.

References longToBinary().

{
return base64_encode($this->longToBinary($str));
}

+ Here is the call graph for this function:

Auth_OpenID_MathLibrary::longToBinary (   $long)

Given a long integer, returns the number converted to a binary string.

This function accepts long integer values of arbitrary magnitude and uses the local large-number math library when available.

Parameters
integer$longThe long number (can be a normal PHP integer or a number created by one of the available long number libraries)
Returns
string $binary The binary version of $long

Definition at line 45 of file BigMath.php.

Referenced by longToBase64(), and rand().

{
$cmp = $this->cmp($long, 0);
if ($cmp < 0) {
$msg = __FUNCTION__ . " takes only positive integers.";
trigger_error($msg, E_USER_ERROR);
return null;
}
if ($cmp == 0) {
return "\x00";
}
$bytes = array();
while ($this->cmp($long, 0) > 0) {
array_unshift($bytes, $this->mod($long, 256));
$long = $this->div($long, pow(2, 8));
}
if ($bytes && ($bytes[0] > 127)) {
array_unshift($bytes, 0);
}
$string = '';
foreach ($bytes as $byte) {
$string .= pack('C', $byte);
}
return $string;
}

+ Here is the caller graph for this function:

Auth_OpenID_MathLibrary::rand (   $stop)

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.

Parameters
integer$startThe start of the range, or the minimum random number to return
integer$stopThe end of the range, or the maximum random number to return
integer$stepThe step size, such that $result - ($step
  • N) = $start for some N
Returns
integer $result The resulting randomly-generated number

Definition at line 142 of file BigMath.php.

References $n, binaryToLong(), Auth_OpenID\bytes(), Auth_OpenID_CryptUtil\getBytes(), and longToBinary().

{
static $duplicate_cache = array();
// Used as the key for the duplicate cache
$rbytes = $this->longToBinary($stop);
if (array_key_exists($rbytes, $duplicate_cache)) {
list($duplicate, $nbytes) = $duplicate_cache[$rbytes];
} else {
if ($rbytes[0] == "\x00") {
$nbytes = Auth_OpenID::bytes($rbytes) - 1;
} else {
$nbytes = Auth_OpenID::bytes($rbytes);
}
$mxrand = $this->pow(256, $nbytes);
// If we get a number less than this, then it is in the
// duplicated range.
$duplicate = $this->mod($mxrand, $stop);
if (count($duplicate_cache) > 10) {
$duplicate_cache = array();
}
$duplicate_cache[$rbytes] = array($duplicate, $nbytes);
}
do {
$bytes = "\x00" . Auth_OpenID_CryptUtil::getBytes($nbytes);
$n = $this->binaryToLong($bytes);
// Keep looping if this value is in the low duplicated range
} while ($this->cmp($n, $duplicate) < 0);
return $this->mod($n, $stop);
}

+ Here is the call graph for this function:


The documentation for this class was generated from the following file: