I created a fun password cracker using literal brute force, searching each character to see if it matches an ASCII character 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
. The password is randomly generated and will vary from 100,000 to 250,000 characters long. I use a wrapper timeit
function to time the function and the output is a basic print
statement using .format()
:
import random
import time
password = ""
attempted_password = ""
list_of_chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
for letter in range(0, random.randint(100000, 250000)):
password += list_of_chars[random.randint(0, 61)]
def timeit(func):
def wrapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
print "The function {.__name__} took {:.15f} seconds to finish.".format(func, time.time() - start)
return result
return wrapper
@timeit
def solve_password(word):
global attempted_password
for character in word:
for entry in list_of_chars:
if character == entry:
attempted_password += character
continue
return attempted_password
print "The password: {0:}\nLength of password was: {1:}\nIs correct? : {2:}".format(solve_password(password),
len(password),
attempted_password == password)
An example output (without the password):
The function solve_password took 3.540999889373779 seconds to finish.
The password: *password is here*
Length of password was: 246416
Is correct? : True
So my questions are:
Am I following coding standards for Python 2 (like PEP8)
Is there anyway to improve performance, readability, etc.
Is there any way to make my code more "Pythonic" (like a native Python coder)?