繁体   English   中英

PHP / MYSQL使用Salt密码验证用户登录名吗?

[英]PHP/MYSQL Authenticate user login using salt password?

我很抱歉对MySQL和PHP感到陌生,但在尝试使我的代码正常工作时遇到了问题。 用户可以使用用户名和密码在我的网站上注册。 我知道应该使用称为salt的加密方法来存储密码。 因此,现在完成该操作后,我希望用户能够登录,但不确定自己是否正确。

此刻我得到一个错误:

Fatal error: Call to undefined function db() in C:\xampp\htdocs\hewden\ssa\suppliers\include\validate_login.php on line 16

这是我的注册码:

<?php 
    session_start();
    include("../include/config.php");
    //retrieve our data from POST
    $contactname = $_POST['contactname'];
    $compname = $_POST['compname'];
    $username = $_POST['emailaddress'];
    $password1 = $_POST['password1'];
    $password2 = $_POST['password2'];

    if($password1 != $password2) {
    $_SESSION['message2'] = '<div id="message_box2"><div class="boxclose" id="boxclose" onclick="this.parentNode.parentNode.removeChild(this.parentNode);"></div><h23>Ooops!</h23><p>The Password&#39;s did not match.</p> </div>';
    header("location:..\sign-up.php");

    }else{
    if(strlen($username) > 30) {
    $_SESSION['message2'] = '<div id="message_box2"><div class="boxclose" id="boxclose" onclick="this.parentNode.parentNode.removeChild(this.parentNode);"></div><h23>Ooops!</h23><p>The Username you have selected is incorrect.</p> </div>';
    header("location:..\sign-up.php");

    }else{

    $hash = hash('sha256', $password1);

    function createSalt(){
    $text = md5(uniqid(rand(), true));
    return substr($text, 0, 3); }
    $salt = createSalt();
    $password = hash('sha256', $salt . $hash);

    $username = mysql_real_escape_string($username);
    $query = "INSERT INTO supplier_pre_sign (contact_name, company_name, supplier_email, password, salt, date) VALUES ('$contactname','$compname','$username', '$password', '$salt', now())";
    mysql_query($query); //You forgot to add this line
    echo $salt;

    } }?>

并将其存储在我的表“ supplier_pre_sign”中,如下所示:

ID (AI)   |   contact_name   |     company_name   |   supplier_email    |    password     |    salt

1               Dave                Hewden            hewden@hewden.com     test(hashed)        431

一旦注册获得批准,它们就会复制到我的其他表“ supplier_users”中

该表如下所示:

ID   |   USERNAME             |   PASSWORD       |   SALT  |  ONLINE
1        hewden@hewden.com        test(hashed)       431      Offline

这是我的登录代码:

<?php
session_start();
include("config.php");
$tbl_name="internal_users";  
$tbl_name2="supplier_users";  
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql = "select * from $tbl_name where username = '$myusername' and password = '$mypassword',
union
select * from $tbl_name2 where username = '$myusername' and password = '$mypassword'";

$sql = db("select * from $tbl_name where username = '$myusername' and password = '$mypassword',
    union
select * from $tbl_name2 where username = '$myusername' and password = '$mypassword'",
    $_POST['myusername'], hmac_hash("sha256", $_POST['mypassword'], $salt));
if(numRows($res) > 0) {


$result=mysql_query($sql);
$count=mysql_num_rows($result);
$row=mysql_fetch_array($result);
if($count==1){
session_start();
$_SESSION['user']=$myusername;
$_SESSION['username']=$row['First_Name'];
if(isset($_SESSION['val']))
$_SESSION['val']=$_SESSION['val']+1;
if($result){
$sql2 = "UPDATE $tbl_name2 SET online = 'online' WHERE online = 'offline'";  
$result2=mysql_query($sql2); }
else
$_SESSION['val']=1;
header("location:../dashboard.php");
}
else {
    echo mysql_error();
$_SESSION['message2'] = '<div id="message_box2"><div class="boxclose" id="boxclose" onclick="this.parentNode.parentNode.removeChild(this.parentNode);"></div><h23>Oooops!</h23><p>The Username and Password Combination do not match. Please try again.</p> </div>';
header("location:../index.php");
} }
ob_end_flush();
?>

现在我的问题是如何获取登录脚本以对用户进行身份验证,因为我并不真正了解salt的工作原理,我的用户只需输入“ test”以期望它可以登录,那么如何登录通过检查盐密码匹配?

提前致谢

为此,PHP现在具有内置函数password_hash() 该函数生成长度为60个字符(包括盐)的BCrypt哈希,因此您不必单独存储盐。

// Hash a new password for storing in the database.
// The function automatically generates a cryptographically safe salt.
$hashToStoreInDb = password_hash($password, PASSWORD_BCRYPT);

// Check if the hash of the entered login password, matches the stored hash.
// The salt and the cost factor will be extracted from $existingHashFromDb.
$isPasswordCorrect = password_verify($password, $existingHashFromDb);

PS不要使用SHA- *或MD5之类的任何哈希算法,因为它们没有成本因素,而且速度太快。 有关更多信息,请参阅我的有关安全存储密码的教程

您是对的,因为使用盐是个好主意,但您似乎对使用方法有误解。 Salt不是一个单独的密码,而是在对密码进行哈希处理时添加到密码中的额外数据,以便在数据库受到威胁时使破解变得更加困难。

阅读一个很好的教程,如“ 安全盐密密码哈希-如何正确执行”,将有助于您更好地理解该想法。

因此,当您对密码进行哈希处理时,您应该执行以下操作:

$hash = hash('sha256', $password1.$salt);

注意 :不是PHP程序员,所以语法错误是我

至于登录用户,您需要首先根据用户名从数据库中查找salt,然后在对数据库进行哈希检查之前,用salt对提供的密码进行哈希处理。

请注意,使用预构建的库(例如PHPass)进行密码哈希处理比执行自己的实现要好得多

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM