ILIAS  release_4-4 Revision
All Data Structures Namespaces Files Functions Variables Modules 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. More...
 
 binaryToLong ($str)
 Given a binary string, returns the binary string converted to a long number. More...
 
 base64ToLong ($str)
 
 longToBase64 ($str)
 
 rand ($stop)
 Returns a random number in the specified range. More...
 

Detailed Description

Definition at line 33 of file BigMath.php.

Member Function Documentation

◆ base64ToLong()

Auth_OpenID_MathLibrary::base64ToLong (   $str)

Definition at line 112 of file BigMath.php.

References binaryToLong().

113  {
114  $b64 = base64_decode($str);
115 
116  if ($b64 === false) {
117  return false;
118  }
119 
120  return $this->binaryToLong($b64);
121  }
binaryToLong($str)
Given a binary string, returns the binary string converted to a long number.
Definition: BigMath.php:86
+ Here is the call graph for this function:

◆ binaryToLong()

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().

87  {
88  if ($str === null) {
89  return null;
90  }
91 
92  // Use array_merge to return a zero-indexed array instead of a
93  // one-indexed array.
94  $bytes = array_merge(unpack('C*', $str));
95 
96  $n = $this->init(0);
97 
98  if ($bytes && ($bytes[0] > 127)) {
99  trigger_error("bytesToNum works only for positive integers.",
100  E_USER_WARNING);
101  return null;
102  }
103 
104  foreach ($bytes as $byte) {
105  $n = $this->mul($n, pow(2, 8));
106  $n = $this->add($n, $byte);
107  }
108 
109  return $n;
110  }
$n
Definition: RandomTest.php:80
+ Here is the caller graph for this function:

◆ longToBase64()

Auth_OpenID_MathLibrary::longToBase64 (   $str)

Definition at line 123 of file BigMath.php.

References longToBinary().

124  {
125  return base64_encode($this->longToBinary($str));
126  }
longToBinary($long)
Given a long integer, returns the number converted to a binary string.
Definition: BigMath.php:45
+ Here is the call graph for this function:

◆ longToBinary()

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().

46  {
47  $cmp = $this->cmp($long, 0);
48  if ($cmp < 0) {
49  $msg = __FUNCTION__ . " takes only positive integers.";
50  trigger_error($msg, E_USER_ERROR);
51  return null;
52  }
53 
54  if ($cmp == 0) {
55  return "\x00";
56  }
57 
58  $bytes = array();
59 
60  while ($this->cmp($long, 0) > 0) {
61  array_unshift($bytes, $this->mod($long, 256));
62  $long = $this->div($long, pow(2, 8));
63  }
64 
65  if ($bytes && ($bytes[0] > 127)) {
66  array_unshift($bytes, 0);
67  }
68 
69  $string = '';
70  foreach ($bytes as $byte) {
71  $string .= pack('C', $byte);
72  }
73 
74  return $string;
75  }
+ Here is the caller graph for this function:

◆ rand()

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().

143  {
144  static $duplicate_cache = array();
145 
146  // Used as the key for the duplicate cache
147  $rbytes = $this->longToBinary($stop);
148 
149  if (array_key_exists($rbytes, $duplicate_cache)) {
150  list($duplicate, $nbytes) = $duplicate_cache[$rbytes];
151  } else {
152  if ($rbytes[0] == "\x00") {
153  $nbytes = Auth_OpenID::bytes($rbytes) - 1;
154  } else {
155  $nbytes = Auth_OpenID::bytes($rbytes);
156  }
157 
158  $mxrand = $this->pow(256, $nbytes);
159 
160  // If we get a number less than this, then it is in the
161  // duplicated range.
162  $duplicate = $this->mod($mxrand, $stop);
163 
164  if (count($duplicate_cache) > 10) {
165  $duplicate_cache = array();
166  }
167 
168  $duplicate_cache[$rbytes] = array($duplicate, $nbytes);
169  }
170 
171  do {
172  $bytes = "\x00" . Auth_OpenID_CryptUtil::getBytes($nbytes);
173  $n = $this->binaryToLong($bytes);
174  // Keep looping if this value is in the low duplicated range
175  } while ($this->cmp($n, $duplicate) < 0);
176 
177  return $this->mod($n, $stop);
178  }
longToBinary($long)
Given a long integer, returns the number converted to a binary string.
Definition: BigMath.php:45
binaryToLong($str)
Given a binary string, returns the binary string converted to a long number.
Definition: BigMath.php:86
static bytes($str)
Count the number of bytes in a string independently of multibyte support conditions.
Definition: OpenID.php:462
$n
Definition: RandomTest.php:80
static getBytes($num_bytes)
Get the specified number of random bytes.
Definition: CryptUtil.php:40
+ Here is the call graph for this function:

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