This is what I wrote...
def brute(m,pattern=None):
letters = 'abcdefghijklmnopqrstuvwxyz'
spec = '#@&$%*() '
upper = letters.upper()
number = '1234567890'
info = {'@':spec,'^':upper,'%':letters,'*':number}
chars = [info.get(p,letters) for _,p in zip(range(m),pattern or letters)]
def inner(m):
if m:
for l in chars[~m]:
for j in inner(m-1):
yield(l j)
else:
for l in chars[~m]:
yield l
for i in inner(m-1):
print(i)
I want to know how to write a tool similar to crunch in kali...
I would be grateful if you could implement it in Python.
And why is my code so slow even when I write the output to file?? How to make it faster??
CodePudding user response:
Here is an itertools based approach which might do what you want:
import itertools, string
def brute(m,pattern=None):
if pattern is None:
pattern = '%'*m
letters = string.ascii_lowercase
upper = string.ascii_uppercase
spec = '#@&$%*() '
number = '1234567890'
info = {'@':spec,'^':upper,'%':letters,'*':number}
chars = [info.get(d,letters) for d in pattern]
return [''.join(p) for p in itertools.product(*chars)]
For example, words = brute(6,'@%%*@^')
takes about 2 seconds to evaluate to a list of 14236560
words.