P_n
P_n

Reputation: 992

Python MD5 cracker improvement

I was just writing a very simple MD5 cracker in python. What it does is loads 2 wordlists. 1 wordlist from pass.txt in clear text and other list from hash.txt with all MD5 hashes. It takes the clear text passwords and generates a MD5 hash line by line and compares to all MD5 in hash.txt. The cracker works fine, so far it works as intended, but my question is if it can be improved. Let's say can it be faster or if I load a huge list in it with millions of passwords, can that be an issue on resources? Etc. Or even the mechanics of comparing the strings.

Code:

def clear_pass():
with open("pass.txt", "r", encoding="latin-1") as file:
    for x in file:
        x = x.strip()
        #print(x)
        str2hash = (x)
        result = hashlib.md5(str2hash.encode())
        final_result = (result.hexdigest())
        #print(final_result)
        with open("hash.txt", "r") as hash_file:
            for z in hash_file:
                z = z.strip()
                if z == final_result:
                    print("[+] " + final_result+ " :", x)
clear_pass()

enter image description here

Upvotes: 0

Views: 1339

Answers (1)

Frank Yellin
Frank Yellin

Reputation: 11297

Your program is a doubly nested for loop. That's pretty terrible. For each word that you calculate that hash value of, you're then reading the entire file hash.txt. You're reading that file over and over again.

You should instead do something like:

hash_to_string = {}
with open("pass.txt", "r", encoding="latin-1") as file:
    for x in file:
        ... strip it.  Generate md5.  Call results hash...
        hash_to_string[hash] = x
with open("hash.txt") as hash_file:
    for x in file:
        if x.strip() is a key in the hash_to_string table, you've got the plain text
        otherwise, you're done.

Your code is now linear rather than O(n^2).

Upvotes: 2

Related Questions