# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3 (ipykernel)
#     language: python
#     name: python3
# ---

# +
import hashlib
import binascii
import itertools
import datetime

password_a4 = "<PASSWORD>"
password_a4_bin = password_a4.encode("utf-8")
hash_object_a4 = hashlib.sha256(password_a4_bin)
print("hash4\t\t", hash_object_a4.hexdigest())
print("hash4bin\t", hash_object_a4.digest())
print("")

password_a5 = "<PASSWORD>"
password_a5_bin = password_a5.encode("utf-8")
hash_object_a5 = hashlib.sha256(password_a5_bin)
print("hash5\t\t", hash_object_a5.hexdigest())
print("hash5bin\t", hash_object_a5.digest())
print("")

password_6 = "<PASSWORD>"
password_6_bin = password_6.encode("utf-8")
hash_object_6 = hashlib.sha256(password_6_bin)
print("hash6\t\t", hash_object_6.hexdigest())
print("hash6bin\t", hash_object_6.digest())


#x1 = binascii.unhexlify(hash_object.hexdigest())
#print(x1)

# -

alphabet_a = "abcdefghijklmnopqrstuvwxyz"
alphabet_Aa = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
alphabet_a_bin = alphabet_a.encode("utf-8")
print(alphabet_a_bin)

# ### Prosty łamacz haseł

# +
import hashlib
import binascii
import itertools
import datetime

def break_password(hash_sha256, letter_num, alphabet, write_every=None):
    print("Debug: łamanie hasła")
    done = 0
    start_moment = datetime.datetime.now()
    
    for candidate_t in itertools.product(alphabet, repeat=letter_num):
        
        #konwertowanie iterowanego obiektu, aby móc go hashować
        candidate = "".join(candidate_t)
        candidate_bin = candidate.encode("utf-8")
        
        #hashowanie
        hash_obj = hashlib.sha256(candidate_bin)
        calculated_hash = hash_obj.digest()
        
        #sprawdzamy warunek
        if calculated_hash == hash_sha256:
            #kończymy działanie i zwracany wynik jeśli znaleziono
            end_moment = datetime.datetime.now()
            print("Odnaleziono w ciągu: " + str((end_moment - start_moment) / datetime.timedelta(seconds = 1, microseconds=1)))
            return candidate
        
        #zwiększamy licznik prób
        done = done + 1
        if  done %10000 == 0 and write_every is not None :
            #jeśli spełnione są warunki wypisania postępu to wypisujemy
            print("checking: " + candidate)
    end_moment = datetime.datetime.now()
    print("Porażka, nie odnaleziono; sprawdzono: " +str(done))
    print("\t czas działania: " + str((end_moment - start_moment) / datetime.timedelta(seconds = 1, microseconds=1)) + "us")



# -

# 4 znakowe alfabet a 
hash_to_break = b'\xf1\xac\x93\xb9\\I\x00Z\x86r5\xc6\x19QDX\xb2{\xad2q8i\xc3\xb5%\xe74T\xc9\x04f'
break_password(hash_to_break, 4, alphabet_a)

# 5 znakowe alfabet a 
hash_to_break = b'0\xc2\xcbH\xd3c\xdb\x8d\xfc\xcd\x7f\xb8(\xea=w=/N\x88\xe8&\xa1F-\x9e\x15\x02\xc9\xc2\x85\x85'
break_password(hash_to_break, 4, alphabet_a)

# ### Przeszukanie

#pełne przeszukanie 4a
hash_to_break = b''
break_password(hash_to_break, 4, alphabet_a)


#pełne przeszukanie 5a
hash_to_break = b''
break_password(hash_to_break, 5, alphabet_a)

#pełne przeszukanie 6a
hash_to_break = b''
break_password(hash_to_break, 6, alphabet_a)

#pełne przeszukanie 4Aa
hash_to_break = b''
break_password(hash_to_break, 4, alphabet_Aa)

StarcoderdataJupyterScriptsDedupFiltered