2

I am using sha1 for my password security. I have stored password in this way in register.php

// secure password
$salt = openssl_random_pseudo_bytes(20);
$secured_password = sha1($password . $salt);

//Send it to mysql table
$result = $access->registerUser($username, $secured_password, $salt, $email, $fullname);

This all is working fine.

Problem is here:

In my login.php

 $password = htmlentities($_POST["password"]);
$secure_password = $user["password"];
$salt = $user["salt"];


// 4.2 Check if entered passwords match with password from database
if ($secure_password == sha1($password . $salt)) {
//do something 
} else {

//do something
 }

I am always getting as password does not match. where am I going wrong?

Paulw11
  • 108,386
  • 14
  • 159
  • 186
swift_USer
  • 159
  • 3
  • 15

1 Answers1

7

First is first. NEVER USE SHA OR MCRYPT TO STORE YOUR PASSWORD.

EDIT : The password_hash() function generates a long password hash, so make sure that your column in the mysql is a VARCHAR of 500 space

All these useless practises is the root reason why almost many websites get hacked. To tackle the situation, php did a lot of research and then at last came with the most secure function called the password_hash(). I am not more onto explaining about password_hash() here as there are already many documents on the internet.

You can always hash a password like this

<?php

$securePassword = password_hash($_POST['password'], PASSWORD_DEFAULT);

$query = $db->query('INSERT INTO users ......');

?>

And, to verify the password, you can simply use this function

<?php

$passwordHash = $query['password']; //Password from database
$userPassword = $_POST['password']; //Password from form

if(password_verify($userPassword, $passwordHash)) {
    echo 'Password is correct, logged in!';
} else {
    echo 'Password is wrong, try again';
}

?>

And, answer for your question.

PLEASE DON'T USE SHA OR MCRYPT OR BCRYPT. IF YOU WANNA GET YOUR WEBSITE HACKED, THEN CONTINUE. OR USE password_hash()

The reason you don't get the hash genereated each time because the openssl_random_pseudo_bytes() generates random numbers each time. So each time, during execution, the function returns different numbers and you get your sha result wrong and thus giving a FALSE alert.

PLEASE, AGAIN. I BEG YOU TO USE password_hash() FUNCTION


For more information on password_hash() and password_verify() :

Mike Doe
  • 16,349
  • 11
  • 65
  • 88
xXAlphaManXx
  • 161
  • 1
  • 6
  • You are welcome @Spurti. I am just 15yrs old. Any help? Comment down here :) – xXAlphaManXx May 09 '17 at 08:58
  • 1
    very impressive from a 15 year :) big up – Masivuye Cokile May 09 '17 at 09:14
  • Actually, password_hash is using bcrypt. Also, what's the relation between a website getting hacked and using "weak" (once again, it's not proved) hashing algorithm? And, maybe 500 is a bit too much, even the documentation recommand 255. – LoïcR May 09 '17 at 09:26
  • Well, I merely agree with you @Sakuto. But, as the test reveals, the password_hash() function is the most secure way of hashing password in this era (or at least in PHP7). And, about the relation matter, I agree with you :) – xXAlphaManXx May 09 '17 at 09:28
  • Password given to password_hash is truncated to 72 characters. And no, it won't give you 500 character long string, it will always be 60 characters in length. – Mike Doe May 09 '17 at 09:30
  • Hmm, I didn't know about that. Merely, to accept, I was kind of lazy to search the exact length of `password_hash` @mike – xXAlphaManXx May 09 '17 at 09:32
  • 1
    The recommendation is a VARCHAR(255). While the current hash is only 60 characters long, if the default algorithm changes with a new version of PHP, then that length might change – Mark Baker May 09 '17 at 09:38