密码哈希无法登录 codeigniter
password hashing cannot login codeigniter
我有这个帮手来加密我的密码
<?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;
}
}
您认为可能是什么问题?
你把它弄得有点过于复杂了——你正在使用的函数的动作放在一个函数中没有什么意义——它是一个简单的单行代码。并且 password_verify()
已经 returns 一个布尔值 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 个字符)。
我有这个帮手来加密我的密码
<?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;
}
}
您认为可能是什么问题?
你把它弄得有点过于复杂了——你正在使用的函数的动作放在一个函数中没有什么意义——它是一个简单的单行代码。并且 password_verify()
已经 returns 一个布尔值 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 个字符)。