Open to everyone Reading, lesson 46 of 100, 2 min read
Hashing
A fingerprint for data
A hash function takes any input (a word, a file, a whole disk) and produces a short, fixed-length value called a hash or digest.
SHA-256("hello") = 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
SHA-256("hello.") = (a completely different value)A good cryptographic hash has these properties:
- Deterministic: the same input always gives the same hash.
- One-way: you cannot work back from the hash to the input.
- Collision resistant: it is practically impossible to find two inputs with the same hash.
- Avalanche effect: a tiny change in the input changes the hash completely.
Uses
- Checking downloads: a website publishes the SHA-256 of an installer; you compute it on your copy and compare. A difference means the file was corrupted or tampered with.
- Detecting changes: security tools hash important system files and alert when a hash changes.
- Storing passwords (below).
- Digital signatures sign the hash of a document rather than the whole document.
Algorithms
| Algorithm | Status |
|---|---|
| MD5 | Broken for security; collisions are easy. Only for non-security checks |
| SHA-1 | Broken; collisions demonstrated. Avoid |
| SHA-256, SHA-3 | Current, recommended |
Storing passwords properly
A service should never store your actual password. It stores a hash, and when you sign in it hashes what you type and compares. But plain fast hashes are not enough: attackers who steal the database can try billions of guesses per second.
Good password storage uses:
- a salt: a random value added to each password before hashing, so identical passwords get different hashes and pre-computed tables are useless;
- a slow, memory-hard password hashing function such as Argon2, bcrypt or scrypt, which makes each guess expensive.
Tip
If a website can email you your current password, it is not storing it safely. Treat that site as a risk and never reuse that password.