From db17ece31b7874dc794a762c89a61ab7896dcc82 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Thu, 29 May 2014 23:11:51 +0200 Subject: [PATCH] Implementing a FallbackPassword hasher to assist people in migrating users to a stronger password algorithm --- .../Component/Auth/FallbackPasswordHasher.php | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/Controller/Component/Auth/FallbackPasswordHasher.php diff --git a/src/Controller/Component/Auth/FallbackPasswordHasher.php b/src/Controller/Component/Auth/FallbackPasswordHasher.php new file mode 100644 index 00000000000..b5d496f943a --- /dev/null +++ b/src/Controller/Component/Auth/FallbackPasswordHasher.php @@ -0,0 +1,84 @@ + ['Simple', 'Weak'] + ]; + +/** + * Holds the list of password hasher objects that will be used + * + * @var array + */ + protected $_hashers = []; + +/** + * Constructor + * + */ + protected function __construct() { + foreach ($this->_config['hashers'] as $hasher) { + $this->_hashers = PasswordHasherFactory::build($hasher); + } + } + +/** + * Generates password hash. + * + * Uses the first password hasher in the list to generate the hash + * + * @param string $password Plain text password to hash. + * @return string Password hash + */ + public function hash($password) { + return $this->_hashers[0]->hash($password); + } + +/** + * Verifies that the provided password corresponds to its hashed version + * + * This will iterate over all configured hashers until one of them return + * true. + * + * @param string $password Plain text password to hash. + * @param string $hashedPassword Existing hashed password. + * @return bool True if hashes match else false. + */ + public function check($password, $hashedPassword) { + foreach ($this->_hashers as $hasher) { + if ($hasher->check($password, $hashedPassword)) { + return true; + } + } + return false; + } + +}