Password hashing with password_hash in PHP - Time & Space Complexity
When we use password_hash in PHP, it takes some time to turn a password into a secure hash.
We want to understand how the time it takes changes as the password or settings change.
Analyze the time complexity of the following code snippet.
$password = 'mysecretpassword';
$hash = password_hash($password, PASSWORD_DEFAULT);
echo $hash;
This code creates a secure hash from a password using PHP's built-in function.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The hashing algorithm runs multiple internal rounds to secure the password.
- How many times: The number of rounds depends on the algorithm and its cost setting, repeating the hashing steps many times.
The time to hash grows mainly with the number of rounds, not the password length.
| Input Size (Rounds) | Approx. Operations |
|---|---|
| 10 | 10 hashing steps |
| 20 | 20 hashing steps |
| 50 | 50 hashing steps |
Pattern observation: Doubling the rounds roughly doubles the time, while password length has little effect.
Time Complexity: O(r)
This means the time grows linearly with the number of hashing rounds set in the algorithm.
[X] Wrong: "Hashing time depends mostly on the password length."
[OK] Correct: The hashing time depends mainly on the number of rounds, not the password length, which is usually short and fixed.
Understanding how hashing time grows helps you explain security trade-offs clearly and shows you know how to balance safety and performance.
"What if we increased the cost parameter in password_hash? How would the time complexity change?"
Practice
password_hash function in PHP?Solution
Step 1: Understand what
This function takes a plain password and creates a secure hashed version that cannot be reversed.password_hashdoesStep 2: Compare options with function purpose
To securely convert a plain password into a hashed string correctly describes hashing. To verify if a password matches a hash is forpassword_verify. Options A and D are incorrect because passwords should never be stored in plain text or decrypted.Final Answer:
To securely convert a plain password into a hashed string -> Option DQuick Check:
password_hash = create secure hash [OK]
- Confusing hashing with verifying passwords
- Thinking passwords can be decrypted
- Storing passwords as plain text
password_hash with the default algorithm?Solution
Step 1: Recall the correct function signature
Thepassword_hashfunction requires the password string and the algorithm constant, e.g.,PASSWORD_DEFAULT.Step 2: Check each option's syntax
$hash = password_hash($password, PASSWORD_DEFAULT); correctly passes the password variable and the constant. $hash = password_hash($password, 'default'); uses a string instead of constant. $hash = password_hash($password, default); uses undefined constant "default". $hash = password_hash(PASSWORD_DEFAULT, $password); swaps arguments incorrectly.Final Answer:
$hash = password_hash($password, PASSWORD_DEFAULT); -> Option AQuick Check:
password_hash(password, algorithm) correct syntax [OK]
- Using string 'default' instead of PASSWORD_DEFAULT
- Omitting the algorithm parameter
- Swapping argument order
$password = 'mypassword';
$hash = password_hash($password, PASSWORD_DEFAULT);
echo password_verify('mypassword', $hash) ? 'Valid' : 'Invalid';Solution
Step 1: Understand password_hash and password_verify usage
The code hashes 'mypassword' and then verifies the same password against the hash.Step 2: Evaluate the verification result
Since the password matches the hash,password_verifyreturns true, so 'Valid' is printed.Final Answer:
Valid -> Option BQuick Check:
password_verify correct password = true [OK]
- Expecting hash to be plain text
- Confusing verify with hash
- Assuming password_verify returns the hash
$password = 'secret';
$hash = password_hash($password);
if (password_verify($hash, $password)) {
echo 'Access granted';
} else {
echo 'Access denied';
}Solution
Step 1: Check password_verify function call
Thepassword_verifyfunction takes the plain password as first argument and the hash as second. In the code, the arguments are reversed: hash first, then password.Step 2: Verify other parts of the code
Thepassword_hashcall is correct (uses PASSWORD_DEFAULT by default), $password is defined, echo statements are fine.Final Answer:
password_verify arguments are reversed -> Option CQuick Check:
password_verify(plain_password, hash) [OK]
- Forgetting the algorithm argument
- Swapping arguments in password_verify
- Assuming password_hash works with one argument
password_hash and password_verify?Solution
Step 1: Check correct usage of password_hash and password_verify
$hash = password_hash($userPassword, PASSWORD_DEFAULT); // Store $hash in database // Later during login if (password_verify($inputPassword, $hash)) { echo 'Login successful'; } else { echo 'Login failed'; }hashes the password withPASSWORD_DEFAULTand stores the hash. Later, it verifies the input password against the stored hash correctly.Step 2: Identify errors in other options
$hash = password_hash($userPassword); // Store $hash if (password_verify($hash, $inputPassword)) { echo 'Login successful'; } else { echo 'Login failed'; }misses the algorithm and reverses arguments inpassword_verify.$hash = md5($userPassword); // Store $hash if (password_verify($inputPassword, $hash)) { echo 'Login successful'; } else { echo 'Login failed'; }uses insecuremd5instead ofpassword_hash.$hash = password_hash($userPassword, PASSWORD_DEFAULT); // Store $userPassword if (password_verify($inputPassword, $userPassword)) { echo 'Login successful'; } else { echo 'Login failed'; }stores plain password and verifies incorrectly.Final Answer:
Option A correctly hashes and verifies passwords securely -> Option AQuick Check:
Hash with password_hash, verify with password_verify [OK]
- Using md5 instead of password_hash
- Swapping arguments in password_verify
- Storing plain passwords instead of hashes
Ready to prove it?
Pass the test and this pattern moves unknown
