Hash functions are fundamental to web security. Understanding their strengths and weaknesses is crucial for every developer.
What is a Hash Function?
A hash function takes input data of any size and produces a fixed-size output (hash). Key properties include:
- Deterministic - Same input always produces same output
- One-way - Cannot reverse the hash to get original data
- Collision-resistant - Hard to find two inputs with same hash
Common Hash Algorithms
MD5 (128-bit)
Fast but cryptographically broken. Only use for checksums, never for security.
SHA-1 (160-bit)
Deprecated for security use. Collisions have been demonstrated.
SHA-256 (256-bit)
Currently secure and widely recommended. Part of the SHA-2 family.
Security Tip
For password hashing, use specialized algorithms like bcrypt, scrypt, or Argon2 instead of plain SHA-256.
Use Cases
- File integrity - Verify downloads haven't been tampered with
- Password storage - Store hashes instead of plain passwords
- Digital signatures - Verify document authenticity
- Caching - Generate cache keys from content