[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/vendor/phpseclib/phpseclib/phpseclib/Crypt/DSA/ -> PublicKey.php (source)

   1  <?php
   2  
   3  /**
   4   * DSA Public Key
   5   *
   6   * @author    Jim Wigginton <terrafrost@php.net>
   7   * @copyright 2015 Jim Wigginton
   8   * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
   9   * @link      http://phpseclib.sourceforge.net
  10   */
  11  
  12  namespace phpseclib3\Crypt\DSA;
  13  
  14  use phpseclib3\Crypt\Common;
  15  use phpseclib3\Crypt\DSA;
  16  use phpseclib3\Crypt\DSA\Formats\Signature\ASN1 as ASN1Signature;
  17  
  18  /**
  19   * DSA Public Key
  20   *
  21   * @author  Jim Wigginton <terrafrost@php.net>
  22   */
  23  final class PublicKey extends DSA implements Common\PublicKey
  24  {
  25      use Common\Traits\Fingerprint;
  26  
  27      /**
  28       * Verify a signature
  29       *
  30       * @see self::verify()
  31       * @param string $message
  32       * @param string $signature
  33       * @return mixed
  34       */
  35      public function verify($message, $signature)
  36      {
  37          $format = $this->sigFormat;
  38  
  39          $params = $format::load($signature);
  40          if ($params === false || count($params) != 2) {
  41              return false;
  42          }
  43          extract($params);
  44  
  45          if (self::$engines['OpenSSL'] && in_array($this->hash->getHash(), openssl_get_md_methods())) {
  46              $sig = $format != 'ASN1' ? ASN1Signature::save($r, $s) : $signature;
  47  
  48              $result = openssl_verify($message, $sig, $this->toString('PKCS8'), $this->hash->getHash());
  49  
  50              if ($result != -1) {
  51                  return (bool) $result;
  52              }
  53          }
  54  
  55          $q_1 = $this->q->subtract(self::$one);
  56          if (!$r->between(self::$one, $q_1) || !$s->between(self::$one, $q_1)) {
  57              return false;
  58          }
  59  
  60          $w = $s->modInverse($this->q);
  61          $h = $this->hash->hash($message);
  62          $h = $this->bits2int($h);
  63          list(, $u1) = $h->multiply($w)->divide($this->q);
  64          list(, $u2) = $r->multiply($w)->divide($this->q);
  65          $v1 = $this->g->powMod($u1, $this->p);
  66          $v2 = $this->y->powMod($u2, $this->p);
  67          list(, $v) = $v1->multiply($v2)->divide($this->p);
  68          list(, $v) = $v->divide($this->q);
  69  
  70          return $v->equals($r);
  71      }
  72  
  73      /**
  74       * Returns the public key
  75       *
  76       * @param string $type
  77       * @param array $options optional
  78       * @return string
  79       */
  80      public function toString($type, array $options = [])
  81      {
  82          $type = self::validatePlugin('Keys', $type, 'savePublicKey');
  83  
  84          return $type::savePublicKey($this->p, $this->q, $this->g, $this->y, $options);
  85      }
  86  }