Home >Database >Mysql Tutorial >Why is My Salted Password Authentication Always Returning True?
Retracting Salted Passwords for Authentication
When implementing a member site with salted passwords stored in a database, the login mechanism requires careful attention. While using a salted password approach enhances security, it can also present challenges in verifying user credentials.
Problem:
An error occurs on the member login page, where any entry seems to pass the check for verifying the user's existence. Specifically, the evaluation of $result === false always returns false, preventing the expected behavior.
Solution:
The root cause of the issue lies in the approach for retrieving the password hash from the database. To verify user credentials, one must first retrieve the salted password hash corresponding to the provided username:
$saltQuery = "SELECT salt FROM users WHERE name = '$name';"; $result = mysqli_query($connect, $saltQuery); $row = mysqli_fetch_assoc($result); $salt = $row['salt'];
Once the salt is obtained, it should be used to hash the provided password:
$saltedPW = $password.$salt; $hashedPW = hash('sha256', $saltedPW);
The resulting hashed password should then be used to query the database for the user's existence:
$sqlQuery = "SELECT * FROM users WHERE name = '$name' AND password = '$hashedPW'";
Further Considerations:
When verifying passwords, it's crucial to use strong password hashing functions like bcrypt or Argon2, which provide secure and time-intensive hashing algorithms. Additionally, prepared statements should be used to prevent SQL injection attacks.
The above is the detailed content of Why is My Salted Password Authentication Always Returning True?. For more information, please follow other related articles on the PHP Chinese website!