密码哈希无法登录codeigniter

时间:2019-10-03 05:04:05

标签: php codeigniter encryption bcrypt

我有这个助手来输入密码

<?php 
defined('BASEPATH') OR exit('No direct script access allowed');

/*
 * This function used to generate the hashed password
 * @param {string} $plainPassword : This is plain text password
 */
if(!function_exists('getHashedPassword'))
{
    function getHashedPassword($plainPassword)
    {
        return password_hash($plainPassword, PASSWORD_DEFAULT);
    }
}
/**
 * This function used to generate the hashed password
 * @param {string} $plainPassword : This is plain text password
 * @param {string} $hashedPassword : This is hashed password
 */
if(!function_exists('verifyHashedPassword'))
{
    function verifyHashedPassword($plainPassword, $hashedPassword)
    {
        return password_verify($plainPassword, $hashedPassword) ? true : false;
    }
}

?>

现在我将散列密码存储到我的数据库中没有任何问题,我正在这样做

在我的model

function saveAccount($userinfo)
{
    $data = array(
       'username' => $this->input->post('username'),
       'password' => $userinfo,
       'type' => $this->input->post('accountType')
    );

    return $this->db->insert('users', $data);

}

和我的controller

$userInfo = getHashedPassword($this->input->post('password'));
$this->employee->saveAccount($userInfo);

现在此注册过程正在运行,现在我将其存储在数据库中没有任何问题,我的问题是,例如,当我尝试登录到当前注册的用户时,就会遇到问题。 用户名:admin5 密码:admin5(在我的数据库中已加密)

在我的控制器上登录

$username = $this->input->post('username');  
$password = $this->input->post('password');
$user_data = $this->employee->can_login($username,$password);

在我的模型上

function can_login($username, $password)
    {

        $this->db->where('username', $username);
        $this->db->where('password', $password);
        $query = $this->db->get('users');
        $user = $query->result();

        verifyHashedPassword($password, $user[0]->password);
        if($query->num_rows() > 0)
        {
            return $query->row_array();
        }
        else
        {
            return false;
        }
    }

您认为可能是什么问题?

1 个答案:

答案 0 :(得分:2)

这使它有点过于复杂-您正在使用的函数的动作在函数内部放入几乎没有意义-它是简单的单行代码。并且password_verify()已经返回了布尔值true / false,因此您也不需要使用三元运算符。

通过执行以下操作(并删除您的getHashedPassword()函数),您的插入内容将更加清晰明了

function saveAccount()
{
    $data = array(
       'username' => $this->input->post('username'),
       'password' => password_hash($this->input->post('password'), PASSWORD_DEFAULT),
       'type' => $this->input->post('accountType')
    );

    return $this->db->insert('users', $data);
}

然后,在can_login()函数中,无法在WHERE子句中查询密码。这样,您将永远不会得到结果(因为哈希不能通过比较运算符进行比较)。您需要获取它,然后使用password_verify()比较检索到的哈希。调用verifyHashedPassword()而不检查结果不会神奇地检查任何内容。现在,您还可以删除verifyHashedPassword()函数。

function can_login($username, $password) {
    $this->db->where('username', $username);
    $query = $this->db->get('users');
    $user = $query->result();

    if ($query->num_rows() > 0 && password_verify($password, $user[0]->password)) {
        return $query->row_array();
    } else {
        return false;
    }
}

您的password列的长度至少应为60个字符,尽管为了适应将来的更改,它也可以更长(例如255个字符)。