Home > Backend Development > PHP Tutorial > Should I Store the Salt Separately When Using PHP's `password_hash()`?

Should I Store the Salt Separately When Using PHP's `password_hash()`?

Barbara Streisand
Release: 2024-12-29 06:23:10
Original
926 people have browsed it

Should I Store the Salt Separately When Using PHP's `password_hash()`?

Using PHP 5.5's password_hash and password_verify Functions for Secure Password Storage

Question:

When using PHP 5.5's password_hash() function to store user passwords, should the salt be stored separately from the hash?

Answer:

No, storing the salt separately from the hash is incorrect. The password_hash() function generates a string that contains both the hash and the salt. Here's the proper way to use it:

$hashAndSalt = password_hash($password, PASSWORD_BCRYPT);
// Insert $hashAndSalt into database against user
Copy after login

To verify the password:

if (password_verify($password, $hashAndSalt)) {
    // Verified
}
Copy after login

This approach provides optimal security as it prevents attackers from accessing the salt and compromising the hash. Additionally, it is recommended to use mysqli instead of ext/mysql, which is deprecated in PHP 5.5, and to be aware of SQL injection vulnerabilities.

The above is the detailed content of Should I Store the Salt Separately When Using PHP's `password_hash()`?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template