Debian Codegraph @

lib/3rdParty/composer/facile-it/php-jose-verifier/src/Internal/Checker/AbstractHashChecker.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Facile\JoseVerifier\Internal\Checker;
6
7 use Base64Url\Base64Url;
8 use Jose\Component\Checker\ClaimChecker;
9 use Jose\Component\Checker\InvalidClaimException;
10
11 use function hash;
12 use function round;
13 use function sprintf;
14 use function strlen;
15 use function substr;
16
17 /**
18 * @internal
19 */
20 abstract class AbstractHashChecker implements ClaimChecker
21 {
22 private string $valueToCheck;
23
24 private string $alg;
25
26 /**
27 * SHashChecker constructor.
28 */
29 public function __construct(string $valueToCheck, string $alg)
30 {
31 $this->valueToCheck = $valueToCheck;
32 $this->alg = $alg;
33 }
34
35 private function getShaSize(string $alg): string
36 {
37 $size = substr($alg, -3);
38
39 switch ($size) {
40 case '512':
41 return 'sha512';
42 case '384':
43 return 'sha384';
44 default:
45 return 'sha256';
46 }
47 }
48
49 /**
50 * @param mixed $value
51 *
52 * @throws InvalidClaimException
53 */
54 public function checkClaim($value): void
55 {
56 $hash = hash($this->getShaSize($this->alg), $this->valueToCheck, true);
57 $generated = Base64Url::encode(substr($hash, 0, (int) round(strlen($hash) / 2)));
58
59 if ($value !== $generated) {
60 throw new InvalidClaimException(sprintf($this->supportedClaim() . ' mismatch, expected %s, got: %s', $generated, (string) $value), $this->supportedClaim(), $value);
61 }
62 }
63 }