Example #1
0
 /**
  * @param PublicKeyInterface $key
  * @param SignatureInterface $signature
  * @param $hash
  * @return bool
  */
 public function verify(PublicKeyInterface $key, SignatureInterface $signature, $hash)
 {
     $generator = $key->getGenerator();
     $n = $generator->getOrder();
     $point = $key->getPoint();
     $r = $signature->getR();
     $s = $signature->getS();
     $math = $this->adapter;
     if ($math->cmp($r, 1) < 1 || $math->cmp($r, $math->sub($n, 1)) > 0) {
         return false;
     }
     if ($math->cmp($s, 1) < 1 || $math->cmp($s, $math->sub($n, 1)) > 0) {
         return false;
     }
     $modMath = $math->getModularArithmetic($n);
     $c = $math->inverseMod($s, $n);
     $u1 = $modMath->mul($hash, $c);
     $u2 = $modMath->mul($r, $c);
     $xy = $generator->mul($u1)->add($point->mul($u2));
     $v = $math->mod($xy->getX(), $n);
     return $math->cmp($v, $r) == 0;
 }
Example #2
0
 /**
  * @param SignatureInterface $signature
  * @return Sequence
  */
 public function toAsn(SignatureInterface $signature)
 {
     return new Sequence(new Integer($signature->getR()), new Integer($signature->getS()));
 }
Example #3
0
 public function serialize(SignatureInterface $signature, string $algorithm) : string
 {
     return pack('H*', sprintf('%s%s', $this->addPadding($signature->getR(), self::LENGTH[$algorithm]), $this->addPadding($signature->getS(), self::LENGTH[$algorithm])));
 }