[TASK] Re-work/simplify copyright header in PHP files - Part 8
[Packages/TYPO3.CMS.git] / typo3 / sysext / saltedpasswords / Classes / Salt / AbstractSalt.php
1 <?php
2 namespace TYPO3\CMS\Saltedpasswords\Salt;
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 * Abstract class with methods needed to be extended
19 * in a salted hashing class.
20 *
21 * @author Marcus Krause <marcus#exp2009@t3sec.info>
22 */
23 abstract class AbstractSalt {
24
25 /**
26 * Method applies settings (prefix, optional hash count, optional suffix)
27 * to a salt.
28 *
29 * @param string $salt A salt to apply setting to
30 * @return string Salt with setting
31 */
32 abstract protected function applySettingsToSalt($salt);
33
34 /**
35 * Generates a random base salt settings for the hash.
36 *
37 * @return string A string containing settings and a random salt
38 */
39 abstract protected function getGeneratedSalt();
40
41 /**
42 * Returns a string for mapping an int to the corresponding base 64 character.
43 *
44 * @return string String for mapping an int to the corresponding base 64 character
45 */
46 abstract protected function getItoa64();
47
48 /**
49 * Returns setting string to indicate type of hashing method.
50 *
51 * @return string Setting string of hashing method
52 */
53 abstract protected function getSetting();
54
55 /**
56 * Encodes bytes into printable base 64 using the *nix standard from crypt().
57 *
58 * @param string $input The string containing bytes to encode.
59 * @param integer $count The number of characters (bytes) to encode.
60 * @return string Encoded string
61 */
62 public function base64Encode($input, $count) {
63 $output = '';
64 $i = 0;
65 $itoa64 = $this->getItoa64();
66 do {
67 $value = ord($input[$i++]);
68 $output .= $itoa64[$value & 63];
69 if ($i < $count) {
70 $value |= ord($input[$i]) << 8;
71 }
72 $output .= $itoa64[$value >> 6 & 63];
73 if ($i++ >= $count) {
74 break;
75 }
76 if ($i < $count) {
77 $value |= ord($input[$i]) << 16;
78 }
79 $output .= $itoa64[$value >> 12 & 63];
80 if ($i++ >= $count) {
81 break;
82 }
83 $output .= $itoa64[$value >> 18 & 63];
84 } while ($i < $count);
85 return $output;
86 }
87
88 /**
89 * Method determines required length of base64 characters for a given
90 * length of a byte string.
91 *
92 * @param integer $byteLength Length of bytes to calculate in base64 chars
93 * @return integer Required length of base64 characters
94 */
95 protected function getLengthBase64FromBytes($byteLength) {
96 // Calculates bytes in bits in base64
97 return (int)ceil($byteLength * 8 / 6);
98 }
99
100 }