Search code examples
pythonpython-itertoolscombinatorics

Using itertools in Python password cracker


I made a simple password cracker:

import string as s
import itertools as it

things = s.ascii_letters

for digits in range(2, 6):
    for combo in it.combinations(things, digits):
        output = ''.join(combo)
        print(output)

but it doesn't print out all possibilities.

For example, the last output this program gives out is:

VWXYZ

If I had a password starting with "X", the program wouldn't be able to find it.

Is there any way I can fix this? What is wrong with my code?


Solution

  • Use itertools.product() with the repeat parameter. Using itertools.combinations() doesn't allow for the same letter to be selected more than once:

    import string as s
    import itertools as it
    
    things = s.ascii_letters
    
    for digits in range(2, 6):
        for combo in it.product(things, repeat=digits):
            output = ''.join(combo)
            print(output)