123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452 |
- <?php
- require_once 'Auth/OpenID/CryptUtil.php';
- require_once 'Auth/OpenID.php';
- class Auth_OpenID_MathLibrary {
-
- function longToBinary($long)
- {
- $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;
- }
-
- function binaryToLong($str)
- {
- if ($str === null) {
- return null;
- }
-
-
- $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;
- }
- function base64ToLong($str)
- {
- $b64 = base64_decode($str);
- if ($b64 === false) {
- return false;
- }
- return $this->binaryToLong($b64);
- }
- function longToBase64($str)
- {
- return base64_encode($this->longToBinary($str));
- }
-
- function rand($stop)
- {
- static $duplicate_cache = array();
-
- $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);
-
-
- $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);
-
- } while ($this->cmp($n, $duplicate) < 0);
- return $this->mod($n, $stop);
- }
- }
- class Auth_OpenID_BcMathWrapper extends Auth_OpenID_MathLibrary{
- var $type = 'bcmath';
- function add($x, $y)
- {
- return bcadd($x, $y);
- }
- function sub($x, $y)
- {
- return bcsub($x, $y);
- }
- function pow($base, $exponent)
- {
- return bcpow($base, $exponent);
- }
- function cmp($x, $y)
- {
- return bccomp($x, $y);
- }
- function init($number, $base = 10)
- {
- return $number;
- }
- function mod($base, $modulus)
- {
- return bcmod($base, $modulus);
- }
- function mul($x, $y)
- {
- return bcmul($x, $y);
- }
- function div($x, $y)
- {
- return bcdiv($x, $y);
- }
-
- function _powmod($base, $exponent, $modulus)
- {
- $square = $this->mod($base, $modulus);
- $result = 1;
- while($this->cmp($exponent, 0) > 0) {
- if ($this->mod($exponent, 2)) {
- $result = $this->mod($this->mul($result, $square), $modulus);
- }
- $square = $this->mod($this->mul($square, $square), $modulus);
- $exponent = $this->div($exponent, 2);
- }
- return $result;
- }
- function powmod($base, $exponent, $modulus)
- {
- if (function_exists('bcpowmod')) {
- return bcpowmod($base, $exponent, $modulus);
- } else {
- return $this->_powmod($base, $exponent, $modulus);
- }
- }
- function toString($num)
- {
- return $num;
- }
- }
- class Auth_OpenID_GmpMathWrapper extends Auth_OpenID_MathLibrary{
- var $type = 'gmp';
- function add($x, $y)
- {
- return gmp_add($x, $y);
- }
- function sub($x, $y)
- {
- return gmp_sub($x, $y);
- }
- function pow($base, $exponent)
- {
- return gmp_pow($base, $exponent);
- }
- function cmp($x, $y)
- {
- return gmp_cmp($x, $y);
- }
- function init($number, $base = 10)
- {
- return gmp_init($number, $base);
- }
- function mod($base, $modulus)
- {
- return gmp_mod($base, $modulus);
- }
- function mul($x, $y)
- {
- return gmp_mul($x, $y);
- }
- function div($x, $y)
- {
- return gmp_div_q($x, $y);
- }
- function powmod($base, $exponent, $modulus)
- {
- return gmp_powm($base, $exponent, $modulus);
- }
- function toString($num)
- {
- return gmp_strval($num);
- }
- }
- function Auth_OpenID_math_extensions()
- {
- $result = array();
- if (!defined('Auth_OpenID_BUGGY_GMP')) {
- $result[] =
- array('modules' => array('gmp', 'php_gmp'),
- 'extension' => 'gmp',
- 'class' => 'Auth_OpenID_GmpMathWrapper');
- }
- $result[] = array('modules' => array('bcmath', 'php_bcmath'),
- 'extension' => 'bcmath',
- 'class' => 'Auth_OpenID_BcMathWrapper');
- return $result;
- }
- function Auth_OpenID_detectMathLibrary($exts)
- {
- $loaded = false;
- foreach ($exts as $extension) {
- if (extension_loaded($extension['extension'])) {
- return $extension;
- }
- }
- return false;
- }
- function Auth_OpenID_getMathLib()
- {
-
-
-
- static $lib = null;
- if (isset($lib)) {
- return $lib;
- }
- if (Auth_OpenID_noMathSupport()) {
- $null = null;
- return $null;
- }
-
-
-
- $ext = Auth_OpenID_detectMathLibrary(Auth_OpenID_math_extensions());
- if ($ext === false) {
- $tried = array();
- foreach (Auth_OpenID_math_extensions() as $extinfo) {
- $tried[] = $extinfo['extension'];
- }
- $triedstr = implode(", ", $tried);
- Auth_OpenID_setNoMathSupport();
- $result = null;
- return $result;
- }
-
- $class = $ext['class'];
- $lib = new $class();
- return $lib;
- }
- function Auth_OpenID_setNoMathSupport()
- {
- if (!defined('Auth_OpenID_NO_MATH_SUPPORT')) {
- define('Auth_OpenID_NO_MATH_SUPPORT', true);
- }
- }
- function Auth_OpenID_noMathSupport()
- {
- return defined('Auth_OpenID_NO_MATH_SUPPORT');
- }
|