Security

MD5 vs SHA256 vs bcrypt: Which Hash Function to Use

The Debuggers Engineering Team
11 min read

Cryptographic hash function concept with digital fingerprint patterns

Hash functions are the silent workhorses of modern security. They verify file integrity, secure passwords, sign digital documents, and power blockchain consensus. But not all hash functions are created equal.

Using MD5 for password storage is like using a padlock to guard a vault. Using bcrypt for file checksums is like using a vault to guard a padlock. Each hash function has a specific purpose, and using the wrong one creates unnecessary risk or overhead.

This guide compares the most common hash functions and tells you exactly which one to use for each scenario.

What Is a Hash Function

A hash function takes an input of any length and produces a fixed-length output called a digest or hash. Good hash functions have three critical properties:

  1. Deterministic - The same input always produces the same output
  2. One-way - You cannot reverse the hash to recover the original input
  3. Collision-resistant - It is computationally infeasible to find two different inputs that produce the same hash

MD5 - Fast but Broken

MD5 produces a 128-bit (32-character hex) hash. It was designed in 1991 by Ronald Rivest and was the standard for over a decade.

Input:  "hello world"
MD5:    5eb63bbbe01eeed093cb22bb8f5acdc3

Why MD5 Is Insecure

MD5's collision resistance was broken in 2004. Researchers demonstrated that two different files could produce the same MD5 hash. By 2008, researchers created a rogue CA certificate by exploiting MD5 collisions.

MD5 should never be used for:

  • Password hashing
  • Digital signatures
  • Certificate verification
  • Any security-critical application

MD5 is acceptable for:

  • Non-security checksums (verifying download integrity where tampering is not a concern)
  • Cache invalidation keys
  • Non-cryptographic deduplication

Hash function comparison showing security levels of different algorithms

SHA-256 - The Current Standard

SHA-256 is part of the SHA-2 family designed by the NSA. It produces a 256-bit (64-character hex) hash and is the backbone of TLS, code signing, and blockchain.

Input:  "hello world"
SHA-256: b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9

SHA-256 Strengths

  • No known practical collision attacks
  • Widely supported across every platform and language
  • Required by security standards (PCI DSS, HIPAA, GDPR)
  • Powers Bitcoin and most blockchain systems

SHA-256 vs SHA-512

SHA-512 produces a 512-bit hash and is actually faster than SHA-256 on 64-bit processors, despite producing a longer hash. Use SHA-512 when storage is not a concern and you want maximum collision resistance.

PropertySHA-256SHA-512
Output Size32 bytes64 bytes
Block Size64 bytes128 bytes
Rounds6480
Speed (64-bit)FastFaster
Security Level128-bit256-bit

When to Use SHA-256

  • File integrity verification
  • Digital signatures
  • API request signing (HMAC-SHA256)
  • Data deduplication
  • Blockchain and merkle trees

When NOT to Use SHA-256

Do not use SHA-256 for password hashing. SHA-256 is intentionally fast. Modern GPUs can compute billions of SHA-256 hashes per second, making brute-force attacks trivial against password databases.

bcrypt - Purpose-Built for Passwords

bcrypt was designed specifically for password hashing in 1999. It incorporates three critical features that generic hash functions lack:

  1. Salt - Automatically generates and embeds a random salt, preventing rainbow table attacks
  2. Cost factor - Configurable work factor that makes hashing deliberately slow
  3. Adaptive - The cost factor can be increased as hardware gets faster
Input:     "mypassword"
bcrypt:    $2b$12$LJ3m4ys6mY8RlK7eSVqPBOxWJsK5rYPGCTZJ5Q6P2y3h5VpRqWVe

Why bcrypt Wins for Passwords

At a cost factor of 12, bcrypt takes approximately 250ms to hash a single password. This is imperceptible for a user logging in, but it means an attacker can only try 4 passwords per second per CPU core - compared to billions with SHA-256.

bcrypt Alternatives

  • Argon2id - The winner of the Password Hashing Competition (2015). Memory-hard, resistant to GPU attacks. Recommended for new projects.
  • scrypt - Memory-hard, used by some cryptocurrencies. More complex to configure than bcrypt.
  • PBKDF2 - Standards-compliant (NIST). Uses SHA-256 internally with configurable iterations. Simpler but not memory-hard.

The Decision Matrix

Use CaseRecommendedWhy
Passwordsbcrypt or Argon2idIntentionally slow, salted
File checksumsSHA-256Fast, collision-resistant
API signing (HMAC)SHA-256Industry standard
Digital signaturesSHA-256/SHA-512Proven security
Quick fingerprintingSHA-256Fast and reliable
Legacy non-securityMD5Only when no security needed
BlockchainSHA-256Proven, widely adopted

Implementing Hash Functions

In JavaScript (Browser)

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('');
}

In Node.js

const crypto = require('crypto');
const bcrypt = require('bcrypt');

// SHA-256
const sha256Hash = crypto.createHash('sha256').update('hello').digest('hex');

// bcrypt
const bcryptHash = await bcrypt.hash('mypassword', 12);
const isValid = await bcrypt.compare('mypassword', bcryptHash);

Try It Yourself

Generate hashes instantly with our free Hash Generator. It supports MD5, SHA-1, SHA-256, SHA-512, and bcrypt - all computed locally in your browser. Your input data is never transmitted or stored.

For encrypting data (not just hashing), try our AES Encryption Tool which uses AES-256-GCM for secure bidirectional encryption.

Developer comparing hash outputs in a terminal window

Frequently Asked Questions

Can you reverse a hash to get the original input?

No. Hash functions are mathematically one-way. You cannot derive the original input from the hash output. The only attack is brute-force - trying every possible input until you find a match.

Is MD5 still used anywhere?

Yes, for non-security purposes like cache keys and quick checksums. It should never be used for passwords, signatures, or any context where collision resistance matters.

How many rounds should I use for bcrypt?

A cost factor of 12 is the current recommended minimum (approximately 250ms per hash). Increase the cost factor every 2-3 years as hardware improves. Aim for 250ms-500ms per hash operation.

Need Help Implementing This in a Real Project?

Our team supports end-to-end development for web and mobile software, from architecture to launch.

hash generator onlinemd5 vs sha256bcrypt hashhash function comparisonpassword hashing

Found this helpful?

Join thousands of developers using our tools to write better code, faster.