‪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 ‪create(string ‪$value, ‪HashType $type = HashType::sha256): self
31  {
32  return new self(‪$value, $type);
33  }
34 
39  public function ‪__construct(string ‪$value, public readonly ‪HashType $type = HashType::sha256)
40  {
41  $length = strlen(‪$value);
42  if ($length === $this->type->length()) {
43  ‪$value = base64_encode(‪$value);
44  } elseif ($length === $this->type->length() * 2 && ctype_xdigit(‪$value)) {
45  ‪$value = base64_encode(hex2bin(‪$value));
46  } elseif (strlen(base64_decode(‪$value) ?: '') !== $this->type->length()) {
47  throw new \LogicException('Invalid base64 encoded value', 1678620881);
48  }
49  $this->value = ‪$value;
50  }
51 
52  public function ‪__toString(): string
53  {
54  return sprintf("'%s-%s'", $this->type->value, $this->value);
55  }
56 
57  public static function ‪knows(string ‪$value): bool
58  {
59  return preg_match(self::createParsingPattern(), ‪$value) === 1;
60  }
61 
62  public static function ‪parse(string ‪$value): self
63  {
64  if (preg_match(self::createParsingPattern(), ‪$value, $matches) !== 1) {
65  throw new \LogicException(sprintf('Parsing "%s" is not known', ‪$value), 1678621397);
66  }
67  return new self($matches['value'], HashType::from($matches['type']));
68  }
69 
70  private static function ‪createParsingPattern(): string
71  {
72  $types = array_map(static fn(‪HashType $type): string => $type->value, HashType::cases());
73  return sprintf("/^'(?P<type>%s)-(?P<value>.+)'$/", implode('|', $types));
74  }
75 
76  public function ‪compile(?‪FrontendInterface $cache = null): ?string
77  {
78  return (string)$this;
79  }
80 
81  public function ‪serialize(): ?string
82  {
83  return (string)$this;
84  }
85 }
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\SourceValueInterface
Definition: SourceValueInterface.php:31
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\HashValue\knows
‪static knows(string $value)
Definition: HashValue.php:57
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\HashValue
Definition: HashValue.php:27
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\HashValue\parse
‪static parse(string $value)
Definition: HashValue.php:62
‪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:30
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy
Definition: ConsumableNonce.php:18
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\HashValue\serialize
‪serialize()
Definition: HashValue.php:81
‪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:39
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\HashValue\createParsingPattern
‪static createParsingPattern()
Definition: HashValue.php:70
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\HashValue\__toString
‪__toString()
Definition: HashValue.php:52
‪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:76