Skip to content

Commit

Permalink
Prepare L23 flipped note
Browse files Browse the repository at this point in the history
  • Loading branch information
h365chen committed Mar 5, 2024
1 parent 50c789b commit f14b531
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 40 deletions.
53 changes: 25 additions & 28 deletions lectures/flipped/L23.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,40 @@ passwords.

## Creating rainbow tables [25 minutes]

I've created Python file "hash-and-reduce.py" which does almost the
thing in "[How Rainbow Tables
Work](https://kestas.kuliukas.com/RainbowTables)" by Kuliukas; it
operates on 7-digit numbers instead of 6-digit numbers, but otherwise
it's the same. That is, it takes a 7-digit number (e.g. a phone
number) as command-line input and outputs the MD5sum and the
reduction, which is another 7-digit number obtained from the MD5sum.
A real rainbow table also uses different reduction functions
at different positions on chains, but we won't do that here for simplicity.
I've created Python file "hash-and-reduce.py" which does almost the thing in
"[How Rainbow Tables Work](https://kestas.kuliukas.com/RainbowTables)" by
Kuliukas; it operates on 7-digit numbers instead of 6-digit numbers, but
otherwise it's the same. That is, it takes a 7-digit number (e.g., to give it a
real world context, a phone number) as command-line input and outputs the MD5sum
and the reduction, which is another 7-digit number obtained from the MD5sum. A
real rainbow table also uses different reduction functions at different
positions on chains, but we won't do that here for simplicity.

* Your task: run "hash-and-reduce.py" on an input of your choice through 10
cycles and report the initial plaintext and final hash. Imagine how a GPU can do
this efficiently. Come up and write your plaintext and hash on the board.

## Using rainbow tables [20 minutes]

OK, now we have information about some precomputed hash chains
(a simpler version of rainbow tables). Let's use it to reverse the hash
for an input. I will do an example and then you can try one.
OK, now we have information about some precomputed hash chains (a simpler
version of rainbow tables). Let's use it to reverse the hash for an input. I
will do an example and then you can try one.

Let's say that you have a hash that you want to lookup the plaintext
for. We'll assume that it's one of the plaintexts that is in our
table. How do you get the hash you want to lookup? For the purpose
of this exercise, take one of your plaintexts and hash it.
Let's say that you have a hash that you want to lookup the plaintext for. We'll
assume that it's one of the plaintexts that is in our table. How do you get the
hash you want to lookup? For the purpose of this exercise, take one of your
plaintexts and hash it.

OK, so now you have a hash that you want to reverse. But the hash
function is one-way. So you repeatedly reduce it and hash it
and (given your choice of hash) you'll eventually encounter one of the
final hashes.
OK, so now you have a hash that you want to reverse. But the hash function is
one-way. So you repeatedly reduce it and hash it and (given your choice of hash)
you'll eventually encounter one of the final hashes.

* How many times do you need to do this before you can give up?

OK, now you have a final hash that matches. Start with the initial
plaintext and run through until you hit the hash that you're
trying to reverse, to check that you didn't have a false positive collision.
The thing just before that is the plaintext
you're looking for.
OK, now you have a final hash that matches. Start with the initial plaintext and
run through until you hit the hash that you're trying to reverse, to check that
you didn't have a false positive collision. The thing just before that is the
plaintext you're looking for.

## Rant 2: Bitcoin mining [5 minutes]

Expand All @@ -70,6 +67,6 @@ hardware to mine Bitcoin.
# After-action report, plam, 27 Feb 2023

I did the rainbow table exercise and realized that it wasn't as clear as it
could be. After lecture, I rewrote the rainbow table exercise to clarify it.
I did talk about Bitcoins, although the popularity of NFTs has waned
now at least, and Ethereum has moved to proof-of-stake.
could be. After lecture, I rewrote the rainbow table exercise to clarify it. I
did talk about Bitcoins, although the popularity of NFTs has waned now at least,
and Ethereum has moved to proof-of-stake.
35 changes: 23 additions & 12 deletions lectures/live-coding/L23/hash-and-reduce.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
"""Rainbow table operates on 7-digit numbers.
$ python hash-and-reduce.py 1234567
Plaintext: 1234567
MD5sum: fcea920f7412b5da7be0cf42b8c93759
Reduced: 9207412
"""

from hashlib import md5
from sys import argv


def reduce(digest):
reduced = ""
for c in digest:
if c.isdigit():
reduced = reduced + c
if len(reduced) == 7:
return reduced

plaintext=argv[1]
"""Reduce a hash into a new 7-digit number."""
reduced = ""
for c in digest:
if c.isdigit():
reduced = reduced + c
if len(reduced) == 7:
return reduced


plaintext = argv[1]
md5_digest = md5(plaintext.encode()).hexdigest()
reduced = reduce(md5_digest)

print ("Plaintext: ", plaintext)
print ("MD5sum: ", md5_digest)
print ("Reduced: ", reduced)

print("Plaintext: ", plaintext)
print("MD5sum: ", md5_digest)
print("Reduced: ", reduced)

0 comments on commit f14b531

Please sign in to comment.