PHP SHA256和Salt无法正常工作

问题描述

| 我正在尝试创建使用$ salt变量进行sha256哈希加密的密码。但是由于某种原因,它将无法正常工作。现在已经为此工作了3个小时,而我正要努力工作。这是我的代码: 我会再试一次,对不起; o) 好的,我的脚本运行良好,直到尝试将sha256添加到密码中。我有一个用于创建用户文件,该文件是:
$salt = \"lollol\";  
$password = hash(\'sha256\',$salt.$_POST[\'password\']);  
$sql = (\"INSERT INTO members (username,password,name,last_name,company)VALUES(\'$username\',\'$password\',\'$name\',\'$last_name\',\'$company\')\")or die(MysqL_error());

if(MysqL_query($sql))
    echo \"Your accuont has been created.\";
似乎已正确添加数据库中。我可以看到它被散列了一些字母和数字。 但是,当我尝试登录时,它不会。 我的login.PHP代码是:
$sql= \"SELECT * FROM members WHERE username=\'$username\' and password=\'$password\'\";  
$result=MysqL_query($sql);    
$row=MysqL_fetch_array($result);  
$username = MysqL_real_escape_string($_POST[\'username\']);  
$password = $_POST[\'password\'];  
$salt = \"lollol\";  
$auth_user = hash(\'sha256\',$salt.$password);  
if($password == $salt.$auth_user){  
    echo \"Logged in\";  
} else {  
    echo \"Not logged in\";  
}  
我想到了这一点,当我想登录时必须加密密码,但是我不确定。我希望你们中的一些可以帮助我。     

解决方法

        尝试登录时,您将哈希与盐再次连接
$auth_user = hash(\'sha256\',$salt.$password);
if($password == $salt.$auth_user){ // <-- $salt once more
  echo \"Logged in\";
} else {
  echo \"Not logged in\";
}
如果您将其删除,它应该可以工作
$auth_user = hash(\'sha256\',$salt.$password);
if($password == $auth_user){
  echo \"Logged in\";
} else {
  echo \"Not logged in\";
}
更新:进一步 这里
$sql= \"SELECT * FROM members WHERE username=\'$username\' and password=\'$password\'\";
您尝试检索该行,其中用户名匹配“ 5”,密码匹配“ 6”。在数据库中,密码已经被散列了(似乎根本未定义“ 6”),因此该查询将永远不会返回任何行。
$password = hash(\'sha256\',$salt.$_POST[\'password\']);
$username = mysql_real_escape_string($_POST[\'username\']);
$sql= \"SELECT * FROM members WHERE username=\'$username\' and password=\'$password\'\";
$result=mysql_query($sql);
“ 9”现在应该包含唯一与给定凭据匹配的用户。现在很容易
if (mysql_num_rows($result) === 1) {
  echo \"Logged in\";
} else {
  echo \"Not logged in\";
}
    ,        您正在存储加密的密码,但是您的选择查询正在寻找未加密的密码。 只需获取匹配的用户名(无密码条件)-用户名是唯一的,对吧?
$sql= \"SELECT * FROM members WHERE username=\'$username\'\";  
$result=mysql_query($sql);    
$row=mysql_fetch_array($result);  
$username = mysql_real_escape_string($_POST[\'username\']);  
$password = $_POST[\'password\'];  
$salt = \"lollol\";  
$auth_user = hash(\'sha256\',$salt.$password);  
if($row[\"password\"] == $auth_user){  
    echo \"Logged in\";  
} else {  
    echo \"Not logged in\";  
}  
    ,        
$password = $_POST[\'password\'];

// This should be the users actual salt after you\'ve found the user
// in the database by username or email,or other means
$salt = $users_stored_salt;

// This should be the exact method you use to salt passwords on creation
// Consider creating a functon for it,you must use the same salt
// on creation and on validation
$hashed_password = hash(\'sha256\',$salt.$password.$salt);

// This is the user\'s hashed password,as stored in the database
$stored_password = $users_stored_password;

// We compare the two strings,as they should be the same if given the
// same input and hashed the same way
if ($stored_password === $hashed_password){
    echo \"Logged in\";
} else {
    echo \"Not logged in\";
}
错过了您的修改,但希望对您有所帮助。 编辑:我看到你没有存储唯一的哈希。 如果要通过密码查找用户,则需要使用与存储密码相同的方式对查询中的密码进行哈希处理:
$salt = $your_salt;

$hashed_password = hash(\'sha256\',$salt.$_POST[\'password\']);

$sql= \"SELECT * FROM members WHERE username=\'$username\' and password=\'$hashed_password\'\";  
否则,您可以按唯一的用户名(而不是密码)进行查找,然后将哈希输入与存储的密码值进行比较。   我现在很困惑。如果我应该使用顶部给出的代码来制作我的login_ac.php,应该如何? 只需将查询更改为通过哈希密码(存储方式)进行查询即可。
$sql= \"SELECT * FROM members WHERE username=\'$username\' and password=\'\".hash(\'sha256\',$salt.$_POST[\'password\']).\"\'\";  
您可以删除其他验证和哈希-如果找到了用户,则知道输入有效。 请注意,仅当您知道对输入进行哈希处理的方式与创建时对密码进行哈希处理的方式完全相同时,此方法才有效。     ,        值得检查的是,数据库中的字段长度足够大,可以存储整个散列密码而不截断它。如果存储的密码末尾丢失,则登录时永远不会获得密码匹配。