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

标签 python hash md5 hashlib

我正在开发一个小型 python 程序,该程序本质上会使用 word 文件暴力破解 md5 哈希值。该程序会获取您的哈希值,然后您可以选择一个文件用作单词列表。然后它会在文件中逐行生成一个 md5 哈希版本来与您输入的版本进行检查。如果它们匹配,那么它会告诉您产生该哈希值的单词。问题在于,当程序将行转换为哈希值时,它不会生成正确的可识别 md5 哈希值。例如,它表示 test 的 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

最佳答案

line 包括行末尾的换行符。替换:

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

与:

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

即使字符串末尾有一个换行符也会完全改变哈希值。

关于Python hashlib 没有为 md5 生成正确的哈希值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31999669/

相关文章:

jquery - 如果 URL 具有哈希值,则更改 CSS

python - 搜索并打印具有相同 MD5 的文件

python - md5多线程暴力破解

security - 如何迁移密码哈希?

python - SQLAlchemy,获取未绑定(bind)到 session 的对象

python - 使用 python 使用 selenium 从弹出窗口中获取项目列表

python - django 使用 uuid 主键访问链接模型

python - 在 Python 中查找文本中所有出现的整数

hash - "evenly distributing"跨可能值空间的连续数字的函数

perl - 如何有效地创建连续数字的 perl 哈希?