Vexxoz
Vexxoz

Reputation: 88

Python hashlib not producing the correct hash for md5

I am working on a little python program that will essentially brute force md5 hashes using a word file. The program takes your hash and then you can select a file to use as a word list. It will then go line by line in the file and make a md5 hash version to check against the one you entered. If they match then it will tell you the word that produces that hash. The problem is that when the program is turning the lines into hashes it doesn't produce a correct recognizable md5 hash. For example it says that the md5 hash of test is d8e8fca2dc0f896fd7cb4cb0031ba249. I've tried multiple way of encoding the text and whatnot, but can't find the right answer. What am I doing wrong?

import hashlib

mainhash = raw_input("What hash would you like to try and break?")
filename = raw_input("What file would you like to use?")
times = 0


if filename == "":
    print "A list file is required."
    exit()

f = open(filename)
for line in iter(f):
    times = times + 1
    word = line
    line = hashlib.md5(line.encode("utf")).hexdigest()
    print line
    if line == mainhash:
        print "Matching Hash found. Word is:"
        print word
        print times
        exit()

f.close()
print "Sorry no match was found. Please try a different word file or make sure the hash is md5."
print times

Upvotes: 2

Views: 2644

Answers (1)

Cyphase
Cyphase

Reputation: 12012

line includes the newline at the end of the line. Replace:

line = hashlib.md5(line.encode("utf")).hexdigest()

with:

line = hashlib.md5(line.encode("utf").strip()).hexdigest()

Even a single newline at the end of a string will completely change the hash.

Upvotes: 5

Related Questions