Hashing explained (MD5, SHA-1, SHA-256, password hashing)
What hash functions do, which algorithms are still safe, and why passwords need Argon2id, bcrypt, scrypt or PBKDF2 instead of a plain hash.
By the tools4free.dev team · Last reviewed 4 December 2026
A hash function turns any input into a fixed-length fingerprint. The same input always gives the same output, a tiny change gives a completely different output, and you cannot work backwards from the fingerprint to the input. Use SHA-256 (or SHA-3) to check file integrity, and never use MD5 or SHA-1 where an attacker could craft a collision. For storing passwords, do not use a plain hash at all: use a slow, salted password-hashing function such as Argon2id, bcrypt, scrypt or PBKDF2. This guide explains why, with real outputs you can verify.
What a hash function does
A cryptographic hash function takes input of any length and produces a digest of fixed length. Good ones have these properties:
- Deterministic. The same input always gives the same digest.
- Fixed size. SHA-256 always gives 256 bits (64 hexadecimal characters), whether you hash one letter or a whole movie.
- Preimage resistance. Given a digest, it is infeasible to find any input that produces it.
- Second-preimage resistance. Given one input, it is infeasible to find a different one with the same digest.
- Collision resistance. It is infeasible to find any two different inputs with the same digest.
- Avalanche effect. A one-bit change in the input changes about half of the output bits.
Hashing is not encryption. There is no key and no way to decrypt. You can only guess an input and compare.
Worked example 1: seeing the avalanche effect
These are real SHA-256 digests of two inputs that differ by one letter's case:
SHA-256("hello") = 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
SHA-256("Hello") = 185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969
Comparing them bit by bit, 125 of the 256 bits differ, close to the expected half. There is no pattern that tells you the two inputs were nearly identical. For reference, the digest of the empty string is e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855, and the digest of "abc" begins ba7816bf8f01cfea, which is the standard test vector from the SHA-2 specification.
The common algorithms
| Algorithm | Output | Status | Reasonable use |
|---|---|---|---|
| MD5 | 128 bits | Broken for collision resistance | Non-security checksums only, such as detecting accidental corruption |
| SHA-1 | 160 bits | Broken for collision resistance; a practical collision was shown in 2017 | Legacy compatibility only, for example in older Git object names |
| SHA-256, SHA-512 | 256 and 512 bits | No practical attacks (SHA-2 family) | Integrity, signatures, HMAC, fingerprints |
| SHA-3 | 224 to 512 bits | No practical attacks; different internal design from SHA-2 | The same as SHA-2, when a different construction is wanted |
| Argon2id, scrypt, bcrypt, PBKDF2 | Configurable | Designed to be slow and salted | Password storage and key derivation |
MD5 collisions were demonstrated in 2004 and have since become cheap to generate. That has been used to make two different files, or even two different certificates, with the same MD5. SHA-1 followed with the "SHAttered" collision in 2017. Both still behave like hash functions for random accidental changes, which is why you still see MD5 checksums beside downloads, but a checksum posted next to a file protects you from corruption only, not from an attacker who controls both.
What hashing is good for
Verifying a download
Publish a SHA-256 digest beside a file. The user hashes their copy and compares. If the digest is delivered over a channel the attacker cannot change, a match shows the file is intact and the one intended. Compare the entire digest, not the first few characters.
Detecting changes and deduplication
Backup tools and caches hash content to tell whether it has changed or already exists.
Message authentication (HMAC)
A plain hash of a secret and a message is not safe to use for authentication with the old Merkle-Damgard algorithms, because of length-extension attacks. Use HMAC, which mixes a key into the hash in a safe way. For example, HMAC-SHA-256 with the key key over The quick brown fox jumps over the lazy dog is f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8.
Digital signatures
Signing schemes sign the hash of a document rather than the document. That makes collision resistance essential, which is why SHA-1 certificates were retired.
Why a plain hash is wrong for passwords
It is tempting to store SHA-256(password). That is a serious mistake, for two reasons.
- Hashes are fast. A general-purpose hash is built to be quick, which helps attackers. With a stolen table of digests, specialised hardware can test billions of candidates per second.
- Identical passwords give identical digests. Without a salt, one precomputed list of common passwords cracks every user at once, and anyone with the same password is visible as a duplicate.
The remedies are a unique random salt per user, stored beside the hash, and a deliberately expensive algorithm with a tunable cost. Purpose-built password hashing functions do both:
- Argon2id is the usual first choice. It is memory-hard, which makes parallel hardware attacks costly.
- scrypt is also memory-hard.
- bcrypt is long established and widely available, though it limits input to 72 bytes and uses little memory.
- PBKDF2 is acceptable where compliance requires it (it is approved in NIST guidance), with a high iteration count.
OWASP's Password Storage Cheat Sheet lists current recommended parameters. As an indication, at the time of writing it suggests Argon2id with at least 19 MiB of memory, 2 iterations and 1 degree of parallelism, and PBKDF2-HMAC-SHA256 with 600,000 iterations. These numbers rise as hardware improves, so check the cheat sheet rather than copying figures from an article.
Worked example 2: the same password, two storage designs
Two users both choose the password password. Naive design: the database stores the SHA-256 of each password, which for this password is 5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8 for both rows. An attacker who steals the table sees two identical values, looks the value up in a list of precomputed hashes of common passwords, and recovers both passwords in one step.
Better design: Argon2id with a random 16-byte salt per user. Now the two rows store different strings, even though the passwords match, because each includes its own salt. A precomputed table is useless, and each guess costs the attacker the full memory and time cost, so they can only try a small fraction as many candidates per second. Strong, unique passwords then become practical protection, as the password entropy guide explains.
Note also that a stored hash should be compared in constant time where the library offers it, and that most frameworks provide a ready-made password-hashing function. Use that rather than assembling your own from hash primitives.
Common mistakes
- Storing passwords with MD5, SHA-1 or a single round of SHA-256, with or without a salt.
- Using one global salt (sometimes called a pepper by mistake) for every user. A salt must be unique per password. A pepper, a secret held outside the database, is an extra layer and not a substitute.
- Inventing a scheme, such as hashing a hash ten times, instead of using a vetted function.
- Relying on MD5 or SHA-1 for signatures, certificates or anything where an attacker picks the input.
- Calling hashing "encryption" and assuming a hash can be reversed. It cannot, though weak inputs can be guessed.
- Hashing small, guessable values such as phone numbers or email addresses and assuming it makes them anonymous. An attacker can hash every possible value.
- Comparing hex digests in a different case or with stray whitespace, then concluding the file was altered.
- Choosing a work factor once and never revisiting it as hardware improves.
Frequently asked questions
Can a hash be reversed?
Not by calculation. But if the original input came from a small set, an attacker can hash every candidate and compare, which is why weak passwords are cracked even though the algorithm is one-way.
Is SHA-256 safe for passwords if I add a salt?
It is better than nothing, but it is still too fast. Use a password-hashing function designed to be slow, such as Argon2id or bcrypt.
Why is MD5 still around?
It is fast and ubiquitous, and it works well for catching accidental corruption. It must not be used where someone could attack the integrity deliberately.
What is a collision?
Two different inputs that produce the same digest. With a 256-bit hash, finding one by chance is out of reach, but a weakness in an algorithm, as with MD5 and SHA-1, lets an attacker find them on purpose.