What a hash is
A hash function turns any input, one word or a whole film, into a short fingerprint called a digest. SHA-256 always gives 256 bits: 32 bytes, written as 64 hex characters (0 to 9, a to f).
Line 1 loads Python's hashlib module. Line 2 feeds the bytes b"hello" into SHA-256. The b means bytes: hash functions work on bytes, so text must be turned into bytes first.
Line 3 prints the digest in hex: 2cf24dba…9824, 64 characters. Run it on any computer, any day, and hello gives exactly this digest. Hash functions are deterministic: same input, same output.
Checking data
Pretend hello is a file you downloaded. The maker publishes its SHA-256. You hash your copy and compare. Same digest: the file arrived unchanged. CISA advises downloading only from the vendor's own site.
Lines 4 and 5 change one letter: Hello. That is one bit of input, yet 125 of the 256 output bits flip. The digest, 185f8db3…1969, looks unrelated and no longer matches. Any change to a file shows up.
Nobody can run SHA-256 backwards to get hello from its digest; they can only guess inputs and hash each guess. Finding two inputs with the same digest, a collision, must also be out of reach. Hashing is not encryption: there is no key.
Older hashes have failed. Researchers have made real collisions for MD5 and SHA-1, so a forged file could share a digest with a genuine one. NIST says to phase out SHA-1 by 2030. Use SHA-256 or SHA-3.
Storing passwords
A site must never store your password. It stores a hash of it, mixed with a salt: random bytes made fresh for each user. Line 6 makes 16, as NIST advises. Same passwords then hash differently, and precomputed tables become useless.
OWASP warns that plain SHA-256 is too fast for passwords: a thief with the hashes can test guesses quickly. PBKDF2 repeats the hash 600,000 times, OWASP's setting, so every guess costs far more. OWASP's first choice is Argon2id.
At login the site runs lines 7 to 9 again with the typed password and the saved salt, then compares the result with the saved key. Equal means the password was right. Python's hmac.compare_digest does the comparing safely.
What happens
;; step 1/10 · idea hash function any bytes in → fixed-size digest out SHA-256 FIPS 180-4, part of the SHA-2 family digest size 256 bits = 32 bytes = 64 hex characters input size a few bytes or many gigabytes: same size out
The figure shows only the first 8 of the 32 digest bytes, and it uses a fixed example salt (00 01 … 0f) so its numbers can be checked. Real code must use a fresh random salt for every user, as line 6 does. A digest only proves a file matches the published value. If an attacker can change both the file and the page listing its digest, the check passes. That is why the digest must come from a trusted channel, and why software makers also sign their releases.