CoreFiles by mySites.guru
HSKeyAnalyzer.php42 lines, 1,145 bytes, from Joomla_5.1.0-beta1-Beta-Full_Package.zip
Raw History Compare with 5.0.0-beta3
md5
9ba53ac505cf1c535b664eadb0072ad6
sha1
88aab1210dcd39aca7aed682aee5005beca46b62
sha256
8a11c6e9217eb56a91e489bacdca04593352f433fd2c1e9f1a6e9da6a9df158b
<?php

declare(strict_types=1);

namespace Jose\Component\KeyManagement\Analyzer;

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

abstract class HSKeyAnalyzer implements KeyAnalyzer
{
    public function analyze(JWK $jwk, MessageBag $bag): void
    {
        if ($jwk->get('kty') !== 'oct') {
            return;
        }
        if (! $jwk->has('alg') || $jwk->get('alg') !== $this->getAlgorithmName()) {
            return;
        }
        $k = $jwk->get('k');
        if (! is_string($k)) {
            $bag->add(Message::high('The key is not valid'));

            return;
        }
        $k = Base64UrlSafe::decodeNoPadding($k);
        $kLength = 8 * mb_strlen($k, '8bit');
        if ($kLength < $this->getMinimumKeySize()) {
            $bag->add(
                Message::high(sprintf(
                    'HS512 algorithm requires at least %d bits key length.',
                    $this->getMinimumKeySize()
                ))
            );
        }
    }

    abstract protected function getAlgorithmName(): string;

    abstract protected function getMinimumKeySize(): int;
}

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.