How can the hash_algos() function be used to determine the most suitable hashing algorithm for password storage in PHP?
When storing passwords in PHP, it is important to use a secure hashing algorithm to protect user data. The `hash_algos()` function in PHP can be used to retrieve a list of available hashing algorithms supported by the system. By iterating through this list and selecting a strong algorithm such as `password_hash()` with PASSWORD_DEFAULT, you can ensure that passwords are securely stored.
$available_algorithms = hash_algos();
$preferred_algorithm = '';
foreach ($available_algorithms as $algorithm) {
if ($algorithm == 'bcrypt') {
$preferred_algorithm = $algorithm;
break;
}
}
if (empty($preferred_algorithm)) {
$preferred_algorithm = PASSWORD_DEFAULT;
}
// Use $preferred_algorithm for password hashing