I've a question regarding the suggestion on the 2 top answers on this question.
On the last part of their answers, they're saying it's better to encrypt the password hash than using hardcoded pepper for maintainability (in case your hashes leak, you can rotate the keys).
Is this the pseudo-code of what they're saying?
$key = 'random_key_stored_elsewhere';
$hash = bcrypt($password);
$encrypted = encrypt($hash, $key);
// store $encrypted to DB
Now to check a login attempt:
if (bcrypt($user_input) == decrypt($encrypted, $key))
{
// proceed login...
}
Say hash leaked, now we need to change the key and re-encrypt the hashes:
$decrypted_data = decrypt($encrypted, $key)
$new_key = 'new_random_key_stored_elsewhere';
$encrypted = encrypt($decrypted_data, $new_key);
// store $encrypted to DB
Is that it? If yes, then how can rotating the keys in case of a hash leak invalidate the would-be cracked passwords if the same procedure is used for checking login attempts? E.g.,
if (bcrypt($user_input) == decrypt($encrypted, $new_key))
{
// proceed login...
}
Rotating the keys would've been useless right or am I missing something?