首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >密码salt和PBKDF2

密码salt和PBKDF2
EN

Stack Overflow用户
提问于 2013-10-24 14:41:03
回答 1查看 1.9K关注 0票数 2

我正在研究关于在数据库中存储密码的不同解决方案。看了很多书之后,我想我会和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

所以,如果我假设正确的话,我所需要的就是在函数中提供一个密码,然后函数来处理剩下的部分呢?

EN

回答 1

Stack Overflow用户

发布于 2014-01-30 11:54:19

我是CodeIgniter PBKDF2库的创造者。刚刚在SO上找到了这个主题,我决定澄清这个库是如何工作的。

下面是文档中的示例代码:

代码语言:javascript
复制
# 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()方法,返回数组,它有三个键:saltpasswordhashhash的值是saltpassword的连接。

此特性允许用户选择如何使用此库,是否使用salt和密码或散列(salt +密码)。

encrypt()方法的语法:

代码语言:javascript
复制
encrypt( string $password [, mixed $good_hash = NULL [, bool $object_output = FALSE]] )

该函数使用给定的$good_hash 生成加密的密码。如果不给出参数,则使用随机生成的salt。

因此,如果单独存储了salt,则可以将其作为第二个参数传递给函数,以加密给定的密码:

代码语言:javascript
复制
$pbkdf2 = $this->pbkdf2->encrypt($password, $salt);

另一方面,,如果将saltpassword的连接存储到数据库中,也可以将其作为第二个参数传递给函数:

代码语言:javascript
复制
$pbkdf2 = $this->pbkdf2->encrypt($password, $hash);

函数$hash自动获取salt

因此,您可以将salt和密码的级联存储在列(默认情况下64个字符)中,然后使用旧存储的密码加密新的给定密码。

拼凑在一起

在下面的文章中,我将向您展示如何使用这个库注册/登录用户,而无需分别存储salt和密码。

注册用户:

代码语言:javascript
复制
$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

在用户中登录:

代码语言:javascript
复制
$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... 
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19569042

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档