简体   繁体   English

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

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

I am brand new to MySQL and PHP so apologies, but I'm having problems trying to get my code to work. 我很抱歉对MySQL和PHP感到陌生,但在尝试使我的代码正常工作时遇到了问题。 Users can register on my site with a username and password. 用户可以使用用户名和密码在我的网站上注册。 I understand that a password should be stored using an encryption method called salt. 我知道应该使用称为salt的加密方法来存储密码。 So having done that now I want the user to be able to login and I'm not sure I'M doing it right. 因此,现在完成该操作后,我希望用户能够登录,但不确定自己是否正确。

at the moment I get an error: 此刻我得到一个错误:

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

here's my registration code: 这是我的注册码:

<?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;

    } }?>

and this gets stored in my table 'supplier_pre_sign' like so: 并将其存储在我的表“ supplier_pre_sign”中,如下所示:

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

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

once registrations get approved they are copied into my other table 'supplier_users' 一旦注册获得批准,它们就会复制到我的其他表“ supplier_users”中

this table is like so: 该表如下所示:

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

here's my log in code: 这是我的登录代码:

<?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();
?>

now my question is how do I get my login script to authenticate the user as I don't really understand how the salt works, my user would just type in 'test' expecting it to log them in, so how do I log them in by checking the salt password matches? 现在我的问题是如何获取登录脚本以对用户进行身份验证,因为我并不真正了解salt的工作原理,我的用户只需输入“ test”以期望它可以登录,那么如何登录通过检查盐密码匹配?

Thanks in advance 提前致谢

PHP has now a built in function password_hash() for this purpose. 为此,PHP现在具有内置函数password_hash() The function generates a BCrypt hash of length 60 characters (including the salt), so you don't have to store the salt separately. 该函数生成长度为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 Do not use any hash algorithms like SHA-* or MD5 because they do not have a cost factor and are ways too fast. PS不要使用SHA- *或MD5之类的任何哈希算法,因为它们没有成本因素,而且速度太快。 For further information you can have a look at my tutorial about safely storing passwords. 有关更多信息,请参阅我的有关安全存储密码的教程

You're correct in that using salt is a good idea but you appear to have misunderstood how to use it. 您是对的,因为使用盐是个好主意,但您似乎对使用方法有误解。 Salt is not a separate password but rather extra data added to the password when hashing it so that it makes cracking harder should your database be compromised. Salt不是一个单独的密码,而是在对密码进行哈希处理时添加到密码中的额外数据,以便在数据库受到威胁时使破解变得更加困难。

Reading a good tutorial like Secure Salted Password Hashing - How to do it Properly will help you understand the idea better. 阅读一个很好的教程,如“ 安全盐密密码哈希-如何正确执行”,将有助于您更好地理解该想法。

So when you hash the password what you should be doing is the following: 因此,当您对密码进行哈希处理时,您应该执行以下操作:

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

NB - Not a PHP programmer so syntax errors are me 注意 :不是PHP程序员,所以语法错误是我

As for logging in a user you need to first look up the salt from the database based on the username and then hash the provided password with the salt before checking the hash against your database. 至于登录用户,您需要首先根据用户名从数据库中查找salt,然后在对数据库进行哈希检查之前,用salt对提供的密码进行哈希处理。

Note that it is almost always much better to use a pre-built library such as PHPass for password hashing rather than doing your own implementation 请注意,使用预构建的库(例如PHPass)进行密码哈希处理比执行自己的实现要好得多

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

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