A cryptographic hash function is a mathematical algorithm that takes an input of any size (from a single character to a multi-gigabyte database backup) and maps it to a fixed-size string of bytes (the **hash digest**).
Hashing is a one-way operation. Unlike symmetric encoding (like Base64) or encryption, you cannot reverse-engineer the original input from a hash digest. Let's explore the math and safety margins of standard hashing algorithms.
1. What is a Cryptographic Hash?
For a mathematical function to be considered cryptographically secure, it must possess five core properties:
- Deterministic: The same input will always produce the exact same output digest.
- Quick Computation: It calculates the digest quickly for any given message.
- Pre-Image Resistance: Given a hash digest, it is computationally infeasible to find the original input.
- Small Changes Matter: A tiny modification to the input (like changing a single bit) changes the output so completely that the new digest appears unrelated to the old one (known as the **avalanche effect**).
- Collision Resistance: It is computationally infeasible to find two different inputs that produce the same output digest.
2. MD5 and SHA-1: Why They Are Broken
Historically, the MD5 (128-bit) and SHA-1 (160-bit) algorithms were the industry standard. However, mathematical breakthroughs exposed weaknesses in their collision resistance:
- MD5 Collisions: In 2004, researchers proved that MD5 could be collided in seconds. Attackers can generate two distinct files (e.g., a benign PDF contract and a malicious script) that produce the same MD5 hash, rendering it useless for file verification.
- SHA-1 SHAttered: In 2017, Google announced the first successful real-world collision attack against SHA-1 (known as SHAttered), generating two PDFs with different content but matching SHA-1 digests.
3. The Current Standard: SHA-256
Part of the SHA-2 family designed by the NSA, **SHA-256** produces a 256-bit (32-byte) digest, usually represented as a 64-character hexadecimal string.
SHA-256 remains secure and collision-free. There are no known practical attacks against it, making it the default choice for TLS certificates, file integrity checks, Git commit verification, and blockchain validation.
4. Hashing Passwords: Why SHA-256 is Not Enough
A common mistake is storing user passwords by running them directly through SHA-256. Because SHA-256 is designed to be fast, it is vulnerable to **brute-force attacks**. Using specialized hardware (GPUs and ASICs), an attacker can compute billions of SHA-256 hashes per second to guess user passwords from stolen database tables.
The Solution: Salts and Slow Hashing:
- Always append a cryptographically random string (a **Salt**) to the password before hashing to prevent dictionary and precomputed rainbow table attacks.
- Use slow, CPU/memory-hard hashing algorithms like **Argon2**, **bcrypt**, or **PBKDF2** for credentials. These algorithms run loops of computations to delay validation by a fraction of a second, making bulk brute-force attacks economically impossible.