What is the purpose of the password_hash function in PHP?
The password_hash function creates a secure hash of a password. It helps protect passwords by converting them into a form that is hard to reverse or guess.
Click to reveal answer
beginner
Which algorithm does password_hash use by default?
By default, password_hash uses the BCRYPT algorithm, which is strong and includes a salt automatically.
Click to reveal answer
intermediate
Why is it important that password_hash automatically adds a salt?
Adding a salt means a unique random value is mixed into the password before hashing. This stops attackers from using pre-made tables to guess passwords and makes each hash unique.
Click to reveal answer
beginner
How do you verify a password against a hash created by password_hash?
Use the password_verify function. It takes the plain password and the stored hash, then checks if they match securely.
Click to reveal answer
intermediate
What is the benefit of using password_hash over simple hashing functions like md5 or sha1?
Simple hashes like md5 or sha1 are fast and lack salts, making them vulnerable to attacks. password_hash is slower and adds salts, making password storage much safer.
Click to reveal answer
What does the password_hash function return?
AA boolean true or false
BThe original password
CA hashed string of the password
DAn encrypted password file
✗ Incorrect
password_hash returns a hashed string that represents the password securely.
Which PHP function should you use to check if a password matches a hash?
Apassword_verify()
Bpassword_check()
Chash_equals()
Dpassword_compare()
✗ Incorrect
password_verify() is the correct function to verify a password against a hash.
Why is it unsafe to use md5 for password hashing?
AIt is too slow
BIt automatically salts passwords
CIt uses too much memory
DIt is fast and unsalted, making it vulnerable to attacks
✗ Incorrect
md5 is fast and does not add a salt, so attackers can guess passwords easily.
What does the salt in password hashing do?
AMakes the password longer
BAdds a unique random value to the password before hashing
CEncrypts the password
DRemoves special characters from the password
✗ Incorrect
Salt adds a unique random value to each password before hashing to make hashes unique.
Which algorithm is used by default in password_hash?
ABCRYPT
BMD5
CSHA256
DAES
✗ Incorrect
The default algorithm for password_hash is BCRYPT.
Explain how password_hash helps keep passwords safe.
Think about what makes a password hard to guess or reverse.
You got /4 concepts.
Describe the steps to securely check a user's password using PHP functions.
Consider how you store and then check the password.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the password_hash function in PHP?
easy
A. To store passwords in plain text
B. To verify if a password matches a hash
C. To decrypt a hashed password back to plain text
D. To securely convert a plain password into a hashed string
Solution
Step 1: Understand what password_hash does
This function takes a plain password and creates a secure hashed version that cannot be reversed.
Step 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 for password_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 D
Quick Check:
password_hash = create secure hash [OK]
Hint: Hash means secure one-way password conversion [OK]
Common Mistakes:
Confusing hashing with verifying passwords
Thinking passwords can be decrypted
Storing passwords as plain text
2. Which of the following is the correct syntax to hash a password using password_hash with the default algorithm?
easy
A. $hash = password_hash($password, PASSWORD_DEFAULT);
B. $hash = password_hash($password, 'default');
C. $hash = password_hash($password, default);
D. $hash = password_hash(PASSWORD_DEFAULT, $password);
Solution
Step 1: Recall the correct function signature
The password_hash function 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 A
The password_verify function 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
The password_hash call is correct (uses PASSWORD_DEFAULT by default), $password is defined, echo statements are fine.
Final Answer:
password_verify arguments are reversed -> Option C
Quick Check:
password_verify(plain_password, hash) [OK]
Hint: password_verify(plain, hash) order [OK]
Common Mistakes:
Forgetting the algorithm argument
Swapping arguments in password_verify
Assuming password_hash works with one argument
5. You want to store user passwords securely and later verify them during login. Which of the following code snippets correctly implements this using password_hash and password_verify?
hard
A.
$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';
}
B.
$hash = password_hash($userPassword);
// Store $hash
if (password_verify($hash, $inputPassword)) {
echo 'Login successful';
} else {
echo 'Login failed';
}
C.
$hash = md5($userPassword);
// Store $hash
if (password_verify($inputPassword, $hash)) {
echo 'Login successful';
} else {
echo 'Login failed';
}