Hashing functions

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

Hashing functions

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A hash function is a one-way function. Which statement is correct?
A A hash cannot be reversed to get the original input
B A hash can be decrypted with the right key to get the original input
2. A student hashes the word 'apple' and the hash is 32 characters long. What will the hash of a 5,000-word essay look like?
A It will also be 32 characters long
B It will be much longer than 32 characters
3. You hash the same file twice using the same hash function. What happens?
A You get exactly the same hash both times
B You get a different hash each time
4. A single letter in a document is changed and the document is hashed again. What is the result?
A A hash that is almost the same as before
B A completely different hash
5. Which describes the difference between hashing and encryption?
A Hashing can be reversed with a key, while encryption is one-way
B Hashing is one-way, while encryption can be reversed with a key

Match the Pairs

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

Left columnRight column
1SHA-256("password") AThe hash length depends on the input, but it should be fixed whatever the input size.
2hash("data") == hash("data") Bhash("Cat") != hash("Cap")
3A small change to the input gives a completely different hash CThe same input always gives the same hash, so the comparison is true
4The output is a fixed length, however long the input is DA hash function is one-way, so a hash cannot be reversed or decrypted to get the original input back.
5original = decrypt(hashValue) Elen(hash("a")) == len(hash(bigFile))
6hash("Hi") = 2 characters long FRuns the input through a hash function to produce a fixed-length 64-character hash value

Definitions

hash function

Fill in the Blanks

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

1.
______ hashlib h = hashlib.sha256(b"hi").hexdigest() print(h)
2.
h1 = hashlib.sha256(b"cat").hexdigest() h2 = hashlib.sha256(b"cat").hexdigest() print(h1 __ h2)
3.
h = hashlib.sha256(b"a").hexdigest() print(___(h))
4.
h1 = hashlib.sha256(b"pass1").hexdigest() h2 = hashlib.sha256(b"pass2").hexdigest() print(h1 __ h2)
5.
stored = hashIt("Secret1") entered = "Secret1" print(______(entered) == stored)

Spot the Error

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

Question 1
A SET a ← HASH("cat")
B SET b ← HASH("Cat")
C IF a = b THEN
D OUTPUT "Same hash"
Question 2
A SET h1 ← HASH("a")
B SET h2 ← HASH("a much longer text")
C IF LEN(h1) < LEN(h2) THEN
D OUTPUT "Fixed length"
Question 3
A SET original ← file
B SET received ← HASH(download)
C IF original = received THEN
D OUTPUT "File unchanged"
Question 4
A SET h1 ← HASH("score 90")
B SET h2 ← HASH("score 91")
C IF h1 = h2 THEN
D OUTPUT "Very different"
Question 5
A import hashlib
B text = "hello".encode()
C h = hashlib.sha256[text]
D print(h.hexdigest())

Answer Key

Hashing functions
Section A: Quick Questions
Q1
A — A hash cannot be reversed to get the original input
Q2
A — It will also be 32 characters long
Q3
A — You get exactly the same hash both times
Q4
B — A completely different hash
Q5
B — Hashing is one-way, while encryption can be reversed with a key
Section B: Match the Pairs
Q1
SHA-256("password") → Runs the input through a hash function to produce a fixed-length 64-character hash value
Q2
hash("data") == hash("data") → The same input always gives the same hash, so the comparison is true
Q3
A small change to the input gives a completely different hash → hash("Cat") != hash("Cap")
Q4
The output is a fixed length, however long the input is → len(hash("a")) == len(hash(bigFile))
Q5
original = decrypt(hashValue) → A hash function is one-way, so a hash cannot be reversed or decrypted to get the original input back.
Q6
hash("Hi") = 2 characters long → The hash length depends on the input, but it should be fixed whatever the input size.
Section C: Definitions
Q1
hash function — A one-way function that turns any input into a fixed-length value. The same input always gives the same hash, a tiny change gives a very different one, and it cannot be reversed.
Section D: Fill in the Blanks
Q1
import hashlib h = hashlib.sha256(b"hi").hexdigest() print(h)
Q2
h1 = hashlib.sha256(b"cat").hexdigest() h2 = hashlib.sha256(b"cat").hexdigest() print(h1 == h2)
Q3
h = hashlib.sha256(b"a").hexdigest() print(len(h))
Q4
h1 = hashlib.sha256(b"pass1").hexdigest() h2 = hashlib.sha256(b"pass2").hexdigest() print(h1 != h2)
Q5
stored = hashIt("Secret1") entered = "Secret1" print(hashIt(entered) == stored)
Section E: Spot the Error
Q1
B — The program is checking that the same input gives the same hash. Changing 'cat' to 'Cat' changes the input, so the hashes would differ. Line 2 should hash "cat" again.
Q2
C — A hash function always gives a fixed-length output, whatever the input size. The two hashes have equal lengths, so the test should be LEN(h1) = LEN(h2), not <.
Q3
A — To check integrity we compare two hashes. Line 1 stores the raw file instead of its hash, so it would never equal the hash on line 2. It should be SET original ← HASH(file).
Q4
C — A tiny change to the input gives a very different hash. The hashes of "score 90" and "score 91" are not equal, so the test should be h1 ≠ h2.
Q5
C — sha256 is a hashing function, so it must be called with round brackets, sha256(text). Square brackets try to index it and cause an error.