hexdigest() 抛出 TypeError:找不到必需的参数 'length' (pos 1)



我正在研究一个简单的字典攻击脚本,它检查每个针对密文散列的英语小写单词,我的第一个版本运行良好。它看起来像这样:

import hashlib
import random

def dict_attack(pwds):
f = open('Dictionary.txt', 'r')
words = f.readlines()
f.close()
cracked = []
for pwd in pwds:
for w in words:
word = w.strip('n')
word = word.strip(' ')
hashed = hashlib.md5(word.encode())
if hashed.hexdigest() == pwd:
print("[+] Found {} as {}, updating...".format(pwd, word))
cracked.append(word)
break
print("[-] {}/{} passwords found!".format(len(cracked), len(pwds)))
return cracked
def main():
# To generate new ciphertext
f = open('Dictionary.txt', 'r')
words = f.readlines()
f.close()
for b in range(0, 10):
passwords.append(random.choice(words))
passwords[b] = passwords[b].strip('n')
passwords[b] = passwords[b].strip(' ')
hashed_passwords = []
for p in passwords:
hashed_passwords.append(hashlib.md5(p.encode()).hexdigest())
#print(hashed_passwords)
print(dict_attack(hashed_passwords))
main()

如您所见,该函数dict_attack仅使用 MD5 哈希。在此脚本的下一个版本中,我计划循环遍历hashlib.algorithms_guaranteed库中的每个算法,并使用每种算法加密字典中的每个单词,并根据密文进行检查。该代码如下所示:

import hashlib

arguments = [[hashlib.md5('hello'.encode())]]
f = open('Dictionary.txt', 'r')
words = f.readlines()
f.close()
# Remember to strip the n's
cracked = {}
for ciphertext in arguments[0]:
for word in words:
for alg in hashlib.algorithms_guaranteed:
exec("hashed = hashlib.{}(word.encode())".format(alg))
if hashed.hexdigest() == ciphertext:
cracked[ciphertext] = [word, alg]
print("[+] Found {} as {} with {} algorithm!".format(ciphertext, word, alg))
break
print(cracked)

但是,当我运行代码时,它抛出了此错误:

TypeError: Required argument 'length' (pos 1) not found

为什么会发生这种情况,我该如何解决?

hashlib包含一些具有动态长度输出的哈希函数。具体来说,shake_128shake_256.

import hashlib

arguments = [[hashlib.md5('hello'.encode())]]
f = open('Dictionary.txt', 'r')
words = f.readlines()
f.close()
# Remember to strip the n's
cracked = {}
for ciphertext in arguments[0]:
digest = ciphertext.digest()
for word in words:
for alg in hashlib.algorithms_guaranteed:
if alg.startswith('shake_'):
continue
hashed = getattr(hashlib, alg)(word.encode())
if hashed.digest() == digest:
cracked[ciphertext] = [word, alg]
print("[+] Found {} as {} with {} algorithm!".format(ciphertext, word, alg))
break
print(cracked)

相关内容

最新更新