diff --git a/classes/AdminTab.php b/classes/AdminTab.php index 2f06fe13e..0e54eac07 100644 --- a/classes/AdminTab.php +++ b/classes/AdminTab.php @@ -1181,8 +1181,8 @@ abstract class AdminTabCore } /* Automatically hash password */ if ($key == 'passwd' && !empty($value)) { - $crypto = new Hashing(); - $value = $crypto->hash($value); + $objHash = new PasswordHashing(); + $value = $objHash->passwordHash($value); } $object->{$key} = $value; } diff --git a/classes/Customer.php b/classes/Customer.php index 6ce60debc..232fb7192 100644 --- a/classes/Customer.php +++ b/classes/Customer.php @@ -390,8 +390,8 @@ class CustomerCore extends ObjectModel return false; } if (isset($passwd)) { - $crypto = new Hashing(); - if (!$crypto->checkHash($passwd, $result['passwd'])) { + $objHash = new PasswordHashing(); + if (!$objHash->validateHash($passwd, $result['passwd'])) { return false; } } @@ -401,8 +401,8 @@ class CustomerCore extends ObjectModel $this->{$key} = $value; } } - if (isset($passwd) && !$crypto->isFirstHash($passwd, $result['passwd'])) { - $this->passwd = $crypto->hash($passwd); + if (isset($passwd) && !$objHash->isPrimaryHash($passwd, $result['passwd'])) { + $this->passwd = $objHash->passwordHash($passwd); $this->update(); } return $this; @@ -861,8 +861,8 @@ class CustomerCore extends ObjectModel } $this->is_guest = 0; - $crypto = new Hashing(); - $this->passwd = $crypto->hash($password); + $objHash = new PasswordHashing(); + $this->passwd = $objHash->passwordHash($password); $this->cleanGroups(); $this->addGroups(array(Configuration::get('PS_CUSTOMER_GROUP'))); // add default customer group $this->id_default_group = (int) Configuration::get('PS_CUSTOMER_GROUP'); @@ -897,8 +897,8 @@ class CustomerCore extends ObjectModel public function setWsPasswd($passwd) { if ($this->id == 0 || $this->passwd != $passwd) { - $crypto = new Hashing(); - $this->passwd = $crypto->hash($passwd); + $objHash = new PasswordHashing(); + $this->passwd = $objHash->passwordHash($passwd); } return true; } diff --git a/classes/Employee.php b/classes/Employee.php index 84e95fa7f..7d1209fb2 100644 --- a/classes/Employee.php +++ b/classes/Employee.php @@ -287,8 +287,8 @@ class EmployeeCore extends ObjectModel } if ($passwd !== null) { - $crypto = new Hashing(); - if (!$crypto->checkHash($passwd, $result['passwd'])) { + $objHash = new PasswordHashing(); + if (!$objHash->validateHash($passwd, $result['passwd'])) { return false; } } @@ -300,8 +300,8 @@ class EmployeeCore extends ObjectModel } } - if ($passwd !== null && !$crypto->isFirstHash($passwd, $result['passwd'])) { - $this->passwd = $crypto->hash($passwd); + if ($passwd !== null && !$objHash->isPrimaryHash($passwd, $result['passwd'])) { + $this->passwd = $objHash->passwordHash($passwd); $this->update(); } return $this; @@ -375,20 +375,20 @@ class EmployeeCore extends ObjectModel // validate and set password for the employee public function setWsPasswd($passwd) { - $crypto = new Hashing(); + $objHash = new PasswordHashing(); if ($this->id != 0) { if ($this->passwd != $passwd) { if (!Validate::isHashedPassword($passwd, Validate::ADMIN_PASSWORD_LENGTH)) { WebserviceRequest::getInstance()->setError(400, 'The password must be at least '.Validate::ADMIN_PASSWORD_LENGTH.' characters long.', 134); } else { - $this->passwd = $crypto->hash($passwd); + $this->passwd = $objHash->passwordHash($passwd); } } } else { if (!Validate::isHashedPassword($passwd, Validate::ADMIN_PASSWORD_LENGTH)) { WebserviceRequest::getInstance()->setError(400, 'The password must be at least '.Validate::ADMIN_PASSWORD_LENGTH.' characters long.', 134); } else { - $this->passwd = $crypto->hash($passwd); + $this->passwd = $objHash->passwordHash($passwd); } } diff --git a/classes/Hashing.php b/classes/Hashing.php deleted file mode 100644 index ca8a245b5..000000000 --- a/classes/Hashing.php +++ /dev/null @@ -1,125 +0,0 @@ - - * @copyright 2007-2017 PrestaShop SA - * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) - * International Registered Trademark & Property of PrestaShop SA - */ - - -class HashingCore -{ - /** @var array should contain hashing methods */ - private $hashMethods = array(); - - /** - * Check if it's the first function of the array that was used for hashing - * - * @param string $passwd The password you want to check - * @param string $hash The hash you want to check - * @param string $staticSalt A static salt - * - * @return bool Result of the verify function - */ - public function isFirstHash($passwd, $hash, $staticSalt = _COOKIE_KEY_) - { - if (!count($this->hashMethods)) { - $this->initHashMethods(); - } - - $closure = reset($this->hashMethods); - - return $closure['verify']($passwd, $hash, $staticSalt); - } - - /** - * Iterate on hash_methods array and return true if it matches - * - * @param string $passwd The password you want to check - * @param string $hash The hash you want to check - * @param string $staticSalt A static salt - * - * @return bool `true` is returned if the function find a match else false - */ - public function checkHash($passwd, $hash, $staticSalt = _COOKIE_KEY_) - { - if (!count($this->hashMethods)) { - $this->initHashMethods(); - } - - foreach ($this->hashMethods as $closure) { - if ($closure['verify']($passwd, $hash, $staticSalt)) { - return true; - } - } - - return false; - } - - /** - * Hash the `$plaintextPassword` string and return the result of the 1st hashing method - * contained in Hashing::hashMethods - * - * @param string $plaintextPassword The password you want to hash - * @param string $staticSalt The static salt - * - * @return string - */ - public function hash($plaintextPassword, $staticSalt = _COOKIE_KEY_) - { - if (!count($this->hashMethods)) { - $this->initHashMethods(); - } - - $closure = reset($this->hashMethods); - - return $closure['hash']($plaintextPassword, $staticSalt, $closure['option']); - } - - /** - * Init hash methods - * - * @return void - */ - private function initHashMethods() - { - $this->hashMethods = array( - 'bcrypt' => array( - 'option' => array(), - 'hash' => function ($passwd, $staticSalt, $option) { - return password_hash($passwd, PASSWORD_BCRYPT); - }, - 'verify' => function ($passwd, $hash, $staticSalt) { - return password_verify($passwd, $hash); - }, - ), - 'md5' => array( - 'option' => array(), - 'hash' => function ($passwd, $staticSalt, $option) { - return md5($staticSalt.$passwd); - }, - 'verify' => function ($passwd, $hash, $staticSalt) { - return md5($staticSalt.$passwd) === $hash; - }, - ), - ); - } -} diff --git a/classes/ObjectModel.php b/classes/ObjectModel.php index 8c316c571..e960ee837 100644 --- a/classes/ObjectModel.php +++ b/classes/ObjectModel.php @@ -1188,8 +1188,8 @@ abstract class ObjectModelCore implements Core_Foundation_Database_EntityInterfa } if ($field == 'passwd') { if ($value = Tools::getValue($field)) { - $crypto = new Hashing(); - $this->{$field} = $crypto->hash($value); + $objHash = new PasswordHashing(); + $this->{$field} = $objHash->passwordHash($value); } } else { $this->{$field} = $value; diff --git a/classes/PasswordHashing.php b/classes/PasswordHashing.php new file mode 100644 index 000000000..f327cc390 --- /dev/null +++ b/classes/PasswordHashing.php @@ -0,0 +1,122 @@ +passwordHashMethods)) { + $this->initPasswordHashMethods(); + } + + $closure = reset($this->passwordHashMethods); + + return $closure['verify']($passwd, $hash, $staticSalt); + } + + /** + * Validate a password against all supported hashing methods. + * + * @param string $passwd Plain-text password to validate. + * @param string $hash Stored password hash. + * @param string $staticSalt Static salt used by legacy hash methods. + * + * @return bool + */ + public function validateHash($passwd, $hash, $staticSalt = _COOKIE_KEY_) + { + if (!count($this->passwordHashMethods)) { + $this->initPasswordHashMethods(); + } + + foreach ($this->passwordHashMethods as $closure) { + if ($closure['verify']($passwd, $hash, $staticSalt)) { + return true; + } + } + + return false; + } + + /** + * Hash a plain-text password with the primary hashing method. + * + * @param string $plaintextPassword Password to hash. + * @param string $staticSalt Static salt reserved for legacy hash methods. + * + * @return string + */ + public function passwordHash($plaintextPassword, $staticSalt = _COOKIE_KEY_) + { + if (!count($this->passwordHashMethods)) { + $this->initPasswordHashMethods(); + } + + $closure = reset($this->passwordHashMethods); + + return $closure['hash']($plaintextPassword, $staticSalt, $closure['option']); + } + + /** + * Initialize the supported password hashing methods. + * + * @return void + */ + private function initPasswordHashMethods() + { + $this->passwordHashMethods = array( + 'bcrypt' => array( + 'option' => array(), + 'hash' => function ($passwd, $staticSalt, $option) { + return password_hash($passwd, PASSWORD_BCRYPT); + }, + 'verify' => function ($passwd, $hash, $staticSalt) { + return password_verify($passwd, $hash); + }, + ), + 'md5' => array( + 'option' => array(), + 'hash' => function ($passwd, $staticSalt, $option) { + return md5($staticSalt.$passwd); + }, + 'verify' => function ($passwd, $hash, $staticSalt) { + return md5($staticSalt.$passwd) === $hash; + }, + ), + ); + } +} diff --git a/classes/Validate.php b/classes/Validate.php index d47f64b66..84c789661 100644 --- a/classes/Validate.php +++ b/classes/Validate.php @@ -91,7 +91,7 @@ class ValidateCore /** * Check for password hash validity (legacy MD5 or bcrypt), or plaintext password. * - * @param string $hash + * @param string $hashedPasswd * @return bool Validity is ok or not */ public static function isHashedPassword($hashedPasswd) diff --git a/classes/controller/AdminController.php b/classes/controller/AdminController.php index 9388bf96e..cdcf6144b 100644 --- a/classes/controller/AdminController.php +++ b/classes/controller/AdminController.php @@ -3840,8 +3840,8 @@ class AdminControllerCore extends Controller } /* Automatically hash password */ if ($key == 'passwd' && !empty($value)) { - $crypto = new Hashing(); - $value = $crypto->hash($value); + $objHash = new PasswordHashing(); + $value = $objHash->passwordHash($value); } $object->{$key} = $value; } diff --git a/classes/webservice/WebserviceSpecificManagementBookings.php b/classes/webservice/WebserviceSpecificManagementBookings.php index 4e542bdcf..d8bece974 100644 --- a/classes/webservice/WebserviceSpecificManagementBookings.php +++ b/classes/webservice/WebserviceSpecificManagementBookings.php @@ -1378,8 +1378,8 @@ class WebserviceSpecificManagementBookingsCore Extends ObjectModel implements We $objCustomer->firstname = $customerDetails['firstname']; $objCustomer->lastname = $customerDetails['lastname']; $objCustomer->email = $customerDetails['email']; - $crypto = new Hashing(); - $objCustomer->passwd = $crypto->hash(Tools::passwdGen(32, 'RANDOM')); + $objHash = new PasswordHashing(); + $objCustomer->passwd = $objHash->passwordHash(Tools::passwdGen(32, 'RANDOM')); $objCustomer->phone = (isset($customerDetails['phone']) ? $customerDetails['phone'] : ''); $objCustomer->cleanGroups(); $objCustomer->add(); diff --git a/controllers/admin/AdminImportController.php b/controllers/admin/AdminImportController.php index f7ba432e1..d0934b671 100644 --- a/controllers/admin/AdminImportController.php +++ b/controllers/admin/AdminImportController.php @@ -2835,8 +2835,8 @@ class AdminImportControllerCore extends AdminController AdminImportController::arrayWalk($info, array('AdminImportController', 'fillInfo'), $customer); if ($customer->passwd) { - $crypto = new Hashing(); - $customer->passwd = $crypto->hash($customer->passwd); + $objHash = new PasswordHashing(); + $customer->passwd = $objHash->passwordHash($customer->passwd); } $customers_shop = array(); diff --git a/controllers/admin/AdminLoginController.php b/controllers/admin/AdminLoginController.php index e438f022e..6300da814 100644 --- a/controllers/admin/AdminLoginController.php +++ b/controllers/admin/AdminLoginController.php @@ -263,8 +263,8 @@ class AdminLoginControllerCore extends AdminController if (!count($this->errors)) { $pwd = Tools::passwdGen(10, 'RANDOM'); - $crypto = new Hashing(); - $employee->passwd = $crypto->hash($pwd); + $objHash = new PasswordHashing(); + $employee->passwd = $objHash->passwordHash($pwd); $employee->last_passwd_gen = date('Y-m-d H:i:s', time()); $params = array( diff --git a/controllers/front/IdentityController.php b/controllers/front/IdentityController.php index 9f8a1c904..b24881336 100644 --- a/controllers/front/IdentityController.php +++ b/controllers/front/IdentityController.php @@ -71,7 +71,7 @@ class IdentityControllerCore extends FrontController $this->errors[] = Tools::displayError('This email address is not valid'); } elseif ($this->customer->email != $email && Customer::customerExists($email, true)) { $this->errors[] = Tools::displayError('An account using this email address has already been registered.'); - } elseif (!Tools::getIsset('old_passwd') || !(new Hashing())->checkHash($old_passwd, $this->context->cookie->passwd)) { + } elseif (!Tools::getIsset('old_passwd') || !(new PasswordHashing())->validateHash($old_passwd, $this->context->cookie->passwd)) { $this->errors[] = Tools::displayError('The password you entered is incorrect.'); } elseif (Tools::getValue('passwd') != Tools::getValue('confirmation')) { $this->errors[] = Tools::displayError('The password and confirmation do not match.'); diff --git a/controllers/front/PasswordController.php b/controllers/front/PasswordController.php index 5bfd526ee..a2bb002b1 100644 --- a/controllers/front/PasswordController.php +++ b/controllers/front/PasswordController.php @@ -73,8 +73,8 @@ class PasswordControllerCore extends FrontController } elseif ((strtotime($customer->last_passwd_gen.'+'.(int)Configuration::get('PS_PASSWD_TIME_FRONT').' minutes') - time()) > 0) { Tools::redirect('index.php?controller=authentication&error_regen_pwd'); } else { - $crypto = new Hashing(); - $customer->passwd = $crypto->hash($password = Tools::passwdGen(MIN_PASSWD_LENGTH, 'RANDOM')); + $objHash = new PasswordHashing(); + $customer->passwd = $objHash->passwordHash($password = Tools::passwdGen(MIN_PASSWD_LENGTH, 'RANDOM')); $customer->last_passwd_gen = date('Y-m-d H:i:s', time()); if ($customer->update()) { Hook::exec('actionPasswordRenew', array('customer' => $customer, 'password' => $password)); diff --git a/install/fixtures/fashion/install.php b/install/fixtures/fashion/install.php index e14ad92bf..da0969f32 100644 --- a/install/fixtures/fashion/install.php +++ b/install/fixtures/fashion/install.php @@ -35,8 +35,8 @@ class InstallFixturesFashion extends InstallXmlLoader public function createEntityCustomer($identifier, array $data, array $data_lang) { if ($identifier == 'John') { - $crypto = new Hashing(); - $data['passwd'] = $crypto->hash('123456789'); + $objHash = new PasswordHashing(); + $data['passwd'] = $objHash->passwordHash('123456789'); $data['last_passwd_gen'] = date('Y-m-d H:i:s'); $data['birthday'] = date('Y-m-d', strtotime('-30 years')); $data['newsletter_date_add'] = date('Y-m-d H:i:s'); diff --git a/install/models/install.php b/install/models/install.php index 2a36638d8..461237b9c 100644 --- a/install/models/install.php +++ b/install/models/install.php @@ -566,8 +566,8 @@ class InstallModelInstall extends InstallAbstractModel $employee->firstname = Tools::ucfirst($data['admin_firstname']); $employee->lastname = Tools::ucfirst($data['admin_lastname']); $employee->email = $data['admin_email']; - $crypto = new Hashing(); - $employee->passwd = $crypto->hash($data['admin_password']); + $objHash = new PasswordHashing(); + $employee->passwd = $objHash->passwordHash($data['admin_password']); $employee->last_passwd_gen = date('Y-m-d h:i:s', strtotime('-360 minutes')); $employee->bo_theme = 'default'; $employee->default_tab = 1;