首页 > 解决方案 > 如何从 SHA3-512 哈希值生成密码?

问题描述

我试图找出这个 SHA3-512 哈希值的密码:11af05af85d7656ee0f2e3260760bccdc2af88dee449f682ab2e367003856166edc045c4164a4d543ea4a43d6dd022d3c290866f2d2a7a97ab08a8af.40 我遇到的问题是,当我运行我的代码时,它仍然返回哈希值。我的问题是我需要在代码中更改哪些内容才能生成密码?这是运行我的代码的结果的一些屏幕截图。在此处输入图像描述

import itertools
import time
import hashlib
from binascii import hexlify
import shutil
import os

data = input("Enter Password:")
data1 = data.encode('utf-8')
sha3_512 = hashlib.sha3_512(data1)
sha3_512_digest = sha3_512.digest()
sha3_512_hex_digest = sha3_512.hexdigest()
print(sha3_512_hex_digest)


# 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, 800):
        # Build up a string to test against, character by character
        for letter in itertools.product(chars, repeat=value):
            attempts += 1
            letter = ''.join(letter)

            #if the string we are building matches the password given to us, return from the function
            if letter == passwordSet:
                end = time.time()
                distance = end - start
                return (attempts, distance)



tries, timeAmount = tryPassword(data)
print("The password %s was cracked in %s tries and %s seconds!" % (data, tries, timeAmount))

在此处输入图像描述

标签: python-3.xhashpython-3.7shabrute-force

解决方案


您的代码当前不使用哈希来查找密码。相反,您当前会遍历您的字母表以匹配您也输入的密码。

您需要将散列密码而不是实际密码传递给您的tryPassword函数。此外,在函数内部,您需要对生成的候选密码进行哈希处理,并将其与传入的哈希值进行比较。

这是完整的代码。我已经更改了一些变量名称以明确它们是什么。

import itertools
import time
import hashlib
from binascii import hexlify
import shutil
import os

pw = input("Enter Password:")
pw = pw.encode('utf-8')
pwHashHex = hashlib.sha3_512(pw).hexdigest()
print(pwHashHex)


# Function to brute force the password
def tryPassword(pwHashHex):
    start = time.time()
 
    # Allowed characters in the password
    alphabet = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`~!@#$%^&*()_-+=[{]}|:;'\",<.>/?"
    
    attempts = 0

    for value in range(1, 800):
        # Build up a string to test against, character by character
        for pwCandidate in itertools.product(alphabet, repeat=value):
            attempts += 1
            pwCandidate = ''.join(pwCandidate)

            #if the string we are building matches the password given to us, return from the function
            if hashlib.sha3_512(pwCandidate).hexdigest() == pwHashHex:
                end = time.time()
                distance = end - start
                return (pwCandidate, attempts, distance)

pwFound, tries, timeAmount = tryPassword(pwHashHex)
print("The password %s was cracked in %s tries and %s seconds!" % (pwFound, tries, timeAmount))

推荐阅读