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".
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.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
hashlib.sha256(text.encode()).hexdigest()Fill in the Blanks
Spot the Error
Student with a class code? Join your class →