问题描述
我是 Python 新手,目前正在攻读 IT 硕士学位。我正在从哈希值解码密码。
这就是我目前的设置方式。我知道这是错误的,任何帮助将不胜感激。
import itertools
import time
from Crypto.Hash import SHA3_512
# Function to brute force the password
def tryPassword(passwordSet):
start = time.time()
# Allowed characters in the password
chars = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMnopQRSTUVWXYZ`~!@#$%^&*()_-+=[{]}|:;'\",<.>/?"
attempts = 0
for value in range(1,9):
# Build up a string to test against,character by character
for letter in itertools.product(chars,repeat=value):
attempts += 1
letter = ''.join(letter)
hash_object = SHA3_512.new()
hash_object.update((letter).encode("utf-8"))
tmp_hash = hash_object.hexdigest()
print(tmp_hash)
#if the string we are building matches the password given to us,return from the function
if tmp_hash == passwordSet:
end = time.time()
distance = end - start
return (attempts,distance)
password = input("Password >")
tries,timeAmount,= tryPassword(password)
print("The password %s was cracked in %s tries and %s seconds!" % (password,tries,timeAmount))
解决方法
由于目的是破解散列密码,所以应该是函数的参数,而不是明文密码。
然后在循环中它需要对每个候选密码进行散列,并将散列与输入进行比较。
jenkins.B.com