The Trap of Manual Hashing in Password Verification
The code snippet you are looking at illustrates a very common pattern that leads to authentication failures: attempting to nest cryptographic functions where they don't belong. Specifically, wrapping password_hash() around hash('sha512', $password) creates a double-hash scenario that breaks the verification logic entirely. This isn't just a matter of incorrect syntax; it is a fundamental misunderstanding of what modern password hashing libraries are designed to do versus what legacy SHA functions were built for.
When you call hash('sha512', $password), you are generating a raw 40-character hexadecimal string based strictly on the input bytes. This function has no concept of salting, cost factors, or memory hardness. It is deterministic and fast. If an attacker ever obtains your database dump, they could compute this exact same value for a rainbow table attack in milliseconds.
The password_hash() function, however, expects its first argument to be the raw password string, not another hash. When you pass the SHA-512 output into password_hash(), it treats that 40-character hex string as your "secret." It then wraps that string in a legacy format (specifically the $2y$ algorithm if using PASSWORD_DEFAULT on older PHP versions, or $2b$/ on modern ones) and appends a random salt and cost factor.
The critical failure occurs during verification. Your database stores a value like $2y$10$abc123.... When the user logs in, you take their plaintext password, run it through hash('sha512', ...) to get a 40-character hex string, and then try to verify that hex string against the stored $2y$ hash.
The password_verify() function is smart enough to detect that its first argument does not look like a valid password (it's too short) or, in some implementations, it simply hashes the input again using the algorithm defined by the stored hash. Since you are passing a SHA-512 hex string (which contains characters like a, b, c but lacks the structure of a raw plaintext password), the internal hashing logic produces a result that will never match the stored $2y$ prefix. The check returns false not because the user entered the wrong password, but because you handed the verification function a malformed input.
How to Fix the Logic
To fix this, you must strip away the manual hash('sha512', ...) calls entirely. The password_hash() and password_verify() functions handle SHA-512 (specifically via the bcrypt or Argon2id algorithms) internally. They generate their own salt, manage the cost factor, and ensure compatibility across different PHP versions.
Here is the corrected approach for registering a user:
$hashed = password_hash($password, PASSWORD_DEFAULT);
// Store $hashed directly in the database
And for the login verification:
public function isValidLogin($username, $password) {
// Note: In production, use prepared statements to prevent SQL injection
// Do not use real_escape_string on raw user input without parameter binding
$stmt = $this->connect()->prepare("SELECT password FROM users WHERE name=?");
$stmt->execute([$username]);
if ($result = $stmt->get_result()) {
while($row = $result->fetch_assoc()) {
// Pass the raw plaintext password directly to password_verify
if (password_verify($password, $row['password'])) {
return true;
}
}
}
return false;
}
Why We Moved Away from SHA-512
You might wonder why we aren't just using hash('sha512', $password) directly for storage. The original reason for using SHA-512 in the past was speed and simplicity. However, modern attackers use GPUs and ASICs that can compute billions of hashes per second. A fast hash function like SHA-512 is computationally cheap for attackers.
The password_hash() library solves this by introducing a "cost factor." For bcrypt (the default on many systems), you specify how many rounds to perform. This makes the hashing process slow and memory-intensive for legitimate logins, but equally expensive for attackers trying to brute-force your database. It turns password verification into a deliberate bottleneck that slows down attacks without hindering user experience significantly.
If you need to support legacy data where users were previously hashed with raw SHA-512 or other algorithms like MD5, password_hash() can actually upgrade them. If you provide an existing hash string to password_verify(), it checks compatibility. To migrate old users, you simply run password_hash($existing_hash, PASSWORD_DEFAULT) to re-hash the old value into a modern format with a new salt.
Security Considerations
Beyond fixing the code logic, there is a critical security flaw in your database query structure: using real_escape_string on variables before inserting them into a string-based SQL query is a fragile defense against SQL injection. It does not handle all edge cases as robustly as prepared statements with bound parameters. Always prefer parameterized queries for user input like usernames and passwords.
For high-security applications, consider the PASSWORD_ARGON2ID constant if your PHP version supports it (7.4+). Argon2id is specifically designed to be resistant to side-channel attacks and GPU-based cracking, offering better protection than bcrypt or the older SHA-512 approach.