Decryption of Password Hashes in PHP with Password_hash Function
Question:
You need to decrypt a password that is encrypted using the password_hash function. Assuming the hashed password is stored in a database, and you have a plaintext password entered by the user, how do you determine if they match without compromising security?
Answer:
Bcrypt is the hashing algorithm used by the password_hash function, and it is irreversible. Therefore, there is no direct way to decrypt hashed passwords.
Instead, to validate the user's password, follow these steps:
Example Code for Password Validation:
<code class="php">// Assume $hash is the hashed password from the database if (password_verify($inputPassword, $hash)) { echo 'Password is valid!'; } else { echo 'Invalid password.'; }</code>
Important Note:
It's crucial to parameterize user input in SQL queries to prevent SQL injection attacks. Consult the Stack Overflow answer provided for guidance on this practice.
The above is the detailed content of How to Verify User Passwords Without Decrypting Hashes Using PHP\'s `password_hash` Function?. For more information, please follow other related articles on the PHP Chinese website!