MD5 vs SHA-256: Hashing Algorithms Explained
How hash functions work, why MD5 and SHA-1 are weak for security, and when to use SHA-256 or SHA-512 for integrity and passwords.
A hash function turns input of any size into a fixed-length value called a digest. This article is about cryptographic hash functions, which are designed so that the digest works as a fingerprint of the input: easy to compute, impractical to reverse, and (ideally) impossible to match with a different input.
The Golden Rules of Hashing
- Deterministic: The same input must always produce the same output.
- One-way: It should be computationally infeasible to work backwards from the hash to the original input.
- Avalanche Effect: A tiny change in input (like changing one letter) should produce a drastically different hash.
Common Algorithms
MD5 (Message Digest 5)
Status: Broken / Unsafe
Once the standard for file integrity, MD5 is now considered cryptographically broken. Collisions (two different inputs producing the same hash) can be generated in seconds on a laptop. Do not use MD5 for passwords.
SHA-1 (Secure Hash Algorithm 1)
Status: Deprecated
SHA-1 was designed after MD5 and is stronger, but it has also fallen to practical collision attacks, starting with the SHAttered attack from CWI Amsterdam and Google in 2017. It is no longer safe for digital signatures.
SHA-256
Status: Secure Standard
Part of the SHA-2 family, SHA-256 is the most widely used secure hash today. It appears in Bitcoin, in TLS certificate signatures and in file verification. It produces a 256-bit (64 character hex) hash.
Salting Passwords
Even with SHA-256, hashing a password directly is unsafe due to "Rainbow Tables" (precomputed lists of hashes).
// Bad
hash = sha256("password123")
// Better, but still not enough for passwords (see below)
salt = random_string()
hash = sha256("password123" + salt)Always add a unique "salt" to every user's password before hashing.
SHA-512 and Beyond
SHA-512 is part of the SHA-2 family, like SHA-256, but produces a 512-bit (128 character hex) hash. The longer output gives a larger security margin, and on 64-bit CPUs it is often as fast as or faster than SHA-256 for large inputs. For most applications either is fine, and SHA-256 is the more common default.
SHA-3 has a different internal design from SHA-2 and is covered in more detail below. SHA-256 is still the one you will meet most often.
Practical Applications
File Integrity Verification
When downloading software or files, providers often publish checksums (hashes) alongside the download. After downloading, you can hash the file and compare it to the published checksum to verify it wasn't corrupted or tampered with during transmission.
Password Storage
Modern password storage uses specialized algorithms like bcrypt, Argon2, or PBKDF2, which are designed specifically for passwords. These are based on hashing but include additional features like:
- Salting: Adding random data to each password before hashing
- Key stretching: Running the hash function multiple times to slow down brute-force attacks
- Adaptive cost: Ability to increase computational cost as hardware improves
Digital Signatures
Hash functions are used in digital signatures to ensure data integrity. The data is hashed, and the hash (not the whole message) is signed with a private key. Anyone with the public key can check the signature against a freshly computed hash of the data. The exact mechanics depend on the signature scheme, such as RSA or ECDSA.
Blockchain and Cryptocurrency
Bitcoin and other cryptocurrencies use SHA-256 extensively. Each block contains a hash of the previous block, so altering an old block changes every hash after it, which makes tampering evident. Mining involves finding a hash that meets certain criteria (proof of work).
Collision Resistance
A hash collision occurs when two different inputs produce the same hash output. For secure hash functions, finding collisions should be computationally infeasible:
- MD5: Collisions can be found in seconds (broken)
- SHA-1: Collisions can be found with significant computational resources (deprecated)
- SHA-256: No known collisions found (secure)
The security of a hash function is directly related to its resistance to collision attacks. This is why MD5 and SHA-1 are no longer recommended for security-critical applications.
Real Outputs: Same Input, Different Algorithms
Here are the digests of the string hello (UTF-8, no trailing newline), computed with Node's crypto module. Note the output lengths:
const { createHash } = require("crypto");
createHash("sha256").update("hello").digest("hex");
md5 5d41402abc4b2a76b9719d911017c592
sha1 aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
sha256 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
sha512 9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca7
2323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043MD5 is 128 bits (32 hex characters), SHA-1 is 160 bits (40), SHA-256 is 256 bits (64) and SHA-512 is 512 bits (128). The avalanche effect is easy to see by changing one letter, hello to hellp:
sha256("hello") 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
sha256("hellp") fdd7585e08c4e2afd71dcabdb4636c89d557a3f42db9e2040c8bbd1708aa4ce7The empty string has a well-known SHA-256 value, e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855, which is handy for recognizing "I hashed nothing" bugs, such as a request body that never arrived. You can reproduce the SHA-1, SHA-256 and SHA-512 values in the Hash Generator. The browser's Web Crypto API does not include MD5, so compute MD5 locally with md5sum or openssl dgst -md5.
The SHA-2 Family and SHA-3
SHA-2 is specified in FIPS 180-4 and includes SHA-224, SHA-256, SHA-384 and SHA-512 (plus truncated SHA-512 variants). SHA-3, specified in FIPS 202, is built on a different internal construction (Keccak) and was standardized as a fallback in case SHA-2 weaknesses were ever found, not because SHA-2 is broken. Both are fine choices today. SHA-256 is simply the one with the widest support.
One structural property worth knowing: SHA-256 and SHA-512 are vulnerable to length extension. Given SHA-256(secret + message) and the length of the secret, an attacker can compute a valid hash for message + padding + extra without knowing the secret. This is why you must never build a message authentication code as hash(secret + message).
HMAC: Hashing with a Key
HMAC (RFC 2104) combines a hash function with a secret key in a way that avoids length extension. It is the right tool for signing webhooks, API requests and tokens, and it is the algorithm behind JWT's HS256 (see the JWT guide).
createHmac("sha256", "secret").update("hello").digest("hex");
// 88aab3ede8d3adf94d26ab90d3bafd4a2083070c3bcce9c014ee04a443847c0bWhen you verify an HMAC or any secret-derived hash, compare with a constant-time function such as Node's crypto.timingSafeEqual (both buffers must have the same length) rather than ===, which can leak how many leading characters matched.
Why Plain SHA-256 Is Wrong for Passwords
The salted SHA-256 snippet above shows the idea of salting, but a general-purpose hash is the wrong tool for passwords. SHA-256 is designed to be fast, so an attacker who steals a database can test enormous numbers of guesses per second against each salted hash. Password hashing functions are deliberately slow and, in the case of scrypt and Argon2, memory-hungry, which makes large-scale guessing much more expensive.
Node ships scrypt in its standard library. This example uses a fixed salt only so the output is reproducible. In real code, generate a fresh random salt per user with crypto.randomBytes(16) and store it next to the hash:
const salt = Buffer.from("00112233445566778899aabbccddeeff", "hex");
createHash("sha256").update("password123").digest("hex");
// ef92b778bafe771e89245b89ecbc08a44a4e166c06659911881f383d4473e94f
scryptSync("password123", salt, 32).toString("hex");
// 7b052cef88fa90f0538e05097ff63183bb6d601da2543868545d7e89d2894fc3The SHA-256 value is the same for every site and every user who picks that password, so it can be looked up in precomputed tables. The scrypt value depends on the salt and the cost parameters. In practice, use a maintained library for Argon2id or bcrypt and let it encode the salt and parameters into one string for you. To judge how guessable a password is, try the Password Checker, and see The Importance of Secure Password Generation for the generation side.
Verifying Downloads with Checksums
A published checksum only proves integrity if you got it from a source an attacker could not also modify. A hash listed on the same page as the file protects against corruption, but not against a compromised server. Signed releases or checksums served from a separate trusted channel close that gap. To compute a SHA-256 checksum locally:
sha256sum file.iso # Linux
shasum -a 256 file.iso # macOSCompare the full digest, not just the first few characters, and ignore case when comparing hex. For content-addressed storage or cache keys, SHA-256 is a safe default. MD5 is still acceptable for non-adversarial uses like detecting accidental duplicates, but not where someone could craft colliding files.
Hashes also pair with encoding: digests are binary data, so they are usually shown as hex or Base64.
Generate Hashes Instantly
Need to verify a file download or generate a quick checksum? Use our Hash Generator to create MD5, SHA-1, SHA-256 and SHA-512 hashes of text or files directly in your browser. This tool is perfect for:
- Verifying file integrity after downloads
- Generating checksums for your own files
- Comparing hashes to detect file changes
All processing happens client-side, so your data never leaves your browser, ensuring privacy and security.
Frequently Asked Questions
Can a hash be reversed or decrypted?
Not mathematically. A hash discards information. Attackers instead guess inputs and compare hashes, which works against short or common values like weak passwords, and is why salting and slow password hashes matter.
Is MD5 still safe to use?
Not for anything security related, because practical collision attacks exist. It remains usable for non-adversarial tasks such as spotting accidental file duplicates, but SHA-256 costs little extra and avoids the question.
What is the difference between hashing and encryption?
Encryption is reversible with the right key. Hashing is one-way and has no key. Use encryption to protect data you need to read again, and hashing to fingerprint data or verify it without storing it.
Should I use SHA-256 or SHA-512?
Both are secure. SHA-256 produces shorter output and is the common default. SHA-512 can be faster on 64-bit CPUs for large inputs, but for most applications the choice is about compatibility rather than security.
Written by Kashyap Thakar
Spotted a mistake? Let me know