SHA-256 is one of those primitives you end up using whether you planned to or not. Git commits, TLS certificates, blockchain, file integrity checks, API signatures — they all lean on it. Most developers know it produces a hash, fewer know exactly what guarantees it gives and where it falls short. Here's the practical version.
What SHA-256 actually does
SHA-256 is a cryptographic hash function from the SHA-2 family. Feed it any input — a byte, a password, a 4GB video — and it spits out a fixed 256-bit value, usually written as 64 hexadecimal characters. The same input always yields the same output, and even a one-bit change produces a completely different hash.
Three properties matter in practice:
- Deterministic: the same bytes always hash to the same value.
- One-way: you can't feasibly reverse the hash back to the original input.
- Collision-resistant: finding two different inputs that hash to the same value is computationally infeasible.
That last point is why SHA-256 replaced MD5 and SHA-1 for anything security-sensitive. MD5 and SHA-1 both have practical collision attacks. SHA-256 does not, and there's no credible threat to it today.
When you'd reach for it
File and data integrity
Download a Linux ISO and you'll often see a published SHA-256 checksum. Hash your copy, compare the two strings, and you know the file wasn't corrupted or tampered with in transit. Same idea for deduplication, cache keys, or detecting whether a config file changed.
Signatures and message authentication
SHA-256 is the hashing half of HMAC-SHA256, which webhooks (Stripe, GitHub, Shopify) use to let you verify a payload really came from them. It's also the digest used in most JWT signing and in TLS certificate chains.
Content addressing
Git identifies objects by hashing their contents. IPFS, Docker image layers, and many content-addressable stores do the same. If two things have the same hash, you treat them as identical — no need to store duplicates.
When NOT to use it
This is where people get burned. SHA-256 is fast, and for password storage that's exactly the wrong property. If an attacker gets your database, raw-SHA-256 passwords can be brute-forced with billions of guesses per second on commodity GPUs.
For passwords, use a purpose-built slow hash: bcrypt, scrypt, or Argon2. These are deliberately expensive and support per-user salts and tunable work factors. Plain SHA-256 — even salted — is not good enough for credentials in 2024.
Common gotchas
- Encoding matters. "Hash the string" is ambiguous. Hashing the UTF-8 bytes of
cafégives a different result than hashing its Latin-1 bytes. Always be explicit about encoding. - Hex vs Base64 vs raw bytes. The hash is 32 bytes. Hex gives 64 characters, Base64 gives 44. If two systems disagree on representation, your comparison fails even when the underlying hash is identical.
- Trailing newlines and whitespace. A file that ends in
\nhashes differently from one that doesn't. Many "my hash doesn't match the website" bugs are a stray newline. - Timing attacks on comparison. When verifying a signature, compare hashes with a constant-time function (like
crypto.timingSafeEqualin Node orhmac.compare_digestin Python), not===. - SHA-256 is not encryption. It's one-way. You can't "decrypt" a hash. If a vendor claims to recover your data from a hash, they're lying or storing it elsewhere.
A short worked example
Say you're verifying a downloaded file against a published checksum. In Node.js:
const crypto = require('crypto');
const fs = require('fs');
function sha256File(path) {
const buffer = fs.readFileSync(path);
return crypto.createHash('sha256').update(buffer).digest('hex');
}
const expected = 'e3b0c44298fc1c149afbf4c8996fb924...';
const actual = sha256File('./ubuntu.iso');
if (actual === expected) {
console.log('Checksum OK');
} else {
console.error('Mismatch — do not trust this file');
}Hashing a plain string is even simpler, and notice how one character flips the entire output:
crypto.createHash('sha256').update('hello', 'utf8').digest('hex');
// 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
crypto.createHash('sha256').update('Hello', 'utf8').digest('hex');
// 185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969In Python the equivalent is just as terse:
import hashlib
hashlib.sha256('hello'.encode('utf-8')).hexdigest()
# 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824When you just need to check a value by hand — confirming a checksum, debugging why two services disagree, or testing a webhook signature — a browser tool beats spinning up a REPL. Our SHA-256 Hash Generator hashes text instantly and runs the computation in your browser, so you can paste in sensitive strings without them leaving your machine.
A note on empty input
One value worth memorising: the SHA-256 of an empty string is e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855. If you ever see that hash where you expected real data, something upstream handed you nothing — an unread buffer, a failed fetch, a null body. It's a surprisingly handy debugging tell.
The short version
Use SHA-256 for integrity, content addressing, and as a building block in HMAC and signatures. Don't use raw SHA-256 for passwords — reach for Argon2 or bcrypt instead. Be precise about encoding and output format, because that's where almost every mismatch comes from. And compare hashes in constant time when a security decision rides on the result.
Get those few things right and SHA-256 is one of the most reliable tools in your kit — fast, well-understood, and not going anywhere for a long time.
Photo by Mohamed Marey on Unsplash