python - 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()
解决方案
你的程序是一个双重嵌套的 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)。
推荐阅读
- go - 如何在 GoLang 中使用 GOB 编码序列化嵌套结构?
- xcode10 - 将错误绑定到变量的 Swift Catch 模式
- spring-boot - SpringBoot 的详细请求和响应日志记录
- google-apps-script - 将 Google Drive 中的 PDF 转换为 PPTX 后,文件已损坏,只能查看第一张幻灯片
- vba - 如何更改 powerpoint 幻灯片母版中的特定文本框
- javascript - 为什么自定义字体会减慢我的程序速度?
- javascript - 如何使用 firebase auth 在 redux 中成功注册用户
- python - 在带有标题的excel中导出表格小部件
- java - 带有上下文信息的静态/构造函数 Java AWS Lambda 函数初始化
- azure-powershell - 从容器中读取 Blob 时出现奇怪的字符