我正在尝试对 sha512 哈希进行字典攻击。我知道哈希由两个单词组成,全小写,用空格分隔。这些单词来自一个已知的字典 (02-dictionary.txt),其中包含 172,820 个单词。目前,我的代码如下:
import hashlib
import sys
import time
def crack_hash(word, target):
dict_hash = hashlib.sha512(word.encode())
if dict_hash.hexdigest() == target:
return (True, word)
else:
return (False, None)
if __name__ == "__main__":
target_hash = sys.argv[1].strip()
fp = open("02-dictionary.txt", "r")
words = []
start_time = time.time()
for word in fp:
words.append(word)
fp.close()
for word1 in words:
for word2 in words:
big_word = word1.strip() + " " + word2.strip()
print(big_word)
soln_found, soln_word = crack_hash(big_word.strip(), target_hash)
if soln_found:
print('Solution found')
print("The word was:", soln_word)
break
end_time = time.time()
total_time = end_time - start_time
print("Time taken:", round(total_time, 5), "seconds")
但是,当我运行此代码时,程序运行速度非常慢。我知道 Python 不是最有效的语言,但我猜这个问题更多源于数据结构的选择。有没有更高效的数据结构?我尝试使用该array
模块,但文档使它看起来好像被设计用于更原始的类型(整数、浮点数、短裤、布尔值、字符等),而不是更复杂的类型,如字符串(或列表个字符)。改进此代码的最佳方法是什么?在大约一个小时的运行时间中,我只完成了所有可能的单词组合中的大约 1%。