ResearchCybersecurityPublished on this site18 min read
Hashing algorithms: MD5, SHA-1 and SHA-256 in production security
A comparison of threat models and attack vectors, with a migration guide for legacy systems.
In brief
- Context
- MD5 and SHA-1 have been broken for collisions for years, yet they live on in legacy systems. SHA-256 is sound, but it is often used where it is not the right tool, such as password storage.
- Approach
- Primary sources (NIST, IETF, IACR, OWASP), three reproducible measurements on ordinary hardware (speed, avalanche effect, length extension) and the published GPU speeds, mapped onto seven attack vectors.
- Conclusions
- Pick the algorithm by use, not by name: SHA-256 for integrity and signatures, HMAC for authentication, Argon2id for passwords. A seven-step migration plan follows.
What it covers
- 3algorithms compared
- 7attack vectors
- 1seven-step migration guide
Introduction
The hash function is one of the few cryptographic primitives that almost every production system uses, often without really having chosen it: it shows up in passwords, certificates, download checksums, API signatures and cache keys. Because it is everywhere, the choice is made out of habit, and habit leads to two opposite mistakes.
The first is to stay on MD5 or SHA-1 because "it still works". The second, subtler one, is to believe that a sound algorithm like SHA-256 is safe for any use: it is not safe for passwords, and it is not safe for authenticating a message with the wrong construction. "Secure" is not a property of the algorithm: it is a property of the pair formed by the algorithm and the threat model.
This study compares MD5, SHA-1 and SHA-256 against seven concrete attack vectors. It relies on primary sources and on measurements anyone can repeat, and it ends with a migration guide for whoever inherits a codebase and must decide what to touch first.
It is not a cryptanalysis paper: it presents no new attacks. It is a security engineering study and, as such, states its own limits, listed at the end of the method.
Context
Three algorithms, three stories
MD5 (Rivest, RFC 1321, 1992) produces 128 bits; SHA-1 (NIST, FIPS 180-1, 1995) produces 160; SHA-256 belongs to the SHA-2 family (FIPS 180-2, 2002) and produces 256. All three use the Merkle–Damgård construction: the message is split into blocks and an internal state is updated block after block. This design choice explains both their speed and one of the most exploited attack vectors, length extension, described below.
What we ask of a hash function
- Collision resistance: it should be infeasible to find two different messages with the same hash. For an n-bit hash the generic attack costs about 2^(n/2): 2^64 for MD5, 2^80 for SHA-1, 2^128 for SHA-256.
- Pre-image resistance: given a hash, it should be infeasible to find a message that produces it. Generic cost 2^n.
- Second pre-image resistance: given a message, it should be infeasible to find another with the same hash. Generic cost 2^n.
- Avalanche effect: flipping a single input bit should change about half of the output bits.
An algorithm is "broken" for one of these properties when an attack exists that costs less than the generic one. The properties do not fall together: as we will see, MD5 and SHA-1 collisions are practical, while pre-images remain out of reach for all three.
Timeline of the main results
| Year | Algorithm | Event | Source |
|---|---|---|---|
| 2004 | MD5 | First practical collision published (Wang et al.) | [2] |
| 2008 | MD5 | Rogue CA certificate created with chosen-prefix collisions | [3] |
| 2009 | MD5 | Pre-image on the full algorithm at 2^123.4 operations: theoretical | [5] |
| 2012 | MD5 | The Flame malware forges a Microsoft certificate with a chosen-prefix collision | [4] |
| 2017 | SHA-1 | First collision on the full algorithm (SHAttered) | [6] |
| 2020 | SHA-1 | First chosen-prefix collision, about 45,000 dollars of rented GPUs | [7] |
| 2021 | MD5, SHA-1 | RFC 9155 forbids them in TLS 1.2 and DTLS 1.2 signatures | [12] |
| 2021 | SHA-1 | OpenSSH 8.8 disables RSA signatures with SHA-1 by default | [13] |
| 2022 | SHA-1 | NIST: after 31 December 2030, FIPS 140 modules with SHA-1 move to the historical list | [8] |
| 2026 | SHA-256 | Collision on 37 of 64 steps (31 in 2013); pre-image on 46-47 steps | [9][10][11] |
Full SHA-256 has 64 steps. Attacks on reduced steps are not attacks on the algorithm, but they measure its safety margin.
Approach
The study uses two kinds of evidence and keeps them apart in the text: results published by others, cited with the primary source and its number in square brackets, and measurements made for this work, repeatable by anyone.
Sources
Standards and guidelines (NIST, IETF, OWASP), cryptanalysis papers on IACR ePrint and in conference proceedings, vendor advisories. Where a figure comes from a preprint or a secondary source, the text says so. All sources were consulted on 13 November 2026.
Measurements
- Speed:
hashlib(OpenSSL 3.0.13) in Python 3.13, a single thread, 64-byte, 1 KiB and 1 MiB buffers, median of three one-second runs. - Avalanche effect: 20,000 pairs of random 64-byte messages per algorithm, with one bit flipped; the output bits that change are counted.
- Length extension: a complete attack against H(key ‖ message) with SHA-256, compared with HMAC-SHA-256.
- Cost of password functions (Argon2id, bcrypt, PBKDF2) at the minimum parameters from the OWASP cheat sheet [14], median of five runs.
Environment: a cloud virtual machine, Intel Xeon at 2.3 GHz, 2 vCPUs, with the CPU's hardware SHA instructions (SHA-NI). The script `lab.py` runs every measurement and writes the results to `results.json`. It needs Python 3, argon2-cffi and bcrypt.
GPU speeds were not measured: they come from a public hashcat benchmark on an RTX 4090 [16]. They are used to estimate brute-force times and are labelled as such.
Limits
- A single environment, and a virtualised one: absolute values depend on the machine. Relative comparisons are what matter, and those too depend on the presence of hardware SHA instructions.
- The GPU benchmark dates from 2022 (hashcat 6.2.6, stock clocks) [16]: newer hardware and software are faster.
- The brute-force model assumes random, uniform passwords over 95 characters. Passwords chosen by people are far more predictable, so real times are shorter.
- Some of the 2026 cryptanalytic results are preprints or revisions of conference papers: read them as a provisional state of the art [10][11].
- No cryptanalysis was independently verified: the study reports the authors' results.
Analysis
1. Collisions and pre-images: where they really break
MD5. The first practical collision dates from 2004 [2]. In 2008 a group of researchers used chosen-prefix collisions to build a rogue certificate authority certificate, accepted as valid because a commercial CA still signed with MD5: the computation took about 18 hours on more than 200 PlayStation 3 consoles [3]. In 2012 a similar technique let the Flame malware forge a Terminal Services certificate and pass it off as a Microsoft one [4]. On pre-images, the best known attack on full MD5 costs 2^123.4 operations against the 2^128 of exhaustive search [5]: a negligible gain, still impractical.
SHA-1. The first collision on the full algorithm, published by Google and CWI in 2017, took about 9.22×10^18 SHA-1 computations, roughly 6,500 CPU-years plus 110 GPU-years, over 100,000 times faster than generic search [6]. In 2020 Leurent and Peyrin showed the chosen-prefix collision, the variant needed to forge certificates and signatures: 2^63.4 operations, about 45,000 dollars of rented GPUs, applied to the PGP web of trust [7]. NIST, which had deprecated SHA-1 in 2011 and banned it in signatures from 2013, asks agencies to move off it for every application as soon as possible [8].
SHA-256. There are no collisions or pre-images on the full algorithm. The best collision reaches 37 of 64 steps, six more than the 31 reached in 2013 [9][10]; the pre-image reaches 46-47 steps [11]. These are margins that erode slowly, not a breach: in 2022 NIST stated that no transition from SHA-2 to SHA-3 is needed [8].
| Vector | MD5 | SHA-1 | SHA-256 |
|---|---|---|---|
| Chosen-prefix collision (documents, certificates, signatures) | Broken for years [3][4] | Broken: practical, about 45,000 dollars [7] | No collision on the full algorithm; 37 of 64 steps [10] |
| Pre-image and second pre-image | Theoretical: 2^123.4 against 2^128 [5] | No known practical attack | Up to 46-47 of 64 steps [11]; impractical |
| Length extension on H(key ‖ message) | Vulnerable | Vulnerable | Vulnerable (shown below) |
| Passwords: offline GPU attack | 164 billion guesses per second [16] | 50.6 billion per second [16] | 22.0 billion per second [16] |
| Precomputed tables (rainbow tables) on unsalted hashes | Vulnerable | Vulnerable | Vulnerable |
| Content identifiers (VCS, deduplication) with attacker-supplied input | Unsuitable | At risk; mitigated by collision detection | Suitable |
| Signatures in protocols (TLS 1.2, SSH) | Forbidden [12] | Forbidden in TLS 1.2 [12]; RSA/SHA-1 off by default in OpenSSH 8.8 [13] | Allowed |
"Vulnerable" for length extension and precomputed tables depends on the construction, not on the strength of the algorithm: it holds for any fast hash used that way.
2. Speed: an advantage that is gone
MD5 also survives out of habit: "it's fast". On the test machine, with hardware SHA instructions, it is not faster than the alternatives.
- SHA-12,042
- SHA-2561,840
- BLAKE2b854
- MD5828
- SHA-512780
- SHA3-256560
One thread, Python 3.13 with OpenSSL 3.0.13, median of three runs. The result depends on the hardware: without dedicated SHA instructions MD5 tends to be faster, so the comparison holds for modern CPUs with SHA-NI.
SHA-256 is more than twice as fast as MD5 (1,840 against 828 MB/s). On 1 KiB buffers the ratio still favours SHA-256 (1,339 against 696 MB/s). For integrity and signatures, then, speed is no argument for staying on MD5.
3. Avalanche effect: it does not tell the three apart
| Algorithm | Output bits | Bits changed (mean) | Percentage | Min-max |
|---|---|---|---|---|
| MD5 | 128 | 64.03 | 50.02% | 40-85 |
| SHA-1 | 160 | 79.97 | 49.98% | 55-110 |
| SHA-256 | 256 | 128.12 | 50.05% | 97-161 |
The three algorithms are indistinguishable on this property: all change about half of the bits. The result is useful because it removes an illusion: a hash that "mixes well" can still have computable collisions. The collisions in MD5 and SHA-1 do not come from a weak avalanche effect, but from differential paths that cryptanalysts manage to control [2][6].
4. Passwords: where even SHA-256 is the wrong answer
For passwords, speed is the flaw. An attacker who has stolen the database tries candidates offline, as fast as possible. A single SHA-256 costs a fraction of a microsecond (about 0.3 µs at 64 bytes in this study's measurement): exactly what you do not want.
- MD5164.1 G/s
- SHA-150.6 G/s
- SHA-25622.0 G/s
- SHA-5127.5 G/s
- bcrypt (cost 5)184 k/s
Published data [16], not measured here. The scale is logarithmic: between MD5 and bcrypt there are six orders of magnitude. The benchmark uses bcrypt at cost 5, well below the minimum of 10 recommended by OWASP [14]: each cost increment doubles the work.
| Algorithm | 8 characters | 10 characters |
|---|---|---|
| MD5 | 11.2 hours | 11.6 years |
| SHA-1 | 36.4 hours | 37.5 years |
| SHA-256 | 3.5 days | 86.3 years |
| SHA-512 | 10.3 days | 253.5 years |
| bcrypt (cost 5) | 1,143 years | about 10 million years |
Time to walk the whole space; on average half is enough. Calculation: space ÷ published speed [16]. Real passwords are far more predictable than random ones, and attackers often have more than one GPU.
A random 8-character password protected by SHA-256 falls in about three and a half days on a single consumer-grade card. The same space protected by bcrypt, even at cost 5, takes centuries. It is not about picking a stronger algorithm: the difference is a deliberate, tunable cost and, for Argon2id, a memory cost that penalises GPUs.
| Function | Parameters | Time per hash |
|---|---|---|
| Argon2id | 19 MiB, 2 iterations, parallelism 1 | 17.8 ms |
| Argon2id | 46 MiB, 1 iteration, parallelism 1 | 24.7 ms |
| Argon2id | 12 MiB, 3 iterations, parallelism 1 | 16.4 ms |
| bcrypt | cost 10 | 57.8 ms |
| bcrypt | cost 12 | 230.0 ms |
| PBKDF2-HMAC-SHA256 | 600,000 iterations | 77.0 ms |
| PBKDF2-HMAC-SHA512 | 220,000 iterations | 83.8 ms |
| PBKDF2-HMAC-SHA1 | 1,400,000 iterations (legacy only) | 181.6 ms |
For a user 20-200 ms at login is imperceptible; for an attacker it multiplies the cost of every guess by tens of thousands compared with a single SHA-256 (17.8 ms against 0.3 µs).
5. Length extension and message authentication
MD5, SHA-1 and SHA-256 return their final internal state as the digest. Whoever knows a hash H(k ‖ m) can resume the computation from that state and append data without knowing the key k, as long as the length of k is known. If an API signs its parameters with sha256(secret + parameters), an attacker can append parameters and produce a valid signature.
secret = os.urandom(16) # unknown to the attacker
msg = b"user=guest&amount=10"
tag = hashlib.sha256(secret + msg).digest() # home-made MAC: H(k || m)
# The attacker knows msg, tag and len(secret). Not secret.
glue = pad(16 + len(msg)) # the padding SHA-256 already added
ext = b"&amount=1000000&admin=true"
state = list(struct.unpack(">8I", tag)) # the internal state is the hash itself
forged_tag = sha_from_state(state, ext, 16 + len(msg) + len(glue))
forged_msg = msg + glue + ext
hashlib.sha256(secret + forged_msg).digest() == forged_tag # True
# Against HMAC-SHA-256 the same forgery fails: FalseIn the test the forgery succeeds against sha256(secret + message) and fails against HMAC-SHA-256. HMAC [17] hides the internal state behind a double application of the function, and it is the construction to use when authenticating messages with a hash. Truncated variants (SHA-384, SHA-512/256) and SHA-3 do not have this flaw by construction, but HMAC remains the choice that requires remembering nothing. HMAC with SHA-1 is not deprecated by RFC 9155 [12], but there is no reason to choose it for new systems.
Conclusions
The starting question, which algorithm is "secure", is badly posed. The answer depends on the use, and the use also decides how the algorithm must be employed.
Rule of thumb
MD5 and SHA-1: never in a new security function. SHA-256: the default for integrity, signatures, HMAC and key derivation. Passwords: never a fast hash, not even SHA-256, but Argon2id.
Which function for which use
| Use | Choice | Avoid |
|---|---|---|
| Storing passwords | Argon2id (minimum 19 MiB, 2 iterations, parallelism 1) [14]; alternatively scrypt, bcrypt at cost 10 or more for legacy only, PBKDF2-HMAC-SHA256 with 600,000 iterations if FIPS compliance is needed | Plain MD5, SHA-1 or SHA-256, even with a salt |
| Authenticating messages and signing API requests | HMAC-SHA-256 [17], with constant-time comparison | H(key ‖ message) |
| Integrity of files, backups, artefacts | SHA-256, with the value published on a separate channel or signed | MD5 or SHA-1 if the attacker can prepare the file |
| Digital signatures and certificates | SHA-256 or stronger [12] | MD5, SHA-1 |
| Deriving keys from a secret that is not a password | HKDF with SHA-256 | Plain concatenated hashes |
| Cache keys, ETags, deduplication of trusted data | Any function; SHA-256 if the input comes from outside | No security issue if there is no adversary |
| Systems subject to FIPS | SHA-2 or SHA-3; SHA-1 only until 2030 [8] | New modules with SHA-1 |
Migration guide for legacy systems
Order matters more than speed. Start with what fails most seriously and is most exploitable: passwords, then signatures, then message authentication, finally integrity. The seven steps below apply system by system.
Step 1. Inventory
Look for every use of MD5 and SHA-1 in code, dependencies, databases and configuration: library functions, columns with 32 or 40-character hexadecimal strings, certificates signed with sha1WithRSA, SSH settings with ssh-rsa, Content-MD5 headers.
grep -rnE "md5\(|sha1\(|hashlib\.(md5|sha1)|createHash\(['\"](md5|sha1)|MessageDigest\.getInstance\(\"(MD5|SHA-?1)\"\)" src/Step 2. Classify by purpose
For each occurrence, ask what it is for, using the table of choices. Uses with no adversary, such as an ETag or a checksum required by a protocol, are documented and left alone: changing them for no reason breaks compatibility. The rest goes into the plan.
Step 3. Rank by risk
- Passwords stored with a fast hash.
- Signatures and certificates with MD5 or SHA-1.
- Message authentication with H(key ‖ message) or with weak hashes.
- File and artefact integrity with third-party-controlled input.
- Everything else.
Step 4. Make the format versioned
Before changing algorithm, store the algorithm and its parameters next to every hash, for example in the PHC format ($argon2id$v=19$m=19456,t=2,p=1$...) [14]. Old and new can then coexist and the next upgrade becomes a configuration change.
Step 5. Passwords: wrap now, rewrite at login
You do not need to know the passwords to protect them better: Argon2id can be applied to the stored MD5 hash, in a single database operation [14]. On the user's next successful login, the plaintext password, available at that moment, lets you replace the wrapped hash with a direct Argon2id. Accounts inactive for a long time can be expired and asked for a reset.
from argon2 import PasswordHasher
import hashlib
ph = PasswordHasher(time_cost=2, memory_cost=19456, parallelism=1)
# Batch step: no plaintext password, wrap the stored MD5
user["hash"] = ph.hash(legacy_md5_hex)
user["fmt"] = "md5-argon2id"
def verify(user, password):
if user["fmt"] == "md5-argon2id":
try:
ph.verify(user["hash"], hashlib.md5(password.encode()).hexdigest())
except Exception:
return False
user.update(fmt="argon2id", hash=ph.hash(password)) # direct rewrite
return True
try:
ph.verify(user["hash"], password)
except Exception:
return False
if ph.check_needs_rehash(user["hash"]):
user["hash"] = ph.hash(password)
return TrueA wrapped hash is easier to attack than a direct Argon2id, because the attacker can start from the MD5 [14]: it is a transitional phase, to be closed by the rewrite at login. A pepper, a shared secret kept outside the database, is a useful addition but does not replace the cost of the hash [14]. NIST's password rules are consistent: salted hashes with a suitable scheme, no composition rules, no mandatory periodic rotation [15].
Step 6. Messages and APIs: two signatures for a while
Introduce HMAC-SHA-256 with a new key, stating the version in the request. The server accepts both signatures for a defined period, then switches the old one off. Signatures are compared in constant time.
Step 7. Verify and retire
- Regression tests on the login and signing flows, with old and new formats.
- A metric counting how many verifications still use the legacy format, and a shut-off date.
- Removal of the legacy code on the set date.
- A continuous-integration rule that fails the build if MD5 or SHA-1 appear in a security module.
What is left out
The study does not measure SHA-3 and BLAKE beyond speed figures, presents no cryptanalysis of its own and does not repeat the GPU benchmark. To strengthen it, the same tests would be needed on more CPUs, with and without SHA instructions, and an up-to-date GPU benchmark.
Key point
You do not choose "a secure hash": you choose the right function for the use. SHA-256 for integrity and signatures, HMAC-SHA-256 for authentication, Argon2id for passwords. MD5 and SHA-1 only where there is no adversary, and with an exit date.
References
- Rivest, R. (1992). The MD5 Message-Digest Algorithm (RFC 1321). IETF. https://www.rfc-editor.org/info/rfc1321 (external site, opens in a new tab)
- Wang, X., Feng, D., Lai, X., Yu, H. (2004). Collisions for Hash Functions MD4, MD5, HAVAL-128 and RIPEMD. IACR Cryptology ePrint Archive 2004/199. https://eprint.iacr.org/2004/199 (external site, opens in a new tab)
- Sotirov, A., Stevens, M., Appelbaum, J., Lenstra, A., Molnar, D., Osvik, D. A., de Weger, B. (2008). MD5 considered harmful today: creating a rogue CA certificate. Progetto HashClash. https://www.win.tue.nl/hashclash/rogue-ca/ (external site, opens in a new tab)
- Microsoft Security Response Center (2012). Flame malware collision attack explained. MSRC Blog, 6 giugno 2012. https://www.microsoft.com/en-us/msrc/blog/2012/06/flame-malware-collision-attack-explained/ (external site, opens in a new tab)
- Sasaki, Y., Aoki, K. (2009). Finding preimages in full MD5 faster than exhaustive search. EUROCRYPT 2009, LNCS 5479
- Stevens, M., Bursztein, E., Karpman, P., Albertini, A., Markov, Y. (2017). The first collision for full SHA-1 (SHAttered). CWI Amsterdam e Google, 23 febbraio 2017. https://shattered.io/ (external site, opens in a new tab)
- Leurent, G., Peyrin, T. (2020). SHA-1 is a Shambles: first chosen-prefix collision on SHA-1 and application to the PGP web of trust. IACR Cryptology ePrint Archive 2020/014. https://eprint.iacr.org/2020/014 (external site, opens in a new tab)
- NIST (2022). NIST Policy on Hash Functions. Aggiornamento del 15 dicembre 2022. https://csrc.nist.gov/projects/hash-functions/nist-policy-on-hash-functions (external site, opens in a new tab)
- Mendel, F., Nad, T., Schläffer, M. (2013). Improving local collisions: new attacks on reduced SHA-256. EUROCRYPT 2013
- Zhang, Z., Li, M., Gao, L., Wang, M. (2026). Collision attacks on SHA-256 up to 37 steps with improved trail search. IACR Cryptology ePrint Archive 2026/232 (revisione di un articolo di EUROCRYPT 2026). https://eprint.iacr.org/2026/232 (external site, opens in a new tab)
- Guo, J., Li, H., Liu, M., Wang, S., Zhang, T. (2026). Dual-syncopation meet-in-the-middle attacks: new results on SHA-2 and MD5. IACR Cryptology ePrint Archive 2026/353 (EUROCRYPT 2026). https://eprint.iacr.org/2026/353 (external site, opens in a new tab)
- Velvindron, L., Moriarty, K., Ghedini, A. (2021). Deprecating MD5 and SHA-1 signature hashes in TLS 1.2 and DTLS 1.2 (RFC 9155). IETF, dicembre 2021. https://www.rfc-editor.org/info/rfc9155 (external site, opens in a new tab)
- OpenSSH project (2021). Announce: OpenSSH 8.8 released. oss-security, 26 settembre 2021. https://openwall.com/lists/oss-security/2021/09/26/1 (external site, opens in a new tab)
- OWASP (2026). Password Storage Cheat Sheet. OWASP Cheat Sheet Series, consultato il 13 novembre 2026. https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html (external site, opens in a new tab)
- NIST. Digital Identity Guidelines: Authentication and Authenticator Management (SP 800-63B-4). Sezione 3.1.1.2, consultata il 13 novembre 2026. https://pages.nist.gov/800-63-4/sp800-63b/authenticators (external site, opens in a new tab)
- Chick3nman (2022). Hashcat v6.2.6 benchmark, NVIDIA GeForce RTX 4090. GitHub Gist, 14 ottobre 2022. https://gist.github.com/Chick3nman/32e662a5bb63bc4f51b847bb422222fd (external site, opens in a new tab)
- Krawczyk, H., Bellare, M., Canetti, R. (1997). HMAC: keyed-hashing for message authentication (RFC 2104). IETF. https://www.rfc-editor.org/info/rfc2104 (external site, opens in a new tab)
- Barbato, F. (2026). lab.py e results.json: script e risultati delle misure di questo studio. Materiale allegato. /research/hashing-algorithms/lab.py (external site, opens in a new tab)