public static function convert($number, $inputFormat = DiffieHellman::FORMAT_NUMBER, $outputFormat = DiffieHellman::FORMAT_BINARY)
 {
     $math = Math\BigInteger\BigInteger::factory();
     if ($inputFormat == $outputFormat) {
         return $number;
     }
     // convert to number
     switch ($inputFormat) {
         case DiffieHellman::FORMAT_BINARY:
         case DiffieHellman::FORMAT_BTWOC:
             $number = $math->binToInt($number);
             break;
         case DiffieHellman::FORMAT_NUMBER:
         default:
             // do nothing
             break;
     }
     // convert to output format
     switch ($outputFormat) {
         case DiffieHellman::FORMAT_BINARY:
             return $math->intToBin($number);
             break;
         case DiffieHellman::FORMAT_BTWOC:
             return $math->intToBin($number, true);
             break;
         case DiffieHellman::FORMAT_NUMBER:
         default:
             return $number;
             break;
     }
 }
示例#2
0
 public function getUserIdFromHash()
 {
     $code = $this->getCode();
     $bigInt = BigInteger::factory('bcmath');
     $userId = \Eva\Stdlib\String\Hash::shortHash($code, true);
     $userId = $bigInt->sub($userId, '100000000');
     return $userId;
 }
示例#3
0
 public function formBigInteger()
 {
     $bigInt = BigInteger::factory('bcmath');
     $x = Rand::getString(100, '0123456789');
     $y = Rand::getString(100, '0123456789');
     $sum = $bigInt->add($x, $y);
     $len = strlen($sum);
     $this->data->bigint = "{$x} + {$y} = {$sum}";
     $this->render();
 }
示例#4
0
 public function setUp()
 {
     try {
         $math = BigInteger\BigInteger::factory();
     } catch (MathException $e) {
         if (strpos($e->getMessage(), 'math support is not detected') !== false) {
             $this->markTestSkipped($e->getMessage());
         } else {
             throw $e;
         }
     }
 }
示例#5
0
 /**
  * Decode a binary long sequence
  *
  * @param  string $data
  * @return int|float|string
  */
 protected function decodeBinLong($data)
 {
     $nbytes = strlen($data);
     if ($nbytes == 0) {
         return 0;
     }
     $long = 0;
     if ($nbytes > 7) {
         if ($this->bigIntegerAdapter === null) {
             $this->bigIntegerAdapter = BigInteger\BigInteger::getDefaultAdapter();
         }
         if (self::$isLittleEndian === true) {
             $data = strrev($data);
         }
         $long = $this->bigIntegerAdapter->binToInt($data, true);
     } else {
         for ($i = 0; $i < $nbytes; $i++) {
             $long += ord($data[$i]) * pow(256, $i);
         }
         if (0x80 <= ord($data[$nbytes - 1])) {
             $long -= pow(2, $nbytes * 8);
             // $long-= 1 << ($nbytes * 8);
         }
     }
     return $long;
 }
示例#6
0
 /**
  * @param mixed $value
  */
 public function __construct($value)
 {
     $this->value = BigIntegerMath::factory()->init($value, 10);
     $this->type = self::XMLRPC_TYPE_I8;
 }
示例#7
0
 /**
  * Constructor; if set construct the object using the parameter array to
  * set values for Prime, Generator and Private.
  * If a Private Key is not set, one will be generated at random.
  *
  * @param string $prime
  * @param string $generator
  * @param string $privateKey
  * @param string $privateKeyFormat
  */
 public function __construct($prime, $generator, $privateKey = null, $privateKeyFormat = self::FORMAT_NUMBER)
 {
     $this->setPrime($prime);
     $this->setGenerator($generator);
     if ($privateKey !== null) {
         $this->setPrivateKey($privateKey, $privateKeyFormat);
     }
     // set up BigInteger adapter
     $this->math = Math\BigInteger\BigInteger::factory();
 }
示例#8
0
 public function testFactoryUnknownAdapterRaisesServiceManagerException()
 {
     $this->setExpectedException('Zend\\ServiceManager\\Exception\\ExceptionInterface');
     BigInt::factory('unknown');
 }