Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
227 views
in Technique[技术] by (71.8m points)

python 3.x - How can I generate a password from SHA3-512 hash value?

I am trying to figure out the password for this SHA3-512 hash value: 11af05af85d7656ee0f2e3260760bccdc2af88dee449f682ab2e367003856166edc045c4164a4d543ea4a43d6dd022d3c290866f2d2a7a92a38400bd3a5f7ab0. The issue I am having is that when I run my code it still returns the hash value. My question is what do I need to change in my code to produce the password? Here are some screenshots of the results from running my code. enter image description here

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))

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your code currently doesn't use the hash to find the password. Instead you currently iterate over your alphabet to match the password that you've also put in.

You need to pass in the hashed password instead of the actual password into your tryPassword function. Additionally, inside of the function, you need to hash your generated password candidate and compare it to the passed in hash.

Here is the full code. I've changed some variable names to make it clear what they are.

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))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...