X Xerobit

Hash Functions Comparison — MD5, SHA-1, SHA-256, bcrypt, Argon2

Hash functions have different speed, output size, and security properties. MD5 and SHA-1 are broken for security. SHA-256 works for data integrity. bcrypt and Argon2 are for...

Mian Ali Khalid · · 6 min read
Use the tool
Hash Generator
Generate MD5, SHA-1, SHA-256, and SHA-512 hashes client-side.
Open Hash Generator →

Hash functions take input data and produce a fixed-size output (the hash or digest). Different hash functions are designed for different purposes: fast general-purpose hashing (SHA-256), file integrity verification, or deliberately slow password hashing (bcrypt, Argon2). Choosing the wrong one for security purposes is a critical vulnerability.

Use the Hash Generator to generate SHA-256, MD5, and other hash values.

Hash function properties

A cryptographic hash function must be:

  1. Deterministic: Same input always produces the same output
  2. One-way: Infeasible to reverse (find input from output)
  3. Collision-resistant: Infeasible to find two inputs with the same hash
  4. Avalanche effect: Small input change → completely different output
SHA-256("hello")  = 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
SHA-256("hello!") = c0535e4be2b79ffd93291305436bf889314e4a3faec05ecffcbb7df31ad9e51a

Comparison table

HashOutput (bits)SpeedSecurityUse case
MD5128Very fastBrokenLegacy checksums only
SHA-1160FastBrokenLegacy only
SHA-256256FastSecureData integrity, HMAC
SHA-512512Fast (64-bit)SecureHigh-security contexts
SHA-3/256256ModerateSecurePost-SHA-2 alternative
bcrypt184Slow (intentional)SecurePasswords
Argon2idVariableSlow (intentional)SecurePasswords (recommended)
BLAKE3256Very fastSecureGeneral purpose (modern)

MD5 and SHA-1: broken for security

Both MD5 and SHA-1 have known collision attacks — it’s possible to find two different inputs that produce the same hash:

# Collision example (simplified): same MD5 for different files
File A (malware): MD5 = a87ff679a2f3e71d9181a67b7542122c
File B (safe):    MD5 = a87ff679a2f3e71d9181a67b7542122c

Real-world attacks:

  • Flame malware (2012): Used MD5 collision to forge a Microsoft code signing certificate
  • SHAttered (2017): Google and CWI produced two PDF files with the same SHA-1 hash

Never use MD5 or SHA-1 for:

  • Digital signatures
  • Certificate fingerprints
  • Password storage
  • File integrity in security contexts

MD5 is still acceptable for:

  • Non-security checksums (detecting accidental file corruption)
  • Generating unique-ish identifiers from content
  • Legacy systems where migration isn’t yet possible

SHA-256 and SHA-2 family

SHA-256 is the current standard for most security applications. It’s part of the SHA-2 family:

  • SHA-224 (224-bit)
  • SHA-256 (256-bit) — most common
  • SHA-384 (384-bit)
  • SHA-512 (512-bit)
// Node.js SHA-256:
const crypto = require('crypto');
const hash = crypto.createHash('sha256').update('hello').digest('hex');
// "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"

// HMAC-SHA256 (keyed hash for authentication):
const hmac = crypto.createHmac('sha256', 'secret-key')
  .update('message')
  .digest('hex');
import hashlib, hmac

# SHA-256:
h = hashlib.sha256(b'hello').hexdigest()
# "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"

# HMAC-SHA256:
h = hmac.new(b'secret-key', b'message', hashlib.sha256).hexdigest()

SHA-512 vs SHA-256:

  • SHA-512 is faster than SHA-256 on 64-bit processors (fewer rounds relative to output size)
  • SHA-256 is faster on 32-bit and mobile processors
  • Both are secure — use SHA-256 unless you specifically need SHA-512

Password hashing: bcrypt

Passwords require slow hash functions. Fast hashes (SHA-256) let attackers test billions of passwords per second with GPUs.

bcrypt adds a cost factor that controls compute time:

// Node.js bcrypt:
const bcrypt = require('bcrypt');

// Hashing (cost factor 12 = ~300ms):
const hash = await bcrypt.hash('mypassword', 12);
// "$2b$12$LQv3c1yqBWVHxkd0LHAkCOYz6TtxMQJqhN8/LcdYmd..."

// Verifying:
const match = await bcrypt.compare('mypassword', hash);
// true

// Comparing timing (NOT bcrypt):
const wrongMatch = await bcrypt.compare('wrongpassword', hash);
// false

Cost factor guidelines:

  • 10: ~100ms (minimum for web)
  • 12: ~300ms (recommended for web)
  • 14: ~1200ms (use for high-security accounts)

Increase cost factor as hardware gets faster. The goal is making brute-force expensive even for attackers with dedicated hardware.

Argon2 won the 2015 Password Hashing Competition. It’s more configurable than bcrypt:

// Node.js argon2:
const argon2 = require('argon2');

// Hash:
const hash = await argon2.hash('mypassword', {
  type: argon2.argon2id,  // Argon2id is recommended (hybrid)
  memoryCost: 65536,      // 64MB memory usage
  timeCost: 3,            // 3 iterations
  parallelism: 4,         // 4 parallel threads
});

// Verify:
const match = await argon2.verify(hash, 'mypassword');
import argon2

ph = argon2.PasswordHasher(
    time_cost=3,
    memory_cost=65536,  # 64MB
    parallelism=4,
    hash_len=32,
    salt_len=16,
)

# Hash:
hash = ph.hash('mypassword')

# Verify:
try:
    ph.verify(hash, 'mypassword')
    print('Password correct')
except argon2.exceptions.VerifyMismatchError:
    print('Wrong password')

Argon2 vs bcrypt:

  • Argon2 uses configurable memory (makes GPU attacks expensive)
  • bcrypt is CPU-only (GPUs are less advantaged but still faster)
  • For new systems: prefer Argon2id
  • bcrypt is still acceptable for existing systems

BLAKE3: modern fast hashing

BLAKE3 is a new general-purpose hash function (2020) that’s very fast:

import { blake3 } from 'hash-wasm';

const hash = await blake3('hello');
// "ea8f163db38682925e4491c5e58d4bb3506ef8c14eb78a86e908c5624a67200f"

BLAKE3 is:

  • Significantly faster than SHA-256 (often 10x+)
  • Secure (no known attacks)
  • Supports variable-length output, streaming, keyed hashing, and key derivation

Use BLAKE3 for: checksums, content-addressable storage, file deduplication, any high-throughput hashing need.

Choosing the right hash function

NeedFunction
Password storageArgon2id (new) or bcrypt (existing)
Data integrity / checksumsSHA-256
HMAC (message authentication)HMAC-SHA-256
High-throughput content hashingBLAKE3
Digital signaturesSHA-256 (with RSA/ECDSA)
Git object IDsSHA-1 (legacy) / SHA-256 (new)
Legacy checksum (non-security)MD5 or SHA-1 (still OK here)

Related posts

Related tool

Hash Generator

Generate MD5, SHA-1, SHA-256, and SHA-512 hashes client-side.

Written by Mian Ali Khalid. Part of the Encoding & Crypto pillar.