Hashing text with hashlib in Python

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Hashing text with hashlib in Python

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line must come first in a Python program before you can use hashlib.sha256()?
A include hashlib
B import hashlib
2. Which code correctly turns the text in the variable text into bytes for hashing?
A text.encode()
B text.hexdigest()
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()
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
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

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

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

Definitions

hashlib.sha256(text.encode()).hexdigest()

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
import _______
2.
d = hashlib.______(s.encode()).hexdigest()
3.
d = hashlib.sha256(s.______()).hexdigest()
4.
d = hashlib.sha256(s.encode())._________()
5.
h1 = hashlib.sha256(s.encode()).hexdigest() h2 = hashlib.sha256(t.encode()).hexdigest() print(h1 __ h2)

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A import hashlb
B text = "cat"
C data = text.encode()
D h = hashlib.sha256(data)
E print(h.hexdigest())
Question 2
A import hashlib
B pw = blue7
C b = pw.encode()
D h = hashlib.sha256(b)
E print(h.hexdigest())
Question 3
A import hashlib
B word = "cat"
C data = word.encode()
D h = hashlib.sha265(data)
E print(h.hexdigest())
Question 4
A import hashlib
B s = "hello"
C h = hashlib.sha256(s.encode())
D print(h.hexdigest)
Question 5
A import hashlib
B t = "dog"
C h = hashlib.sha256(t)
D print(h.hexdigest())

Answer Key

Hashing text with hashlib in Python
Section A: Quick Questions
Q1
B — import hashlib
Q2
A — text.encode()
Q3
B — hashlib.sha256(text.encode()).hexdigest()
Q4
A — The exact same hash both times
Q5
A — The two hashes are completely different
Section B: Match the Pairs
Q1
import hashlib → Loads Python's built-in hashing module so you can use hash functions like SHA-256
Q2
text.encode() → Turns a string into bytes, which is the form the hash function needs as input
Q3
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()
Q4
Turn the finished SHA-256 hash into a fixed-length hexadecimal string you can print → print(hashlib.sha256('cat'.encode()).hexdigest())
Q5
hashlib.sha256('cat').hexdigest() → The text was not turned into bytes with encode(), so Python raises a TypeError. It should be 'cat'.encode().
Q6
hashlib.sha256('cat'.encode()) → hexdigest() is missing, so this gives a hash object, not the readable hex text. Add .hexdigest() at the end.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
import hashlib
Q2
d = hashlib.sha256(s.encode()).hexdigest()
Q3
d = hashlib.sha256(s.encode()).hexdigest()
Q4
d = hashlib.sha256(s.encode()).hexdigest()
Q5
h1 = hashlib.sha256(s.encode()).hexdigest() h2 = hashlib.sha256(t.encode()).hexdigest() print(h1 == h2)
Section E: Spot the Error
Q1
A — The module is called hashlib. 'hashlb' is misspelt, so Python raises a ModuleNotFoundError. It should be import hashlib.
Q2
B — Text must be inside quotes. Without them Python treats blue7 as a variable name and gives a NameError. It should be pw = "blue7".
Q3
D — The function is sha256, not sha265. hashlib has no sha265, so this gives an AttributeError. It should be hashlib.sha256(data).
Q4
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()).
Q5
C — sha256 needs bytes, not a string. Calling it on t gives a TypeError. The text must be converted first with t.encode().