[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/vendor/phpseclib/phpseclib/phpseclib/Crypt/EC/Formats/Keys/ -> MontgomeryPublic.php (source)

   1  <?php
   2  
   3  /**
   4   * Montgomery Public Key Handler
   5   *
   6   * PHP version 5
   7   *
   8   * @author    Jim Wigginton <terrafrost@php.net>
   9   * @copyright 2015 Jim Wigginton
  10   * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
  11   * @link      http://phpseclib.sourceforge.net
  12   */
  13  
  14  namespace phpseclib3\Crypt\EC\Formats\Keys;
  15  
  16  use phpseclib3\Crypt\EC\BaseCurves\Montgomery as MontgomeryCurve;
  17  use phpseclib3\Crypt\EC\Curves\Curve25519;
  18  use phpseclib3\Crypt\EC\Curves\Curve448;
  19  use phpseclib3\Math\BigInteger;
  20  
  21  /**
  22   * Montgomery Public Key Handler
  23   *
  24   * @author  Jim Wigginton <terrafrost@php.net>
  25   */
  26  abstract class MontgomeryPublic
  27  {
  28      /**
  29       * Is invisible flag
  30       *
  31       */
  32      const IS_INVISIBLE = true;
  33  
  34      /**
  35       * Break a public or private key down into its constituent components
  36       *
  37       * @param string $key
  38       * @param string $password optional
  39       * @return array
  40       */
  41      public static function load($key, $password = '')
  42      {
  43          switch (strlen($key)) {
  44              case 32:
  45                  $curve = new Curve25519();
  46                  break;
  47              case 56:
  48                  $curve = new Curve448();
  49                  break;
  50              default:
  51                  throw new \LengthException('The only supported lengths are 32 and 56');
  52          }
  53  
  54          $components = ['curve' => $curve];
  55          $components['QA'] = [$components['curve']->convertInteger(new BigInteger(strrev($key), 256))];
  56  
  57          return $components;
  58      }
  59  
  60      /**
  61       * Convert an EC public key to the appropriate format
  62       *
  63       * @param \phpseclib3\Crypt\EC\BaseCurves\Montgomery $curve
  64       * @param \phpseclib3\Math\Common\FiniteField\Integer[] $publicKey
  65       * @return string
  66       */
  67      public static function savePublicKey(MontgomeryCurve $curve, array $publicKey)
  68      {
  69          return strrev($publicKey[0]->toBytes());
  70      }
  71  }