Skip to content
Merged
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
125 changes: 71 additions & 54 deletions Password-Generator/password.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,80 @@
import random
import secrets
import string

def generatePassword(pwlength):

alphabet = "abcdefghijklmnopqrstuvwxyz"

passwords = []

for i in pwlength:

password = ""
for j in range(i):
next_letter_index = random.randrange(len(alphabet))
password = password + alphabet[next_letter_index]

password = replaceWithNumber(password)
password = replaceWithUppercaseLetter(password)

passwords.append(password)
def generate_password(length: int = 12, digits: bool = True, symbols: bool = True) -> str:

return passwords


def replaceWithNumber(pword):
for i in range(random.randrange(1,3)):
replace_index = random.randrange(len(pword)//2)
pword = pword[0:replace_index] + str(random.randrange(10)) + pword[replace_index+1:]
return pword


def replaceWithUppercaseLetter(pword):
for i in range(random.randrange(1,3)):
replace_index = random.randrange(len(pword)//2,len(pword))
pword = pword[0:replace_index] + pword[replace_index].upper() + pword[replace_index+1:]
return pword



def main():
min_required = int(digits) + int(symbols)
if length < min_required:
raise ValueError("Password length too small for selected options")

numPasswords = int(input("How many passwords do you want to generate? "))
pool = string.ascii_letters
required_chars = []

print("Generating " +str(numPasswords)+" passwords")

passwordLengths = []

print("Minimum length of password should be 3")
if digits:
pool += string.digits
required_chars.append(secrets.choice(string.digits))

for i in range(numPasswords):
length = int(input("Enter the length of Password #" + str(i+1) + " "))
if length<3:
length = 3
passwordLengths.append(length)
if symbols:
pool += string.punctuation
required_chars.append(secrets.choice(string.punctuation))

remaining_length = length - len(required_chars)

Password = generatePassword(passwordLengths)

for i in range(numPasswords):
print ("Password #"+str(i+1)+" = " + Password[i])

password = required_chars + [secrets.choice(pool) for _ in range(remaining_length)]
secrets.SystemRandom().shuffle(password)

return ''.join(password)

def save(passwords: list) -> None:
try:
agree: str = input("Do you want to save the passwords to a file? (y/n): ").lower().strip()
if agree not in ["y", "yes"]:
return
else:
fileName: str = input("Enter the name of the file (without .txt): ").strip()
if not fileName:
fileName: str = "passwords"
print("\nSaving passwords to file...")
with open(fileName + ".txt", "w") as f:
for password in passwords:
f.write(password + "\n")
print("Passwords saved successfully!")
except OSError as e:
print(f"Error saving passwords: {e}")

def main():
try:
length: int = int(input("Enter the length of the password: "))
count: int = int(input("Enter the number of passwords to generate: "))

if length < 1 or count < 1:
print("Please enter positive integers only.")
return

if length > 128:
print("Password length cannot be greater than 128 characters.")
return

if length < 6:
print("Password is too short - Generating Passwords of length 6")
length = 6

digits: bool = input("Include digits? (y/n): ").lower().strip() in ["y", "yes"]
symbols: bool = input("Include symbols? (y/n): ").lower().strip() in ["y", "yes"]

print("\nGenerating Secure Passwords...\n")
passwords = []
for i in range(count):
password = generate_password(length, digits=digits, symbols=symbols)
passwords.append(password)
print(f"Password #{i + 1}: {password}")

save(passwords)

except ValueError:
print("Please Enter Valid Integers Only")
except Exception as e:
print(f"An unexpected error occurred: {e}")

main()
if __name__ == "__main__":
main()
Loading