‪TYPO3CMS  ‪main
HashValue.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
21 
26 final class ‪HashValue implements \Stringable, ‪SourceValueInterface
27 {
28  public readonly string ‪$value;
29 
30  public static function ‪hash(string $payload, ‪HashType $type = HashType::sha256): self
31  {
32  ‪$value = ‪hash($type->value, $payload, true);
33  return ‪self::create(‪$value, $type);
34  }
35 
36  public static function ‪create(string ‪$value, ‪HashType $type = HashType::sha256): self
37  {
38  return new self(‪$value, $type);
39  }
40 
45  public function ‪__construct(string ‪$value, public readonly ‪HashType $type = HashType::sha256)
46  {
47  $length = strlen(‪$value);
48  if ($length === $this->type->length()) {
49  ‪$value = base64_encode(‪$value);
50  } elseif ($length === $this->type->length() * 2 && ctype_xdigit(‪$value)) {
51  ‪$value = base64_encode(hex2bin(‪$value));
52  } elseif (strlen(base64_decode(‪$value) ?: '') !== $this->type->length()) {
53  throw new \LogicException('Invalid base64 encoded value', 1678620881);
54  }
55  $this->value = ‪$value;
56  }
57 
58  public function ‪__toString(): string
59  {
60  return sprintf("'%s-%s'", $this->type->value, $this->value);
61  }
62 
63  public static function ‪knows(string ‪$value): bool
64  {
65  return preg_match(self::createParsingPattern(), ‪$value) === 1;
66  }
67 
68  public static function ‪parse(string ‪$value): self
69  {
70  if (preg_match(self::createParsingPattern(), ‪$value, $matches) !== 1) {
71  throw new \LogicException(sprintf('Parsing "%s" is not known', ‪$value), 1678621397);
72  }
73  return new self($matches['value'], HashType::from($matches['type']));
74  }
75 
76  private static function ‪createParsingPattern(): string
77  {
78  $types = array_map(static fn(‪HashType $type): string => $type->value, HashType::cases());
79  return sprintf("/^'(?P<type>%s)-(?P<value>.+)'$/", implode('|', $types));
80  }
81 
82  public function ‪compile(?‪FrontendInterface $cache = null): ?string
83  {
84  return (string)$this;
85  }
86 
87  public function ‪serialize(): ?string
88  {
89  return (string)$this;
90  }
91 }
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\HashValue\hash
‪static hash(string $payload, HashType $type=HashType::sha256)
Definition: HashValue.php:30
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\SourceValueInterface
Definition: SourceValueInterface.php:31
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\HashValue\knows
‪static knows(string $value)
Definition: HashValue.php:63
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\HashValue
Definition: HashValue.php:27
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\HashValue\parse
‪static parse(string $value)
Definition: HashValue.php:68
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\HashValue\$value
‪readonly string $value
Definition: HashValue.php:28
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\HashValue\create
‪static create(string $value, HashType $type=HashType::sha256)
Definition: HashValue.php:36
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy
Definition: ConsumableNonce.php:18
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\HashValue\serialize
‪serialize()
Definition: HashValue.php:87
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\HashValue\__construct
‪__construct(string $value, public readonly HashType $type=HashType::sha256)
Definition: HashValue.php:45
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\HashValue\createParsingPattern
‪static createParsingPattern()
Definition: HashValue.php:76
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\HashValue\__toString
‪__toString()
Definition: HashValue.php:58
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\HashType
‪HashType
Definition: HashType.php:25
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\HashValue\compile
‪compile(?FrontendInterface $cache=null)
Definition: HashValue.php:82