Python hashlib没有为md5生成正确的哈希

Vex*_*xoz 2 python hash md5 hashlib

我正在研究一个小的python程序,它将使用word文件强制执行md5哈希.程序将获取您的哈希值,然后您可以选择要用作单词列表的文件.然后它将在文件中逐行进行并生成md5哈希版本以检查您输入的版本.如果它们匹配,那么它将告诉你产生该哈希的单词.问题是,当程序将行转换为哈希值时,它不会产生正确的可识别md5哈希值.例如,它说测试的md5哈希是d8e8fca2dc0f896fd7cb4cb0031ba249.我已经尝试了多种编码文本和诸如此类的方法,但找不到正确的答案.我究竟做错了什么?

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
Run Code Online (Sandbox Code Playgroud)

Cyp*_*ase 5

line包括行尾的换行符.更换:

line = hashlib.md5(line.encode("utf")).hexdigest()
Run Code Online (Sandbox Code Playgroud)

有:

line = hashlib.md5(line.encode("utf").strip()).hexdigest()
Run Code Online (Sandbox Code Playgroud)

即使字符串末尾的单个换行也会完全改变散列.