Skip to content

Instantly share code, notes, and snippets.

@bsidhom
Created August 4, 2018 17:44
Show Gist options
  • Save bsidhom/359c66a486361acf5d073c3d44d47d1e to your computer and use it in GitHub Desktop.
Save bsidhom/359c66a486361acf5d073c3d44d47d1e to your computer and use it in GitHub Desktop.
Password generator
#!/usr/bin/env python3
import argparse
import secrets
import string
def generate_diceware_password(length, word_file):
with open(word_file) as file:
lines = [line.rstrip() for line in file]
# TODO: Consider using only words longer than 3 chars.
return ' '.join(secrets.choice(lines) for i in range(length))
def generate_standard_password(length):
alphabet = string.ascii_letters + string.digits
return ''.join(secrets.choice(alphabet) for i in range(length))
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--length",
type=int,
required=True,
help="Pasword length (in words or characters)")
parser.add_argument(
"--diceware",
action="store_true",
help=
"Generate a diceware-style password rather than a string of random characters. Words will be randomly chosen from --word-file."
)
parser.add_argument(
"--word-file",
default="/usr/share/dict/words",
help=
"File to draw words from for diceword passwords. Each line should contain a single word."
)
args = parser.parse_args()
if args.diceware:
password = generate_diceware_password(args.length, args.word_file)
else:
password = generate_standard_password(args.length)
print(password)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment