Navbar menu

Building a Custom Password Generator in Python: A Comprehensive Guide

In today's digital age, security is paramount. One of the simplest yet crucial aspects of digital security is having strong passwords. In this blog post, we'll dive into creating a Python script that generates secure passwords based on user preferences. The source code is at the end of this post.

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.
 
import string
import secrets
2. Helper Functions: 
    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 False
3. Password Generation Function: The generate_password function creates a new password based on the specified length and user preferences including symbols and uppercase letters. It builds a combination of characters based on the options selected and generates a random password using secrets.randbelow.

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
  
4. Main Function and User Input: In the main function, we ask the user for input regarding the number of passwords needed, the length of each password, and whether to include symbols and uppercase letters.
 
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"
  
Finally, it generates the requested number of passwords based on the user's input and displays each password along with its characteristics (whether it contains uppercase letters and special symbols).
 
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})")
 
How to Use the Password Generator
1. Run the Script: Execute the script in your Python environment. 

Building a Custom Password Generator in Python






2. Input Parameters: 
    • Number of passwords needed. 
    • Desired password length.
    • Include special characters (yes/no).
    • Include uppercase letters (yes/no).

Building a Custom Password Generator in Python











3. Generated Passwords: The script will then generate and display the requested passwords along with their characteristics (uppercase letters and special symbols).

Building a Custom Password Generator in Python






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})")    
GitHub link: https://github.com/JasierDeen/Python_Basic_Projects/blob/main/src/password_generator/password_generator.py

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