Understanding the Code:
Let's break down the code step by step: 
1. Imports: We start by importing the necessary modules string and secrets. string provides a collection of string constants, and secrets is used for generating secure random numbers suitable for managing data such as passwords.
    • contains_upper(password): Checks if the password contains uppercase letters. 
    • contains_symbols(password): Checks if the password contains special symbols.
def contains_upper(password):
    for char in password:
        if char.isupper():
            return True
    return False    
def contains_symbols(password):
    for char in password:
        if char in string.punctuation:
            return True
    return Falsedef generate_password(length, symbols, upper_case):
    combination = string.ascii_lowercase + string.digits
    if symbols:
        combination += string.punctuation
    if upper_case:
        combination += string.ascii_uppercase
    combination_length = len(combination)
    new_password = ''
    for _ in range(length):
        new_password += combination[secrets.randbelow(combination_length)]
    return new_password
  if __name__ == "__main__":
    print("\n---- PASSWORD GENERATOR ----\n")
    
    no_of_passwords = int(input("How many password/s you need: "))
    password_length = int(input("\nEnter the length of the password/s ? "))
    has_symbols = input("\nDo you want special characters included in your password (y/n) ? ").lower() == "y"
    has_upper_case = input("\nDo you want upper case included in your password (yes/no) ? ").lower() == "y"
  print("\nYour password/s: \n")
    for i in range(no_of_passwords):
        new_pass = generate_password(password_length, has_symbols, has_upper_case)
        specs = f"U: {contains_upper(new_pass)}, S: {contains_symbols(new_pass)}"
        print(f"{i} --> {new_pass} ({specs})")
 1. Run the Script: Execute the script in your Python environment. 
2. Input Parameters: 
    • Number of passwords needed. 
    • Desired password length.
    • Include special characters (yes/no).
    • Include uppercase letters (yes/no).
3. Generated Passwords: The script will then generate and display the requested passwords along with their characteristics (uppercase letters and special symbols).
Explanation video:
Source Code:
import string
import secrets
def contains_upper(password):
    for char in password:
        if char.isupper():
            return True
    return False    
def contains_symbols(password):
    for char in password:
        if char in string.punctuation:
            return True
    return False
def generate_password(length, symbols, upper_case):
    combination = string.ascii_lowercase + string.digits
    if symbols:
        combination += string.punctuation
    if upper_case:
        combination += string.ascii_uppercase
    combination_length = len(combination)
    new_password = ''
    for _ in range(length):
        new_password += combination[secrets.randbelow(combination_length)]
    return new_password
if __name__ == "__main__":
    print("\n---- PASSWORD GENERATOR ----\n")
    
    no_of_passwords = int(input("How many password/s you need: "))
    password_length = int(input("\nEnter the length of the password/s ? "))
    has_symbols = input("\nDo you want special characters included in your password (y/n) ? ").lower() == "y"
    has_upper_case = input("\nDo you want upper case included in your password (yes/no) ? ").lower() == "y"
    
    print("\nYour password/s: \n")
    for i in range(no_of_passwords):
        new_pass = generate_password(password_length, has_symbols, has_upper_case)
        specs = f"U: {contains_upper(new_pass)}, S: {contains_symbols(new_pass)}"
        print(f"{i} --> {new_pass} ({specs})")    
Conclusion: 
With the code provided, you can create a robust password generator tailored to your specific security needs. Remember, using strong and unique passwords is crucial for safeguarding your digital assets.
Feel free to modify the code further to suit additional requirements or integrate it into your applications for enhanced security measures.
No comments:
Post a Comment