Why md5 hash of a word from a file doesn't match the hash of a string?

自作多情 提交于 2019-12-11 06:07:07

问题


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:


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)



来源:https://stackoverflow.com/questions/41751690/why-md5-hash-of-a-word-from-a-file-doesnt-match-the-hash-of-a-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!