(30-60min)
As biometric authentication and passkeys become more popular, we have to remember fewer passwords. But some devices and services still need passwords. One method is to pick four random words from the dictionary to generate a passphrase[1] .
Write a program that generates 10 passphrases consisting of four words randomly picked from the word list words.txt:
Random passphrases:
UnfreezeSandbankRevivableHeader
QuartetOverlordSpoilsOverall
DealmakerDroopValiumDeeply
MonotoneAghastAwaitRemnant
UselesslyMarinaDreadfulSuspect
WillfullyAnteaterTroubleJumble
PretendedReminderEngravingDeniable
BackpedalReprintAwakenAffirm
TrapperCuppedStrungBlend
ComplyVitallySpreeProtrude
Example interaction
Use the following template:
passphrase_generator.template.py
"""Generates a passphrase based on four words from the English dictionary"""
FILENAME = "words.txt"
PASSPHRASE_WORD_COUNT = 4
PASSPHRASE_COUNT = 10
WORD_LIST_URL = "https://www.eff.org/files/2016/07/18/eff_large_wordlist.txt"
WORD_START_INDEX = 6
import urllib.request
from random import randint
try:
with open(FILENAME) as _:
pass
except FileNotFoundError:
_ = urllib.request.urlretrieve(WORD_LIST_URL, FILENAME)
def line_count(filename: str) -> int:
"""Returns the number non-empty lines in a file"""
## YOUR CODE HERE
return count
def get_word(line_number: int = 1, filename: str = FILENAME) -> str:
"""Returns words at a specific line number"""
## YOUR CODE HERE
def random_passphrase(
word_count: int = PASSPHRASE_WORD_COUNT, words_filename: str = FILENAME
) -> str:
words_file_word_count = line_count(words_filename)
password = ""
## YOUR CODE HERE
return password
print("Random passphrases:")
## YOUR CODE HERE
The template downloads words.txt if the file does not exist. The file contains lines like:
64364 unpledged
64365 unplowed
64366 unplug
64411 unpopular
64412 unproven
64413 unquoteThe file is designed for that you throw a dice five times to pick a single word from the file. However, for a computer, it would be easier to generate a random number in the range [1, number_of_lines] and then reading the word at the random line.
To extract the actual word with the first letter capitalized, use the following code[2] .
WORD_START_INDEX = 6
# Capitalizes the first letter and excludes the newline
line[WORD_START_INDEX].upper() + line[WORD_START_INDEX+1:-1]Explanation: The words begin at the 7th position of each line, which corresponds to the index 6. We capitalize the first letter. The last character is a newline, to exclude the newline character, we use -1.