‪TYPO3CMS  10.4
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 
23 
31 {
39  public function ‪generateHmac(string $string): string
40  {
41  $encryptionKey = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'];
42  if (!$encryptionKey) {
43  throw new ‪InvalidArgumentForHashGenerationException('Encryption Key was empty!', 1255069597);
44  }
45  return hash_hmac('sha1', $string, $encryptionKey);
46  }
47 
56  public function ‪appendHmac(string $string): string
57  {
58  $hmac = $this->‪generateHmac($string);
59  return $string . $hmac;
60  }
61 
69  public function ‪validateHmac(string $string, string $hmac): bool
70  {
71  return hash_equals($this->‪generateHmac($string), $hmac);
72  }
73 
87  public function ‪validateAndStripHmac(string $string): string
88  {
89  if (strlen($string) < 40) {
90  throw new ‪InvalidArgumentForHashGenerationException('A hashed string must contain at least 40 characters, the given string was only ' . strlen($string) . ' characters long.', 1320830276);
91  }
92  $stringWithoutHmac = substr($string, 0, -40);
93  if ($this->‪validateHmac($stringWithoutHmac, substr($string, -40)) !== true) {
94  throw new ‪InvalidHashException('The given string was not appended with a valid HMAC.', 1320830018);
95  }
96  return $stringWithoutHmac;
97  }
98 }
‪TYPO3\CMS\Extbase\Security\Cryptography
Definition: HashService.php:18
‪TYPO3\CMS\Extbase\Security\Exception\InvalidArgumentForHashGenerationException
Definition: InvalidArgumentForHashGenerationException.php:26
‪TYPO3\CMS\Extbase\Security\Cryptography\HashService\validateAndStripHmac
‪string validateAndStripHmac(string $string)
Definition: HashService.php:87
‪TYPO3\CMS\Extbase\Security\Cryptography\HashService
Definition: HashService.php:31
‪TYPO3\CMS\Extbase\Security\Cryptography\HashService\appendHmac
‪string appendHmac(string $string)
Definition: HashService.php:56
‪TYPO3\CMS\Extbase\Security\Exception\InvalidHashException
Definition: InvalidHashException.php:26
‪TYPO3\CMS\Extbase\Security\Cryptography\HashService\generateHmac
‪string generateHmac(string $string)
Definition: HashService.php:39
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:23
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Extbase\Security\Cryptography\HashService\validateHmac
‪bool validateHmac(string $string, string $hmac)
Definition: HashService.php:69