What is a hash function (hashing)?
A hash function is a mathematical algorithm that takes data of variable size (a short password, or an entire PDF file) and turns it into a fixed-size string of characters (the fingerprint). This is called a Hash.
The main characteristic of a hash is that it is one-way : it is mathematically impossible to recover the original text from the hash (unlike encryption or Base64 encoding). Moreover, the slightest change to the original text changes the hash completely.
Encryption vs. hashing
The encryption is designed to be reversible (you lock the door with a key, and you can unlock it with that same key). hashing is designed to verify integrity (you check that the password entered produces the same hash as the one stored in the database, without ever knowing the actual password).
Which algorithm should you choose today?
- MD5 and SHA-1: Do not use them EVER AGAIN to store passwords. With today's computing power, they are vulnerable to collision attacks. They are now only useful for verifying the integrity of a downloaded file.
- SHA-256 / SHA-512 : This is the current standard (SHA-2 family). These algorithms are used by the HTTPS protocol, the Bitcoin blockchain, and to secure government data.
- Note: to store passwords in a database, use algorithms that are deliberately slow by design, such as Bcrypt or Argon2.