← ACARA Digital Technologies Years 9–10

Hashing text with hashlib in Python

Python code that turns text into bytes, hashes it with SHA-256 and returns a fixed-length hex string. Same input gives the same hash; one changed letter gives a new hash.

Understanding Hashing text with hashlib in Python in ACARA Digital Technologies Years 9–10

What it does: Hashing both texts and comparing with == shows whether they are identical, because the same input always gives the same hash. hexdigest() returns the hash as a 64-character hex string, which print() can show. Python uses the import keyword to load a module such as hashlib before you can use its functions. encode() converts a string into bytes. sha256() needs bytes, not a normal string. hexdigest() is used later, to get the result. Changing even one letter gives a totally different hash. You cannot tell the inputs were similar by looking at the hashes. SHA-256 always produces a fixed-length output, no matter how short or long the input is.

Common mistakes: The module is called hashlib. 'hashlb' is misspelt, so Python raises a ModuleNotFoundError. It should be import hashlib. Text must be inside quotes. Without them Python treats blue7 as a variable name and gives a NameError. It should be pw = "blue7".

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 876–944) and word search (Modules 860–944).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line must come first in a Python program before you can use hashlib.sha256()?
include hashlibimport hashlib
Matches
hashlib.sha256('cat').hexdigest()
↓
The text was not turned into bytes with encode(), so Python raises a TypeError. It should be 'cat'.encode().
Applying
Meanings
Term
hashlib.sha256(text.encode()).hexdigest()
Meaning
Python code that turns text into bytes, hashes it with SHA-256 and returns a fixed-length hex string. Same input gives the same hash; one changed letter gives a new hash.
Practice
import hashlib
Testing
Gaps
import
Check Answer
Spots
1import hashlb
2text = "cat"

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Red Panda' badge — Forest Explorer, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which line must come first in a Python program before you can use hashlib.sha256()?
A. include hashlib
B. import hashlib ✓
Python uses the import keyword to load a module such as hashlib before you can use its functions.
2. Which code correctly turns the text in the variable text into bytes for hashing?
A. text.encode() ✓
B. text.hexdigest()
encode() converts a string into bytes. sha256() needs bytes, not a normal string. hexdigest() is used later, to get the result.
3. Which call hashes a string and gives back the result as hexadecimal text?
A. hashlib.sha256(text).hexdigest()
B. hashlib.sha256(text.encode()).hexdigest() ✓
Without encode(), Python raises a TypeError because sha256() expects bytes rather than a string.
4. You hash the word 'cat' twice using SHA-256. What do you get?
A. The exact same hash both times ✓
B. A different hash each time
Hashing is deterministic. The same input always gives the same hash, which is why hashes can be compared.
5. You hash 'password' and then 'Password' (capital P). What happens?
A. The two hashes are completely different ✓
B. The two hashes are almost the same, with one character different
Changing even one letter gives a totally different hash. You cannot tell the inputs were similar by looking at the hashes.

Match the Pairs

import hashlib → Loads Python's built-in hashing module so you can use hash functions like SHA-256
text.encode() → Turns a string into bytes, which is the form the hash function needs as input
The same input always gives the same hash, so two hashes can be compared to check that text matches → hashlib.sha256(a.encode()).hexdigest() == hashlib.sha256(b.encode()).hexdigest()
Turn the finished SHA-256 hash into a fixed-length hexadecimal string you can print → print(hashlib.sha256('cat'.encode()).hexdigest())
hashlib.sha256('cat').hexdigest() → The text was not turned into bytes with encode(), so Python raises a TypeError. It should be 'cat'.encode().
hashlib.sha256('cat'.encode()) → hexdigest() is missing, so this gives a hash object, not the readable hex text. Add .hexdigest() at the end.

Key Term

hashlib.sha256(text.encode()).hexdigest()
Python code that turns text into bytes, hashes it with SHA-256 and returns a fixed-length hex string. Same input gives the same hash; one changed letter gives a new hash.

Fill in the Blanks

1. import hashlib
Hint: Type the name of the Python module that provides hash functions.
2. d = hashlib.sha256(s.encode()).hexdigest()
Hint: Type the name of the hash algorithm that gives a 64-character hex result.
3. d = hashlib.sha256(s.encode()).hexdigest()
Hint: Type the string method that turns text into bytes before hashing.
4. d = hashlib.sha256(s.encode()).hexdigest()
Hint: Type the method that returns the hash as readable hexadecimal text.
5. h1 = hashlib.sha256(s.encode()).hexdigest() h2 = hashlib.sha256(t.encode()).hexdigest() print(h1 == h2)
Hint: Type the operator that checks whether two hashes are equal.

Spot the Error

Question 1
A import hashlb
B text = "cat"
C data = text.encode()
D h = hashlib.sha256(data)
E print(h.hexdigest())
Line A — The module is called hashlib. 'hashlb' is misspelt, so Python raises a ModuleNotFoundError. It should be import hashlib.
Question 2
A import hashlib
B pw = blue7
C b = pw.encode()
D h = hashlib.sha256(b)
E print(h.hexdigest())
Line B — Text must be inside quotes. Without them Python treats blue7 as a variable name and gives a NameError. It should be pw = "blue7".
Question 3
A import hashlib
B word = "cat"
C data = word.encode()
D h = hashlib.sha265(data)
E print(h.hexdigest())
Line D — The function is sha256, not sha265. hashlib has no sha265, so this gives an AttributeError. It should be hashlib.sha256(data).
Question 4
A import hashlib
B s = "hello"
C h = hashlib.sha256(s.encode())
D print(h.hexdigest)
Line D — hexdigest is a method, so it needs brackets to run. Without them print shows the method object, not the hash text. It should be print(h.hexdigest()).
Question 5
A import hashlib
B t = "dog"
C h = hashlib.sha256(t)
D print(h.hexdigest())
Line C — sha256 needs bytes, not a string. Calling it on t gives a TypeError. The text must be converted first with t.encode().

Student with a class code? Join your class →