Debian Codegraph @

lib/3rdParty/composer/web-token/jwt-library/KeyManagement/Analyzer/HSKeyAnalyzer.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Jose\Component\KeyManagement\Analyzer;
6
7 use Jose\Component\Core\JWK;
8 use Jose\Component\Core\Util\Base64UrlSafe;
9 use function is_string;
10
11 abstract class HSKeyAnalyzer implements KeyAnalyzer
12 {
13 public function analyze(JWK $jwk, MessageBag $bag): void
14 {
15 if ($jwk->get('kty') !== 'oct') {
16 return;
17 }
18 if (! $jwk->has('alg') || $jwk->get('alg') !== $this->getAlgorithmName()) {
19 return;
20 }
21 $k = $jwk->get('k');
22 if (! is_string($k)) {
23 $bag->add(Message::high('The key is not valid'));
24
25 return;
26 }
27 $k = Base64UrlSafe::decodeNoPadding($k);
28 $kLength = 8 * mb_strlen($k, '8bit');
29 if ($kLength < $this->getMinimumKeySize()) {
30 $bag->add(
31 Message::high(sprintf(
32 'HS512 algorithm requires at least %d bits key length.',
33 $this->getMinimumKeySize()
34 ))
35 );
36 }
37 }
38
39 abstract protected function getAlgorithmName(): string;
40
41 abstract protected function getMinimumKeySize(): int;
42 }