SHA-1
A 160-bit hash once used everywhere (certificates, Git, signatures), now retired for security purposes.
Why it's at risk
Practical collisions were demonstrated in 2017 (SHAttered) and chosen-prefix collisions in 2020 (SHA-1 is a Shambles), so it is unsafe for signatures and certificates. NIST has announced that SHA-1 will be retired completely by the end of 2030. HMAC-SHA1 is not broken in the same way, but new designs should use SHA-256 or better.
Move to
- BestSHA-256The workhorse 256-bit hash used in TLS, certificates, Git (newer repositories), blockchains and countless APIs.
- OrSHA-384A SHA-2 hash with a 384-bit output, required for US national security systems under CNSA 2.0.
- OrSHA-3 / SHAKEThe SHA-3 family (SHA3-256, SHA3-384, SHA3-512 and the SHAKE extendable-output functions), built on a different design from SHA-2.
- Used for
- Hash
- Family
- Hash function (Merkle-Damgard)
- Classical security
- 63 bits
- Standards
- FIPS 180-4, NIST SP 800-131A Rev. 2
Migration guides
Concrete code changes to move off SHA-1. Pick your stack.
node:crypto · javascriptNode.js: replace MD5 and SHA-1 (hashes, HMACs and password hashing)Production-ready
Why
MD5 and SHA-1 are broken by ordinary computers: collisions are cheap (seconds for MD5; demonstrated in practice for SHA-1, including chosen-prefix collisions). They must not be used for signatures, certificates, integrity against attackers, or passwords. This is not a quantum problem, it is a today problem, and fixing it is usually quick.
Choose the replacement by purpose:
| Purpose | Use |
|---|---|
| Integrity / fingerprints / content addressing | SHA-256 (or SHA-384/512, SHA3-256) |
| Message authentication with a key | HMAC-SHA-256 |
| Password storage | Argon2id (or scrypt; bcrypt is acceptable) — never a plain hash |
| Non-security checksums (cache keys, dedup with no attacker) | MD5 is tolerable, but SHA-256 avoids scanner noise and future misuse |
Before
import { createHash, createHmac } from 'node:crypto';
const etag = createHash('md5').update(body).digest('hex');
const sig = createHmac('sha1', secret).update(payload).digest('hex');
const pwHash = createHash('sha1').update(password + salt).digest('hex'); // never do thisAfter
import { createHash, createHmac, timingSafeEqual, scrypt, randomBytes } from 'node:crypto';
import { promisify } from 'node:util';
const etag = createHash('sha256').update(body).digest('hex');
const sig = createHmac('sha256', secret).update(payload).digest();
const expected = Buffer.from(receivedSignatureHex, 'hex');
const ok = expected.length === sig.length && timingSafeEqual(expected, sig); // constant-time compare
// Passwords: Argon2id via a maintained package (e.g. `argon2`), or scrypt built into Node:
const salt = randomBytes(16);
const hash = await promisify(scrypt)(password, salt, 32, { N: 2 ** 17, r: 8, p: 1, maxmem: 256 * 1024 * 1024 });
// store: algorithm id + params + salt + hash, e.g. "scrypt$17$8$1$<salt b64>$<hash b64>"Gotchas
- Stored data: existing MD5/SHA-1 digests (file manifests, dedup tables) need a migration plan: dual-write both hashes, backfill, then switch reads.
- Password migration: you cannot re-hash without the password. Re-hash on next successful login, or wrap the old hash (
argon2id(sha1(password))) immediately and mark the record. - Interop: external APIs that require HMAC-SHA1 (some webhooks, OAuth 1.0a, TOTP authenticator apps) may force you to keep it at that boundary; HMAC-SHA1 is not broken in the way plain SHA-1 is.
- Git still uses SHA-1 object IDs by default (with collision detection); that is outside your code.
- scrypt memory:
N=2^17, r=8needs about 128 MiB, so raisemaxmem(default 32 MiB) as shown, and size your servers accordingly.
Verify
npx -y pqe-cli scan . should no longer report md5/sha1 findings outside allow-listed boundaries; add a unit test that the HMAC output length is 32 bytes.
hashlib / hmac / argon2-cffi · pythonPython: replace MD5 and SHA-1, and store passwords with Argon2idProduction-ready
Why
MD5 and SHA-1 collisions are practical with ordinary computers, so they are unsafe for signatures, integrity against attackers and password storage. Pick the replacement by purpose: SHA-256 for integrity, HMAC-SHA-256 for keyed authentication, Argon2id for passwords.
Before
import hashlib, hmac
digest = hashlib.md5(data).hexdigest()
tag = hmac.new(secret, payload, hashlib.sha1).hexdigest()
pw = hashlib.sha1((password + salt).encode()).hexdigest() # never do thisAfter
import hashlib, hmac
digest = hashlib.sha256(data).hexdigest()
tag = hmac.new(secret, payload, hashlib.sha256).digest()
ok = hmac.compare_digest(tag, received_tag) # constant time
# Passwords: pip install argon2-cffi
from argon2 import PasswordHasher
ph = PasswordHasher() # Argon2id with RFC 9106 low-memory defaults
stored = ph.hash(password) # encodes params + salt + hash
ph.verify(stored, password) # raises on mismatch
if ph.check_needs_rehash(stored):
stored = ph.hash(password)If you need MD5 for a non-security purpose (e.g. a legacy cache key), say so explicitly: hashlib.md5(data, usedforsecurity=False) (Python 3.9+). It also keeps FIPS-mode systems from rejecting the call.
Gotchas
- PBKDF2 (
hashlib.pbkdf2_hmac) is acceptable when FIPS is required; use SHA-256 with at least 600,000 iterations (OWASP guidance) and a 16-byte random salt. - Migrating passwords: re-hash on next login, or wrap old hashes (
argon2id(old_sha1_hex)) right away and flag the records. - Django/Flask-Security etc. have their own hasher settings (
PASSWORD_HASHERS); put Argon2 first rather than calling hashlib yourself. - External protocols that mandate HMAC-SHA1 (TOTP apps, some webhooks) can stay at that boundary.
Verify
npx -y pqe-cli scan . should show no remaining md5/sha1 findings outside intentional, documented spots.
JCA (javax.crypto / java.security) · javaJava: fix Cipher.getInstance("AES") (it means ECB), DES/3DES, MD5 and SHA-1Production-ready
Why
The most common Java crypto bug is Cipher.getInstance("AES"): with the default SunJCE provider that means AES/ECB/PKCS5Padding, which leaks patterns and has no integrity. DES/3DES/RC4/Blowfish and MD5/SHA-1 are weak against ordinary computers. Fixing these is quick and independent of the post-quantum work.
Before
Cipher c = Cipher.getInstance("AES"); // = AES/ECB/PKCS5Padding
Cipher d = Cipher.getInstance("DESede/CBC/PKCS5Padding");
MessageDigest md = MessageDigest.getInstance("MD5");After
import javax.crypto.*;
import javax.crypto.spec.GCMParameterSpec;
import java.security.*;
KeyGenerator kg = KeyGenerator.getInstance("AES");
kg.init(256);
SecretKey key = kg.generateKey();
byte[] iv = new byte[12];
SecureRandom.getInstanceStrong().nextBytes(iv); // or new SecureRandom()
Cipher c = Cipher.getInstance("AES/GCM/NoPadding");
c.init(Cipher.ENCRYPT_MODE, key, new GCMParameterSpec(128, iv));
c.updateAAD(aad);
byte[] ct = c.doFinal(plaintext); // ciphertext || 16-byte tag
// store iv + ct (+ a format version byte)
MessageDigest sha = MessageDigest.getInstance("SHA-256");
Mac mac = Mac.getInstance("HmacSHA256");Gotchas
- Always spell out the full transformation (
algorithm/mode/padding); provider defaults differ. - GCM IVs must be unique per key; the JDK refuses to reuse the same key+IV pair on one encrypting
Cipherinstance, but cannot protect you across instances. getInstanceStrong()may block on some Linux configurations;new SecureRandom()is fine for IVs and keys.- Passwords: use a dedicated password hashing library (Argon2id, e.g. via Bouncy Castle or Spring Security's
Argon2PasswordEncoder), notMessageDigest. - Legacy data: keep a decrypt-only path for old formats, then re-encrypt.
Verify
Round-trip test plus tamper test (AEADBadTagException expected), then npx -y pqe-cli scan ..