1import hashlib, os2a = hashlib.sha256(b"hello")3print(a.hexdigest())4b = hashlib.sha256(b"Hello")5print(b.hexdigest())6salt = os.urandom(16)7key = hashlib.pbkdf2_hmac(8 "sha256", b"tulip-42",9 salt, 600_000)b"hello"SHA-2560–12–34–56–7os.urandom2cf24dba…9824user amiraMATCH?01✗
The program, the input bytes, the hash function and the digestPublished digest and match lamp; salt maker and user databaseTokens: input bytes, digest, salt, stored keyOn the left, a nine-line Python program. SHA-256 turns the bytes hello into a 32-byte digest; the figure shows its first 8 bytes. Comparing it with the published digest proves a download is unchanged, and one changed letter flips 125 of the 256 bits. For passwords, a random salt and 600,000 rounds of PBKDF2 make the stored key slow to guess; at login the same lines run again and the results are compared. 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.

What a hash is

  1. 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).

Checking data

Storing passwords

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.