Secure hash and salt for PHP passwords

Secure hash and salt for PHP passwords

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Secure hash and salt for PHP passwords

Choosing the right mechanism for password protection in PHP requires moving past the legacy functions found in older documentation. When evaluating options like md5() or sha1(), it is crucial to recognize that these algorithms are computationally fast by design. That speed, which once made them suitable for verifying file integrity, makes them disastrous for authentication because attackers can run brute-force attacks against millions of guesses per second using specialized hardware.

The consensus in the security community is that double hashing—hashing a hash multiple times—is not a viable strategy. While increasing iterations might slow down an attacker, it also slows down legitimate login attempts linearly. More importantly, if an attacker obtains your database, they can pre-compute rainbow tables or utilize GPU acceleration to crack standard hashes regardless of how many layers you add. The only robust solution is to use algorithms specifically designed with a "work factor" that allows you to adjust computational cost dynamically as hardware improves.

In PHP, the native choice for this task is password_hash(). This function wraps the PBKDF2 algorithm (using SHA-256), bcrypt, or scrypt depending on your PHP version and configuration. These algorithms are intentionally slow and incorporate a random salt automatically, solving two of your requirements simultaneously: they ensure unique hashes even for identical passwords and they prevent pre-computed attacks.

Generating Salts

You asked if all salts are equally good and how to generate them. In the context of password_hash(), you do not need to worry about generating salts manually or storing them separately. The function generates a cryptographically secure random salt internally for every password stored.

The resulting hash string includes the algorithm identifier, the cost factor (iterations), and the salt all in one standardized format. For example, a bcrypt hash looks like $2y$10$r.... When verifying a password later, you simply pass the user-provided password to password_verify(), and the function reads the salt embedded within that stored hash string. This eliminates the risk of generating "bad" salts or accidentally reusing a global salt across multiple users, which would render the hashes useless against rainbow table attacks.

Database Schema Design

Regarding your question about storing two fields (one for MD5 and one for SHA), this approach is unsafe and unnecessary. Maintaining legacy hash formats alongside modern ones creates a false sense of security while increasing your attack surface. If you ever decide to migrate from MD5 to bcrypt, you still have the old hashes sitting in your database until you delete them. Furthermore, having multiple columns per user increases query complexity without adding any cryptographic value.

If you are on an older PHP version where password_hash() is not available or you need to handle legacy data, you might consider using hash_pbkdf2(). This function allows you to define the salt, the number of iterations, and the length of the output. You can generate a constant-length hash by specifying the desired byte length in the third parameter. However, be aware that managing salts manually requires strict adherence to security best practices: ensure the salt is at least 16 bytes long and generated using random_bytes().

Implementation Example

Here is how you should structure your code to achieve a safe balance between speed and security. The "cost" parameter in bcrypt or PBKDF2 represents the number of iterations. You should not hardcode this number. Instead, read it from the existing hash string when verifying a password, and update it if the stored cost is too low for modern hardware standards.

// Storing a new password
$hash = password_hash($password, PASSWORD_DEFAULT);
// Store $hash in your database

// Verifying a password
if (password_verify($inputPassword, $storedHash)) {
    // Success - login successful
}

// Updating the cost factor if necessary
$cost = password_get_info($storedHash)['cost'];
if ($cost < 12) { // Adjust threshold based on your server capabilities
    $newHash = password_hash($inputPassword, PASSWORD_DEFAULT);
    // Update database with $newHash to increase the work factor
}

Why Not Double Hash?

You mentioned that hashing a million times might be safer. While PBKDF2 and bcrypt do internally perform thousands of iterations, manually wrapping a hash function like hash('sha256', hash('sha256', $password . $salt)) is less flexible. You cannot easily increase the difficulty later without updating your application code to add more layers. Modern algorithms allow you to incrementally raise the cost factor in the database row itself, allowing your defense to scale with computing power over years without requiring a database schema change or a massive application redeployment.

For robust PHP applications, relying on password_hash() is the standard. It abstracts away the complexity of salt generation and iteration counting while ensuring that your password storage remains resilient against brute-force and rainbow table attacks.

Our network