Reference By ToolsHub

Cryptographic Hash Functions: MD5, SHA-256, and Beyond

Hash functions are the unsung heroes of modern computing. They secure your passwords, verify software downloads, power blockchain networks, and protect digital signatures. Yet many developers use them without fully understanding what makes a good hash function — or why some older ones should be retired. Let’s change that.

What Is a Hash Function?

A hash function takes an input of any size and produces a fixed-size output (called a digest, hash, or fingerprint). A cryptographic hash function adds security properties that make it suitable for use in security-critical applications.

Input: "Hello"
SHA-256: 185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969

Input: "Hello." (one character added)
SHA-256: 2d8bd7d9bb5f85ba643f0110d50cb506a1fe439e769a22503c0e90f1f6e3b

→ Completely different output from a tiny change

Essential Properties of Cryptographic Hash Functions

PropertyDefinitionWhy It Matters
DeterministicSame input always produces same outputEnables verification and comparison
One-way (Pre-image resistance)Cannot reverse the hash to find the inputProtects passwords and secrets
Avalanche effectTiny input change flips ~50% of output bitsPrevents pattern analysis
Collision resistanceInfeasible to find two inputs with same hashGuarantees data integrity
Fixed output sizeOutput length is constant regardless of input sizePredictable storage and comparison

Comparing Hash Algorithms

AlgorithmOutput SizeSpeedSecurity StatusUse Today?
MD5128 bits (32 hex)Very fastBroken (collisions found)No — checksums only
SHA-1160 bits (40 hex)FastBroken (2017 collision)No — being phased out
SHA-256256 bits (64 hex)ModerateSecureYes — recommended
SHA-512512 bits (128 hex)Moderate (faster on 64-bit)SecureYes — when extra security needed
SHA-3Variable (224-512)ModerateSecure (different design)Yes — alternative to SHA-2
BLAKE3256 bits (default)Very fastSecureYes — high performance

How Hash Functions Work (Simplified)

While each algorithm differs in implementation, most follow these general steps:

  1. Padding: The input message is padded to a specific block size (e.g., 512 bits for SHA-256)
  2. Parsing: The padded message is divided into fixed-size blocks
  3. Initialization: Internal state variables are set to predefined constants
  4. Compression: Each block is processed through rounds of bitwise operations (AND, OR, XOR, rotations, additions)
  5. Finalization: The final internal state is output as the hash digest

SHA-256 specifically uses 64 rounds of compression per block with a message schedule that expands 16 input words into 64 working words.

Real-World Use Cases

Password Storage

Never store passwords in plaintext. Hash them before storage so a database breach doesn’t expose credentials. However, general-purpose hashes (SHA-256) are too fast for passwords — use specialized algorithms:

  • bcrypt: Adaptive work factor, industry standard
  • Argon2: Winner of the Password Hashing Competition (2015), memory-hard
  • scrypt: Memory-hard, resistant to hardware attacks

File Integrity Verification

Software downloads provide SHA-256 checksums so you can verify the file wasn’t tampered with:

# Linux/Mac: verify a downloaded file
sha256sum ubuntu-24.04-desktop-amd64.iso
# Compare output with the published hash

Digital Signatures

Rather than signing an entire document (slow), the document is hashed and the hash is signed. The recipient hashes the document again and verifies the signature against that hash.

Data Deduplication

Cloud storage services hash file contents to detect duplicates. If two users upload the same file, only one copy needs to be stored.

Blockchain and Proof of Work

Bitcoin uses double SHA-256 hashing. Miners must find an input that produces a hash below a target threshold — computationally expensive but trivial to verify.

Understanding Hash Collisions

A collision occurs when two different inputs produce the same hash output. For a 128-bit hash like MD5, the birthday paradox means a collision can be found in roughly 2^64 operations — well within reach of modern hardware.

Why MD5 Is Broken

In 2004, researchers demonstrated practical MD5 collisions. By 2012, the Flame malware exploited an MD5 collision to forge a Microsoft code-signing certificate. MD5 should never be used for security purposes.

Why SHA-1 Is Broken

In 2017, Google’s SHAttered project produced the first practical SHA-1 collision using 6,500 years of CPU time and 110 years of GPU time (feasible for well-funded attackers). Major browsers and certificate authorities have since deprecated SHA-1.

Choosing the Right Hash Function

Use CaseRecommended AlgorithmReason
Password hashingArgon2 / bcryptIntentionally slow, memory-hard
File integritySHA-256 / BLAKE3Fast, collision-resistant
Digital signaturesSHA-256 / SHA-512Industry standard, well-audited
Non-security checksumsCRC32 / xxHashExtremely fast, not cryptographic
BlockchainSHA-256 / KeccakProven security margin

Hash Functions in JavaScript

// Using the Web Crypto API (browser & Node.js)
async function sha256(message) {
  const encoder = new TextEncoder();
  const data = encoder.encode(message);
  const hashBuffer = await crypto.subtle.digest("SHA-256", data);
  const hashArray = Array.from(new Uint8Array(hashBuffer));
  return hashArray.map(b => b.toString(16).padStart(2, "0")).join("");
}

// Usage
const hash = await sha256("Hello, World!");
// "dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f"

Try it yourself: Generate MD5, SHA-1, SHA-256, and SHA-512 hashes instantly with our free Hash Generator — compare algorithms side-by-side, all processing happens locally in your browser.