I wrote a SHA256 password cracker. I initially used lists to store the information in list1.txt
and list2.txt
but I would end up getting memory overloads. So I now use files.
I noticed that when I made this transition, the programme speed reduced hugely. Is there a way to optimise the speed of this programming without running into memory overload issues?
Thanks
import hashlib
import time
import os
def extract_password(fname):
if fname == 0:
filename = "list2.txt"
else:
filename = "list1.txt"
for line in open(filename, "r"):
yield line.strip()
def save_password(fname, password):
if fname == 0:
filename = "list1.txt"
else:
filename = "list2.txt"
file = open(filename, "a")
file.write(password)
file.write("\n")
file.close()
def next_password(lsta, item,next_char, secure_password):
found = 0
guess = item + chr(next_char)
if hash_password(guess) == secure_password:
print(guess)
found = 1
save_password(lsta, guess)
return found
def hash_password(pwrd):
#Generates hash of original password
pwrd = pwrd.encode("UTF-8")
password = hashlib.sha256()
password.update(pwrd)
return password.hexdigest()
def delete_file(ltsa):
try:
if ltsa == 1:
os.remove("list2.txt")
else:
os.remove("list1.txt")
except:
pass
def reset_file_status():
try:
os.remove("list2.txt")
os.remove("list1.txt")
except:
pass
def find_password(secure_password):
#Brute force to find original password
found = 0
lsta = 1
for length in range(1, 15):
if found == 1: break
lsta = lsta^1
delete_file(lsta)
for next_char in range(65, 123):
if found == 1: break
if length == 1:
found = next_password(lsta, "", next_char, secure_password)
if found == 1: break
else:
for item in extract_password(lsta):
found = next_password(lsta, item, next_char, secure_password)
if found == 1: break
if __name__ == "__main__":
reset_file_status()
start = time.time()
secure_password = hash_password("AAAA")
find_password(secure_password)
print(f"{(time.time() - start)} seconds")