Hashing with hashlib
Published by Brayan K
Hashing turns any data into a short, fixed-size fingerprint called a digest, and hashlib is Python's standard module of one-way hash functions for producing it.
Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
Hash bytes, then choose a representation
A SHA-256 digest is a fixed-size summary of a byte sequence. The same bytes give the same digest, so a program can compare a received payload with a trusted expected checksum without storing the original payload alongside it. The hash object is the running calculation; digest() and hexdigest() retrieve its result in different representations.
import hashlib
# The b prefix makes this a bytes value, not a text string.
calculation = hashlib.sha256(b"tea")
raw = calculation.digest()
hex_text = calculation.hexdigest()
print(len(raw))
print(len(hex_text))
print(hex_text)
print(raw.hex() == hex_text)
# ✅ Expected output:
# 32
# 64
# a9f74d1ec36ebdeb2da3f6e5868090cd2a2d20b3dcca7b62f60304b1d3d9ef42
# TrueSHA-256 produces 32 bytes. Hexadecimal uses two characters per byte, so its text representation has 64 characters; it is not a stronger second hash. Calling hexdigest does not destroy the object or clear the data already fed into it. Avoid printing the hash object itself, whose representation identifies an object rather than the digest.
Text must be encoded before it can be hashed
Hash functions consume bytes-like data. Passing a Python string directly is an error:
import hashlib
hashlib.sha256("tea")
# ✅ Expected error:
# TypeError: Strings must be encoded before hashingEncode the text deliberately, using the same encoding as the producer. UTF-8 is a common text boundary, but a file checksum should normally hash the original bytes rather than decode and re-encode them.
import hashlib
text = "café"
payload = text.encode("utf-8")
print(len(text), len(payload))
print(hashlib.sha256(payload).hexdigest())
# ✅ Expected output:
# 4 5
# 850f7dc43910ff890f8879c0ed26fe697c93a067ad93a7d50f466a7028a9bf4eThe character count and byte count differ. Even visually similar text can have different byte representations, and a trailing newline is data too. Normalizing or stripping content changes what the checksum covers.
import hashlib
plain = hashlib.sha256(b"tea").hexdigest()
with_newline = hashlib.sha256(b"tea" + bytes([10])).hexdigest()
print(plain == with_newline)
print(len(plain) == len(with_newline))
# ✅ Expected output:
# False
# TrueText encoding and stream modes are taught in Working with Files, Streams & Large Datasets. Here the important decision is which exact bytes the digest is meant to describe.
update accumulates input and returns None
A hash object remembers all the bytes fed into it. Updating with two chunks has the same effect as hashing their concatenation. It does not hash each chunk separately, and update does not return a new hash object.
import hashlib
calculation = hashlib.sha256()
result = calculation.update(b"tea")
calculation.update(b" and milk")
print(result)
print(calculation.hexdigest() == hashlib.sha256(b"tea and milk").hexdigest())
print(calculation.hexdigest() == calculation.hexdigest())
# ✅ Expected output:
# None
# True
# TrueKeep update on its own line. Assigning its return value back to the variable replaces your hash object with None:
import hashlib
calculation = hashlib.sha256()
calculation = calculation.update(b"tea")
calculation.hexdigest()
# ✅ Expected error:
# AttributeError: 'NoneType' object has no attribute 'hexdigest'The repair is to remove the assignment, then call hexdigest on the original object. Also create a fresh hash object for each independent payload; reusing one without resetting it hashes the combined history.
Copy an intermediate calculation before branching
Reading a digest does not reset the object. If two messages share a prefix, copy the current hash state before adding their different suffixes. Assignment alone would give the same mutable calculation another name.
import hashlib
prefix = hashlib.sha256(b"order:")
tea = prefix.copy()
tea.update(b"tea")
milk = prefix.copy()
milk.update(b"milk")
print(tea.hexdigest() == hashlib.sha256(b"order:tea").hexdigest())
print(milk.hexdigest() == hashlib.sha256(b"order:milk").hexdigest())
print(prefix.hexdigest() == hashlib.sha256(b"order:").hexdigest())
# ✅ Expected output:
# True
# True
# TrueEach copy owns its subsequent state, and the prefix object remains available for another branch. The order and boundaries of messages still matter: concatenating fields without an unambiguous encoding can make different field lists produce the same byte string before hashing even begins.
Read a stream in chunks instead of loading it all
To hash a large file, update one calculation with each byte chunk until read returns empty bytes. The in-memory BytesIO below behaves like a binary stream and makes the example runnable without a filesystem path. A real binary file opened with "rb" can be consumed by the same loop.
import hashlib
from io import BytesIO
payload = b"tea and milk"
stream = BytesIO(payload)
calculation = hashlib.sha256()
chunks = 0
chunk = stream.read(4)
while chunk:
calculation.update(chunk)
chunks += 1
chunk = stream.read(4)
print(chunks)
print(calculation.hexdigest() == hashlib.sha256(payload).hexdigest())
# ✅ Expected output:
# 3
# TrueOnly the current chunk needs to be held for the hash calculation. The fixture itself already stores the whole payload to make comparison possible; that is a teaching choice, not a claim that this particular snippet saves memory. Start at the stream position whose remaining bytes you intend to hash.
A matching checksum needs a trustworthy reference
A checksum detects a mismatch with an expected digest. If an attacker can replace both a download and its posted digest, an ordinary hash does not establish who supplied the content. Obtain the expected value through a trusted channel or use an authenticated mechanism suited to the system.
For shared-secret message authentication, Python provides HMAC. The following fixed key is public demonstration data, not a usable application secret. Both parties in a real HMAC system must protect their shared key; publishing it would remove the authentication guarantee.
import hmac
demo_key = b"public teaching example only"
message = b"quantity=3"
tag = hmac.digest(demo_key, message, "sha256")
same = hmac.digest(demo_key, b"quantity=3", "sha256")
changed = hmac.digest(demo_key, b"quantity=4", "sha256")
print(hmac.compare_digest(tag, same))
print(hmac.compare_digest(tag, changed))
# ✅ Expected output:
# True
# FalseUse compare_digest for authentication-tag comparisons, following the HMAC reference, rather than writing a character-by-character comparison. Compare bytes with bytes or ASCII hexadecimal text with ASCII text; do not mix the two representations. This avoids content-based early exits, but is not a promise to hide mismatched lengths or types. HMAC authenticates possession of a shared key; it does not encrypt the message or provide a public-key signature.
A plain fast SHA-256 digest is also not a password-storage scheme. Password verification needs a password-hashing design with a salt and an appropriate work factor; follow the hashlib password-derivation guidance and the maintained authentication library for your application. This lesson does not implement a login system.
A salt is public input, not an encryption key
A password-derived value is recomputed during verification, not decrypted. A salt gives each password record a separate input so equal passwords need not have equal stored values. Store the salt with the derived value and the algorithm's parameters; it does not need to be secret. A real system generates a fresh random salt for each record, rather than reusing a username or the fixed teaching bytes below.
import hashlib
import hmac
# Fixed public fixtures and a low iteration count keep this demonstration repeatable.
# They are not production password-storage settings.
DEMO_SALT_A = b"public-salt-A-001"
DEMO_SALT_B = b"public-salt-B-002"
DEMO_ITERATIONS = 1000
def derive(password, salt):
return hashlib.pbkdf2_hmac(
"sha256", password.encode("utf-8"), salt, DEMO_ITERATIONS, dklen=32
)
stored = derive("tea and milk", DEMO_SALT_A)
print("bytes:", len(stored))
print("correct:", hmac.compare_digest(stored, derive("tea and milk", DEMO_SALT_A)))
print("wrong:", hmac.compare_digest(stored, derive("tea and sugar", DEMO_SALT_A)))
print("different salt:", hmac.compare_digest(stored, derive("tea and milk", DEMO_SALT_B)))
# ✅ Expected output:
# bytes: 32
# correct: True
# wrong: False
# different salt: FalsePBKDF2 repeats work to make each password guess more costly; the salt does not replace that work factor or make a weak password unguessable. The deliberately low demonstration count is not a deployment recommendation. Use a maintained authentication library's current password-hashing scheme and parameters for an application, and follow the Python key-derivation guidance for this API's inputs.
Keep the three purposes separate: an ordinary checksum compares bytes with a trusted reference; HMAC authenticates a message using a secret shared key; a password-derived value checks a guess using stored salt and parameters. None of these encrypts the original data. HMAC also does not prevent replay by itself: an old valid message still has a valid tag unless the surrounding protocol rejects it.
Complete a digest from two text pieces
Encode the first piece, add the second as bytes and retrieve the hexadecimal result. The comparison proves that the two updates cover the same bytes as one complete message.
import hashlib
calculation = hashlib.sha256("tea".___("utf-8"))
# 👉 replace ___ with encode
calculation.___(b" and milk")
# 👉 replace ___ with update
actual = calculation.___()
# 👉 replace ___ with hexdigest
expected = hashlib.sha256(b"tea and milk").hexdigest()
print(actual == expected)
print(len(actual))
# ✅ Expected output:
# True
# 64Check received content against a trusted fixture
Create expected SHA-256 digests for the original byte payloads alpha: b"tea" and beta: b"milk". Compare received alpha containing b"tea" and received beta containing b"milks". Print each label with "match" or "changed", in sorted label order. These labels are identifiers, not files to open. The fixture stands in for a trusted manifest; do not derive the expected hashes from the received data.
import hashlib
originals = {"alpha": b"tea", "beta": b"milk"}
expected = {
name: hashlib.sha256(payload).hexdigest()
for name, payload in originals.items()
}
received = {"alpha": b"tea", "beta": b"milks"}
for name in sorted(expected):
actual = hashlib.sha256(received[name]).hexdigest()
status = "match" if actual == expected[name] else "changed"
print(name, status)
# ✅ Expected output:
# alpha match
# beta changedThe check compares received bytes with a previously chosen reference. Rebuilding expected from received would make every comparison pass while checking nothing. This brief assumes the same labels on both sides; a download verifier must also report missing and unexpected entries.
Practice quiz
What does this program print? import hashlib print(len(hashlib.sha256(b"x").hexdigest()))
- 32
- 64
- 256
- 16
Answer: 64. SHA-256 has 32 digest bytes, represented by 64 hexadecimal characters.
What does this program print? import hashlib h = hashlib.sha256() print(h.update(b"x"))
- x
- True
- sha256
- None
Answer: None. update mutates the calculation and returns None.
What does this program print? import hashlib h = hashlib.sha256(b"a") h.update(b"b") print(h.digest() == hashlib.sha256(b"ab").digest())
- True
- False
- None
- TypeError
Answer: True. Successive updates hash the concatenation of their bytes.
What does this program print? import hashlib print(hashlib.sha256(b"x").digest() == hashlib.sha256(b"x" + bytes([10])).digest())
- True
- None
- False
- TypeError
Answer: False. A newline is an additional input byte; these payloads have different digests.
What does this program print? import hashlib h = hashlib.sha256(b"a") c = h.copy() c.update(b"b") print(h.digest() == hashlib.sha256(b"a").digest())
- False
- True
- None
- TypeError
Answer: True. The copied calculation changes independently; the original still covers only a.
Which information should be retained with a password-derived value so a later guess can be checked?
- The plaintext password and nothing else
- Only a secret salt that is thrown away
- An HMAC tag with no algorithm identifier
- The salt, algorithm identifier and work parameters
Answer: The salt, algorithm identifier and work parameters. Verification repeats the same derivation using the stored parameters. The salt is public per-record input, not a key for decrypting the password.
A download and its checksum were both replaced by an attacker. What does a matching plain hash establish?
- The bytes match that supplied checksum, not who supplied them.
- The publisher must be authentic.
- The file is encrypted.
- The content is safe to execute.
Answer: The bytes match that supplied checksum, not who supplied them.. An ordinary checksum cannot authenticate an untrusted reference.
Which comparison is appropriate for an externally supplied HMAC tag?
- Compare only the first character.
- Print both tags and inspect their length.
- Use hmac.compare_digest with compatible types.
- Hash the username with Python hash().
Answer: Use hmac.compare_digest with compatible types.. compare_digest is designed for verification comparisons; a partial or length-only comparison does not verify the tag.
Continue this course
- Previous: Running Commands with subprocess
- Next: Testing with unittest — Write TestCase classes with asserts, setUp/tearDown, and assertRaises
- Quick reference: Python cheat sheet