SHA1 vs md5 vs SHA256: which to use for a PHP login?

SHA1 vs md5 vs SHA256: which to use for a PHP login?

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

Stefan Bogdanescu

Founder & Senior Architect

SHA1 vs MD5 vs SHA256: Which to Use for a PHP Login?

Choosing between MD5, SHA-1, and SHA-256 for password storage is a classic question that often leads developers down a rabbit hole of cryptographic history. The short answer is that none of these algorithms are suitable for securing passwords in their raw form today. You might see them discussed in older tutorials or legacy codebases, but relying on them directly exposes your application to rainbow table attacks and brute-force vulnerabilities.

If you are building a new login system in PHP, the immediate decision isn't which hash function to pick between those three; it is realizing that you need an algorithm specifically designed for password hashing. MD5 and SHA-1 were designed for speed and data integrity checks, not security against deliberate attacks. They are too fast. Attackers can calculate billions of hashes per second on modern hardware, making it trivial to crack a database containing millions of weakly hashed passwords.

Why Simple Salting Isn't Enough

Your source material shows an attempt to add a salt using md5(uniqid(rand(), true)) and then hashing the concatenation of the salt and password with SHA-1. While adding a random string before hashing is conceptually correct, the implementation has critical flaws.

First, the salt length is insufficient. A 3-character string (derived from a 16-character hex MD5 hash) only offers $4^3 = 64$ possible combinations if using digits, or slightly more with letters, but it falls drastically short of security requirements. You need a salt that is at least 128 bits (16 bytes) long to prevent attackers from precomputing hashes for every possible salt value.

Second, the method of generating the salt is weak. Using md5 and uniqid does not produce cryptographically secure random numbers. An attacker could potentially predict the sequence or find patterns. For password hashing, you must use a CSPRNG (Cryptographically Secure Pseudo-Random Number Generator). In PHP, this means using random_bytes() to generate high-entropy data.

The Modern Standard: Argon2 and bcrypt

The industry standard has moved far beyond MD5, SHA-1, or even SHA-256 for passwords. Algorithms like PBKDF2, bcrypt, scrypt, and Argon2 are the correct choices. These are "key derivation functions" (KDFs). They are intentionally slow and computationally expensive to make brute-force attacks prohibitively costly.

If you are using Laravel, the framework handles this complexity for you via the Hash facade, which defaults to bcrypt or supports Argon2id. If you are writing raw PHP, consider using password_hash(). This function abstracts away the need to choose between MD5 and SHA-256 because it implements the Password Hashing Portability API (PHPASS). It automatically selects a strong algorithm (currently bcrypt) and generates a robust salt for you.

// Recommended approach for raw PHP
$hash = password_hash($password, PASSWORD_DEFAULT);

// Verification
if (password_verify($input_password, $stored_hash)) {
    // Login successful
}

Integrating with Laravel

If your stack includes Laravel, the migration from legacy hashing is even simpler. The framework encourages using the Hash facade which wraps the underlying PHP password hashing functions. This ensures you are always using a strong algorithm like Argon2id if that is the latest stable version available in your environment.

For more details on securing your application within the Laravel ecosystem, you can refer to resources from https://laravelcompany.com regarding their security best practices and official documentation updates.

Database Storage Considerations

Regarding your question about storing hashes in MySQL: yes, storing a hash is secure, provided the hash itself is strong and unique. However, the database column type matters less than the algorithm used to generate the string stored there. You should store the hash as a VARCHAR or TEXT field (depending on length requirements) with no specific numeric constraints.

Do not attempt to implement your own salt logic using MD5 or SHA-256. The combination of a weak PRNG, insufficient salt length, and fast hash algorithms creates a perfect storm for attackers. By switching to password_hash() or Laravel's built-in hashing, you ensure that the cost factor (time per iteration) is adjustable via configuration, allowing you to tune the security level as hardware speeds increase over time.

In summary, stop looking at MD5 and SHA-1. They are deprecated for this purpose. Use Argon2id if available, or bcrypt. Let the framework handle the salt generation and algorithm selection to ensure your login system is robust against modern attack vectors.

Our network