首页 > 解决方案 > Python MD5 破解器改进

问题描述

我只是在 python 中编写了一个非常简单的 MD5 破解程序。它的作用是加载 2 个单词表。1 个来自pass.txt明文的单词列表和来自hash.txt所有 MD5 哈希的其他列表。它采用明文密码并逐行生成 MD5 哈希,并与hash.txt. 饼干工作正常,到目前为止它按预期工作,但我的问题是它是否可以改进。假设它可以更快,或者如果我在其中加载一个包含数百万个密码的巨大列表,这可能是资源问题吗?等等,甚至是比较字符串的机制。

代码:

def clear_pass():
with open("pass.txt", "r", encoding="latin-1") as file:
    for x in file:
        x = x.strip()
        #print(x)
        str2hash = (x)
        result = hashlib.md5(str2hash.encode())
        final_result = (result.hexdigest())
        #print(final_result)
        with open("hash.txt", "r") as hash_file:
            for z in hash_file:
                z = z.strip()
                if z == final_result:
                    print("[+] " + final_result+ " :", x)
clear_pass()

在此处输入图像描述

标签: pythonpython-3.xmd5

解决方案


你的程序是一个双重嵌套的 for 循环。这太可怕了。对于您计算其哈希值的每个单词,您将读取整个文件 hash.txt。你一遍又一遍地阅读那个文件。

您应该改为执行以下操作:

hash_to_string = {}
with open("pass.txt", "r", encoding="latin-1") as file:
    for x in file:
        ... strip it.  Generate md5.  Call results hash...
        hash_to_string[hash] = x
with open("hash.txt") as hash_file:
    for x in file:
        if x.strip() is a key in the hash_to_string table, you've got the plain text
        otherwise, you're done.

您的代码现在是线性的,而不是 O(n^2)。


推荐阅读