CoreFiles by mySites.guru
Hmac.php40 lines, 1,118 bytes, from Joomla_5.0.0-alpha3-Alpha-Full_Package.zip
Raw History Compare with 5.0.0-alpha2
md5
f79e0107c2abb245708aeef75471ad84
sha1
c3dad95c12de0069074a40abd485c358a04f290a
sha256
7fcfc6aac852e0638ee7895262fd3bd88521973a5b4504c83bd6fbe982f7e225
<?php

declare(strict_types=1);

namespace Cose\Algorithm\Mac;

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

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) {
            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 3 releases, Joomla 5.0.0-alpha1 to 5.0.0-alpha3. See them all, or every version of this file.