[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/web-token/jwt-signature-algorithm-rsa/ -> RSA.php (source)

   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  /*
   6   * The MIT License (MIT)
   7   *
   8   * Copyright (c) 2014-2020 Spomky-Labs
   9   *
  10   * This software may be modified and distributed under the terms
  11   * of the MIT license.  See the LICENSE file for details.
  12   */
  13  
  14  namespace Jose\Component\Signature\Algorithm;
  15  
  16  use function in_array;
  17  use InvalidArgumentException;
  18  use Jose\Component\Core\JWK;
  19  use Jose\Component\Core\Util\RSAKey;
  20  use Jose\Component\Signature\Algorithm\Util\RSA as JoseRSA;
  21  
  22  /**
  23   * @deprecated Please use either RSAPSS or RSAPKCS1 depending on the padding mode
  24   */
  25  abstract class RSA implements SignatureAlgorithm
  26  {
  27      public function allowedKeyTypes(): array
  28      {
  29          return ['RSA'];
  30      }
  31  
  32      public function verify(JWK $key, string $input, string $signature): bool
  33      {
  34          $this->checkKey($key);
  35          $pub = RSAKey::createFromJWK($key->toPublic());
  36  
  37          return JoseRSA::verify($pub, $input, $signature, $this->getAlgorithm(), $this->getSignatureMethod());
  38      }
  39  
  40      /**
  41       * @throws InvalidArgumentException if the key is not private
  42       */
  43      public function sign(JWK $key, string $input): string
  44      {
  45          $this->checkKey($key);
  46          if (!$key->has('d')) {
  47              throw new InvalidArgumentException('The key is not a private key.');
  48          }
  49  
  50          $priv = RSAKey::createFromJWK($key);
  51  
  52          return JoseRSA::sign($priv, $input, $this->getAlgorithm(), $this->getSignatureMethod());
  53      }
  54  
  55      abstract protected function getAlgorithm(): string;
  56  
  57      abstract protected function getSignatureMethod(): int;
  58  
  59      /**
  60       * @throws InvalidArgumentException if the key type is not allowed
  61       * @throws InvalidArgumentException if the key is invalid
  62       */
  63      private function checkKey(JWK $key): void
  64      {
  65          if (!in_array($key->get('kty'), $this->allowedKeyTypes(), true)) {
  66              throw new InvalidArgumentException('Wrong key type.');
  67          }
  68          foreach (['n', 'e'] as $k) {
  69              if (!$key->has($k)) {
  70                  throw new InvalidArgumentException(sprintf('The key parameter "%s" is missing.', $k));
  71              }
  72          }
  73      }
  74  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer