Skip to content

Instantly share code, notes, and snippets.

@clickCA
Last active August 20, 2023 13:51
Show Gist options
  • Select an option

  • Save clickCA/d58b8472ee80ae283763dade5cae0359 to your computer and use it in GitHub Desktop.

Select an option

Save clickCA/d58b8472ee80ae283763dade5cae0359 to your computer and use it in GitHub Desktop.
import hashlib
import itertools
import time
import sys
import os
# Function to generate substituted variants of a word
def generate_variants(word):
substitutions = {
'o': '0',
'l': '1',
'i': '1'
}
case_variants = map(''.join, itertools.product(*zip(word.lower(), word.upper())))
final_variants = []
for variant in case_variants:
temp_variants = [variant]
for original, replacement in substitutions.items():
new_variants = []
for temp in temp_variants:
if original in temp:
new_variants.append(temp.replace(original, replacement))
temp_variants.extend(new_variants)
final_variants.extend(temp_variants)
return final_variants
# Load the dictionary
def load_dictionary(filename):
with open(filename, 'r') as f:
words = f.read().splitlines()
return words
# Save the rainbow table to a text file
def save_rainbow_table(table, filename="rainbow_table.txt"):
with open(filename, 'w') as file:
for hash_value, variant in table.items():
file.write(f"{hash_value} -> {variant}\n")
# Main function
def main():
dictionary_words = load_dictionary('./most-common.txt')
total_time = 0
total_size = 0
iterations = 10
for _ in range(iterations):
rainbow_table = {}
start_time = time.time()
for word in dictionary_words:
for variant in generate_variants(word):
sha1_hashed_variant = hashlib.sha1(variant.encode()).hexdigest()
rainbow_table[sha1_hashed_variant] = variant
end_time = time.time()
# Measure the time taken to create the rainbow table for this iteration
time_taken = end_time - start_time
total_time += time_taken
# Measure the size of the rainbow table in memory for this iteration
size_in_memory = sys.getsizeof(rainbow_table)
total_size += size_in_memory
# Save the rainbow table of this iteration to a text file
save_rainbow_table(rainbow_table, filename=f"rainbow_table_{_ + 1}.txt")
# Calculate averages
average_time = total_time / iterations
average_size = total_size / iterations
# Print results
print(f"Average time taken to create rainbow table over {iterations} iterations: {average_time:.4f} seconds")
print(f"Average size of the rainbow table in memory over {iterations} iterations: {average_size} bytes (or {average_size / (1024 * 1024):.2f} MB)")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment