985917e6b9b4810d1194e7df35ea49dbfdca486e
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Security / Cryptography / HashService.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Security\Cryptography;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * A hash service which should be used to generate and validate hashes.
19 *
20 * It will use some salt / encryption key in the future.
21 * @internal only to be used within Extbase, not part of TYPO3 Core API.
22 */
23 class HashService implements \TYPO3\CMS\Core\SingletonInterface
24 {
25 /**
26 * Generate a hash (HMAC) for a given string
27 *
28 * @param string $string The string for which a hash should be generated
29 * @return string The hash of the string
30 * @throws \TYPO3\CMS\Extbase\Security\Exception\InvalidArgumentForHashGenerationException if something else than a string was given as parameter
31 */
32 public function generateHmac($string)
33 {
34 if (!is_string($string)) {
35 throw new \TYPO3\CMS\Extbase\Security\Exception\InvalidArgumentForHashGenerationException('A hash can only be generated for a string, but "' . gettype($string) . '" was given.', 1255069587);
36 }
37 $encryptionKey = $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'];
38 if (!$encryptionKey) {
39 throw new \TYPO3\CMS\Extbase\Security\Exception\InvalidArgumentForHashGenerationException('Encryption Key was empty!', 1255069597);
40 }
41 return hash_hmac('sha1', $string, $encryptionKey);
42 }
43
44 /**
45 * Appends a hash (HMAC) to a given string and returns the result
46 *
47 * @param string $string The string for which a hash should be generated
48 * @return string The original string with HMAC of the string appended
49 * @see generateHmac()
50 * @todo Mark as API once it is more stable
51 */
52 public function appendHmac($string)
53 {
54 $hmac = $this->generateHmac($string);
55 return $string . $hmac;
56 }
57
58 /**
59 * Tests if a string $string matches the HMAC given by $hash.
60 *
61 * @param string $string The string which should be validated
62 * @param string $hmac The hash of the string
63 * @return bool TRUE if string and hash fit together, FALSE otherwise.
64 */
65 public function validateHmac($string, $hmac)
66 {
67 return hash_equals($this->generateHmac($string), $hmac);
68 }
69
70 /**
71 * Tests if the last 40 characters of a given string $string
72 * matches the HMAC of the rest of the string and, if true,
73 * returns the string without the HMAC. In case of a HMAC
74 * validation error, an exception is thrown.
75 *
76 * @param string $string The string with the HMAC appended (in the format 'string<HMAC>')
77 * @return string the original string without the HMAC, if validation was successful
78 * @see validateHmac()
79 * @throws \TYPO3\CMS\Extbase\Security\Exception\InvalidArgumentForHashGenerationException if the given string is not well-formatted
80 * @throws \TYPO3\CMS\Extbase\Security\Exception\InvalidHashException if the hash did not fit to the data.
81 * @todo Mark as API once it is more stable
82 */
83 public function validateAndStripHmac($string)
84 {
85 if (!is_string($string)) {
86 throw new \TYPO3\CMS\Extbase\Security\Exception\InvalidArgumentForHashGenerationException('A hash can only be validated for a string, but "' . gettype($string) . '" was given.', 1320829762);
87 }
88 if (strlen($string) < 40) {
89 throw new \TYPO3\CMS\Extbase\Security\Exception\InvalidArgumentForHashGenerationException('A hashed string must contain at least 40 characters, the given string was only ' . strlen($string) . ' characters long.', 1320830276);
90 }
91 $stringWithoutHmac = substr($string, 0, -40);
92 if ($this->validateHmac($stringWithoutHmac, substr($string, -40)) !== true) {
93 throw new \TYPO3\CMS\Extbase\Security\Exception\InvalidHashException('The given string was not appended with a valid HMAC.', 1320830018);
94 }
95 return $stringWithoutHmac;
96 }
97 }