Hash functions are everywhere in software development โ€” from password storage to file integrity verification. But not all hash algorithms are equal. Understanding the difference between SHA-256 and MD5 can protect your users from security vulnerabilities.

What is a Hash Function?

A hash function takes any input and produces a fixed-length output (the hash). The same input always produces the same hash, but you cannot reverse the hash to get the original input.

// JavaScript example
const crypto = require("crypto");

const md5 = crypto.createHash("md5").update("hello").digest("hex");
// โ†’ 5d41402abc4b2a76b9719d911017c592

const sha256 = crypto.createHash("sha256").update("hello").digest("hex");
// โ†’ 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824

MD5 โ€” Fast but Broken

SHA-256 โ€” The Modern Standard

๐Ÿ”’ Generate hashes instantly

Try our free Hash Generator โ€” supports MD5, SHA-1, SHA-256, SHA-512, SHA-384 and more with file hashing support.

Open Hash Generator โ†’

When to Use Each

Never use MD5 or SHA-1 for password hashing. Use bcrypt, Argon2, or PBKDF2 instead โ€” they are designed to be slow, which makes brute-force attacks impractical.

Use MD5 for:

Use SHA-256 for:

Kotlin Example โ€” File Hashing

import java.security.MessageDigest
import java.io.File

fun hashFile(file: File, algorithm: String = "SHA-256"): String {
    val digest = MessageDigest.getInstance(algorithm)
    file.forEachBlock { buffer, bytesRead ->
        digest.update(buffer, 0, bytesRead)
    }
    return digest.digest().joinToString("") { "%02x".format(it) }
}

// Usage
val hash = hashFile(File("document.pdf"), "SHA-256")
println("SHA-256: $hash")