Skip to content

Instantly share code, notes, and snippets.

@a904guy
Last active March 24, 2018 14:38
Show Gist options
  • Save a904guy/1067b8e51c982119963fe14f94c0f534 to your computer and use it in GitHub Desktop.
Save a904guy/1067b8e51c982119963fe14f94c0f534 to your computer and use it in GitHub Desktop.
Wordlist with Specific Characters Generator (Python 3.5+ crunch). 3.5+ Because of Annotations, Just remove them for Python 2 or < 3.5
import itertools
import string
from typing import List, Union
class crunch:
char_list: List[str] = list(string.ascii_uppercase) + list(string.ascii_lowercase) + list(string.digits)
min: int = 1
max: int = 6
resume: Union[None, str] = None
def __init__(self, char_list: Union[str, list] = None, min_length: int = None, max_length: int = None, resume: str = None):
if char_list:
assert isinstance(char_list, (str, list))
if isinstance(char_list, list):
self.char_list = char_list
if isinstance(char_list, str):
self.char_list = char_list.split()
if min_length:
assert isinstance(min_length, int)
self.min = min_length
if max_length:
assert isinstance(max_length, int)
self.max = max_length
if resume:
assert isinstance(resume, str)
self.resume = resume
def iterator(self) -> iter:
# Warning Resume takes a while as it has to iterate back to the `resume == it`
for r in range(self.min, self.max):
for it in map("".join, itertools.product(self.char_list, repeat=r)):
if self.resume is not None and self.resume != it:
continue
elif self.resume == it:
self.resume = None
yield it
def __iter__(self) -> iter:
return self.iterator()
def __enter__(self) -> iter:
return self.iterator()
def __exit__(self, exc_type, exc_val, exc_tb):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment