我正在研究关于在数据库中存储密码的不同解决方案。看了很多书之后,我想我会和PBKDF2一起结束的。
虽然对于是否应该将salt输入到我的PBKDF2函数中并将salt存储在列中,PBKDF2 2‘d密码存储在另一列中,我有些困惑。
我还在使用CodeIgniter,并为PBKDF2 (https://github.com/HashemQolami/CodeIgniter-PBKDF2-Library)找到了一个库,它声称我不需要单独存储盐。
使用
$pbkdf2['hash']作为建议的用户密码注册用户,不需要单独存储用户的salt。 https://github.com/HashemQolami/CodeIgniter-PBKDF2-Library#step-2
所以,如果我假设正确的话,我所需要的就是在函数中提供一个密码,然后函数来处理剩下的部分呢?
发布于 2014-01-30 11:54:19
我是CodeIgniter PBKDF2库的创造者。刚刚在SO上找到了这个主题,我决定澄清这个库是如何工作的。
下面是文档中的示例代码:
# Load pbkdf2 library into your controller
$this->load->library('pbkdf2');
# Get password, which has been sent via POST method
$password = $this->input->post('password');
# Encrypt the given password using a random generated salt
$pbkdf2 = $this->pbkdf2->encrypt($password);encrypt()方法,返回数组,它有三个键:salt、password、hash。hash的值是salt和password的连接。
此特性允许用户选择如何使用此库,是否使用salt和密码或散列(salt +密码)。
encrypt()方法的语法:
encrypt( string $password [, mixed $good_hash = NULL [, bool $object_output = FALSE]] )该函数使用给定的$good_hash 盐生成加密的密码。如果不给出参数,则使用随机生成的salt。
因此,如果单独存储了salt,则可以将其作为第二个参数传递给函数,以加密给定的密码:
$pbkdf2 = $this->pbkdf2->encrypt($password, $salt);另一方面,,如果将salt和password的连接存储到数据库中,也可以将其作为第二个参数传递给函数:
$pbkdf2 = $this->pbkdf2->encrypt($password, $hash);函数$hash自动获取salt。
因此,您可以将salt和密码的级联存储在列(默认情况下64个字符)中,然后使用旧存储的密码加密新的给定密码。
拼凑在一起
在下面的文章中,我将向您展示如何使用这个库注册/登录用户,而无需分别存储salt和密码。
注册用户:
$this->load->library('pbkdf2');
$password = $this->input->post('password');
$pbkdf2 = $this->pbkdf2->encrypt($password);
# Store $pbkdf2['hash'] into User table as the user's password在用户中登录:
$this->load->library('pbkdf2');
$username = $this->input->post('username', TRUE);
$password = $this->input->post('password');
# Fetch the stored user's password from the database
$user_password = $this->user_model->get_password_by($username);
# Check whether the User exists
if ($user_password)
{
# Encrypt the new given password by using the old one:
$pbkdf2 = $this->pbkdf2->encrypt($password, $user_password);
# Check whether the new generated password matches the old one
if ($pbkdf2['hash'] === $user_password) {
# Log in the user ...
} else {
# Show an error...
}
} else {
# Show an error...
}https://stackoverflow.com/questions/19569042
复制相似问题