MD5 and SHA-256 are both cryptographic hash functions: they take input of any length and produce a fixed-length fingerprint, called a digest. The same input always gives the same digest, and a tiny change to the input gives a completely different one. The two differ in age, output size and, most importantly, in how well they hold up against attacks. MD5 is considered broken for security purposes; SHA-256 is not.

What a hash function should guarantee

A cryptographic hash is expected to have three properties:

  • Preimage resistance: given a digest, it is infeasible to find an input that produces it.
  • Second-preimage resistance: given one input, it is infeasible to find a different input with the same digest.
  • Collision resistance: it is infeasible to find any two different inputs with the same digest.

Collision resistance is the property that fails first as a hash function ages, and it is the one that has failed for MD5 and SHA-1.

Output length

  • MD5 (1992, RFC 1321): 128-bit digest, shown as 32 hex characters.
  • SHA-1 (1995): 160-bit digest, 40 hex characters.
  • SHA-256 (2001, part of the SHA-2 family in FIPS 180): 256-bit digest, 64 hex characters.

A longer digest raises the cost of generic attacks. A brute-force collision search on an n-bit hash takes roughly 2n/2 attempts (the birthday bound), so about 264 for MD5 even without its structural weaknesses, and about 2128 for SHA-256.

Example: hashes of "hello"

Computed with Python's hashlib over the UTF-8 bytes of the lowercase word hello:

import hashlib
for name in ("md5", "sha1", "sha256"):
    print(name, hashlib.new(name, b"hello").hexdigest())

md5     5d41402abc4b2a76b9719d911017c592
sha1    aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
sha256  2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824

Changing one letter shows the avalanche effect. The SHA-256 of Hello with a capital H shares no visible pattern with the one above:

sha256  185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969

You can reproduce these, and compare MD5, SHA-1, SHA-224, SHA-256, SHA-384, SHA-512, SHA3-256 and SHA3-512 side by side, with the Hash Generator.

Speed

MD5 is generally faster than SHA-256 in software because it does less work per block. The exact gap depends on the CPU and implementation, and many modern x86 and ARM processors include dedicated SHA-256 instructions that narrow it considerably. For most applications, such as hashing API payloads or files, either one is fast enough that the difference does not matter.

Speed is not always a good thing. A fast hash also lets an attacker try more guesses per second, which matters a great deal for passwords (see below).

Collision attacks on MD5 and SHA-1

MD5

In 2004, Xiaoyun Wang and colleagues published practical MD5 collisions: two different inputs with the same digest. Later work made chosen-prefix collisions practical, where an attacker can start from two different, meaningful prefixes and append data so both hash the same. In 2008, researchers used this to create a rogue certificate authority certificate that browsers of the time would have trusted. The Flame malware, discovered in 2012, used an MD5 collision to forge a Microsoft code-signing certificate. Generating an MD5 collision now takes seconds on an ordinary computer.

SHA-1

SHA-1 was theoretically weakened in 2005. In 2017, Google and CWI Amsterdam announced SHAttered, the first public SHA-1 collision: two different PDF files with the same SHA-1 digest. In 2020, the "SHA-1 is a Shambles" research demonstrated a practical chosen-prefix collision. Browsers stopped accepting SHA-1 TLS certificates around 2017, and NIST has announced that SHA-1 should be phased out completely by 2030.

SHA-256

No practical collision, preimage or second-preimage attack on full SHA-256 is publicly known. It is the default choice in TLS certificates, code signing, Git's newer object format, Bitcoin and many other systems. SHA-3 (for example SHA3-256) is a newer, structurally different alternative standardised in 2015; it is not a replacement because SHA-2 is broken, but a backup with a different design.

Checksums vs security uses

The key question is whether anyone might deliberately craft inputs to fool the hash.

  • Accidental corruption (a file damaged in transfer, deduplicating your own data, cache keys, partitioning): there is no attacker, and MD5 still detects random changes reliably. Many legacy systems use it for exactly this.
  • Adversarial settings (verifying downloads from a mirror you do not control, digital signatures, certificates, integrity checks on untrusted data, content-addressed storage): collision attacks matter, so use SHA-256 or stronger. A published SHA-256 checksum only helps if it comes from a trusted source, such as the vendor's HTTPS site, separate from the file itself.
  • Message authentication (proving a message came from someone with a shared key): use HMAC, for example HMAC-SHA256, rather than hashing the key and message together yourself.

If you are designing something new and are unsure, choose SHA-256. It costs little extra and removes a class of problems.

Why passwords need bcrypt or Argon2, not plain hashes

Storing sha256(password) is better than storing plain text, but not by much. General-purpose hashes are built to be fast, and GPUs can compute very large numbers of them per second, so common and short passwords fall quickly to guessing. Without a salt, identical passwords have identical hashes, and precomputed lookup tables (including rainbow tables) work against every account at once. Switching from MD5 to SHA-256 does not fix either problem.

Password hashing functions are designed for this job:

  • Argon2id: winner of the 2015 Password Hashing Competition; tunable in time and memory, which makes GPU attacks expensive. The usual first choice for new systems.
  • scrypt: also memory-hard.
  • bcrypt: older and widely supported; note that it uses only the first 72 bytes of the password.
  • PBKDF2 with many iterations: acceptable where FIPS compliance is required.

All of these generate a random salt per password and store it alongside the hash, and all have a cost parameter you can raise as hardware gets faster. Use a well-maintained library (for example argon2-cffi or bcrypt in Python) rather than building your own scheme. The Hash Generator is not a password hashing tool, and real passwords should not be pasted into any online tool.

Quick comparison

                         MD5            SHA-256
Digest size              128 bits       256 bits
Hex length               32             64
Collision attacks        practical      none known
Checksums (no attacker)  ok             ok
Signatures, certs        do not use     yes
Password storage         do not use     do not use (use Argon2id/bcrypt)

FAQ

Is MD5 still safe to use?

Not for anything security-related, because practical collision attacks have existed since 2004. It is still acceptable for non-adversarial jobs such as detecting accidental corruption, deduplication or cache keys, where nobody is trying to craft matching inputs.

Is SHA-256 good enough for storing passwords?

Not on its own. SHA-256 is designed to be fast, which makes guessing passwords cheap for an attacker. Use a dedicated password hashing function such as Argon2id, scrypt or bcrypt, which are deliberately slow and use a unique salt per password.

Can you decrypt an MD5 or SHA-256 hash?

No. Hashes are one-way functions, not encryption, so there is nothing to decrypt. Short or common inputs can still be found by guessing or by looking them up in precomputed tables, which is why websites that claim to reverse hashes work only for known inputs.