Skip to main content
YJD𝕏

The Ultimate Password Generator (DAY 1)

If you've ever used Bitwarden to manage your passwords, you might have noticed the built-in password generator which allows you to generate secure passwords and passphrases. I have attempted to recreate this password generate using python with a comman-line interface.

I first started with the passphrase feature which included:

I sourced the words from a github repository and stored in a text file. I then imported this into a list in the program.

The Code #

import random
import sys
import time

WORD_COUNT = 0
INCLUDE_NUM = False
INCLUDE_SYMBOLS = False

def create(words, symbols, delim):
	global WORD_COUNT
	global INCLUDE_NUM
	global INCLUDE_SYMBOLS

	pass_words = []
	for i in range(WORD_COUNT):
		pass_words.append(random.choice(words))

	# Include numbers if opted for
	num_words = []
	if INCLUDE_NUM == True:
		for i in range(WORD_COUNT):
			num_words.append(random.randint(1, 100))

	# Include symbols if opted for
	sym_words= []
	if INCLUDE_SYMBOLS == True:
		for i in range(WORD_COUNT):
			sym_words.append(random.choice(symbols))

	passphrase = ''
	order = []
	for x in range(WORD_COUNT):
		# Generating the order
		while True:
			num = random.randint(1,3)
			if num not in order:
				order.append(num)
			if len(order) == 3:
				break

		for i in order:
			if i == 1:
				passphrase+=pass_words[x]
			elif i == 2:
				if INCLUDE_NUM == True:
					passphrase+=str(num_words[x])
			elif i == 3:
				if INCLUDE_SYMBOLS == True:
					passphrase+=sym_words[x]
		if x != WORD_COUNT - 1:
			passphrase+=delim
		order = []

	print('\nYour password is', passphrase,'\n')

	# Generating a new password
	if input('Would you like to start over? (y/n): ') == 'y':
		start()
	else:
		sys.exit()

def start():
	global WORD_COUNT
	global INCLUDE_NUM
	global INCLUDE_SYMBOLS

	# Accept the word count as user input
	while True:
		WORD_COUNT = int(input('Enter the number of words: '))
		if WORD_COUNT >= 1:
			break
		else:
			print('The number of words should be greater than "0"!\n')

	# Asking the user whether numbers should be included
	while True:
		INCLUDE_NUM = input('Should numbers be included? (y/n): ')
		if INCLUDE_NUM == 'y' or INCLUDE_NUM == 'n':
			if INCLUDE_NUM == 'y':
				INCLUDE_NUM = True
			elif INCLUDE_NUM == 'n':
				INCLUDE_NUM = False
			break
		else:
			print('Either "y" or "n" should be entered!\n')

	# Asking the user whether symbols should be included
	while True:
		INCLUDE_SYMBOLS = input('Should symbols be included? (y/n): ')
		if INCLUDE_SYMBOLS == 'y' or INCLUDE_SYMBOLS== 'n':
			if INCLUDE_SYMBOLS == 'y':
				INCLUDE_SYMBOLS = True
			elif INCLUDE_SYMBOLS == 'n':
				INCLUDE_SYMBOLS = False
			break
		else:
			print('Either "y" or "n" should be entered!\n')

	# Asking the user to choose a one character deliminator
	delim = ''
	while True:
		delim = input('Enter a one character deliminator: ')
		if len(delim) == 1 or len(delim) == 0:
			break
		else:
			print('The deliminator chosen should be a single charcacter!\n')

	# Extract the words from words.txt #
	words_file = open('words.txt', 'r')
	words = []
	while True:
		line = words_file.readline().strip()
		if len(line) == 0:
			break
		else:
			words.append(line)
	#----------------------------------#
	
	symbols = ('!','@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '+', '=', '{', '}', '[', ']','.','<', '>', '?', '/', '|', '\\', ';', ':', ',', '~', '`', "'", '"')
	create(words, symbols, delim)

print('***********************************')
print('* THE ULTIMATE PASSWORD GENERATOR *')
print('***********************************')
print()
start()

Sample Ouptut #

***********************************
* THE ULTIMATE PASSWORD GENERATOR *
***********************************

Enter the number of words: 7
Should numbers be included? (y/n): y
Should symbols be included? (y/n): y
Enter a one character deliminator: -

Your password is secretariat<36-"lie6-`37anthony-)exposure96-wet15{-^71fathers-/35experiments 

Would you like to start over? (y/n): n

At the very end, the user is asked whether a new passphrase should be generated or not