[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/vendor/phpseclib/phpseclib/phpseclib/Crypt/EC/Formats/Signature/ -> IEEE.php (source)

   1  <?php
   2  
   3  /**
   4   * IEEE P1363 Signature Handler
   5   *
   6   * PHP version 5
   7   *
   8   * Handles signatures in the format described in
   9   * https://standards.ieee.org/ieee/1363/2049/ and
  10   * https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/sign#ecdsa
  11   *
  12   * @author    Jim Wigginton <terrafrost@php.net>
  13   * @copyright 2016 Jim Wigginton
  14   * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
  15   * @link      http://phpseclib.sourceforge.net
  16   */
  17  
  18  namespace phpseclib3\Crypt\EC\Formats\Signature;
  19  
  20  use phpseclib3\Math\BigInteger;
  21  
  22  /**
  23   * ASN1 Signature Handler
  24   *
  25   * @author  Jim Wigginton <terrafrost@php.net>
  26   */
  27  abstract class IEEE
  28  {
  29      /**
  30       * Loads a signature
  31       *
  32       * @param string $sig
  33       * @return array
  34       */
  35      public static function load($sig)
  36      {
  37          if (!is_string($sig)) {
  38              return false;
  39          }
  40  
  41          $len = strlen($sig);
  42          if ($len & 1) {
  43              return false;
  44          }
  45  
  46          $r = new BigInteger(substr($sig, 0, $len >> 1), 256);
  47          $s = new BigInteger(substr($sig, $len >> 1), 256);
  48  
  49          return compact('r', 's');
  50      }
  51  
  52      /**
  53       * Returns a signature in the appropriate format
  54       *
  55       * @param BigInteger $r
  56       * @param BigInteger $s
  57       * @param string $curve
  58       * @param int $length
  59       * @return string
  60       */
  61      public static function save(BigInteger $r, BigInteger $s, $curve, $length)
  62      {
  63          $r = $r->toBytes();
  64          $s = $s->toBytes();
  65          $length = (int) ceil($length / 8);
  66          return str_pad($r, $length, "\0", STR_PAD_LEFT) . str_pad($s, $length, "\0", STR_PAD_LEFT);
  67      }
  68  }