Skip to content

Commit 5cdb58b

Browse files
committed
add password generator tutorial
1 parent c6be8b8 commit 5cdb58b

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
3636
- [How to Extract Chrome Cookies in Python](https://www.thepythoncode.com/article/extract-chrome-cookies-python). ([code](ethical-hacking/chrome-cookie-extractor))
3737
- [How to Extract Saved WiFi Passwords in Python](https://www.thepythoncode.com/article/extract-saved-wifi-passwords-in-python). ([code](ethical-hacking/get-wifi-passwords))
3838
- [How to Make a MAC Address Changer in Python](https://www.thepythoncode.com/article/make-a-mac-address-changer-in-python). ([code](ethical-hacking/mac-address-changer))
39+
- [How to Make a Password Generator in Python](https://www.thepythoncode.com/article/make-a-password-generator-in-python). ([code](ethical-hacking/password-generator))
3940

4041
- ### [Machine Learning](https://www.thepythoncode.com/topic/machine-learning)
4142
- ### [Natural Language Processing](https://www.thepythoncode.com/topic/nlp)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Make a Password Generator in Python](https://www.thepythoncode.com/article/make-a-password-generator-in-python)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
from argparse import ArgumentParser
2+
import random
3+
import string
4+
5+
# Setting up the Argument Parser
6+
parser = ArgumentParser(
7+
prog='Password Generator.',
8+
description='Generate any number of passwords with this tool.'
9+
)
10+
11+
# Adding the arguments to the parser
12+
parser.add_argument("-n", "--numbers", default=0, help="Number of digits in the PW", type=int)
13+
parser.add_argument("-l", "--lowercase", default=0, help="Number of lowercase chars in the PW", type=int)
14+
parser.add_argument("-u", "--uppercase", default=0, help="Number of uppercase chars in the PW", type=int)
15+
parser.add_argument("-s", "--special-chars", default=0, help="Number of special chars in the PW", type=int)
16+
17+
# add total pw length argument
18+
parser.add_argument("-t", "--total-length", type=int,
19+
help="The total password length. If passed, it will ignore -n, -l, -u and -s, " \
20+
"and generate completely random passwords with the specified length")
21+
22+
# The amount is a number so we check it to be of type int.
23+
parser.add_argument("-a", "--amount", default=1, type=int)
24+
parser.add_argument("-o", "--output-file")
25+
26+
# Parsing the command line arguments.
27+
args = parser.parse_args()
28+
29+
# list of passwords
30+
passwords = []
31+
# Looping through the amount of passwords.
32+
for _ in range(args.amount):
33+
if args.total_length:
34+
# generate random password with the length
35+
# of total_length based on all available characters
36+
passwords.append("".join(
37+
[random.choice(string.digits + string.ascii_letters + string.punctuation) \
38+
for _ in range(args.total_length)]))
39+
else:
40+
password = []
41+
# If / how many numbers the password should contain
42+
for _ in range(args.numbers):
43+
password.append(random.choice(string.digits))
44+
45+
# If / how many uppercase characters the password should contain
46+
for _ in range(args.uppercase):
47+
password.append(random.choice(string.ascii_uppercase))
48+
49+
# If / how many lowercase characters the password should contain
50+
for _ in range(args.lowercase):
51+
password.append(random.choice(string.ascii_lowercase))
52+
53+
# If / how many special characters the password should contain
54+
for _ in range(args.special_chars):
55+
password.append(random.choice(string.punctuation))
56+
57+
# Shuffle the list with all the possible letters, numbers and symbols.
58+
random.shuffle(password)
59+
60+
# Get the letters of the string up to the length argument and then join them.
61+
password = ''.join(password)
62+
63+
# append this password to the overall list of password.
64+
passwords.append(password)
65+
66+
# Store the password to a .txt file.
67+
if args.output_file:
68+
with open(args.output_file, 'w') as f:
69+
f.write('\n'.join(passwords))
70+
71+
print('\n'.join(passwords))

0 commit comments

Comments
 (0)