diff options
Diffstat (limited to 'Net')
-rw-r--r-- | Net/OpenID/DiffieHellman.php | 192 | ||||
-rw-r--r-- | Net/OpenID/HMACSHA1.php | 50 | ||||
-rw-r--r-- | Net/OpenID/KVForm.php | 118 |
3 files changed, 180 insertions, 180 deletions
diff --git a/Net/OpenID/DiffieHellman.php b/Net/OpenID/DiffieHellman.php index 47b7cca..dae59ee 100644 --- a/Net/OpenID/DiffieHellman.php +++ b/Net/OpenID/DiffieHellman.php @@ -1,120 +1,120 @@ <?php if (extension_loaded('gmp') || @dl('gmp.' . PHP_SHLIB_SUFFIX) || - @dl('php_gmp.' . PHP_SHLIB_SUFFIX)) { + @dl('php_gmp.' . PHP_SHLIB_SUFFIX)) { - define('Net_OpenID_math_type', 'gmp'); + define('Net_OpenID_math_type', 'gmp'); - // XXX: untested! - class Net_OpenID_DiffieHellman { - var $DEFAULT_MOD = '155172898181473697471232257763715539915724801966915404479707795314057629378541917580651227423698188993727816152646631438561595825688188889951272158842675419950341258706556549803580104870537681476726513255747040765857479291291572334510643245094715007229621094194349783925984760375594985848253359305585439638443'; + // XXX: untested! + class Net_OpenID_DiffieHellman { + var $DEFAULT_MOD = '155172898181473697471232257763715539915724801966915404479707795314057629378541917580651227423698188993727816152646631438561595825688188889951272158842675419950341258706556549803580104870537681476726513255747040765857479291291572334510643245094715007229621094194349783925984760375594985848253359305585439638443'; - var $DEFAULT_GEN = '2'; + var $DEFAULT_GEN = '2'; - var $mod; - var $gen; - var $private; + var $mod; + var $gen; + var $private; - function generateRandom() { - // XXX: not cryptographically secure (potentially predictable) + function generateRandom() { + // XXX: not cryptographically secure (potentially predictable) $limb_cnt = 31; do { $rdm = gmp_random($limb_cnt--); } while (gmp_cmp( $minval, $rdm) > 0); return $rdm; - } + } - function Net_OpenID_DiffieHellman($mod=NULL, $gen=NULL, $private=NULL) { - if ($mod === NULL) { - $this->mod = gmp_init($this->DEFAULT_MOD, 10); - } else { - $this->mod = $mod; - } + function Net_OpenID_DiffieHellman($mod=NULL, $gen=NULL, $private=NULL) { + if ($mod === NULL) { + $this->mod = gmp_init($this->DEFAULT_MOD, 10); + } else { + $this->mod = $mod; + } - if ($gen === NULL) { - $this->gen = gmp_init($this->DEFAULT_GEN, 10); - } else { - $this->gen = $gen; - } + if ($gen === NULL) { + $this->gen = gmp_init($this->DEFAULT_GEN, 10); + } else { + $this->gen = $gen; + } - $this->private = - $private === NULL ? $this->generateRandom() : $private; + $this->private = + $private === NULL ? $this->generateRandom() : $private; - $this->public = user_error("not implemented", E_USER_ERROR); - } + $this->public = user_error("not implemented", E_USER_ERROR); + } - function createKeyExchange( ) { - return Net_OpenID_BigInt::powm( $this->g, $this->x, $this->p); - } + function createKeyExchange( ) { + return Net_OpenID_BigInt::powm( $this->g, $this->x, $this->p); + } - function decryptKeyExchange( $keyEx ) { - return Net_OpenID_BigInt::powm( $keyEx, $this->x, $this->p ); - } - } + function decryptKeyExchange( $keyEx ) { + return Net_OpenID_BigInt::powm( $keyEx, $this->x, $this->p ); + } + } } elseif (extension_loaded('bcmath') || @dl('bcmath.' . PHP_SHLIB_SUFFIX) || - @dl('php_bcmath.' . PHP_SHLIB_SUFFIX)) { - - define('Net_OpenID_math_type', 'bcmath'); - - if (!function_exists('bcpowmod')) { - // PHP4 does not expose bcpowmod, so we have to implement it here - /** - * (base ^ exponent) % modulus - */ - function bcpowmod($base, $exponent, $modulus) { - $square = bcmod($base, $modulus); - $result = '1'; - while( bccomp( $exponent, 0 ) > 0 ) { - if (bcmod($exponent, 2)) { - // result = (result * square) % modulus - $result = bcmod(bcmul($result, $square), $modulus); - } - $square = bcmod(bcmul($square, $square), $modulus); - $exponent = bcdiv($exponent, 2); - } - return $result; - } - } - - class Net_OpenID_DiffieHellman { - var $DEFAULT_MOD = '155172898181473697471232257763715539915724801966915404479707795314057629378541917580651227423698188993727816152646631438561595825688188889951272158842675419950341258706556549803580104870537681476726513255747040765857479291291572334510643245094715007229621094194349783925984760375594985848253359305585439638443'; - - var $DEFAULT_GEN = '2'; - - var $mod; - var $gen; - var $private; - var $public; - - function Net_OpenID_DiffieHellman($mod=NULL, $gen=NULL, $private=NULL) { - $this->mod = $mod === NULL ? $this->DEFAULT_MOD : $mod; - $this->gen = $gen === NULL ? $this->DEFAULT_GEN : $gen; - $this->private = - $private === NULL ? $this->generateRandom() : $private; - - $this->public = bcpowmod($this->gen, $this->private, $this->mod); - } - - function generateRandom() { - // XXX: not cryptographically secure (predictable!!!) - // XXX: also, way too small (usually) - // FIXME - return mt_rand(1, $this->mod); - } - - function getSharedSecret($composite) { - return bcpowmod($composite, $this->private, $this->mod); - } - - function getPublicKey() { - return $this->public; - } - - } + @dl('php_bcmath.' . PHP_SHLIB_SUFFIX)) { + + define('Net_OpenID_math_type', 'bcmath'); + + if (!function_exists('bcpowmod')) { + // PHP4 does not expose bcpowmod, so we have to implement it here + /** + * (base ^ exponent) % modulus + */ + function bcpowmod($base, $exponent, $modulus) { + $square = bcmod($base, $modulus); + $result = '1'; + while( bccomp( $exponent, 0 ) > 0 ) { + if (bcmod($exponent, 2)) { + // result = (result * square) % modulus + $result = bcmod(bcmul($result, $square), $modulus); + } + $square = bcmod(bcmul($square, $square), $modulus); + $exponent = bcdiv($exponent, 2); + } + return $result; + } + } + + class Net_OpenID_DiffieHellman { + var $DEFAULT_MOD = '155172898181473697471232257763715539915724801966915404479707795314057629378541917580651227423698188993727816152646631438561595825688188889951272158842675419950341258706556549803580104870537681476726513255747040765857479291291572334510643245094715007229621094194349783925984760375594985848253359305585439638443'; + + var $DEFAULT_GEN = '2'; + + var $mod; + var $gen; + var $private; + var $public; + + function Net_OpenID_DiffieHellman($mod=NULL, $gen=NULL, $private=NULL) { + $this->mod = $mod === NULL ? $this->DEFAULT_MOD : $mod; + $this->gen = $gen === NULL ? $this->DEFAULT_GEN : $gen; + $this->private = + $private === NULL ? $this->generateRandom() : $private; + + $this->public = bcpowmod($this->gen, $this->private, $this->mod); + } + + function generateRandom() { + // XXX: not cryptographically secure (predictable!!!) + // XXX: also, way too small (usually) + // FIXME + return mt_rand(1, $this->mod); + } + + function getSharedSecret($composite) { + return bcpowmod($composite, $this->private, $this->mod); + } + + function getPublicKey() { + return $this->public; + } + + } } else { - trigger_error("No usable big int library present (gmp or bcmath). " . - "Only dumb mode OpenID is available.", - E_USER_NOTICE); + trigger_error("No usable big int library present (gmp or bcmath). " . + "Only dumb mode OpenID is available.", + E_USER_NOTICE); } diff --git a/Net/OpenID/HMACSHA1.php b/Net/OpenID/HMACSHA1.php index 781e0cd..feb04ef 100644 --- a/Net/OpenID/HMACSHA1.php +++ b/Net/OpenID/HMACSHA1.php @@ -9,33 +9,33 @@ if (FALSE && function_exists('mhash')) { } else { if (!function_exists('sha1')) { - // XXX: include the SHA1 code from Dan Libby's OpenID library - trigger_error('No SHA1 function found', E_USER_ERROR); - } else { - function sha1_raw($text) { - $hex = sha1($text); - $raw = ''; - for ($i = 0; $i < 40; $i += 2) { - $hexcode = substr($hex, $i, 2); - $charcode = (int)base_convert($hexcode, 16, 10); - $raw .= chr($charcode); - } - return $raw; - } - } + // XXX: include the SHA1 code from Dan Libby's OpenID library + trigger_error('No SHA1 function found', E_USER_ERROR); + } else { + function sha1_raw($text) { + $hex = sha1($text); + $raw = ''; + for ($i = 0; $i < 40; $i += 2) { + $hexcode = substr($hex, $i, 2); + $charcode = (int)base_convert($hexcode, 16, 10); + $raw .= chr($charcode); + } + return $raw; + } + } - function Net_OpenID_HMACSHA1($key, $text) { - if (strlen($key) > SHA1_BLOCKSIZE) { - $key = sha1_raw($key, TRUE); - } + function Net_OpenID_HMACSHA1($key, $text) { + if (strlen($key) > SHA1_BLOCKSIZE) { + $key = sha1_raw($key, TRUE); + } - $key = str_pad($key, SHA1_BLOCKSIZE, chr(0x00)); - $ipad = str_repeat(chr(0x36), SHA1_BLOCKSIZE); - $opad = str_repeat(chr(0x5c), SHA1_BLOCKSIZE); - $hash1 = sha1_raw(($key ^ $ipad) . $text, TRUE); - $hmac = sha1_raw(($key ^ $opad) . $hash1, TRUE); - return $hmac; - } + $key = str_pad($key, SHA1_BLOCKSIZE, chr(0x00)); + $ipad = str_repeat(chr(0x36), SHA1_BLOCKSIZE); + $opad = str_repeat(chr(0x5c), SHA1_BLOCKSIZE); + $hash1 = sha1_raw(($key ^ $ipad) . $text, TRUE); + $hmac = sha1_raw(($key ^ $opad) . $hash1, TRUE); + return $hmac; + } } ?>
\ No newline at end of file diff --git a/Net/OpenID/KVForm.php b/Net/OpenID/KVForm.php index 764ad77..ab06d32 100644 --- a/Net/OpenID/KVForm.php +++ b/Net/OpenID/KVForm.php @@ -1,74 +1,74 @@ <?php class Net_OpenID_KVForm { - function arrayToKV($values) { - $serialized = ''; - foreach ($values as $key => $value) { - if (strpos($key, ':') !== FALSE) { - trigger_error('":" in key:' . addslashes($key), - E_USER_WARNING); - return NULL; - } + function arrayToKV($values) { + $serialized = ''; + foreach ($values as $key => $value) { + if (strpos($key, ':') !== FALSE) { + trigger_error('":" in key:' . addslashes($key), + E_USER_WARNING); + return NULL; + } - if (strpos($key, "\n") !== FALSE) { - trigger_error('"\n" in key:' . addslashes($key), - E_USER_WARNING); - return NULL; - } + if (strpos($key, "\n") !== FALSE) { + trigger_error('"\n" in key:' . addslashes($key), + E_USER_WARNING); + return NULL; + } - if (strpos($value, "\n") !== FALSE) { - trigger_error('"\n" in value:' . addslashes($key), - E_USER_WARNING); - return NULL; - } - $serialized .= "$key:$value\n"; - } - return $serialized; - } + if (strpos($value, "\n") !== FALSE) { + trigger_error('"\n" in value:' . addslashes($key), + E_USER_WARNING); + return NULL; + } + $serialized .= "$key:$value\n"; + } + return $serialized; + } - function kvToArray($kvs) { - $lines = explode("\n", $kvs); + function kvToArray($kvs) { + $lines = explode("\n", $kvs); - $last = array_pop($lines); - if ($last !== '') { - trigger_error('No newline at end of kv string:' . addslashes($kvs), - E_USER_WARNING); - array_push($lines, $last); - } + $last = array_pop($lines); + if ($last !== '') { + trigger_error('No newline at end of kv string:' . addslashes($kvs), + E_USER_WARNING); + array_push($lines, $last); + } - $values = array(); + $values = array(); - for ($lineno = 0; $lineno < count($lines); $lineno++) { - $line = $lines[$lineno]; - $kv = explode(':', $line, 2); - if (count($kv) != 2) { - $esc = addslashes($line); - trigger_error("No colon on line $lineno: $esc", - E_USER_WARNING); - continue; - } + for ($lineno = 0; $lineno < count($lines); $lineno++) { + $line = $lines[$lineno]; + $kv = explode(':', $line, 2); + if (count($kv) != 2) { + $esc = addslashes($line); + trigger_error("No colon on line $lineno: $esc", + E_USER_WARNING); + continue; + } - $key = $kv[0]; - $tkey = trim($key); - if ($tkey != $key) { - $esc = addslashes($key); - trigger_error("Whitespace in key on line $lineno: '$esc'", - E_USER_WARNING); - } + $key = $kv[0]; + $tkey = trim($key); + if ($tkey != $key) { + $esc = addslashes($key); + trigger_error("Whitespace in key on line $lineno: '$esc'", + E_USER_WARNING); + } - $value = $kv[1]; - $tval = trim($value); - if ($tval != $value) { - $esc = addslashes($value); - trigger_error("Whitespace in value on line $lineno: '$esc'", - E_USER_WARNING); - } + $value = $kv[1]; + $tval = trim($value); + if ($tval != $value) { + $esc = addslashes($value); + trigger_error("Whitespace in value on line $lineno: '$esc'", + E_USER_WARNING); + } - $values[$tkey] = $tval; - } - - return $values; - } + $values[$tkey] = $tval; + } + + return $values; + } } ?>
\ No newline at end of file |