[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/web-token/jwt-signature-algorithm-rsa/ -> RSAPSS.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  abstract class RSAPSS implements SignatureAlgorithm
  23  {
  24      public function allowedKeyTypes(): array
  25      {
  26          return ['RSA'];
  27      }
  28  
  29      public function verify(JWK $key, string $input, string $signature): bool
  30      {
  31          $this->checkKey($key);
  32          $pub = RSAKey::createFromJWK($key->toPublic());
  33  
  34          return JoseRSA::verify($pub, $input, $signature, $this->getAlgorithm(), JoseRSA::SIGNATURE_PSS);
  35      }
  36  
  37      /**
  38       * @throws InvalidArgumentException if the key is not private
  39       */
  40      public function sign(JWK $key, string $input): string
  41      {
  42          $this->checkKey($key);
  43          if (!$key->has('d')) {
  44              throw new InvalidArgumentException('The key is not a private key.');
  45          }
  46  
  47          $priv = RSAKey::createFromJWK($key);
  48  
  49          return JoseRSA::sign($priv, $input, $this->getAlgorithm(), JoseRSA::SIGNATURE_PSS);
  50      }
  51  
  52      abstract protected function getAlgorithm(): string;
  53  
  54      /**
  55       * @throws InvalidArgumentException if the key type is not allowed
  56       * @throws InvalidArgumentException if the key is not valid
  57       */
  58      private function checkKey(JWK $key): void
  59      {
  60          if (!in_array($key->get('kty'), $this->allowedKeyTypes(), true)) {
  61              throw new InvalidArgumentException('Wrong key type.');
  62          }
  63          foreach (['n', 'e'] as $k) {
  64              if (!$key->has($k)) {
  65                  throw new InvalidArgumentException(sprintf('The key parameter "%s" is missing.', $k));
  66              }
  67          }
  68      }
  69  }


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