[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/vendor/phpseclib/phpseclib/phpseclib/Crypt/DSA/Formats/Signature/ -> SSH2.php (source)

   1  <?php
   2  
   3  /**
   4   * SSH2 Signature Handler
   5   *
   6   * PHP version 5
   7   *
   8   * Handles signatures in the format used by SSH2
   9   *
  10   * @author    Jim Wigginton <terrafrost@php.net>
  11   * @copyright 2016 Jim Wigginton
  12   * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
  13   * @link      http://phpseclib.sourceforge.net
  14   */
  15  
  16  namespace phpseclib3\Crypt\DSA\Formats\Signature;
  17  
  18  use phpseclib3\Common\Functions\Strings;
  19  use phpseclib3\Math\BigInteger;
  20  
  21  /**
  22   * SSH2 Signature Handler
  23   *
  24   * @author  Jim Wigginton <terrafrost@php.net>
  25   */
  26  abstract class SSH2
  27  {
  28      /**
  29       * Loads a signature
  30       *
  31       * @param string $sig
  32       * @return mixed
  33       */
  34      public static function load($sig)
  35      {
  36          if (!is_string($sig)) {
  37              return false;
  38          }
  39  
  40          $result = Strings::unpackSSH2('ss', $sig);
  41          if ($result === false) {
  42              return false;
  43          }
  44          list($type, $blob) = $result;
  45          if ($type != 'ssh-dss' || strlen($blob) != 40) {
  46              return false;
  47          }
  48  
  49          return [
  50              'r' => new BigInteger(substr($blob, 0, 20), 256),
  51              's' => new BigInteger(substr($blob, 20), 256)
  52          ];
  53      }
  54  
  55      /**
  56       * Returns a signature in the appropriate format
  57       *
  58       * @param \phpseclib3\Math\BigInteger $r
  59       * @param \phpseclib3\Math\BigInteger $s
  60       * @return string
  61       */
  62      public static function save(BigInteger $r, BigInteger $s)
  63      {
  64          if ($r->getLength() > 160 || $s->getLength() > 160) {
  65              return false;
  66          }
  67          return Strings::packSSH2(
  68              'ss',
  69              'ssh-dss',
  70              str_pad($r->toBytes(), 20, "\0", STR_PAD_LEFT) .
  71              str_pad($s->toBytes(), 20, "\0", STR_PAD_LEFT)
  72          );
  73      }
  74  }