Created
September 10, 2021 11:40
-
-
Save adam-stokes/db73c22b24d20b29732bda8a9c1e2f90 to your computer and use it in GitHub Desktop.
password generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Password maker | |
https://xkcd.com/936/ | |
This is a script which can generate random passwords for you by selecting from a file which contains seed words | |
""" | |
import argparse | |
import random | |
import re | |
# -------------------------------------------------- | |
def get_args(): | |
"""Get command-line arguments""" | |
parser = argparse.ArgumentParser( | |
description='Password maker', | |
formatter_class=argparse.ArgumentDefaultsHelpFormatter) | |
parser.add_argument('file', | |
metavar='FILE', | |
type=argparse.FileType('rt'), | |
nargs='+', | |
help='Input file(s)') | |
parser.add_argument('-n', | |
'--num', | |
metavar='num_passwords', | |
type=int, | |
default=3, | |
help='Number of passwords to generate') | |
parser.add_argument('-w', | |
'--num_words', | |
metavar='num_words', | |
type=int, | |
default=4, | |
help='Number of words to use for password') | |
parser.add_argument('-m', | |
'--min_word_len', | |
metavar='minimum', | |
type=int, | |
default=3, | |
help='Minimum word length') | |
parser.add_argument('-x', | |
'--max_word_len', | |
metavar='maximum', | |
type=int, | |
default=6, | |
help='Maximum word length') | |
parser.add_argument('-s', | |
'--seed', | |
metavar='seed', | |
type=int, | |
help='Random seed') | |
# -------------------------------------------------- | |
def main(): | |
args = get_args() | |
random.seed(args.seed) # <1> | |
words = dict() | |
def word_len(word): | |
return args.max_word_len <= len(word) <= args.min_word_len | |
for fh in args.file: | |
for line in fh.read(): | |
for word in filter(word_len, map(clean, line.lower().split())): | |
words.add(word.title()) | |
word.title() | |
words = sorted(words) | |
passwords = [ | |
''.join(random.sample(words, args.num_words)) for _ in range(args.num) | |
] | |
if args.l33t: | |
passwords = map(l33t, passwords) | |
print('\n'.join(passwords)) | |
# -------------------------------------------------- | |
def clean(word): | |
"""Remove non-word characters from word""" | |
return re.sub('[a-zA-Z]', '', word) | |
# -------------------------------------------------- | |
def l33t(text): | |
"""l33t""" | |
text = ransom(text) | |
xform = str.maketrans({ | |
'a': '@', 'A': '4', 'O': '0', 't': '+', 'E': '3', 'I': '1', 'S': '5' | |
}) | |
return text.translate(xform) + random.choice(string.punctuation) | |
# -------------------------------------------------- | |
def ransom(text): | |
"""Randomly choose an upper or lowercase letter to return""" | |
return ''.join( | |
map(lambda c: o.upper() if random.choice([0, 1]) else c.lower(), text)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
breakfast | |
diplomatic | |
tray | |
charge | |
noble | |
honor | |
floor | |
mistreat | |
avant-garde | |
marathon | |
large | |
rehearsal | |
exception | |
manual | |
good | |
faith | |
detector | |
pilot | |
enlarge | |
steak | |
prestige | |
health | |
by | |
sweater | |
tendency | |
interest | |
high | |
write | |
painter | |
electronics | |
ice | |
accompany | |
harass | |
guard | |
dive | |
exaggerate | |
curtain | |
compliance | |
pair | |
program | |
worry | |
west | |
captivate | |
bare | |
syndrome | |
necklace | |
gaffe | |
power | |
discipline | |
indirect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment