DevLab

Hash Functions Explained: MD5, SHA-256, and When to Use Each

Learn what hash functions do, why cryptographic hashes are one-way, and which algorithm to use for passwords, checksums, and data integrity.

Crypto

What is a Hash Function?

A hash function takes any input and produces a fixed-size output called a digest. The same input always produces the same output, but you cannot reverse the process. This one-way property, combined with the avalanche effect (a tiny input change produces a completely different hash), makes hash functions the backbone of passwords, checksums, and digital signatures.

The Three Core Properties

  • Deterministic: SHA-256("hello") always equals the same 64-character hex string
  • Avalanche effect: SHA-256("hello") and SHA-256("Hello") share zero recognizable bits
  • Collision resistant: Computationally infeasible to find two different inputs producing the same hash

MD5 — Fast But Broken for Security

MD5 produces a 128-bit (32 hex character) hash. Researchers demonstrated practical collision attacks — two different files with the same MD5 hash. You can forge a file with the same MD5 as a trusted one.

Acceptable: Non-security checksums (verifying a download was not corrupted), cache keys, database partition keys.

Never use for: Passwords, digital signatures, or any security-critical verification.

SHA-256 — The Current Standard

SHA-256 produces a 256-bit (64 hex character) hash. No practical collision attacks exist. Used in TLS certificates, Bitcoin mining, Git commit hashes, and HMAC API signatures. SHA-256 is fast — good for checksums, bad for passwords (a GPU can compute billions per second).

For Passwords: Use Slow Hash Functions

Password hashing needs a deliberately slow algorithm that GPUs cannot parallelize efficiently:

  • bcrypt — most widely supported; tunable cost factor
  • scrypt — memory-hard; harder to attack with specialized hardware
  • Argon2 — winner of the Password Hashing Competition; the modern best choice

Never use MD5, SHA-1, or SHA-256 directly for passwords, even with a salt.

Practical Uses

  • File integrity: SHA-256 checksums confirm a file was not corrupted or tampered with
  • API signatures: HMAC-SHA256 signs API requests to prevent tampering in transit
  • Content addressing: Git uses SHA-1 (migrating to SHA-256) to address every file and commit
  • Deduplication: Storage systems hash files to find duplicates without comparing byte-by-byte

Frequently Asked Questions

What is a hash function?

A hash function takes an input of any size and produces a fixed-length output (the hash or digest). The same input always produces the same hash, but even a tiny change in the input produces a completely different output. Hash functions are one-way — you cannot reverse a hash back to its original input.

What is the difference between MD5 and SHA-256?

MD5 produces a 128-bit hash and is considered cryptographically broken — collisions can be generated in seconds. SHA-256 produces a 256-bit hash and remains secure for cryptographic use. Use SHA-256 or SHA-3 for anything security-related. MD5 is only acceptable for non-security checksums.

Can two different inputs produce the same hash?

Yes, this is called a collision. All hash functions can theoretically collide because they map infinite inputs to a finite output space. Secure hash functions like SHA-256 make collisions computationally infeasible to find deliberately, while broken ones like MD5 allow collisions to be generated quickly.

Should I use hashing or encryption for passwords?

Use hashing, not encryption. Encryption is reversible (anyone with the key can decrypt), but hashing is one-way. For passwords specifically, use a dedicated password hashing algorithm like bcrypt, scrypt, or Argon2 — never raw SHA-256 or MD5, because they are too fast and vulnerable to brute-force attacks.

Practice with these tools

More Learning Topics

Free · no newsletter yet

Want a note if we start a dev tools newsletter?

We don’t send a newsletter yet. Leave your email and we’ll write only if we start one: new tools on DevLab.

Stored with our email provider, Resend; never sold or shared. To be removed, write to us from the contact page.