Why md5 hash of a word from a file doesn't mat

2019-07-27 05:31发布

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.

1条回答
聊天终结者
2楼-- · 2019-07-27 05:52

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:

line.encode('utf_8').rstrip('\r\n')

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/and n contained at the end of the line, it's not like str.split)

查看更多
登录 后发表回答