How SHA-256 Hashing Works
4 min read · Published
Table of contents
SHA-256 is a cryptographic hash function: it takes input of any length and produces a fixed 256-bit (32-byte) digest, usually written as 64 hexadecimal characters. It belongs to the SHA-2 family standardised by NIST (FIPS 180-4) and underpins TLS certificates, Git object IDs in newer repositories, software checksums and Bitcoin. Compute one in the Hash Generator.
SHA-256("hello") =
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
What a good hash function guarantees
- Deterministic: the same input always yields the same digest.
- Fixed size: one byte or one gigabyte in, 256 bits out.
- Fast to compute for any input.
- Pre-image resistance: given a digest, finding an input that produces it is infeasible.
- Second pre-image resistance: given an input, finding a different input with the same digest is infeasible.
- Collision resistance: finding any two inputs with the same digest is infeasible (about 2^128 work for SHA-256).
- Avalanche effect: flipping one input bit changes about half of the output bits.
SHA-256("hello") = 2cf24dba5fb0a30e...
SHA-256("hellp") = 2e1f4c5b8d5e7d1c... (completely different)
How the algorithm works, in outline
- Pad the message: append a
1bit, then zeros, then the original length as a 64-bit number, so the total is a multiple of 512 bits. - Split into 512-bit blocks.
- Start with eight 32-bit working values (initial hash values derived from the square roots of the first eight primes).
- For each block, expand it into a 64-word message schedule using bit rotations and shifts (the σ0/σ1 functions).
- Run 64 rounds. Each round mixes the working values with one schedule word and one round constant (derived from cube roots of the first 64 primes) using bitwise operations (
Ch,Maj, rotations) and addition modulo 2^32. - Add the round output back into the running hash values.
- After the last block, concatenate the eight values: that is the digest.
The design makes every output bit depend on every input bit through many rounds of non-linear mixing, which is why the digest looks random and cannot be run backwards.
Hashing in code
// Browser or Node 19+
async function sha256(text) {
const bytes = new TextEncoder().encode(text);
const digest = await crypto.subtle.digest('SHA-256', bytes);
return [...new Uint8Array(digest)].map((b) => b.toString(16).padStart(2, '0')).join('');
}
import hashlib
hashlib.sha256(b"hello").hexdigest()
echo -n "hello" | sha256sum # -n avoids hashing a trailing newline!
The trailing newline is a classic mistake: echo "hello" hashes six bytes, not five.
Common uses
- File integrity: publish a checksum next to a download; users hash the file and compare.
- Content addressing and caching: name files by hash for deduplication and cache busting.
- Digital signatures and certificates: the signature is computed over a hash of the data.
- HMAC: combines a secret key with SHA-256 to authenticate messages; this is how HS256 JWTs are signed.
- Blockchains and proof of work.
Hashing is not encryption
Encryption is reversible with a key. A hash is one-way: there is no key and no decryption. People sometimes say they "decrypt" a hash, but what actually happens is guessing inputs until one matches. Likewise, Base64 is just an encoding and offers no protection at all.
Why plain SHA-256 is wrong for passwords
SHA-256 is designed to be fast. A modern GPU can compute billions per second, so attackers can test enormous lists of common passwords against a stolen hash. Also, without a unique salt, identical passwords produce identical hashes, enabling precomputed "rainbow table" attacks.
For passwords use a slow, salted, memory-hard algorithm:
- Argon2id (recommended), scrypt or bcrypt
- a unique random salt per user (handled by those libraries)
- a tuned work factor that you raise over time
To create strong passwords or secrets in the first place, try the Password Generator.
Other algorithms
| Algorithm | Digest | Status |
|---|---|---|
| MD5 | 128 bit | Broken; collisions are easy. Checksums only. |
| SHA-1 | 160 bit | Broken for signatures (practical collisions). |
| SHA-256 / 384 / 512 | 256 / 384 / 512 bit | Secure. |
| SHA-3 | variable | Secure; a different design from SHA-2. |
| BLAKE3 | 256 bit | Secure and very fast. |
SHA-256 is vulnerable to length-extension attacks when misused as SHA256(secret || message); use HMAC instead.
Quick checklist
- Use SHA-256 or better for integrity and fingerprints.
- Use HMAC for message authentication.
- Use Argon2id/bcrypt/scrypt for passwords.
- Compare digests with a constant-time comparison on the server.
- Make sure both sides hash the same bytes (encoding, newlines, normalisation).
Related tools
Hash Generator
Generate SHA-256, SHA-384 and SHA-512 hashes with Web Crypto.
Password Generator
Generate strong random passwords with secure randomness.
Base64 Encoder/Decoder
Encode text to Base64 or decode it back, with full Unicode support.
FAQ
Can SHA-256 be reversed?
Not by computation from the digest alone. Attackers can only guess inputs and compare hashes.
How long is a SHA-256 hash?
256 bits: 32 bytes, or 64 hexadecimal characters.
Is SHA-256 safe for passwords?
Not on its own. It is too fast. Use Argon2id, scrypt or bcrypt with a unique salt.
Keep reading
- Base64 Encoding Explained
How Base64 works, why it exists, how much it inflates data, URL-safe Base64, Unicode pitfalls and when not to use it.
- JWT Explained for Beginners
A beginner-friendly explanation of JSON Web Tokens: structure, claims, signing, expiry, and the security mistakes to avoid.