Password Security with PHP 5.5's password_hash and password_verify Functions
Query:
When storing passwords securely using PHP 5.5's password_hash() function, is it appropriate to only store the salt in the database?
Answer:
No. For enhanced password security, it is essential to store both the hash and salt in the database. password_hash conveniently generates a string that includes both elements.
Recommendations:
To implement secure password storage:
-
Generation: Use password_hash() to create a hashed password: $hashAndSalt = password_hash($password, PASSWORD_BCRYPT);
-
Storage: Insert $hashAndSalt into the database for the corresponding user.
-
Verification: To verify a submitted password, retrieve the stored $hashAndSalt from the database and use password_verify(): password_verify($password, $hashAndSalt)
Additional Considerations:
-
Security Practices: Utilize mysqli instead of the outdated ext/mysql to improve security.
-
SQL Injection Prevention: Understand and mitigate the risks of SQL injection by following best practices (e.g., prepared statements).
The above is the detailed content of Should I Only Store the Salt When Using PHP 5.5's `password_hash()` for Password Security?. For more information, please follow other related articles on the PHP Chinese website!