One-way functions

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

One-way functions

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A one-way function is easy to calculate forwards but practically impossible to reverse.
A False
B True
2. Which is the best example of a one-way function?
A Mixing two paint colours together
B Folding a piece of paper in half
3. Which statement is true about multiplying and factoring large prime numbers?
A Multiplying two primes is easy, but factoring the answer back into them is very hard
B Factoring the answer back into two primes is just as easy as multiplying them
4. Why is a one-way function useful for storing passwords as hashes?
A A thief who steals the hash cannot easily work backwards to the password
B The hash can be reversed by the website to show the original password
5. Which direction of a one-way function is the hard one?
A Forward, from the input to the result
B Backward, from the result to the original input

Match the Pairs

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

Left columnRight column
17919 * 7907 AMultiplying two primes is quick and easy, even for a computer. This is the easy forward direction of a one-way function.
2factor(62615533) BThis assumes a one-way function can be reversed. A hash cannot be run backwards to recover the original input.
3Mixing blue and yellow paint to get green is easy, but you cannot easily unmix the green back into the two paints. CWorking backwards from a big product to find the two primes is extremely hard. This is the hard backward direction of a one-way function.
4A hash function turns any input into a fixed-length output, but you cannot work backwards from the output to the input. Ddigest = sha256(password)
5hash = sha256("cat") original = reverse(hash) # gets back "cat" EThe comment has the directions the wrong way round. Multiplying two primes is easy, and factoring the result is the hard direction.
6# Factoring is easy, multiplying is hard n = p * q Fgreen = mix(blue, yellow)

Definitions

one-way function

Fill in the Blanks

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

1.
p = 61 q = 53 print(p _ q)
2.
n = 3233 print(n _ 61)
3.
import hashlib print(hashlib.______(b"paint").hexdigest())
4.
___ mix(blue, yellow): return blue + yellow print(mix(5, 3))
5.
digest = hashlib.sha256(b"cat").hexdigest() print(___(digest))

Spot the Error

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

Question 1
A p = 61
B q = 53
C n = p + q
D print(n)
Question 2
A import hashlip
B data = b'cat'
C h = hashlib.sha256(data)
D print(h.hexdigest())
Question 3
A n = 3233
B for f in range(2, n):
C if n % f == 0:
D print(n)
E break
Question 4
A def one_way(x):
B retrun (x * x) % 10
C print(one_way(7))
Question 5
A import hashlib
B pw = 'cat123'
C h = hashlib.sha256(pw)
D print(h.hexdigest())

Answer Key

One-way functions
Section A: Quick Questions
Q1
B — True
Q2
A — Mixing two paint colours together
Q3
A — Multiplying two primes is easy, but factoring the answer back into them is very hard
Q4
A — A thief who steals the hash cannot easily work backwards to the password
Q5
B — Backward, from the result to the original input
Section B: Match the Pairs
Q1
7919 * 7907 → Multiplying two primes is quick and easy, even for a computer. This is the easy forward direction of a one-way function.
Q2
factor(62615533) → Working backwards from a big product to find the two primes is extremely hard. This is the hard backward direction of a one-way function.
Q3
Mixing blue and yellow paint to get green is easy, but you cannot easily unmix the green back into the two paints. → green = mix(blue, yellow)
Q4
A hash function turns any input into a fixed-length output, but you cannot work backwards from the output to the input. → digest = sha256(password)
Q5
hash = sha256("cat") original = reverse(hash) # gets back "cat" → This assumes a one-way function can be reversed. A hash cannot be run backwards to recover the original input.
Q6
# Factoring is easy, multiplying is hard n = p * q → The comment has the directions the wrong way round. Multiplying two primes is easy, and factoring the result is the hard direction.
Section C: Definitions
Q1
one-way function — A calculation that is easy to do forwards but practically impossible to reverse, like mixing paint. It is the idea behind hashing and public-key encryption.
Section D: Fill in the Blanks
Q1
p = 61 q = 53 print(p * q)
Q2
n = 3233 print(n % 61)
Q3
import hashlib print(hashlib.sha256(b"paint").hexdigest())
Q4
def mix(blue, yellow): return blue + yellow print(mix(5, 3))
Q5
digest = hashlib.sha256(b"cat").hexdigest() print(len(digest))
Section E: Spot the Error
Q1
C — The one-way function here is multiplying two primes, which is easy forwards but very hard to reverse. Adding them would be easy to undo. Line 3 should be n = p * q.
Q2
A — The hashing library is called hashlib, not hashlip. Without the correct import, the program cannot use the one-way hash function sha256. Line 1 should be import hashlib.
Q3
D — The loop is trying every number to factor n, which is the slow backward direction. When a factor f is found, the program should print f, not n. Line 4 should be print(f).
Q4
B — The keyword is spelled return, not retrun. The typo is a syntax error, so the function can never give back its forwards result. Line 2 should be return (x * x) % 10.
Q5
B — hashlib works on bytes, not on a normal string. The b prefix turns the password into bytes so the one-way hash can be calculated. Line 2 should be pw = b'cat123'.