Beispiel #1
0
 /**
  * @param GeneratorPoint $G
  * @param $hash
  * @return int|string
  */
 public function truncateHash(GeneratorPoint $G, $hash)
 {
     $hexSize = strlen($this->adapter->decHex($hash));
     $hashBits = $this->adapter->baseConvert($hash, 10, 2);
     if (strlen($hashBits) < $hexSize * 4) {
         $hashBits = str_pad($hashBits, $hexSize * 4, '0', STR_PAD_LEFT);
     }
     $messageHash = $this->adapter->baseConvert(substr($hashBits, 0, NumberSize::bnNumBits($this->adapter, $G->getOrder())), 2, 10);
     return $messageHash;
 }
Beispiel #2
0
 /**
  * @param MathAdapterInterface $math
  * @param NamedCurveFp $c
  * @return Sequence
  */
 private function getCurveAsn(MathAdapterInterface $math, NamedCurveFp $c)
 {
     $a = new OctetString($math->decHex($math->mod($c->getA(), $c->getPrime())));
     $b = new OctetString($math->decHex($math->mod($c->getB(), $c->getPrime())));
     try {
         $seed = CurveRandomSeed::getSeed($c);
         return new Sequence($a, $b, new BitString($seed));
     } catch (\Exception $e) {
         return new Sequence($a, $b);
     }
 }
Beispiel #3
0
 /**
  * @dataProvider getAdapters
  */
 public function testStrictIntegerReturnValues(MathAdapterInterface $math)
 {
     $x = 10;
     $y = 4;
     $mod = $math->mod($x, $y);
     $this->assertTrue(is_string($mod) && !is_resource($mod));
     $add = $math->add($x, $y);
     $this->assertTrue(is_string($add) && !is_resource($add));
     $sub = $math->sub($add, $y);
     $this->assertTrue(is_string($sub) && !is_resource($sub));
     $mul = $math->mul($x, $y);
     $this->assertTrue(is_string($mul) && !is_resource($mul));
     $div = $math->div($mul, $y);
     $this->assertTrue(is_string($div) && !is_resource($div));
     $pow = $math->pow($x, $y);
     $this->assertTrue(is_string($pow) && !is_resource($div));
     $powmod = $math->powmod($x, $y, $y);
     $this->assertTrue(is_string($powmod) && !is_resource($powmod));
     $bitwiseand = $math->bitwiseAnd($x, $y);
     $this->assertTrue(is_string($bitwiseand) && !is_resource($bitwiseand));
     $hexdec = $math->decHex($x);
     $this->assertTrue(is_string($hexdec) && !is_resource($hexdec));
     $dechex = $math->hexDec($hexdec);
     $this->assertTrue(is_string($dechex) && !is_resource($dechex));
 }
 /**
  * @param PointInterface $point
  * @return string
  */
 public function serialize(PointInterface $point)
 {
     $length = CurveOidMapper::getByteSize($point->getCurve()) * 2;
     if ($this->debug) {
         error_log('Detected length: ' . $length);
         error_log('Unpadded:' . $this->adapter->decHex($point->getX()));
         error_log('Unpadded len:' . strlen($this->adapter->decHex($point->getX())));
         error_log('Padded: ' . str_pad($this->adapter->decHex($point->getX()), $length, '0', STR_PAD_LEFT));
     }
     $hexString = '04';
     $hexString .= str_pad($this->adapter->decHex($point->getX()), $length, '0', STR_PAD_LEFT);
     $hexString .= str_pad($this->adapter->decHex($point->getY()), $length, '0', STR_PAD_LEFT);
     if ($this->debug) {
         error_log('Resulting length: ' . strlen($hexString));
         error_log('Hex: ' . $hexString);
     }
     return $hexString;
 }
Beispiel #5
0
 /**
  * Creates a binary signature with R and S coordinates
  *
  * @param Signature $signature
  *
  * @return string
  */
 private function createSignatureHash(Signature $signature) : string
 {
     $length = $this->getSignatureLength();
     return pack('H*', sprintf('%s%s', str_pad($this->adapter->decHex($signature->getR()), $length, '0', STR_PAD_LEFT), str_pad($this->adapter->decHex($signature->getS()), $length, '0', STR_PAD_LEFT)));
 }
Beispiel #6
0
 /**
  * @return string
  */
 private function convertDecToHex($value)
 {
     return $this->adapter->decHex($value);
 }
 /**
  * @param PrivateKeyInterface $key
  * @return int|mixed|string
  */
 private function formatKey(PrivateKeyInterface $key)
 {
     return $this->adapter->decHex($key->getSecret());
 }