-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassword_generator.py
More file actions
59 lines (55 loc) · 2.48 KB
/
password_generator.py
File metadata and controls
59 lines (55 loc) · 2.48 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
54
55
56
57
58
59
# Generates a random password with a certain provided length
# This script is called in pysworder.py as choice 1
import string
import random
import sys
import os
# Export the generated password to a file to a file called generated_password.txt
# Input: the generated password coming from the generate_password(number_of_chars) function
# Output: print the path of the file that stored the password
def export_to_file(generated_password):
file_path = './generated_password.txt'
try:
with open(file_path, 'w') as password_file:
password_file.write('[+] The generated password\n')
password_file.write(generated_password)
print(f'[+] Password saved to file: {os.path.abspath(file_path)}')
except Exception as error:
print(f'[!] Error while saving the password to file: {error}')
# Generates a random password based on the parameter size
# Input: number of charachter which represet the password length
# Output: prints the generated password and call the export_to_file(generated_password) function
def generate_password(number_of_chars):
upper_case = string.ascii_uppercase
lower_case = string.ascii_lowercase
symbols = string.punctuation
numbers = string.digits
generated_password = ''
while (number_of_chars > 2):
chosen_upper_case = random.randint(0, len(upper_case) - 1)
chosen_lower_case = random.randint(0, len(lower_case) - 1)
chosen_symbol = random.randint(0, len(symbols) - 1)
chosen_number = random.randint(0, len(numbers) - 1)
chosen_chars = [upper_case[chosen_upper_case],
lower_case[chosen_lower_case], symbols[chosen_symbol], numbers[chosen_number]]
chosen_char = random.choice(chosen_chars)
generated_password += str(chosen_char)
number_of_chars -= 1
# Add random symbol to the password
generated_password += symbols[random.randint(
0, len(string.punctuation) - 1)]
# Add random number to the password
generated_password += numbers[random.randint(
0, len(string.digits) - 1)]
# Shuffle the password
shuffled_password = list(generated_password)
random.shuffle(shuffled_password)
final_password = ''.join(shuffled_password)
print(f'[+] The generated password is ==> {final_password}')
question = input(
'[+] Do you want to output the password to a file (y/n)? ')
answer = question.lower()
if (answer == 'y'):
export_to_file(final_password)
else:
sys.exit(0)