-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpass_gen.py
More file actions
53 lines (39 loc) · 1.92 KB
/
pass_gen.py
File metadata and controls
53 lines (39 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import random
import string
import argparse
def create_password(length=12, include_numbers=True, include_symbols=True):
"""Generates a secure, random password."""
# Start with the required pool of lowercase and uppercase letters
character_pool = string.ascii_letters
if include_numbers:
character_pool += string.digits
if include_symbols:
character_pool += string.punctuation
# Check if the character pool is empty (in case user excludes everything)
if not character_pool:
return "Error: Cannot generate password with no characters."
# Generate the password
# uses random.choices() to pick 'length' number of characters from the pool
password_list = random.choices(character_pool, k=length)
# Join the list of characters back into a single string
return "".join(password_list)
# This part makes the script runnable from the command line
if __name__ == "__main__":
# This sets up the command-line argument parser
parser = argparse.ArgumentParser(description="Generate a secure random password.")
# Add an argument for length, default is 12, type is integer
parser.add_argument("-l", "--length", type=int, default=12, help="Set the length of the password (default: 12)")
# Add flag to exclude numbers
parser.add_argument("--no-numbers", action="store_true", help="Do not include numbers in the password")
# Add flag to exclude symbols
parser.add_argument("--no-symbols", action="store_true", help="Do not include symbols in the password")
# Parse the arguments provided by the user
args = parser.parse_args()
# Generate the password
# Note: We pass the *opposite* of the "store_true" flags
new_password = create_password(
length=args.length,
include_numbers=not args.no_numbers,
include_symbols=not args.no_symbols
)
print(f"Generated Password: {new_password}")