2 namespace TYPO3\CMS\Saltedpasswords\Salt
;
4 /***************************************************************
7 * (c) 2009-2013 Marcus Krause <marcus#exp2009@t3sec.info>
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
30 * Contains class "tx_saltedpasswords_salts_blowfish"
31 * that provides Blowfish salted hashing.
34 * Class that implements Blowfish salted hashing based on PHP's
37 * Warning: Blowfish salted hashing with PHP's crypt() is not available
40 * @author Marcus Krause <marcus#exp2009@t3sec.info>
43 class BlowfishSalt
extends \TYPO3\CMS\Saltedpasswords\Salt\Md5Salt
{
46 * The default log2 number of iterations for password stretching.
50 * The default maximum allowed log2 number of iterations for
51 * password stretching.
53 const MAX_HASH_COUNT
= 17;
55 * The default minimum allowed log2 number of iterations for
56 * password stretching.
58 const MIN_HASH_COUNT
= 4;
61 * of iterations for password stretching.
65 static protected $hashCount;
68 * Keeps maximum allowed log2 number
69 * of iterations for password stretching.
73 static protected $maxHashCount;
76 * Keeps minimum allowed log2 number
77 * of iterations for password stretching.
81 static protected $minHashCount;
84 * Keeps length of a Blowfish salt in bytes.
88 static protected $saltLengthBlowfish = 16;
91 * Setting string to indicate type of hashing method (blowfish).
95 static protected $settingBlowfish = '$2a$';
98 * Method applies settings (prefix, hash count) to a salt.
100 * Overwrites {@link tx_saltedpasswords_salts_md5::applySettingsToSalt()}
101 * with Blowfish specifics.
103 * @param string $salt A salt to apply setting to
104 * @return string Salt with setting
106 protected function applySettingsToSalt($salt) {
107 $saltWithSettings = $salt;
108 $reqLenBase64 = $this->getLengthBase64FromBytes($this->getSaltLength());
109 // salt without setting
110 if (strlen($salt) == $reqLenBase64) {
111 $saltWithSettings = $this->getSetting() . sprintf('%02u', $this->getHashCount()) . '$' . $salt;
113 return $saltWithSettings;
117 * Parses the log2 iteration count from a stored hash or setting string.
119 * @param string $setting Complete hash or a hash's setting string or to get log2 iteration count from
120 * @return integer Used hashcount for given hash string
122 protected function getCountLog2($setting) {
124 $setting = substr($setting, strlen($this->getSetting()));
125 $firstSplitPos = strpos($setting, '$');
126 // Hashcount existing
127 if ($firstSplitPos !== FALSE && $firstSplitPos <= 2 && is_numeric(substr($setting, 0, $firstSplitPos))) {
128 $countLog2 = intval(substr($setting, 0, $firstSplitPos));
134 * Method returns log2 number of iterations for password stretching.
136 * @return integer log2 number of iterations for password stretching
139 * @see setHashCount()
141 public function getHashCount() {
142 return isset(self
::$hashCount) ? self
::$hashCount : self
::HASH_COUNT
;
146 * Method returns maximum allowed log2 number of iterations for password stretching.
148 * @return integer Maximum allowed log2 number of iterations for password stretching
149 * @see MAX_HASH_COUNT
151 * @see setMaxHashCount()
153 public function getMaxHashCount() {
154 return isset(self
::$maxHashCount) ? self
::$maxHashCount : self
::MAX_HASH_COUNT
;
158 * Returns wether all prequesites for the hashing methods are matched
160 * @return boolean Method available
162 public function isAvailable() {
163 return CRYPT_BLOWFISH
;
167 * Method returns minimum allowed log2 number of iterations for password stretching.
169 * @return integer Minimum allowed log2 number of iterations for password stretching
170 * @see MIN_HASH_COUNT
172 * @see setMinHashCount()
174 public function getMinHashCount() {
175 return isset(self
::$minHashCount) ? self
::$minHashCount : self
::MIN_HASH_COUNT
;
179 * Returns length of a Blowfish salt in bytes.
181 * Overwrites {@link tx_saltedpasswords_salts_md5::getSaltLength()}
182 * with Blowfish specifics.
184 * @return integer Length of a Blowfish salt in bytes
186 public function getSaltLength() {
187 return self
::$saltLengthBlowfish;
191 * Returns setting string of Blowfish salted hashes.
193 * Overwrites {@link tx_saltedpasswords_salts_md5::getSetting()}
194 * with Blowfish specifics.
196 * @return string Setting string of Blowfish salted hashes
198 public function getSetting() {
199 return self
::$settingBlowfish;
203 * Checks whether a user's hashed password needs to be replaced with a new hash.
205 * This is typically called during the login process when the plain text
206 * password is available. A new hash is needed when the desired iteration
207 * count has changed through a change in the variable $hashCount or
210 * @param string $saltedPW Salted hash to check if it needs an update
211 * @return boolean TRUE if salted hash needs an update, otherwise FALSE
213 public function isHashUpdateNeeded($saltedPW) {
214 // Check whether this was an updated password.
215 if (strncmp($saltedPW, '$2', 2) ||
!$this->isValidSalt($saltedPW)) {
218 // Check whether the iteration count used differs from the standard number.
219 $countLog2 = $this->getCountLog2($saltedPW);
220 return !is_NULL($countLog2) && $countLog2 < $this->getHashCount();
224 * Method determines if a given string is a valid salt.
226 * Overwrites {@link tx_saltedpasswords_salts_md5::isValidSalt()} with
227 * Blowfish specifics.
229 * @param string $salt String to check
230 * @return boolean TRUE if it's valid salt, otherwise FALSE
232 public function isValidSalt($salt) {
233 $isValid = ($skip = FALSE);
234 $reqLenBase64 = $this->getLengthBase64FromBytes($this->getSaltLength());
235 if (strlen($salt) >= $reqLenBase64) {
236 // Salt with prefixed setting
237 if (!strncmp('$', $salt, 1)) {
238 if (!strncmp($this->getSetting(), $salt, strlen($this->getSetting()))) {
240 $salt = substr($salt, strrpos($salt, '$') +
1);
245 // Checking base64 characters
246 if (!$skip && strlen($salt) >= $reqLenBase64) {
247 if (preg_match('/^[' . preg_quote($this->getItoa64(), '/') . ']{' . $reqLenBase64 . ',' . $reqLenBase64 . '}$/', substr($salt, 0, $reqLenBase64))) {
256 * Method determines if a given string is a valid salted hashed password.
258 * @param string $saltedPW String to check
259 * @return boolean TRUE if it's valid salted hashed password, otherwise FALSE
261 public function isValidSaltedPW($saltedPW) {
263 $isValid = !strncmp($this->getSetting(), $saltedPW, strlen($this->getSetting())) ?
TRUE : FALSE;
265 $isValid = $this->isValidSalt($saltedPW);
271 * Method sets log2 number of iterations for password stretching.
273 * @param integer $hashCount log2 number of iterations for password stretching to set
276 * @see getHashCount()
278 public function setHashCount($hashCount = NULL) {
279 self
::$hashCount = !is_NULL($hashCount) && is_int($hashCount) && $hashCount >= $this->getMinHashCount() && $hashCount <= $this->getMaxHashCount() ?
$hashCount : self
::HASH_COUNT
;
283 * Method sets maximum allowed log2 number of iterations for password stretching.
285 * @param integer $maxHashCount Maximum allowed log2 number of iterations for password stretching to set
286 * @see MAX_HASH_COUNT
288 * @see getMaxHashCount()
290 public function setMaxHashCount($maxHashCount = NULL) {
291 self
::$maxHashCount = !is_NULL($maxHashCount) && is_int($maxHashCount) ?
$maxHashCount : self
::MAX_HASH_COUNT
;
295 * Method sets minimum allowed log2 number of iterations for password stretching.
297 * @param integer $minHashCount Minimum allowed log2 number of iterations for password stretching to set
298 * @see MIN_HASH_COUNT
300 * @see getMinHashCount()
302 public function setMinHashCount($minHashCount = NULL) {
303 self
::$minHashCount = !is_NULL($minHashCount) && is_int($minHashCount) ?
$minHashCount : self
::MIN_HASH_COUNT
;