CoreFiles by mySites.guru
Hmac.php43 lines, 1,208 bytes, from Joomla_5.0.0-alpha4-Alpha-Full_Package.zip
Raw History Compare with 5.0.0-alpha3
md5
5d1c9e94dfa9b9ff31d700a320883648
sha1
031a6a2756c5baf39124eec667a93ee771c87dc6
sha256
6457593f33e29b3661580ffa694cb125fd1f058495d7aecc953e7d30df951f4b
<?php

declare(strict_types=1);

namespace Cose\Algorithm\Mac;

use Cose\Key\Key;
use Cose\Key\SymmetricKey;
use InvalidArgumentException;

/**
 * @see \Cose\Tests\Algorithm\Mac\HmacTest
 */
abstract class Hmac implements Mac
{
    public function hash(string $data, Key $key): string
    {
        $this->checKey($key);
        $signature = hash_hmac($this->getHashAlgorithm(), $data, (string) $key->get(SymmetricKey::DATA_K), true);

        return mb_substr($signature, 0, intdiv($this->getSignatureLength(), 8), '8bit');
    }

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

    abstract protected function getHashAlgorithm(): string;

    abstract protected function getSignatureLength(): int;

    private function checKey(Key $key): void
    {
        if ($key->type() !== Key::TYPE_OCT && $key->type() !== Key::TYPE_NAME_OCT) {
            throw new InvalidArgumentException('Invalid key. Must be of type symmetric');
        }

        if (! $key->has(SymmetricKey::DATA_K)) {
            throw new InvalidArgumentException('Invalid key. The value of the key is missing');
        }
    }
}

The same content is in 38 releases, Joomla 5.0.0-alpha4 to 5.2.0-alpha3. See them all, or every version of this file.