How to Create a Secure Password Generator in Kali Linux using the `secrets` Module of Python
In today’s digital world, secure passwords are very important to protect our personal information. Creating strong and hard-to-guess passwords is an easy and effective way to improve our security. In this guide, we will learn how to make a secure password generator using Python’s secrets module in Kali Linux. This module helps create random and secure numbers, making it perfect for creating passwords, tokens, and account authentications.
Creating a secure password generator in Kali Linux using Python is a fun and practical way to improve your programming skills while learning about security. In my journey to learn beginner Python, I found this project both educational and rewarding. Here’s a step-by-step guide to help you create a password generator with Python’s secrets module. By following this guide, you will not only get better at programming but also make your security practices much stronger.
Expanding on the Code
We will also explain the imported modules:
- random: This module generates random numbers. It is useful for various applications but not secure enough for cryptographic purposes.
- string: This module provides a collection of string constants such as letters and digits, which are useful for creating character sets.
- secrets: This module generates cryptographically secure random numbers, making it ideal for managing sensitive data like passwords and tokens.
Understanding these modules will help you see how they contribute to the security and functionality of the password generator.
Step 1: Setting Up Your Environment
Updating and Upgrading Your System
Before you start, it’s a good practice to update and upgrade your system to ensure you have the latest software and security updates. Open your terminal and type
sudo apt-get update && sudo apt-get upgrade
Installing Python
First, ensure you have Python installed. Open your terminal and type:
python3 --version
If Python is not installed, install it using:
sudo apt-get install python3
Step 2: Writing the Code
Creating the Script
Open your terminal and create a new Python file using text editor `nano`:
nano password_generator.py
Writing the Code
Copy and paste the following code into `nano`:
import random
import string
import secrets
def generate_password(nr_letters, nr_symbols, nr_numbers):
if nr_letters < 0 or nr_symbols < 0 or nr_numbers < 0:
raise ValueError("Number of letters, symbols, and numbers must be non-negative")
letters = string.ascii_letters
symbols = '!#$%&()*+'
numbers = string.digits
password_list = []
for _ in range(nr_letters):
password_list.append(secrets.choice(letters))
for _ in range(nr_symbols):
password_list.append(secrets.choice(symbols))
for _ in range(nr_numbers):
password_list.append(secrets.choice(numbers))
random.shuffle(password_list)
password = ''.join(password_list)
return password
def main():
print("Welcome to the Secure PyPassword Generator!")
while True:
try:
nr_letters = int(input("How many letters would you like in your password?\n"))
nr_symbols = int(input("How many symbols would you like?\n"))
nr_numbers = int(input("How many numbers would you like?\n"))
if nr_letters < 0 or nr_symbols < 0 or nr_numbers < 0:
raise ValueError
break
except ValueError:
print("Please enter a valid non-negative integer.")
password = generate_password(nr_letters, nr_symbols, nr_numbers)
print(f"Your password is: {password}")
if __name__ == "__main__":
main()
Explanation of the Code
Imports
- random, string, and secrets modules: These modules are imported for generating cryptographically secure random numbers and handling strings.
Function: generate_password
- Arguments:
nr_letters
,nr_symbols
,nr_numbers
. - Validation: Checks if arguments are non-negative integers.
- Character Sets: Defines sets for letters, symbols, and numbers.
- Password Generation:
- Creates password_list by appending random selections of characters.
- Shuffles password_list and joins it into a single password string.
Function: main
- User Interaction: Prompts the user for the number of letters, symbols, and numbers.
- Validation: Ensures inputs are valid non-negative integers.
- Password Generation: Calls generate_password and prints the result.
Execution Block
- Ensures the script runs the
main
function if executed as the main program.
Character Sets
pythonletters = string.ascii_letters
numbers = string.digits
symbols = '!#$%&()*+'
letters
: All ASCII letters.numbers
: All digit characters.symbols
: Common password symbols.
Password Generation
password_list = []
for _ in range(nr_letters):
password_list.append(secrets.choice(letters))
for _ in range(nr_symbols):
password_list.append(secrets.choice(symbols))
for _ in range(nr_numbers):
password_list.append(secrets.choice(numbers))
secrets.SystemRandom().shuffle(password_list)
password = ''.join(password_list)
return password
- password_list: Stores randomly chosen characters.
- secrets.choice(): Selects a random character from the set.
- shuffle(): Ensures the password is randomized.
- ‘’.join(password_list): Forms the final password.
Input and Output
nr_letters = int(input("How many letters would you like in your password?\n"))
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))
password = generate_password(nr_letters, nr_symbols, nr_numbers)
print(f"Your password is: {password}")
- input(): Collects user input.
- generate_password(): Generates a secure password with the inputs.
- print(): Outputs the generated password.
Saving and Exiting
Save the file and exit `nano` by pressing `Ctrl + O` to save and Enter and `Ctrl + X` to exit.
Step 3: Running Your Script
Now run it:
python3 password_generator.py
Follow the prompts to generate a secure password.
Wrapping Up
You’ve now created a secure password generator using Python’s `secrets` module. This script ensures your passwords are generated with strong randomness, enhancing security. Feel free to customize and improve the script as you learn more about Python and security.