Cryptography & Security 8 min read July 7, 2026

Understanding Cryptographic Hash Functions: MD5, SHA-1, and SHA-256

A comprehensive developer guide to cryptographic hashing. Explore core hash attributes, look at why MD5 and SHA-1 are compromised, and learn secure practices for storing user passwords using SHA-256 and salt.

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:

  1. Deterministic: The same input will always produce the exact same output digest.
  2. Quick Computation: It calculates the digest quickly for any given message.
  3. Pre-Image Resistance: Given a hash digest, it is computationally infeasible to find the original input.
  4. 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**).
  5. 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.
Warning: Never use MD5 or SHA-1 for digital signatures, file checksums, or security validation. Use them only when integrating with legacy APIs or hashing non-sensitive keys (like generating cache lookup tags).

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:

  1. Always append a cryptographically random string (a **Salt**) to the password before hashing to prevent dictionary and precomputed rainbow table attacks.
  2. 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.

Advertisement