Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions code/code/cryptography/src/password_generator/passwordgenerator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This program will generate a strong password using cryptography principles

# Import secrets and string module
# The string module defines the alphabet, and the secret module generates cryptographically sound random numbers
import secrets
import string

# Define the alphabet to be digits, letters, and special characters
letters = string.ascii_letters
digits = string.digits
special = string.punctuation
alphabet = letters + digits + special

# Set the password length
while True:
try:
password_length = int(input("Please enter the length of your password: "))
break
except ValueError:
print("Please enter an integer length.")

# Generates strong password with at least one special character and one digit
while True:
password = ''
for i in range(password_length):
password += ''.join(secrets.choice(alphabet))
if (any(char in special for char in password) and any(char in digits for char in password)):
break

print("--------Your Password Has Been Generated---------")
print(password)
print("--------Make Sure To Keep Your Password Safe---------")