Blog ·
SHA-256 vs SHA-1: Why SHA-1 Is No Longer Secure
SHA-256 is the safer choice: SHA-1 has practical collision attacks, letting two different files share one hash. Here is how they differ, with code.
Hash Generator
Generate SHA-256, SHA-384, SHA-512 and SHA-1 hashes.
SHA-256 is the better choice for anything new. It produces a 256-bit digest, and no practical collision attacks are publicly known against it. SHA-1 produces a 160-bit digest, and researchers have built two different files that share the same SHA-1 hash, so it should not be used where an attacker could influence the input.
You can generate SHA-1 and SHA-256 hashes for any text in the Hash Generator and compare the output directly. The tool runs in your browser, which matters if you are hashing tokens or other secrets.
SHA-256 vs SHA-1 at a glance
- Output size: SHA-1 is 160 bits, shown as 40 hex characters. SHA-256 is 256 bits, shown as 64 hex characters.
- Collision resistance: SHA-1 collisions are practical to produce. No practical SHA-256 collision is publicly known.
- Standards status: NIST has deprecated SHA-1 for digital signatures. SHA-256 is widely recommended for new designs.
- Speed: SHA-1 is somewhat faster, but the difference rarely matters in real workloads, so it is not a good reason to choose it.
- Encoding: both are usually printed as hex, but base64 output is shorter. See Base64 vs Base64URL if you need to pick one.
Why SHA-1 is considered broken
A hash function is supposed to make it infeasible to find two different inputs with the same digest. This is called collision resistance. For a 160-bit hash, a generic birthday attack needs roughly 2^80 work, which is far too much for practical use.
In 2017, Google and CWI Amsterdam published the SHAttered attack. It found a SHA-1 collision using about 2^63 operations, far below the generic bound. The researchers released two different PDF files with identical SHA-1 hashes.
The practical risk comes from who controls the input. If an attacker can supply one of the two files, they can craft a malicious file that carries the same hash as a trusted one. That affects code signing, certificate signing, download verification and any system that uses a hash as proof that content has not changed.
Some version control systems and protocols still rely on SHA-1 by default, and their maintainers have been working on SHA-256 support. If you run into SHA-1 in a file format or protocol you do not control, you may need to match it, but that is an interoperability decision, not a reason to pick SHA-1 for new code.
Generate both hashes in code
In Node.js, the built-in crypto module supports both algorithms. The input below is the ASCII string hello, with no trailing newline.
const crypto = require('crypto');
const input = 'hello';
console.log(crypto.createHash('sha1').update(input).digest('hex'));
console.log(crypto.createHash('sha256').update(input).digest('hex'));aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
In Python, hashlib does the same job:
import hashlib data = b'hello' print(hashlib.sha1(data).hexdigest()) print(hashlib.sha256(data).hexdigest())
On Linux, you can hash a string from the shell with printf. Avoid echo here, because it adds a newline and changes the hash:
printf 'hello' | sha256sum
On macOS, use shasum -a 256 instead of sha256sum.
Pitfalls that cause wrong hashes
- A trailing newline changes the result. echo 'hello' hashes hello plus a newline. Use printf or strip the newline before hashing.
- Hash bytes, not characters. UTF-8 is the usual choice. The same string encoded as UTF-16 produces a different digest.
- Compare like with like. A hex digest and a base64 digest of the same bytes look different but match when decoded.
- The empty string has a fixed SHA-256 digest, e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855. If you see this value, your code is probably hashing an empty input by mistake.
Choosing the right hash
- Verifying a download: do not rely on a checksum published on the same page as the file. An attacker who can replace the download can often replace the checksum too, and the same is true for SHA-256. Instead, get the checksum or a signature through a separate trusted channel, such as a signed release announcement or a package manager's signed metadata. Better still, verify a cryptographic signature (for example GPG or a code-signing signature) made with a key you already trust.
- New digital signatures or fingerprints: use SHA-256 or a longer SHA-2 variant such as SHA-384 or SHA-512.
- Message authentication: use HMAC, not a bare hash of a key concatenated with a message. In Node.js: crypto.createHmac('sha256', key).update(message).digest('hex').
- Password storage: do not use any plain hash, SHA-256 included, because it is too fast to resist guessing. Use a password hashing function such as Argon2, scrypt or bcrypt from a well-tested library.
- Matching an existing system: use the algorithm the other side requires, and document the risk if that algorithm is SHA-1.
FAQ
Is SHA-1 still safe for checksums?
For catching accidental corruption, SHA-1 still detects most random errors. The problem is tampering: if someone can alter the file and the checksum, SHA-1 no longer gives you a reliable guarantee. Use SHA-256 for checksums you publish or rely on, and deliver the checksum through a channel the attacker cannot change.
Is SHA-256 the same as SHA-2?
SHA-256 is one member of the SHA-2 family, which also includes SHA-224, SHA-384 and SHA-512. SHA-3 is a separate design, not a successor that replaces SHA-2.
Why is a SHA-256 hash 64 characters long?
A SHA-256 digest is 256 bits. Each hex character represents 4 bits, so 256 divided by 4 gives 64 characters.
Should I switch existing SHA-1 data to SHA-256?
If the SHA-1 values protect against tampering, yes: plan a migration and recompute with SHA-256 where you control both sides. If they only detect accidental changes or serve as internal cache keys, the urgency is lower.
Hash Generator
Generate SHA-256, SHA-384, SHA-512 and SHA-1 hashes.