MatrhWonder00
MatrhWonder00

Reputation: 15

Brute-Force Function SHA256 hashes

I've already made a python program which hashes your password to sha256.

import hashlib


u = input("Write 3 upper-case letters: ").upper()
d = int(input("Write 4 digits from 0-9 "))

testhashed = u+str(d)


key = hashlib.sha256(testhashed.encode())

print("function 1" +key.hexdigest())

But I litreally have no idea how to make a brute-force function. Would appreciate if someone could guide me through.

Upvotes: 0

Views: 4477

Answers (1)

Boris Lipschitz
Boris Lipschitz

Reputation: 1631

You do have some issues with your code. For example, nothing forces you to only enter 3 letters, or even forces those to be letters. Also, you can enter more, or less, than 4 digits, too. Anyway, this is a simple example of brute-forcing. Its just going over all the options you have, attempting to match the hash. Note that this is sub-optimal to say the least, but hey, running a brute-force in Python isn't a very smart idea to begin from so this is good enough. Note that i've selected the test password close to the beginning of the brute-forced range, otherwise you'll wait forever for this to succeed.

import hashlib, itertools

def brute_force(hash):
    letters = [chr(n) for n in range(ord('A'), ord('Z'))]
    digits = [chr(n) for n in range(ord('0'), ord('9'))]

    for part1 in itertools.product(letters, repeat=3):
        for part2 in itertools.product(digits, repeat=4):
            password_try = ''.join(part1 + part2)
            if hashlib.sha256(password_try.encode()).hexdigest() == hash:
                return password_try


test_password = "BOO8000"
key = hashlib.sha256(test_password.encode()).hexdigest()
print("Hash: %s" % key)
print("Brute-forcing...")
recovered_key = brute_force(key)
print("Brute-force result: %s" % recovered_key) 

Upvotes: 2

Related Questions