I'm trying to create a python program that would read words from a dictionary, create a md5 hash and compare it to a given hash.
Everything works fine when I try to compare two hashes of words that haven't been read from a file:
if hashlib.md5(b"string").hexdigest() == "b45cffe084dd3d20d928bee85e7b0f21":
print("Equal!")
But when I read the words line by line from a file, the hash of the word is different. The code looks like this:
f = open('short.txt', 'r')
stringHash = 'b45cffe084dd3d20d928bee85e7b0f21'
for line in f:
if stringHash == hashlib.md5(line.encode('utf_8')).hexdigest():
print("Found it! Password: %s" % line)
Thanks for any help and explanation.
line-by-line file iterator yields the whole line including line terminators.
In your code, you're including the line terminator of the line.
line.encode('utf_8').rstrip()
will yield the correct result.rstrip()
will fail if your string ends (purposedly) with spaces. In that case, just do:if the file is open in text mode on Unix, possible
\r
(carriage return) could remain (not a problem on windows)(and
rstrip
does not remove the '\r\n' string, but each\r
or/andn
contained at the end of the line, it's not likestr.split
)