What is a hash function?
Short answer
A hash function takes any input and produces a fixed-length string, its hash or digest. The same input always gives the same hash, but you cannot reverse the hash back into the original. It is a fingerprint, not encryption.
A fixed-length fingerprint
No matter how large the input, a hash function returns a short value of fixed length. Change a single character of the input and the output changes completely, which makes hashes ideal for detecting whether data has changed.
"hello" -> 2cf24dba5fb0a30e26e83b2ac5b9e29e...
"hellp" -> 9c2 d4c0 ... (completely different)What makes a hash function good
- Deterministic: the same input always produces the same hash
- Fast to compute for any input
- One-way: you cannot recover the input from the hash
- Collision-resistant: it is impractical to find two inputs with the same hash
Where hashes are used
- Verifying that a downloaded file was not corrupted or tampered with
- Storing passwords as salted hashes instead of plain text
- Deduplicating data by comparing fingerprints
- Building digital signatures and content addresses