SHA-256 vs MD5: Which Hash Should You Use?
By AZ Utils Editorial · · 10 min read
MD5 and SHA-256 are both hash functions, both turn data into a fixed-size fingerprint, and both still appear all over real-world software. But they are not interchangeable: one is considered cryptographically broken, and the other is a current standard. Choosing wrongly can leave a system vulnerable. This guide compares SHA-256 vs MD5 directly — their differences, why MD5 fell, where each is still appropriate, and how to choose.
It is written for developers deciding which hash to use, students learning cryptography, and engineers auditing legacy code that still relies on MD5.
The Short Answer
For anything where security matters, use SHA-256, not MD5. MD5 has been cryptographically broken for years: attackers can deliberately produce two different inputs with the same MD5 hash, which destroys the collision resistance that security uses depend on. SHA-256, by contrast, has no practical collision or preimage attacks and is a current, trusted standard. MD5 survives only in narrow, non-security roles — like a quick checksum to catch accidental corruption — where an attacker is not in the picture.
In short: SHA-256 produces a 256-bit hash and is cryptographically secure; MD5 produces a 128-bit hash and is broken, with practical collision attacks. Use SHA-256 for anything security-related; MD5 is acceptable only as a non-security checksum for accidental corruption.
SHA-256 vs MD5 at a Glance
| Aspect | MD5 | SHA-256 |
|---|---|---|
| Output size | 128 bits (32 hex chars) | 256 bits (64 hex chars) |
| Year introduced | 1991 | 2001 (SHA-2 family) |
| Collision resistance | Broken — practical attacks | Secure — no practical attacks |
| Preimage resistance | Weakened | Secure |
| Speed | Very fast | Fast (a bit slower than MD5) |
| Security status | Deprecated for security | Current standard |
| Appropriate for | Non-security checksums only | Integrity, signatures, certificates |
Why MD5 Fell
MD5 was designed in 1991 and was, for a time, a respected general-purpose hash. Its downfall was the discovery of practical collision attacks — methods to construct two different inputs that hash to the same MD5 value. The first collisions were demonstrated in the mid-2000s, and the techniques rapidly improved until producing MD5 collisions became cheap and fast. Researchers and attackers showed they could craft, for example, two different files or even two different certificates with the same MD5 digest.
This matters enormously because collision resistance is the foundation of MD5's security uses. If a digital signature is computed over an MD5 hash, an attacker who can find a collision can potentially get a malicious document accepted as if it were a legitimate one with the same hash. The same logic undermines any scheme that trusts an MD5 hash to uniquely and securely identify content. Once collisions became practical, MD5 was no longer safe for any purpose where an adversary might benefit from forging a match. Its 128-bit output is also small by modern standards, compounding the problem.
The crucial nuance is that MD5 is broken specifically against deliberate, adversarial attacks. It still reliably detects accidental changes — a corrupted download, a flipped bit in transmission — because random corruption is overwhelmingly unlikely to produce a collision by chance. This is why MD5 lingers as a fast checksum in contexts with no attacker, even though it must never be trusted where someone might try to fool it.
Where Each Is Appropriate Today
Use SHA-256 for every security-relevant hashing task: verifying that a download has not been tampered with by an attacker, digital signatures, TLS certificates, content addressing where integrity matters, and anywhere a hash must be hard to forge. It is the safe default, and the modest speed difference versus MD5 is irrelevant for these purposes.
MD5 is acceptable only in non-security roles where you are guarding against accidental corruption, not a malicious adversary — for instance, a quick internal checksum to detect that a large file copied without error, or as a fast key for caching and deduplication where a collision would be a performance nuisance rather than a security breach. Even here, many teams simply use SHA-256 to avoid having to reason about whether an attacker could ever be involved. When in doubt, default to SHA-256; the only reason to reach for MD5 is a specific, attacker-free performance need.
Neither, it must be said, is appropriate for storing passwords. Both are far too fast, and password storage requires a slow, salted, purpose-built function — the subject of Password Hashing Best Practices.
Seeing the Difference
The two hashes of the same input look quite different in length and value:
Input: "hello"
MD5: 5d41402abc4b2a76b9719d911017c592 (32 hex chars)
SHA-256: 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 (64 hex chars)
You can generate either with our tools — the SHA-256 Hash Generator and the MD5 Hash Generator — and compare them side by side.
In Code
# Python
import hashlib
hashlib.md5(b"hello").hexdigest() # MD5 (avoid for security)
hashlib.sha256(b"hello").hexdigest() # SHA-256 (preferred)
// JavaScript (browser) — SHA-256 via Web Crypto
await crypto.subtle.digest("SHA-256", new TextEncoder().encode("hello"));
// Note: the Web Crypto API does not even offer MD5, a hint about its status.
It is telling that the browser's modern cryptography API offers SHA-256 but not MD5 — the platform deliberately steers you toward secure choices.
Understanding Collision Attacks
To use these hashes wisely, it helps to understand what a collision attack actually is and why it is so damaging, because the abstract phrase "collision resistance is broken" hides a concrete and serious threat. A collision is two different inputs that produce the same hash. Collisions must exist for any hash function in principle, because there are infinitely many possible inputs and only a finite number of outputs; the security question is whether anyone can find them on demand. For a secure hash, finding a collision should require an infeasible amount of work. For MD5, researchers found shortcuts that make collisions cheap to produce — a laptop can generate them in moments.
The danger becomes vivid when you consider chosen-prefix collisions, an advanced form in which an attacker can take two meaningfully different documents and append carefully computed data to each so that both end up with the same MD5 hash. This has been used to demonstrate, for example, two certificates or two documents — one benign, one malicious — that share a digest. Any system that trusts an MD5 hash to vouch for the identity or integrity of content can then be deceived: a signature collected on the harmless version is equally valid on the harmful one, because the signing process only ever saw the shared hash. This is precisely why a broken collision resistance is fatal for signatures, certificates and content authentication, even though MD5 still reliably flags accidental corruption.
The Story of MD5's Decline
The downfall of MD5 is an instructive piece of cryptographic history. When it was published in 1991, MD5 was widely adopted and trusted, baked into protocols, certificate systems and countless applications. The first theoretical cracks appeared within years, but it was the practical collision demonstrations of the mid-2000s that turned concern into alarm. As the techniques sharpened, what had once been a theoretical worry became a cheap, repeatable attack, and the security community moved from recommending caution to declaring MD5 unfit for any security purpose. Real incidents followed, including malware that exploited MD5 collisions to forge trust, which underscored that this was not an academic concern.
The lesson developers should take from this is not simply "avoid MD5" but something more general about how cryptographic primitives age. Algorithms that are secure today can be weakened or broken tomorrow as cryptanalysis advances, which is why systems should be designed to allow the hash function to be swapped out — a property called cryptographic agility. It is also why the community maintains newer standards like SHA-2 and SHA-3 in reserve and deprecates old ones promptly. SHA-1 followed a similar arc to MD5, with practical collisions demonstrated publicly in 2017, reinforcing the pattern. Treating any single algorithm as eternal is a mistake; treating the current recommendation as the right default while staying ready to migrate is the mature posture, and the MD5 saga is the textbook example of why.
Migrating Away from MD5
If you maintain a system that uses MD5 for a security purpose, plan to migrate. For integrity checks and content identification, the switch to SHA-256 is usually straightforward: compute and store SHA-256 alongside or instead of MD5, and update verification accordingly. For anything involving signatures or certificates, treat MD5 as a finding to remediate, since it is a known weakness auditors and attackers both look for. The migration is rarely difficult; the hardest part is usually finding all the places MD5 is used, because it tends to be sprinkled through older codebases as a default "make a hash of this" call. Auditing for those calls and replacing them with SHA-256 is a high-value, low-risk modernisation.
Try Our Free Hash Generators
Compare the two for yourself with our free, browser-based tools:
- ✅ SHA-256 Hash Generator — the secure standard
- ✅ MD5 Hash Generator — for non-security checksums
- ✅ Both run entirely in your browser
👉 Generate a SHA-256 hash now →
Common Mistakes
- Using MD5 for security. Its broken collision resistance makes it unsafe for signatures, integrity against attackers, or anything an adversary could forge.
- Assuming MD5 is "good enough" because it still detects corruption. It does, but only against accidental changes, not deliberate ones.
- Using either MD5 or SHA-256 to store passwords. Both are far too fast; use a dedicated password hash.
- Leaving legacy MD5 in place unexamined. It is a common audit finding worth remediating.
- Choosing MD5 for speed without considering the threat model. If an attacker could ever benefit, the speed is not worth the risk.
Lessons for Choosing Hash Functions
The SHA-256-versus-MD5 question is really a window onto a broader skill: how to choose a hash function responsibly in general. The first lesson is that the right choice depends entirely on your threat model — on whether an adversary is part of the picture. MD5 is genuinely fine for catching accidental corruption and genuinely catastrophic for resisting a deliberate attacker, and the only way to tell which situation you are in is to ask whether anyone might benefit from forging a match. Developers who skip that question and pick a hash by habit or by speed are the ones who end up with MD5 quietly guarding something it cannot protect. Making the threat model explicit, even briefly, prevents that mistake.
The second lesson is to favour the conservative default unless you have a specific, well-understood reason not to. SHA-256 is that default for general-purpose secure hashing: it is fast enough for virtually any workload, secure against all known practical attacks, and universally supported. Reaching for something more exotic should be a deliberate decision driven by a real requirement — extreme performance on huge data, a need for design diversity, or a regulatory mandate — rather than a desire for novelty. And reaching for something older and faster, like MD5, should be an even more deliberate decision, justified by a concrete, attacker-free performance need and documented so the next maintainer understands why. When in doubt, SHA-256 is almost never the wrong answer for secure hashing, which is precisely what makes it such a good default.
The third lesson is to design for change. The history of MD5 and SHA-1 shows that even excellent algorithms can be broken given enough time and cryptanalytic progress, so the wise approach is to build systems where the hash function can be replaced without a painful rewrite. Storing an identifier of which algorithm produced a given hash, abstracting hashing behind a small interface, and avoiding hard-coding assumptions about hash length all make future migration straightforward. You may never need to swap SHA-256 out — it shows no signs of weakness — but a system that could swap it out cheaply is far healthier than one that has the choice welded in. The cost of this agility is small, and the MD5 story is the standing reminder of why it is worth paying.
Best Practices
- Default to SHA-256 for all security-relevant hashing.
- Restrict MD5 to attacker-free checksums, if you use it at all.
- Never store passwords with MD5 or SHA-256; use bcrypt, scrypt or Argon2.
- Audit and migrate legacy MD5 used in security contexts.
- Use constant-time comparison when verifying hashes for security.
Frequently Asked Questions
Is SHA-256 better than MD5?
For security, yes, decisively. SHA-256 is a current standard with no practical attacks, while MD5 is cryptographically broken by practical collision attacks. MD5 is only acceptable as a non-security checksum for accidental corruption.
Why is MD5 considered broken?
Because attackers can deliberately construct two different inputs with the same MD5 hash (a collision), which undermines its use in signatures, integrity against adversaries, and content identification. Its 128-bit output is also small by modern standards.
Is MD5 still safe for checksums?
Only for detecting accidental corruption, where no attacker is involved, because random changes almost never collide. It is not safe wherever someone might deliberately try to forge a matching hash.
Which is faster, MD5 or SHA-256?
MD5 is somewhat faster, which is why it lingers in performance-sensitive, non-security uses. For security purposes the speed difference is irrelevant and SHA-256 should be used.
Can I use SHA-256 to store passwords?
No. SHA-256, like MD5, is too fast for password storage. Use a slow, salted password-hashing function such as bcrypt, scrypt or Argon2 instead.
How do I migrate from MD5 to SHA-256?
Find every place MD5 is used for security, and replace it with SHA-256, recomputing and storing the new hashes and updating verification. The main effort is locating all the usages in older code.
Summary
SHA-256 and MD5 look similar but sit on opposite sides of a critical line: SHA-256 is a secure, current standard, while MD5 is cryptographically broken by practical collision attacks and unsafe for any security purpose. Use SHA-256 by default for integrity, signatures and certificates; confine MD5, if you use it at all, to attacker-free checksums where you are only guarding against accidental corruption. And remember that neither is suitable for passwords, which need slow, salted, purpose-built hashing. If you have MD5 in security-sensitive code, treat migrating to SHA-256 as a worthwhile, low-risk improvement — and use the generators to see the difference for yourself.
The comparison ultimately reduces to a clear rule that is easy to remember and apply: for security, SHA-256; for an attacker-free checksum, MD5 is tolerable but SHA-256 is still fine; for passwords, neither. Carry that rule and you will make the right call every time the question comes up, whether you are writing new code, reviewing a pull request, or auditing an old system. The hashes are just tools, and like any tools they are safe and effective when matched to the right job and dangerous when forced into the wrong one. MD5's long, slow fall from trusted standard to cautionary tale is the clearest possible illustration of why matching the tool to the job, and staying ready to change tools as the ground shifts, is a core part of writing secure software.
👉 Compare hashes with our free tools →
Related Resources
- What Is SHA-256? — the fundamentals
- How SHA-256 Works — the algorithm
- Modern Cryptographic Hashes — the wider landscape
- Password Hashing Best Practices — for passwords
- SHA-256 Hash Generator — the tool