CoreFiles by mySites.guru
HMAC.php49 lines, 1,301 bytes, from Joomla_5.2.0-alpha3-Alpha-Full_Package.zip
Raw History Compare with 5.2.0-alpha2
md5
c8cbe3b678aeb897622b9f0d268bc8ae
sha1
7945a71f09361867126a48f5b7e1ae5fae3c695a
sha256
36ddae8ae703387239e0c7abf734e2da84239d5e0b4a3664c9aea95e1aa0a7e2
<?php

declare(strict_types=1);

namespace Jose\Component\Signature\Algorithm;

use InvalidArgumentException;
use Jose\Component\Core\JWK;
use ParagonIE\ConstantTime\Base64UrlSafe;
use function in_array;
use function is_string;

abstract class HMAC implements MacAlgorithm
{
    public function allowedKeyTypes(): array
    {
        return ['oct'];
    }

    public function verify(JWK $key, string $input, string $signature): bool
    {
        return hash_equals($this->hash($key, $input), $signature);
    }

    public function hash(JWK $key, string $input): string
    {
        $k = $this->getKey($key);

        return hash_hmac($this->getHashAlgorithm(), $input, $k, true);
    }

    protected function getKey(JWK $key): string
    {
        if (! in_array($key->get('kty'), $this->allowedKeyTypes(), true)) {
            throw new InvalidArgumentException('Wrong key type.');
        }
        if (! $key->has('k')) {
            throw new InvalidArgumentException('The key parameter "k" is missing.');
        }
        $k = $key->get('k');
        if (! is_string($k)) {
            throw new InvalidArgumentException('The key parameter "k" is invalid.');
        }

        return Base64UrlSafe::decodeNoPadding($k);
    }

    abstract protected function getHashAlgorithm(): string;
}

The same content is in 19 releases, Joomla 5.1.0-beta1 to 5.2.0-alpha3. See them all, or every version of this file.