Skip to content

Commit

Permalink
Re-implementing SimplePasswordHasher to use the more secure
Browse files Browse the repository at this point in the history
password_hash function

Also requiring a compatible php library for password_hash for users on
version < 5.5
  • Loading branch information
lorenzo committed Jun 3, 2014
1 parent d029a72 commit 0656ed8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -22,7 +22,8 @@
"ext-intl": "*",
"ext-mcrypt": "*",
"ext-mbstring": "*",
"nesbot/Carbon": "1.8.*"
"nesbot/Carbon": "1.8.*",
"ircmaxell/password-compat": "1.0.*"
},
"require-dev": {
"phpunit/phpunit": "*"
Expand Down
15 changes: 15 additions & 0 deletions src/Controller/Component/Auth/AbstractPasswordHasher.php
Expand Up @@ -61,4 +61,19 @@ abstract public function hash($password);
*/
abstract public function check($password, $hashedPassword);

/**
* Returns true if the password need to be rehashed, due to the password being
* created with anything else than the passwords generated by this class.
*
* Returns true by default since the only implementation users should rely
* on is the one provided by default in php 5.5+ or any compatible library
*
* @param string $password The password to verify
* @param mixed $hashType the algorithm used to hash the password
* @return boolean
*/
public function needsRehash($password, $hashType) {
return true;
}

}
21 changes: 18 additions & 3 deletions src/Controller/Component/Auth/SimplePasswordHasher.php
Expand Up @@ -29,7 +29,7 @@ class SimplePasswordHasher extends AbstractPasswordHasher {
* @var array
*/
protected $_defaultConfig = [
'hashType' => null
'hashType' => PASSWORD_DEFAULT
];

/**
Expand All @@ -40,7 +40,7 @@ class SimplePasswordHasher extends AbstractPasswordHasher {
* @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#hashing-passwords
*/
public function hash($password) {
return Security::hash($password, $this->_config['hashType'], true);
return password_hash($password, $this->_config['hash']);
}

/**
Expand All @@ -51,7 +51,22 @@ public function hash($password) {
* @return bool True if hashes match else false.
*/
public function check($password, $hashedPassword) {
return $hashedPassword === $this->hash($password);
return password_verify($password, $hashedPassword);
}

/**
* Returns true if the password need to be rehashed, due to the password being
* created with anything else than the passwords generated by this class.
*
* Returns true by default since the only implementation users should rely
* on is the one provided by default in php 5.5+ or any compatible library
*
* @param string $password The password to verify
* @param mixed $hashType the algorithm used to hash the password
* @return boolean
*/
public function needsRehash($password, $hashType) {
return password_needs_rehash($password, $hashType);
}

}

1 comment on commit 0656ed8

@sitedyno
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 yay :)

Please sign in to comment.