SHA256 password cracker - brute force 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 = &quot;list2.txt&quot;
    else:
        filename = &quot;list1.txt&quot;
    for line in open(filename, &quot;r&quot;):
        yield line.strip()


def save_password(fname, password):
    if fname == 0:
        filename = &quot;list1.txt&quot;
    else:
        filename = &quot;list2.txt&quot;

    file = open(filename, &quot;a&quot;)
    file.write(password)
    file.write(&quot;\n&quot;)
    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(&quot;UTF-8&quot;)
    password = hashlib.sha256()
    password.update(pwrd)
    return password.hexdigest()

def delete_file(ltsa):
    try:
        if ltsa == 1:
            os.remove(&quot;list2.txt&quot;)
        else:
            os.remove(&quot;list1.txt&quot;)
    except:
        pass

def reset_file_status():
    try:
        os.remove(&quot;list2.txt&quot;)
        os.remove(&quot;list1.txt&quot;)
    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, &quot;&quot;, 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__ == &quot;__main__&quot;:
    reset_file_status()
    start = time.time()
    secure_password = hash_password(&quot;AAAA&quot;)
    find_password(secure_password)
    print(f&quot;{(time.time() - start)} seconds&quot;)
The optimization you are looking for is very simple:
Ask your self - what do I really need to keep?
The answer: only the password guess I am currently checking.
There is no need to store all the old guesses, so there is no need for files or huge lists.
All you really need is one string you will keep updating.
To visualize this, think of your string as a growing number, with each letter being a base 58 digit.
Now. all you really need is to do a +1 on the first digit, checking for carry and updating following digits if needed just like usual addition.
Unfortunately, Python strings don't allow assignment by index, but they do support slicing.
Here is a function that would generate sequential passwords running through all the letters and growing the length as needed (one password per call!):
def make_next_guess(guess):
    carry = 1
    next_guess = guess

    for i in range(len(guess)):
        cur_char = ord(guess[i]) + carry
        if cur_char &gt; ord('z'):
            cur_char = ord('A')
            carry = 1
        else:
            carry = 0

        next_guess = next_guess[:i] + chr(cur_char) + guess[i + 1:]
        if carry == 0:
             break

    if carry = 1:
        next_guess += 'A'

    return next_guess

With this you can use one loop for all the possibilities up to the maximum length:
guess = 'A'

for _ in range(58 ** 14): #password maximum length 14 and there are 58 characters that can be used
    
    if hash_password(guess) == secure_password:
        print(guess)
        break

    guess = make_next_guess(guess)