Skip to content

Instantly share code, notes, and snippets.

@nathants
Last active August 20, 2022 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathants/7d7e539d22234b316cff045f85c8160e to your computer and use it in GitHub Desktop.
Save nathants/7d7e539d22234b316cff045f85c8160e to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# The MIT License (MIT)
# Copyright (c) 2022-present Nathan Todd-Stone
# https://en.wikipedia.org/wiki/MIT_License#License_terms
import secrets
import sys
import re
# parse dict words
dict_path = '/usr/share/dict/words'
with open(dict_path) as f:
dict_words = f.read().lower().splitlines()
word_regex = r'^[a-z]+$'
dict_words = list({word for word in dict_words if re.search(word_regex, word)})
num_dict_words = len(dict_words)
# prompt user input
print(f'number of words in {dict_path} matching regex "{word_regex}": {num_dict_words:,}', file=sys.stderr)
print('how many words would you like? ', file=sys.stderr, end='')
num_words = int(input())
print(f'number of possible passwords of word length {num_words}: {num_dict_words ** num_words:,}', file=sys.stderr)
# generate password
print('\n' + ' '.join([dict_words[secrets.randbelow(num_dict_words)] for _ in range(num_words)]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment