Generator of all possible 8 symbols strings. Brute force 8 symbol password. python
I need to write generator which yield all possible 8 symbols strings. From array of symbols like this:
leters = ['1','2','3','4','5','6','7','8','9','0','q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l','z','x','c','v','b','n','m']
The skeleton looks like this:
def generator():
""开发者_如何学C"
here algorithm
"""
yield string
suppose to return list like this ['00000001','00000002','00000003', ......'mmmmmmmm']
itertools.combinations()
and itertools.combinations_with_replacement()
return a generator
>>> letters = ['a', 'b', 'c']
>>> from itertools import combinations
I am using print()
in the examples to illustrate the output. Substitute it with yield
, to get a generator.
>>> for c in combinations(letters, 2):
print(c)
...
('a', 'b')
('a', 'c')
('b', 'c')
>>> for c in combinations(letters, 2):
print(''.join(c))
...
ab
ac
bc
>>>
>>> for c in itertools.combinations_with_replacement(letters, 2):
print(''.join(c))
...
aa
ab
ac
bb
bc
cc
If you brute force it for all 8 letter passwords containing english letters and digits, you're looking to iterate over ~ 2.8 trillion strings
EDIT
If you somehow know there are no repeated elements, use permutations
>>> for c in itertools.permutations(letters, 2):
print(''.join(c))
...
ab
ac
ba
bc
ca
cb
this gives you both ab and ba
For the most general brute force sequence use itertools.product()
as in Cosmologicon's solution
itertools.product(leters, repeat=8)
EDIT: to have it give you strings rather than tuples:
def generator(leters):
a = itertools.product(leters,repeat=3)
while a:
yield "".join(a.next())
import itertools
itertools.combinations_with_replacement(leters, 8)
By the way, letters has two T's.
I was wondering how to do this as well and this is what I came up with I tried it a couple ways but when I wrote it like this it went way quicker than the others...please lmk if I am not seeing
import string
from itertools import permutations
[print(*p,sep='')for p in permutations(list(string.ascii_letters+string.punctuation+string.digits),8)]
精彩评论