抱歉,在我的旧项目中,我使用MD5来加密密码是新的,但是,它不够安全,我发现互联网上的一些资源建议使用密码盐.
问题是,我使用的是codeigniter,是否有任何帮助/库用于此目的/如何更改旧代码以支持生成密码盐?
谢谢你的帮助.我使用的是PHP 5.2
这里是旧代码验证,而用户帐户通过存储生成 md5($password);
function validate_credentials() { $this->load->model('Secure_model'); $username = $this->input->post('username'); $password = md5($this->input->post('password')); $is_valid = $this->Secure_model->validate('customer', $username, $password); if ($is_valid) { $data = array( 'user_id' => $this->get_user_id($username), 'user_name' => $username, 'is_logged_in_user' => true ); $this->session->set_userdata($data); redirect('profile'); } else { $data['message_error'] = TRUE; $data['main_content'] = 'front/login'; $this->load->view('front/includes/template', $data); } }
martinstoeck.. 6
如果你真的坚持使用PHP 5.2,你最好的选择可能是phpass库,因为没有PHP支持BCrypt算法.
PHP版本5.3及更高版本将具有BCrypt的本机支持,因此您可以使用PHP函数password_hash()来哈希密码.5.5之前的版本有兼容包.
// Hash a new password for storing in the database. // The function automatically generates a cryptographically safe salt. $hashToStoreInDb = password_hash($password, PASSWORD_BCRYPT); // Check if the hash of the entered login password, matches the stored hash. // The salt and the cost factor will be extracted from $existingHashFromDb. $isPasswordCorrect = password_verify($password, $existingHashFromDb);
在任何情况下,您都正确地放弃MD5并切换到另一种算法.确保使用像BCrypt或PBKDF2这样的成本因子的算法,像SHA*这样的快速算法不适合散列密码.盐渍是强制性的,虽然盐可以存储在数据库中,即使它已知,它也能实现其目的.