[TASK] Cleanup phpDoc to match CGL
[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 * A hash service which should be used to generate and validate hashes.
18 *
19 * It will use some salt / encryption key in the future.
20 *
21 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser Public License, version 3 or later
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 if (!is_string($string)) {
34 throw new \TYPO3\CMS\Extbase\Security\Exception\InvalidArgumentForHashGenerationException('A hash can only be generated for a string, but "' . gettype($string) . '" was given.', 1255069587);
35 }
36 $encryptionKey = $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'];
37 if (!$encryptionKey) {
38 throw new \TYPO3\CMS\Extbase\Security\Exception\InvalidArgumentForHashGenerationException('Encryption Key was empty!', 1255069597);
39 }
40 return hash_hmac('sha1', $string, $encryptionKey);
41 }
42
43 /**
44 * Appends a hash (HMAC) to a given string and returns the result
45 *
46 * @param string $string The string for which a hash should be generated
47 * @return string The original string with HMAC of the string appended
48 * @see generateHmac()
49 * @todo Mark as API once it is more stable
50 */
51 public function appendHmac($string) {
52 $hmac = $this->generateHmac($string);
53 return $string . $hmac;
54 }
55
56 /**
57 * Tests if a string $string matches the HMAC given by $hash.
58 *
59 * @param string $string The string which should be validated
60 * @param string $hmac The hash of the string
61 * @return bool TRUE if string and hash fit together, FALSE otherwise.
62 */
63 public function validateHmac($string, $hmac) {
64 return $this->generateHmac($string) === $hmac;
65 }
66
67 /**
68 * Tests if the last 40 characters of a given string $string
69 * matches the HMAC of the rest of the string and, if true,
70 * returns the string without the HMAC. In case of a HMAC
71 * validation error, an exception is thrown.
72 *
73 * @param string $string The string with the HMAC appended (in the format 'string<HMAC>')
74 * @return string the original string without the HMAC, if validation was successful
75 * @see validateHmac()
76 * @throws \TYPO3\CMS\Extbase\Security\Exception\InvalidArgumentForHashGenerationException if the given string is not well-formatted
77 * @throws \TYPO3\CMS\Extbase\Security\Exception\InvalidHashException if the hash did not fit to the data.
78 * @todo Mark as API once it is more stable
79 */
80 public function validateAndStripHmac($string) {
81 if (!is_string($string)) {
82 throw new \TYPO3\CMS\Extbase\Security\Exception\InvalidArgumentForHashGenerationException('A hash can only be validated for a string, but "' . gettype($string) . '" was given.', 1320829762);
83 }
84 if (strlen($string) < 40) {
85 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);
86 }
87 $stringWithoutHmac = substr($string, 0, -40);
88 if ($this->validateHmac($stringWithoutHmac, substr($string, -40)) !== TRUE) {
89 throw new \TYPO3\CMS\Extbase\Security\Exception\InvalidHashException('The given string was not appended with a valid HMAC.', 1320830018);
90 }
91 return $stringWithoutHmac;
92 }
93 }