Home > database >  Reversing SHA-256 on a thee letter input
Reversing SHA-256 on a thee letter input

Time:01-30

I keep getting no result. I'm trying to know the three letter on for the hash on my script

import hashlib
import itertools

# Create a string of all possible letters

letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
hash_object = hashlib.sha256()

for combination in itertools.product(letters, repeat=3):
    combination_list = list(combination)
    #join the list as str
    data = ''.join(combination_list)

    # convert str to bytes 
    # Update the hash object with the bytes of the data
    hash_object.update(bytes(data,'utf-8'))
    
    
    # Get the hexadecimal representation of the hash
    hex_hash = hash_object.hexdigest()
    
    # Compare the calculated hash to the target hash
    if hex_hash =='A5EB8E2E5CAF611498411678B5E7A641BA175E443D725F6827849DCB22160FE4' :
        print("The original three capital letters are:")
        print(data)

CodePudding user response:

There are two mistakes here.

First, logic-wise, you need a new hash object in each iteration instead of repeated updating the same object with more data.

Second, hexdigest returns a lowercase result, so you need to convert it to uppercase.

for combination in itertools.product(letters, repeat=3):
    data = ''.join(combination) # no need to convert combination to a list

    # Create a hash object with the bytes of the data
    hash_object = hashlib.sha256()
    hash_object.update(bytes(data,'utf-8'))


    # Get the hexadecimal representation of the hash, converted to uppercase
    hex_hash = hash_object.hexdigest().upper()

    # Compare the calculated hash to the target hash
    if hex_hash == 'A5EB8E2E5CAF611498411678B5E7A641BA175E443D725F6827849DCB22160FE4':
        print("The original three capital letters are:"   data)

CodePudding user response:

1-this is my solution that i got to solve my problem . 2: python code:

import hashlib
import itertools

# Create a string of all possible letters
letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'



for combination in itertools.product(letters , repeat=3):
    combination_list = list(combination)
    #join the list as str 
    data = ''.join(combination_list)
        # convert str to bytes 
    hash_object = hashlib.sha256()
    # Update the hash object with the bytes of the data
    hash_object.update(bytes(data,'utf-8'))
  
    
    
    # Get the hexadecimal representation of the hash
    hex_hash = hash_object.hexdigest()
    hex_hash=hex_hash.upper()
   


    # Compare the calculated hash to the target hash
    if hex_hash =='A5EB8E2E5CAF611498411678B5E7A641BA175E443D725F6827849DCB22160FE4':
        print("The original three capital letters are:")
        print(data)
        break
    
  • Related