mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Randomly generate a salt when the salt is '' or null.
To prevent an issue where any value is accepted as a password when '' is provided as the hashed password. Refs #8650
This commit is contained in:
parent
c6d5bfb2b9
commit
8b5023282e
2 changed files with 22 additions and 2 deletions
|
@ -151,16 +151,36 @@ class SecurityTest extends CakeTestCase {
|
||||||
Security::setHash($_hashType);
|
Security::setHash($_hashType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that blowfish doesn't return '' when the salt is ''
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testHashBlowfishEmptySalt() {
|
||||||
|
$test = Security::hash('password', 'blowfish');
|
||||||
|
$this->skipIf(strpos($test, '$2a$') === false, 'Blowfish hashes are incorrect.');
|
||||||
|
|
||||||
|
$stored = '';
|
||||||
|
$hash = Security::hash('anything', 'blowfish', $stored);
|
||||||
|
$this->assertNotEquals($stored, $hash);
|
||||||
|
|
||||||
|
$hash = Security::hash('anything', 'blowfish', false);
|
||||||
|
$this->assertNotEquals($stored, $hash);
|
||||||
|
|
||||||
|
$hash = Security::hash('anything', 'blowfish', null);
|
||||||
|
$this->assertNotEquals($stored, $hash);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test that hash() works with blowfish.
|
* Test that hash() works with blowfish.
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function testHashBlowfish() {
|
public function testHashBlowfish() {
|
||||||
Security::setCost(10);
|
|
||||||
$test = Security::hash('password', 'blowfish');
|
$test = Security::hash('password', 'blowfish');
|
||||||
$this->skipIf(strpos($test, '$2a$') === false, 'Blowfish hashes are incorrect.');
|
$this->skipIf(strpos($test, '$2a$') === false, 'Blowfish hashes are incorrect.');
|
||||||
|
|
||||||
|
Security::setCost(10);
|
||||||
$_hashType = Security::$hashType;
|
$_hashType = Security::$hashType;
|
||||||
|
|
||||||
$key = 'someKey';
|
$key = 'someKey';
|
||||||
|
|
|
@ -303,7 +303,7 @@ class Security {
|
||||||
* @return string The hashed string or an empty string on error.
|
* @return string The hashed string or an empty string on error.
|
||||||
*/
|
*/
|
||||||
protected static function _crypt($password, $salt = false) {
|
protected static function _crypt($password, $salt = false) {
|
||||||
if ($salt === false) {
|
if ($salt === false || $salt === null || $salt === '') {
|
||||||
$salt = static::_salt(22);
|
$salt = static::_salt(22);
|
||||||
$salt = vsprintf('$2a$%02d$%s', array(static::$hashCost, $salt));
|
$salt = vsprintf('$2a$%02d$%s', array(static::$hashCost, $salt));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue