Skip to content

Commit 1409239

Browse files
committed
Fixed problem with wasted checks, now ~6x more efficient
1 parent 8fb5640 commit 1409239

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

RainbowTable.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Jialin Ding (jding09@stanford.edu) and Charles Lu (charleslu@stanford.edu)
66
CS 55N Autumn 2014 with Dan Boneh
77
----------------------------------------------------------------------------
8-
Contains functionality to create a rainbow table and crack a hash for 6-digit passwords.
8+
Contains functionality to create a rainbow table and crack a hash for 6-digit lowercase passwords.
99
"""
1010

1111
# Use Python 3
@@ -23,8 +23,9 @@
2323
CSV_FILE = "RainbowTable.csv"
2424
CSV_FIELDNAMES = ['start_point', 'endpoint_hash']
2525
PICKLE_FILE = "RainbowTable.pickle"
26+
RAINBOW_TABLE = {}
2627

27-
"""Creates rainbow table using H() and R() with ROWS of CHAIN_LENGTH in CSV_FILE
28+
"""Creates rainbow table using H() and R() with ROWS of CHAIN_LENGTH hashes in CSV_FILE
2829
Precondition: To expand, input number of rows as param expand
2930
"""
3031
def create_rainbow_table(expandRows=None):
@@ -90,16 +91,15 @@ def crack(hashedPassword, reloadTable=False):
9091
print("Cracking hash...")
9192
startTime = time.time()
9293

93-
for col in range(CHAIN_LENGTH):
94+
for startCol in range(CHAIN_LENGTH-1, -1, -1):
9495
candidate = hashedPassword
95-
for column in range(col, CHAIN_LENGTH):
96-
if candidate in RAINBOW_TABLE:
97-
traversalResult = traverse_chain(hashedPassword, RAINBOW_TABLE[candidate])
98-
if traversalResult:
99-
print("Done cracking in {0} secs.".format(time.time() - startTime))
100-
return traversalResult
101-
102-
candidate = H(R(candidate, column))
96+
for col in range(startCol, CHAIN_LENGTH):
97+
candidate = H(R(candidate, col-1))
98+
if candidate in RAINBOW_TABLE:
99+
traversalResult = traverse_chain(hashedPassword, RAINBOW_TABLE[candidate])
100+
if traversalResult:
101+
print("Done cracking in {0} secs.".format(time.time() - startTime))
102+
return traversalResult
103103

104104
"""Traverses a chain in the table to find the plaintext password once we've found a possible one
105105
Postcondition: Returns plaintext password if successful; otherwise returns None
@@ -146,6 +146,6 @@ def test(password=""):
146146

147147
cracked = crack(H(password))
148148
if cracked:
149-
print("Cracked password: {0}".format(cracked))
149+
print("Success! Password: {0}".format(cracked))
150150
else:
151-
print("Unsuccessful :(")
151+
print("Unsuccessful :(")

0 commit comments

Comments
 (0)