On crackstation.net it is stated:
To Validate a Password
- Retrieve the user's salt and hash from the database.
- Prepend the salt to the given password and hash it using the same hash function.
- Compare the hash of the given password with the hash from the database. If they match, the password is correct. Otherwise, the
password is incorrect.
However in the source code listed at the bottom of the page, I can't figure out how the validate_password
function takes into account the salt. I mean where is the salt prepended to the given password?
Here is the function in question:
function validate_password($password, $correct_hash)
{
$params = explode(":", $correct_hash);
if(count($params) < HASH_SECTIONS)
return false;
$pbkdf2 = base64_decode($params[HASH_PBKDF2_INDEX]);
return slow_equals(
$pbkdf2,
pbkdf2(
$params[HASH_ALGORITHM_INDEX],
$password,
$params[HASH_SALT_INDEX],
(int)$params[HASH_ITERATION_INDEX],
strlen($pbkdf2),
true
)
);
}
Looks like the Salt, Hash and interation number are stored in the same string and are separated into three strings (in an array) at the beginning of the function:
The order of the values depends on how the constants HASH_ALGORITHM_INDEX, HASH_SALT_INDEX and HASH_ITERATION_INDEX are defined.