‪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 
23 
31 {
38  public function ‪generateHmac(string $string): string
39  {
40  $secret = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'];
41  return hash_hmac('sha1', $string, $secret);
42  }
43 
52  public function ‪appendHmac(string $string): string
53  {
54  $hmac = $this->‪generateHmac($string);
55  return $string . $hmac;
56  }
57 
65  public function ‪validateHmac(string $string, string $hmac): bool
66  {
67  return hash_equals($this->‪generateHmac($string), $hmac);
68  }
69 
83  public function ‪validateAndStripHmac(string $string): string
84  {
85  if (strlen($string) < 40) {
86  throw new ‪InvalidArgumentForHashGenerationException('A hashed string must contain at least 40 characters, the given string was only ' . strlen($string) . ' characters long.', 1320830276);
87  }
88  $stringWithoutHmac = substr($string, 0, -40);
89  if ($this->‪validateHmac($stringWithoutHmac, substr($string, -40)) !== true) {
90  throw new ‪InvalidHashException('The given string was not appended with a valid HMAC.', 1320830018);
91  }
92  return $stringWithoutHmac;
93  }
94 }
‪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:83
‪TYPO3\CMS\Extbase\Security\Cryptography\HashService
Definition: HashService.php:31
‪TYPO3\CMS\Extbase\Security\Cryptography\HashService\appendHmac
‪string appendHmac(string $string)
Definition: HashService.php:52
‪TYPO3\CMS\Extbase\Security\Exception\InvalidHashException
Definition: InvalidHashException.php:25
‪TYPO3\CMS\Extbase\Security\Cryptography\HashService\generateHmac
‪string generateHmac(string $string)
Definition: HashService.php:38
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Extbase\Security\Cryptography\HashService\validateHmac
‪bool validateHmac(string $string, string $hmac)
Definition: HashService.php:65