[英]Codeigniter and hash password for login?
我正在创建一个登录页面,我需要帮助使用 CodeIgniter 访问我的数据库中的 hashpassword。
我的 hashPassword function 是:
function hashPassword($pass, $passKey){
$key = base64_decode($passwordKey);
return base64_encode(hash_hmac(
'sha256',
$key . mb_convert_encoding($password, 'UTF-16LE', 'ASCII'),
$key, true));
}
我必须执行以下操作:
username
从my_aspnet_Users
表中提取用户的id
my_aspnet_Membership
中提取password
和passwordKey
密钥password
是否等于hashPassword(<password entered by user>, passwordKey)
以了解输入的密码是否有效。所以我有两个模型:
Class Password extends CI_Model{
function getPassword($password, $passwordKey){
$this -> db -> select ('userId, Password, PasswordKey');
$this -> db -> from ('my_aspnet_Membership');
$this -> db -> where ('Password = '. "'" . $password . "'");
$this -> db -> where ('PasswordKey = '. "'" . $passwordKey . "'");
$q = $this -> db -> get();
if ($q -> num_rows() == 1){
return $q ->result();}else{
return false;
}
}
}
和..
Class User extends CI_Model{
function getUser($id){
$this -> db -> select('id, name ');
$this -> db -> from('my_aspnet_Users');
$this -> db -> where('id = '. "'". $id . "'");
$this -> db -> limit(1);
$query = $this -> db -> get();
if($query -> num_rows() == 1){
return $query->result();
}
else{
return false;
}
}
}
我只是不知道如何让它们一起工作以执行密码检查。
首先,对您的设计提出几点意见。
所以伪代码会读到类似
class User extends CI_Model
function getHash($password) {
/* Code to get hash */
return $passwordHash;
}
function validateUser($username, $password) {
$passwordHash = $this->getHash($password);
/* Query MySQL
select * from user
where
username = $username and
password_hash = $passwordHash
*/
if num_rows > 1
return true
else
return false
}
}
如果您没有用户名选项,请在查询中使用用户 ID/用户 email(唯一标识用户的任何内容)。
希望这可以为您澄清。
CodeIgniter 使用了一个名为“Tank Auth”的库: http://konyukhov.com/soft/tank_auth/
它包括 class “PasswordHash.php”: http://bit.ly/1gahwtT
示例代码:
require "PasswordHash.php";
define("phpass_hash_portable",TRUE);
define("phpass_hash_strength",8);
$hasher = new PasswordHash(phpass_hash_strength,phpass_hash_portable);
if ($hasher->CheckPassword($password_to_check, $original_encoded_password)) {
echo "password correct";
} else {
echo "password incorrect";
}
只需重用相同的 function (顺便说一句,这太复杂了)。 因此,如果当我创建一个帐户时,您在存储密码之前对我的密码运行 functionX(),只需对某人在登录时输入的密码运行 functionX() 并查看数据库中输入的用户名是否存在?
编辑:我认为您的问题主要是您需要一个用户名/电子邮件字段来确认用户与输入的密码相同。 以上仍然适用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.