Ask me anything
Ithy Logo
Sign in to save

Creating a Python Script to Capture and Display Username and Password

Creating a Python Script to Capture and Display Username and Password

In this guide, we'll walk you through creating a Python script that prompts the user to enter a username and password, then displays the entered credentials. While this functionality is straightforward, it's crucial to understand the security implications associated with handling sensitive information like passwords.

Prerequisites

Before diving into the script, ensure you have Python installed on your system. You can download it from the official Python website.

Step-by-Step Implementation

1. Import Necessary Modules

To handle password input securely, we'll use Python's built-in getpass module. This module allows users to enter passwords without echoing them to the console, preventing shoulder surfing and enhancing security.

import getpass
    

2. Define Functions for Input

We'll create two functions: one to capture the username and another to capture the password securely.

Function to Get Username

def get_username():
        return input("Enter your username: ")
    

Function to Get Password

def get_password():
        return getpass.getpass("Enter your password: ")
    

3. Main Function to Handle User Input and Display Credentials

The main function orchestrates the input collection and displays the entered credentials.

def main():
        username = get_username()
        password = get_password()
        
        print(f"\nYou have entered:")
        print(f"Username: {username}")
        print(f"Password: {password}")
    

4. Entry Point of the Script

Ensuring that the script runs when executed directly.

if __name__ == "__main__":
        main()
    

Complete Script

Combining all the components, here's the full Python script:

import getpass

def get_username():
    return input("Enter your username: ")

def get_password():
    return getpass.getpass("Enter your password: ")

def main():
    username = get_username()
    password = get_password()
    
    print(f"\nYou have entered:")
    print(f"Username: {username}")
    print(f"Password: {password}")

if __name__ == "__main__":
    main()
    

Running the Script

Follow these steps to execute the script:

  1. Save the Script: Copy the complete script into a file named auth_script.py.

  2. Open Terminal or Command Prompt: Navigate to the directory containing auth_script.py.

  3. Execute the Script: Run the script using Python by typing:

    python auth_script.py
        
  4. Interaction Example:

    
        Enter your username: johndoe
        Enter your password: 
        You have entered:
        Username: johndoe
        Password: mysecretpassword
        

    Note: The password input will not be visible as you type if getpass is functioning correctly.

Security Considerations

While the script accomplishes the task of capturing and displaying user credentials, it's imperative to acknowledge the associated security risks:

  • Plain Text Display: Displaying passwords in plain text poses a significant security threat. If an unauthorized individual gains access to the output, they can easily decipher the password.

  • Storage Risks: Storing or logging passwords in plain text can lead to data breaches. Always ensure that sensitive information is handled securely.

  • Best Practices: In real-world applications, never display or store passwords in plain text. Instead, implement secure methods such as hashing and salting to protect user credentials.

Enhancing Security

To mitigate the risks associated with handling passwords, consider the following enhancements:

1. Hashing Passwords

Instead of displaying or storing passwords directly, use hashing algorithms like bcrypt or sha256 to convert passwords into fixed-size strings that are not easily reversible.

import hashlib

def hash_password(password):
    return hashlib.sha256(password.encode()).hexdigest()
    

2. Salting Passwords

Adding a unique salt to each password before hashing enhances security by preventing attackers from using precomputed tables (rainbow tables) to crack the hashes.

import os

def generate_salt():
    return os.urandom(16)
    
def hash_password(password, salt):
    return hashlib.sha256(salt + password.encode()).hexdigest()
    

3. Secure Storage

Store hashed and salted passwords in a secure database. Ensure that access to the database is restricted and that data is encrypted both in transit and at rest.

Conclusion

Creating a Python script to capture and display user credentials is a simple task, but it's accompanied by significant security considerations. While the provided script serves educational purposes, always prioritize security in real-world applications. Implementing best practices such as hashing, salting, and secure storage of passwords is essential to protect user data and maintain the integrity of your applications.

Additional Resources


December 26, 2024

AI-assisted research. Sources, methodology and corrections · Source-checked guides and tools · Explore published answers

Ask Ithy AI