‪TYPO3CMS  ‪main
HashService.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 
20 use ‪TYPO3\CMS\Core\Crypto\HashService as CoreHashService;
23 
35 {
36  public function ‪__construct(protected readonly CoreHashService $hashService) {}
37 
44  public function ‪generateHmac(string $string): string
45  {
46  trigger_error(
47  __CLASS__ . ' has been marked as deprecated in TYPO3 v13. Use \TYPO3\CMS\Core\Crypto\HashService instead.',
48  E_USER_DEPRECATED,
49  );
50  return $this->hashService->hmac($string, self::class);
51  }
52 
61  public function ‪appendHmac(string $string): string
62  {
63  trigger_error(
64  __CLASS__ . ' has been marked as deprecated in TYPO3 v13. Use \TYPO3\CMS\Core\Crypto\HashService instead.',
65  E_USER_DEPRECATED,
66  );
67  return $this->hashService->appendHmac($string, self::class);
68  }
69 
77  public function ‪validateHmac(string $string, string $hmac): bool
78  {
79  trigger_error(
80  __CLASS__ . ' has been marked as deprecated in TYPO3 v13. Use \TYPO3\CMS\Core\Crypto\HashService instead.',
81  E_USER_DEPRECATED,
82  );
83  return $this->hashService->validateHmac($string, self::class, $hmac);
84  }
85 
99  public function ‪validateAndStripHmac(string $string): string
100  {
101  trigger_error(
102  __CLASS__ . ' has been marked as deprecated in TYPO3 v13. Use \TYPO3\CMS\Core\Crypto\HashService instead.',
103  E_USER_DEPRECATED,
104  );
105 
106  if (strlen($string) < 40) {
107  throw new ‪InvalidArgumentForHashGenerationException('A hashed string must contain at least 40 characters, the given string was only ' . strlen($string) . ' characters long.', 1320830276);
108  }
109  $stringWithoutHmac = substr($string, 0, -40);
110  if ($this->‪validateHmac($stringWithoutHmac, substr($string, -40)) !== true) {
111  throw new ‪InvalidHashException('The given string was not appended with a valid HMAC.', 1320830018);
112  }
113  return $stringWithoutHmac;
114  }
115 }
‪TYPO3\CMS\Extbase\Security\Cryptography
Definition: HashService.php:18
‪TYPO3\CMS\Extbase\Security\Exception\InvalidArgumentForHashGenerationException
Definition: InvalidArgumentForHashGenerationException.php:25
‪TYPO3\CMS\Extbase\Security\Cryptography\HashService\validateAndStripHmac
‪string validateAndStripHmac(string $string)
Definition: HashService.php:99
‪TYPO3\CMS\Extbase\Security\Cryptography\HashService
Definition: HashService.php:35
‪TYPO3\CMS\Extbase\Security\Cryptography\HashService\appendHmac
‪string appendHmac(string $string)
Definition: HashService.php:61
‪TYPO3\CMS\Extbase\Security\Cryptography\HashService\__construct
‪__construct(protected readonly CoreHashService $hashService)
Definition: HashService.php:36
‪TYPO3\CMS\Extbase\Security\Exception\InvalidHashException
Definition: InvalidHashException.php:25
‪TYPO3\CMS\Extbase\Security\Cryptography\HashService\generateHmac
‪string generateHmac(string $string)
Definition: HashService.php:44
‪TYPO3\CMS\Core\Crypto\HashService
Definition: HashService.php:27
‪TYPO3\CMS\Extbase\Security\Cryptography\HashService\validateHmac
‪bool validateHmac(string $string, string $hmac)
Definition: HashService.php:77