includes/diffiehellman.php
changeset 467 e4bbd6fb8df3
parent 436 242353360e37
child 507 586fd7d3202d
--- a/includes/diffiehellman.php	Sat Mar 01 18:55:54 2008 -0500
+++ b/includes/diffiehellman.php	Sat Mar 01 18:56:37 2008 -0500
@@ -14,206 +14,10 @@
  */
 
 /**
- * Class to handle math operations that require the use of >32-bit integers.
- */
-
-class EnanoMath
-{
-  
-  var $api = 'gmp';
-  var $limb = 16;
-  
-  function __construct()
-  {
-    /*
-    // big_int support is untested.
-    if ( extension_loaded('big_int') )
-    {
-      $this->api = 'bigint';
-    }
-    else
-    */
-    if ( function_exists('gmp_init') )
-    {
-      $this->api = 'gmp';
-    }
-    else if ( function_exists('bcpow') )
-    {
-      $this->api = 'bcmath';
-    }
-    else
-    {
-      throw new Exception('dh_err_not_supported');
-    }
-  }
-  
-  function powmod($base, $pow, $mod)
-  {
-    switch($this->api)
-    {
-      case 'bigint':
-        return ( pow($base, $pow) % $mod );
-        break;
-      case 'gmp':
-        return ( function_exists('gmp_powm') ) ? gmp_powm($base, $pow, $mod) : gmp_mod(gmp_pow($base, $pow), $mod);
-        break;
-      case 'bcmath':
-        return ( function_exists('bcpowmod') ) ? bcpowmod($base, $pow, $mod) : bcmod( bcpow($base, $pow), $mod );
-        break;
-    }
-  }
-  
-  function random($len)
-  {
-    switch($this->api)
-    {
-      case 'gmp':
-        return gmp_random($len);
-        break;
-      case 'bcmath':
-        return $this->_bcmath_random($len);
-        break;
-      case 'bigint':
-        return $this->_bigint_random($len);
-        break;
-    }
-  }
-  
-  function _bcmath_random($len)
-  {
-    $len = ( $this->limb / 2 ) * $len;
-    $chars = '0123456789abcdef';
-    $ret = '';
-    for ( $i = 0; $i < $len; $i++ )
-    {
-      $chid = mt_rand ( 0, strlen($chars) - 1 );
-      $chr = $chars{$chid};
-      $ret .= $chr;
-    }
-    return $this->basecon($ret, 16, 10);
-  }
-  
-  function _bigint_random($len)
-  {
-    $len = ( $this->limb / 2 ) * $len;
-    $chars = '0123456789abcdef';
-    $ret = '';
-    for ( $i = 0; $i < $len; $i++ )
-    {
-      $chid = mt_rand ( 0, strlen($chars) - 1 );
-      $chr = $chars{$chid};
-      $ret .= $chr;
-    }
-    return $this->basecon($ret, 16, 10);
-  }
-  
-  function basecon($int, $from, $to)
-  {
-    switch($this->api)
-    {
-      case 'gmp':
-        return gmp_strval(gmp_init($int, $from), $to);
-        break;
-      case 'bcmath':
-        return $this->_bcmath_baseconv($int, $from, $to);
-        break;
-      case 'bigint':
-        return base_convert($int, $from, $to);
-        break;
-    }
-  }
-  
-  function str($res, $base = 10)
-  {
-    switch($this->api)
-    {
-      case 'bigint':
-      case 'bcmath':
-        return strval($this->basecon($res, 10, $base));
-        break;
-      case 'gmp':
-        return gmp_strval($res, $base);
-        break;
-    }
-  }
-  
-  function compare($v1, $v2)
-  {
-    switch($this->api)
-    {
-      case 'bigint':
-        return ( $v1 === $v2 );
-        break;
-      case 'bcmath':
-        return ( bccomp($v1, $v2) === 0 );
-        break;
-      case 'gmp':
-        return ( gmp_cmp($v1, $v2) === 0 );
-        break;
-    }
-  }
-  
-  function _bcmath_baseconv($int, $from, $to)
-  {
-    if ( $from != 10 )
-      $int = $this->_bcmath_base2dec($int, $from);
-    if ( $to != 10 )
-      $int = $this->_bcmath_dec2base($int, $to);
-    return $int;
-  }
-  
-  // from us.php.net/bc:
-  // convert a decimal value to any other base value
-  function _bcmath_dec2base($dec,$base,$digits=FALSE) {
-      if($base<2 or $base>256) die("Invalid Base: ".$base);
-      bcscale(0);
-      $value="";
-      if(!$digits) $digits=$this->_bcmath_digits($base);
-      while($dec>$base-1) {
-          $rest=bcmod($dec,$base);
-          $dec=bcdiv($dec,$base);
-          $value=$digits[$rest].$value;
-      }
-      $value=$digits[intval($dec)].$value;
-      return (string) $value;
-  }
-  
-  // convert another base value to its decimal value
-  function _bcmath_base2dec($value,$base,$digits=FALSE) {
-      if($base<2 or $base>256) die("Invalid Base: ".$base);
-      bcscale(0);
-      if($base<37) $value=strtolower($value);
-      if(!$digits) $digits=$this->_bcmath_digits($base);
-      $size=strlen($value);
-      $dec="0";
-      for($loop=0;$loop<$size;$loop++) {
-          $element=strpos($digits,$value[$loop]);
-          $power=bcpow($base,$size-$loop-1);
-          $dec=bcadd($dec,bcmul($element,$power));
-      }
-      return (string) $dec;
-  }
-  
-  function _bcmath_digits($base) {
-      if($base>64) {
-          $digits="";
-          for($loop=0;$loop<256;$loop++) {
-              $digits.=chr($loop);
-          }
-      } else {
-          $digits ="0123456789abcdefghijklmnopqrstuvwxyz";
-          $digits.="ABCDEFGHIJKLMNOPQRSTUVWXYZ-_";
-      }
-      $digits=substr($digits,0,$base);
-      return (string) $digits;
-  }
-}
-
-/**
  * The Diffie-Hellman key exchange protocol
  */
 
-$GLOBALS['_math'] = new EnanoMath();
+$GLOBALS['_math'] = enanomath_create();
 // Our prime number as a base for operations.
 $GLOBALS['dh_prime'] = '82818079787776757473727170696867666564636261605958575655545352515049484746454443424140393837363534333231302928272625242322212019181716151413121110987654321';