_config = $config; Kohana::log('debug', 'Auth_Basic_Driver Library initialized'); } /** * @see Auth_Driver::set_user_password * * @param int $user_id * @param string $password * @return void */ public function set_user_password($user_id, $password_text) { $password = ORM::factory("password")->where('user_id', $user_id)->find(); $password->password = $this->_hash_password($password_text); if (empty($password->user_id)) { $password->user_id = $user_id; } $password->save(); } /** * Validates a user id password combination. * * @param int user_id * @param string password * @return boolean */ public function is_valid_password($user_id, $password_text) { $password = ORM::factory("password") ->where('user_id', $user_id) ->find(); if ($password->loaded != true) { return false; } // Get the salt from the stored password $salt = $this->_find_salt($password->password); $hashed = $this->_hash_password($password_text, $salt); return $hashed === $password->password; } /** * Creates a hashed password from a plaintext password, inserting salt * based on the configured salt pattern. * * @param string plaintext password * @return string hashed password string */ private function _hash_password($password, $salt = FALSE) { if ($salt === FALSE) { // Create a salt seed, same length as the number of offsets in the pattern $salt = substr($this->_hash(uniqid(NULL, TRUE)), 0, count($this->_config['salt_pattern'])); } // Password hash that the salt will be inserted into $hash = $this->_hash($salt . $password); // Change salt to an array $salt = str_split($salt, 1); // Returned password $password = ''; // Used to calculate the length of splits $last_offset = 0; foreach ($this->_config['salt_pattern'] as $offset) { // Split a new part of the hash off $part = substr($hash, 0, $offset - $last_offset); // Cut the current part out of the hash $hash = substr($hash, $offset - $last_offset); // Add the part to the password, appending the salt character $password .= $part . array_shift($salt); // Set the last offset to the current offset $last_offset = $offset; } // Return the password, with the remaining hash appended return $password . $hash; } /** * Perform a hash, using the configured method. * * @param string string to hash * @return string */ private function _hash($str) { return hash($this->_config['hash_method'], $str); } /** * Finds the salt from a password, based on the configured salt pattern. * * @param string hashed password * @return string */ private function _find_salt($password) { $salt = ''; foreach ($this->_config['salt_pattern'] as $i => $offset) { // Find salt characters... take a good long look.. $salt .= substr($password, $offset + $i, 1); } return $salt; } }