[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/web-token/jwt-signature-algorithm-hmac/ -> HMAC.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 Base64Url\Base64Url;
  17  use function in_array;
  18  use InvalidArgumentException;
  19  use function is_string;
  20  use Jose\Component\Core\JWK;
  21  
  22  abstract class HMAC implements MacAlgorithm
  23  {
  24      public function allowedKeyTypes(): array
  25      {
  26          return ['oct'];
  27      }
  28  
  29      public function verify(JWK $key, string $input, string $signature): bool
  30      {
  31          return hash_equals($this->hash($key, $input), $signature);
  32      }
  33  
  34      public function hash(JWK $key, string $input): string
  35      {
  36          $k = $this->getKey($key);
  37  
  38          return hash_hmac($this->getHashAlgorithm(), $input, $k, true);
  39      }
  40  
  41      /**
  42       * @throws InvalidArgumentException if the key is invalid
  43       */
  44      protected function getKey(JWK $key): string
  45      {
  46          if (!in_array($key->get('kty'), $this->allowedKeyTypes(), true)) {
  47              throw new InvalidArgumentException('Wrong key type.');
  48          }
  49          if (!$key->has('k')) {
  50              throw new InvalidArgumentException('The key parameter "k" is missing.');
  51          }
  52          $k = $key->get('k');
  53          if (!is_string($k)) {
  54              throw new InvalidArgumentException('The key parameter "k" is invalid.');
  55          }
  56  
  57          return Base64Url::decode($k);
  58      }
  59  
  60      abstract protected function getHashAlgorithm(): string;
  61  }


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