Untitled

mail@pastecode.io avatar
unknown
python
3 years ago
1.7 kB
4
Indexable
import string
import time
import random

attempts = 0
pass_to_guess_len = 0


def calculate_max_attempt_num(pass_to_guess, alphabet_size):
    result = 0
    for i in range(0, len(pass_to_guess)):
        result = result + pow(alphabet_size, i + 1)

    return result


def find_pass_match(pass_to_guess, current_pass, available_chars):
    global attempts
    global pass_to_guess_len

    current_pass_len = len(current_pass)
    if current_pass_len == pass_to_guess_len:
        return False

    for char_to_try in available_chars:
        attempts += 1
        pass_to_try = current_pass + char_to_try

        if pass_to_try == pass_to_guess:
            return True
        else:
            pass_to_try_matches = find_pass_match(pass_to_guess, pass_to_try, available_chars)
            if pass_to_try_matches:
                return True

    return False


password = "vladi"
pass_to_guess_len = len(password)
pass_characters = list(string.ascii_lowercase) + list(string.digits) + list(string.ascii_uppercase)
random.shuffle(pass_characters)

max_attempts_num = calculate_max_attempt_num(password, len(pass_characters))
print("Max number of attempts: {:,}".format(max_attempts_num))

start_time = time.time()
match_found = find_pass_match(password, "", pass_characters)
end_time = time.time()

if match_found:
    time_took_sec = end_time - start_time
    attempts_per_sec = attempts / time_took_sec if time_took_sec != 0 else float('inf')
    print("Found match in {:,} attempts, took {:.2f} seconds ({:,.0f} attempts per second)".format(attempts, time_took_sec, attempts_per_sec))