[TASK] Remove deprecated methods/properties in Extbase
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Security / Cryptography / HashService.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Security\Cryptography;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2013 Extbase Team (http://forge.typo3.org/projects/typo3v4-mvc)
8 * Extbase is a backport of TYPO3 Flow. All credits go to the TYPO3 Flow team.
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the text file GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 /**
31 * A hash service which should be used to generate and validate hashes.
32 *
33 * It will use some salt / encryption key in the future.
34 *
35 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser Public License, version 3 or later
36 */
37 class HashService implements \TYPO3\CMS\Core\SingletonInterface {
38
39 /**
40 * Generate a hash (HMAC) for a given string
41 *
42 * @param string $string The string for which a hash should be generated
43 * @return string The hash of the string
44 * @throws \TYPO3\CMS\Extbase\Security\Exception\InvalidArgumentForHashGenerationException if something else than a string was given as parameter
45 */
46 public function generateHmac($string) {
47 if (!is_string($string)) {
48 throw new \TYPO3\CMS\Extbase\Security\Exception\InvalidArgumentForHashGenerationException('A hash can only be generated for a string, but "' . gettype($string) . '" was given.', 1255069587);
49 }
50 $encryptionKey = $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'];
51 if (!$encryptionKey) {
52 throw new \TYPO3\CMS\Extbase\Security\Exception\InvalidArgumentForHashGenerationException('Encryption Key was empty!', 1255069597);
53 }
54 return hash_hmac('sha1', $string, $encryptionKey);
55 }
56
57 /**
58 * Appends a hash (HMAC) to a given string and returns the result
59 *
60 * @param string $string The string for which a hash should be generated
61 * @return string The original string with HMAC of the string appended
62 * @see generateHmac()
63 * @todo Mark as API once it is more stable
64 */
65 public function appendHmac($string) {
66 $hmac = $this->generateHmac($string);
67 return $string . $hmac;
68 }
69
70 /**
71 * Tests if a string $string matches the HMAC given by $hash.
72 *
73 * @param string $string The string which should be validated
74 * @param string $hmac The hash of the string
75 * @return boolean TRUE if string and hash fit together, FALSE otherwise.
76 */
77 public function validateHmac($string, $hmac) {
78 return $this->generateHmac($string) === $hmac;
79 }
80
81 /**
82 * Tests if the last 40 characters of a given string $string
83 * matches the HMAC of the rest of the string and, if true,
84 * returns the string without the HMAC. In case of a HMAC
85 * validation error, an exception is thrown.
86 *
87 * @param string $string The string with the HMAC appended (in the format 'string<HMAC>')
88 * @return string the original string without the HMAC, if validation was successful
89 * @see validateHmac()
90 * @throws \TYPO3\CMS\Extbase\Security\Exception\InvalidArgumentForHashGenerationException if the given string is not well-formatted
91 * @throws \TYPO3\CMS\Extbase\Security\Exception\InvalidHashException if the hash did not fit to the data.
92 * @todo Mark as API once it is more stable
93 */
94 public function validateAndStripHmac($string) {
95 if (!is_string($string)) {
96 throw new \TYPO3\CMS\Extbase\Security\Exception\InvalidArgumentForHashGenerationException('A hash can only be validated for a string, but "' . gettype($string) . '" was given.', 1320829762);
97 }
98 if (strlen($string) < 40) {
99 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);
100 }
101 $stringWithoutHmac = substr($string, 0, -40);
102 if ($this->validateHmac($stringWithoutHmac, substr($string, -40)) !== TRUE) {
103 throw new \TYPO3\CMS\Extbase\Security\Exception\InvalidHashException('The given string was not appended with a valid HMAC.', 1320830018);
104 }
105 return $stringWithoutHmac;
106 }
107 }