How can I create a simple python brute force function?

python-3.x

Solution

gen = itertools.combinations_with_replacement(characters,password_length) #1
for password in gen:                                                      #2 
    check_password(password)                                              #3

here's how it works:

- line 1: this creates a generator. it's like a function that remembers where it left off. Check this out for more info: http://getpython3.com/diveintopython3/generators.html. This particular generator goes through all possible combinations of the given characters of the given length.

- line 2: for each iteration of the for loop next(gen) is called. This yields the next value

- line 3: Do what you need to do

for example if `characters = '01234567890'` and `password_length = 2` then the loop will run through the combinations: `('0','0'), ('0','1'), ('0','2')...('0','9'),('1','0'),('1','1')...('9','9')`.

Problem

I am trying to create a function that will use brute force for an academic python project. The password can be limited I want to pass in the password and the function iterate through a set of characters(a-z,A-Z,0-9) trying combinations till the password is found. I know this will be inefficient so for testing lets assume the password is 4 characters long. Any help getting started on writing this function would be appreciated.

Original source