Password Hashing and Password Verifying [duplicate] - php

This question already has answers here:
How to use PHP's password_hash to hash and verify passwords
(5 answers)
Closed 2 years ago.
I hashed a password through
$hashedpassword = password_hash($Password, PASSWORD_DEFAULT);
This stores the password as a hashed value into a database.
But when I try to login in through
$Password=$_POST['Password'];
$hashedpassword = password_hash($Password, PASSWORD_DEFAULT);
if(password_verify($Password, $hashedpassword))
It will always tell me that the password is correct, regardless on whether it is or not.
Is there a way around this, so I can hash the password but login with the entered (non-hashed) password.

At some point in the past, when the account was created or when the password was last changed, you should have stored the hashed password.
You need to get the stored password hash from the database and use that with password_verify.
Currently, you are hashing the newly submitted password and verifying the submitted password against that, so of course, it always matches.

Related

Change hashing method to a already hashed password [duplicate]

This question already has answers here:
How do I convert password hashing from MD5 to SHA?
(7 answers)
Closed 11 days ago.
I would like if it's possible to change the hashing method for an already hashed password. For example:
$password_input = '123456789';
$hashed_password = md5($password_input);
// The output would be 25f9e794323b453885f5181f1b624d0b
The result was made with the following online tool:
https://helloacm.com/md5/
The next step would be insert the hashed password into the database. When I do this the given hashed password will be in the users table. If I select that password, can I change the md5 hash by a sha-256? For example:
$md5_password = '25f9e794323b453885f5181f1b624d0b';
$sha256_password = hash('sha256', $md5_password);
If this would be possible, would it break the login function? I mean if I use password_verify method, will it return true?
You will not get password back from md5, you can't unhash one way hash algorithms.
What we do - incorporate re-hashing in login flow.
User logins to your system with old hash password
You detect, that this user needs re-hash
While still having sent plain text password you hash it with new algorithm and save to database
Next time user logins with newly hashed password without problems

how password_verify() function actually works in PHP [duplicate]

This question already has answers here:
How does password salt help against a rainbow table attack?
(10 answers)
How can bcrypt have built-in salts?
(5 answers)
Password hashing, salt and storage of hashed values
(4 answers)
Closed 3 months ago.
I am wondering about how password_verify() verifies the hash, I have reviewed the documentation and many answers in StackOverflow, but I didn't get the idea
because, as I understood, this function will compare the hash with entered password after hashing it again, and use the same salt and cost and algorithm,
but the question here: if anyone can separate the salt from the hashed password, then anybody also can try to use rehash and try to match, and the salt will be useless here. Am I right, or what?
The salt have to be generated randomly each time the fonction is used (and it's what this function does, and not accept custom salt anymore).
For example:
<?php
$password = "nothing";
echo password_hash($password, PASSWORD_DEFAULT);
echo PHP_EOL;
echo password_hash($password, PASSWORD_DEFAULT);
Give the response :
$2y$10$mdJRjsoc1vR11SKa2JDyS.qSlxja/a0SUPuXC1NKsRLkzmayKwjku
$2y$10$H2th6dRY/i.xZzXSGxDZ1uaiwZx6s0.FM0NXcBcBQ0E2aNEHCJ57m
It's the same password with differents results.
The hashed password is stored in a database or a file. In this case, an admin system (or someone who's hacked the database) can't say if the same password is used by differents users. Another point, rainbow tables can't be used with hashed password with salt. Only brut force can be done.
Using the same salt for all is not more secure than using simple hash algorytm.

Hash_password make a different password each time

Actually my problem is hash_password make a diffrent hash each time with same word i dont know how to check my input password with data base
I have a hashed password in my db and im trying to hash my input with same algorythm but i get a diffrent value
$pass = password_hash($_post["pass"] , ARGON2I);
If($pass === $admin["Pass"]){
echo "success";
else
echo "failed";
Assuming the language is PHP:
password_hash creates a unique hash each time even for the same password, this is because there is a random salt used each time. The salt is included in the result of password_hash.
To verify the hashed password use password_verify which uses the salt saved with the hash to compare and return a match or not.
The reason for the unique hashing is so that two users with the same password do not hash to the same value so that knowing one does not allow knowing other user's passwords from a matching hash.
See the linked documentation.

Comparing passwords to stored hash [duplicate]

This question already has answers here:
Secure hash and salt for PHP passwords
(14 answers)
Closed 7 years ago.
From my understanding so far (at least I think) the password_hash() function generates a hash based on the algorithm in use, cost and the salt. While the password_verify uses the information provided from e.g. password_hash($pass, PASSWORD_BCRYPT, array('cost'=>10)) to check if the retuned value is true or false as it contains all the information necessary for verifying.
I previously used
$SQL_Query = "SELECT * FROM DB_Table WHERE userName = '".$username."'" AND password = $ID;
which would work as they were stored in plain text and could return true whereas logically it won't work this time around.
I have came across similar questions where they use static passwords in explanations such as
<?php
$to_verify = '$2y$07$BCryptRequires22Chrcte/VlQH0piJtjXl.0t1XkA8pw9dMXTpOq';
if (password_verify('rasmuslerdorf', $to_verify))
{
echo 'Password is valid!';
} else
{
echo 'Wrong password.';
}
The concept I am having trouble understanding is how one would check the password input against the hashed value if it is stored in a database rather than the being known by the page at that point in time? I recently got help regarding storing the values which was a silly error on my part but I guess this isn't clicking with me as well as I hoped for the moment.
Look at the examples for password_hash() and password_verify() together.
The hash-string that's produced by password_hash is self-describing: it incorporates an indication of both the algorithm and the random-salt that was used. password_verify knows about all this. It knows how to "do the right thing" for passwords both recent and vintage.
Therefore, simply query the database to get the (hashed ...) password for this user. Then, use password_verify() to see if this hash-value matches this password-value.
You can't query for the user-name AND password at the same time. Query only for the user-name, get the hashed value, and use password_verify() to check it.
the hash is generated randomly each time
No, the hash is always the same for a given input, salt value and iterations through which the hash algorithm is run (which is controlled by the cost parameter).
The concept I am having trouble understanding is how one would check the password input against the hashed value if it is stored in a database rather than the being known by the page at that point in time?
You would check the password input at login time, using the password provided by the user, and the salt and potentially number of times to apply the hash algorithm associated with that user. Once the password check is successful, use a session or other mechanism to keep the user logged in.

Checking username and hashed/salted password in mysql database using PHP

When validating user logins, is it safe to first search for the username and THEN if found, retrieve the hashed password and salt and compare it with the user input?
Or, should the salt for the user-inputed username be retrieved by itself, then be hashed with the inputed password and compared with the final hash in the database?
In essence, is it safe to store a password for an inputed username from a database before knowing whether the password the user entered is valid?
If you look at the examples of the PHP Password Hashing API https://github.com/ircmaxell/password_compat the answer is: No, you read the user from the database and compare the stored password hash with the password you just got from the login form.
And please try to use this library - PHP 5.5 will support the functions natively, and if you are on PHP 5.3.7 and later, it sounds like a very good idea not to reinvent the wheel and simply use these functions. Doing your own thing is more likely to be attackable.
You may check both loginname and password at the same time. Get the raw password; salt it, then check if salted password and username combination exist. Ofcourse, password in db should have been salted and hashed before.
if($loginname AND $loginpass){
$loginpass=sha1($salt1.$loginpass.$salt2,$raw_output=false );
$userinfo_query="SELECT * FROM users WHERE user_name='$loginname' AND user_pass='$loginpass' LIMIT 1";
}

Categories