开发者

Need help making login page with salts

Alright, I'm trying to make a login page. It seems that all of the pages worked pretty good- until I added salts. I don't really understand them, but doing something as basic as I am shouldn't be to hard to figure out. Here's "loginusr.php":

<html>
<body>
<?php  

//form action = index.php

session_start();

include("mainmenu.php");  

$usrname  = mysql_real_escape_string($_POST['usrname']);    
$pass     = $_POST['password'];  
$salt     = $pass;
$password = sha1($salt.$pass);

$con = mysql_connect("localhost", "root", "g00dfor@boy");
if(!$con)
{
    die("Unable to establish connection with host. We apologize for any inconvienience.");
}

mysql_select_db("users", $con) or die("Can't connect to database.");

$select = "SELECT * FROM data WHERE usrname='$usrname' and password='$password'";
$query  = mysql_query($select);
$verify = mysql_num_rows($query);

if($verify==1)
{
    $_S开发者_如何学运维ESSION["valid_user"] = $usrname;
    header("location:index.php");
}    
else
{
    echo "Wrong username or password. Please check that CAPS LOCK is off.";
    echo "<br/>";
    echo "<a href=\"index.php\">Back to login</a>";
} 

mysql_close($con);    
?> 
</body>
</html>

I used the command echo $password; to show me if the password in the database matched with the script. They did. What am I doing wrong?


It seems like you've misunderstood salts, since you're setting $salt to be the password.

A salt should be a completely random string that's stored in a user record along with the password hash. A new unique salt should be generated for every user. So you need to add a new column to your database, called "password_salt" or similar.

Rather than trying to use the password in the SELECT query and see if you get any records, you actually need to just SELECT using the username/user_id in order to get the password hash and salt so that you can then use those to determine if the user entered the correct password.

When you sign up new users you should add the fields with values like this,

<?php
// This is registeruser.php

$salt = substr(sha1(uniqid(rand(), true)), 0, 20);
$pass = $_POST['password'];
$pass_to_store = hash("sha256", $salt.$pass);

// Then issue a DB query to store the $salt and $pass_to_store in the user record.
// Do not store $pass, you don't need it.
// e.g. INSERT INTO users ('username', 'password_salt', 'password_hash') VALUES (:username, :salt, :pass_to_store);
?>

Then to check the password is the same when logging in, you do something like this,

<?php
// This is loginuser.php

$user = // result from SQL query to retrieve user record
// e.g. SELECT password_hash, password_salt FROM users WHERE username='from_user'

$salt_from_db = $user['password_salt'];
$pass_from_db = $user['password_hash'];
if ($pass_from_db == hash("sha256", $salt_from_db.$_POST['password'])
{
    // Password matches!
}
?>

Don't forget to sanitize user inputs and anything you're putting into your database. You might want to look into using prepared statements instead of having to remember to use mysql_real_escape_string all the time.


It looks like you're salting with the same password? Normally a salt would be a random key that is specific to your site that you prepend to the password input, which it looks like you're doing fine. Just make sure you're using that same salt for checking that you use when the password is created.

Also, to use sessions properly you need to have session_start before anything is output to the page:

<?php

session_start();

?>
<html>
<body>
...


A salt is a random value to prevent an attacker from just looking up the source of a hash in table generated based on common passwords. (Using the username as salt is obviously not a good idea as it only adds very little entropy).

So you need to store the salt in the database and read it from the database in order to calculate the salted password hash for comparison with the stored value.

You misspelled username a couple of times, is it misspelled in the database, too?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜