Why is it important to use a salted hash for storing passwords instead of encryption methods?
Using a salted hash for storing passwords is important because it adds an extra layer of security by generating a unique random value (salt) for each password before hashing it. This makes it more difficult for attackers to use precomputed rainbow tables or dictionary attacks to crack passwords. Encryption methods, on the other hand, can be decrypted if the key is compromised, whereas hash functions are designed to be one-way and irreversible.
// Generate a random salt
$salt = bin2hex(random_bytes(16));
// Hash the password with the salt
$hashed_password = hash('sha256', $password . $salt);