This Python script generates a random password based on user-specified criteria.
- Generates passwords of a specified length.
- Allows users to specify the number of special characters, numbers, uppercase letters, and lowercase letters.
- Provides an option to save the generated password to a file.
- Includes basic input validation to prevent invalid password configurations.
- If a character count vlaue is set to 0 the password will not use that character type.
- Python 3
-
Clone or download passgen:
git clone https://github.com/welbornt/passgen.git
or download passgen.py directly.
-
Run passgen from the command line:
python3 passgen.py [options]
-L,--length: Length of the password (default: 12).-s,--special: Number of special characters (punctuation) in the password (default: 2).-n,--numbers: Number of numbers in the password (default: 2).-c,--capital: Number of capital letters in the password (default: 2).-l,--lower: Number of lowercase letters in the password (default: 2).-f,--file: File path to save the password to.
-
Generate a password with default settings:
python3 passgen.py
-
Generate a password with length 16, 4 special characters, 4 numbers, 4 capital letters, and 4 lowercase letters:
python3 passgen.py -L 16 -s 4 -n 4 -c 4 -l 4
-
Generate a password and save it to a file named
password.txt:python3 passgen.py -f password.txt
-
Generate a password of length 20, and save it to a file named mysecurepassword.txt
python3 passgen.py -L 20 -f mysecurepassword.txt
-
Generate a password of length 100 using only special characters:
python3 passgen.py -L 100 -n 0 -c 0 -l 0
- passgen will display an error message and exit if the specified password length is less than 8 or greater than 128.
- passgen will display an error message and exit if the sum of special, numbers, capital, and lowercase characters exceeds the specified password length.
passgen utilizes the argparse module to handle command line arguments, and the random and string modules to generate the password.
- Argument Parsing:
- passgen parses command-line arguments for password length, number of special characters, numbers, capital letters, lowercase letters, and file output.
- Input Validation:
- It checks if the password length is within the valid range (8-128).
- It checks if the sum of character types does not exceed the password length.
- Password Generation:
- It initializes an empty list
passwordto store the password characters. - It randomly places the specified number of special characters, numbers, capital letters, and lowercase letters in the password list.
- It fills the remaining positions in the password list with random characters from the specified character sets.
- It initializes an empty list
- Output:
- It prints the generated password to the console or saves it to a file, as specified by the user.
- This script is a proof of concept only and should not be used in environments with high security requirements.