-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
125 lines (94 loc) · 3.75 KB
/
main.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from password import Ui_MainWindow
from PyQt5 import QtWidgets, QtGui
from PyQt5.QtWidgets import QMessageBox
import sys
import random
# generates random lowercase password from a to z
def generate_lowercase_password(length):
password = ""
for i in range(length):
letter = random.randint(97, 122)
password += chr(letter)
return password
# generates random lowercase + Uppercase password from A-Z to a-z
def generate_uppercase_password(length):
list = []
password = ""
for i in range(int(length / 2)):
letter = random.randint(65, 90)
letter2 = random.randint(65, 90)
list.append(chr(letter))
list.append(chr(letter2).lower())
random.shuffle(list)
for i in list:
password += i
return password
# generates random password mixed with A-Z, a-z and special characters
def generate_password_with_special_chars(length):
password = ""
for i in range(length):
letter = random.randint(33, 126)
password += chr(letter)
return password
# generates random password mixed with A-Z, a-z and special characters
def generate_password_with_special_chars_lower(length):
list = []
password = ""
for i in range(int(length / 2)):
letter = random.randint(33, 63)
letter2 = random.randint(65, 90)
list.append(chr(letter))
list.append(chr(letter2).lower())
random.shuffle(list)
for i in list:
password += i
return password
class Window(QtWidgets.QMainWindow):
password = ""
def __init__(self):
super(Window, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.btnGenerate.clicked.connect(self.generate_password)
self.ui.btn_add_lenght.clicked.connect(self.add_length)
self.ui.btn_minus_lenght.clicked.connect(self.minus_length)
self.setWindowTitle("Random Password Generator")
self.setWindowIcon(QtGui.QIcon("icon.png"))
# generates password according to user choices
def generate_password(self):
# generates uppercase + lowercase + special characters password
if self.ui.special_chars_cbox.isChecked() and self.ui.uppercase_cbox.isChecked():
self.password = generate_password_with_special_chars(int(self.ui.lbl_lenght.text()))
self.ui.lbl_password.setText(self.password)
# generates uppercase + lowercase
elif self.ui.uppercase_cbox.isChecked():
self.password = generate_uppercase_password(int(self.ui.lbl_lenght.text()))
self.ui.lbl_password.setText(self.password)
# generates lowercase + special characters password
elif self.ui.special_chars_cbox.isChecked():
self.password = generate_password_with_special_chars_lower(int(self.ui.lbl_lenght.text()))
self.ui.lbl_password.setText(self.password)
# generates only lowercase password
else:
self.password = generate_lowercase_password(int(self.ui.lbl_lenght.text()))
self.ui.lbl_password.setText(self.password)
# making length plus one when button clicked
def add_length(self):
length = int(self.ui.lbl_lenght.text())
length += 1
self.ui.lbl_lenght.setText(str(length))
# making length minus one when button clicked
def minus_length(self):
length = int(self.ui.lbl_lenght.text())
if length > 1:
length -= 1
self.ui.lbl_lenght.setText(str(length))
else:
QMessageBox.about(self, "Warning", "Password length cannot be less than 1")
x = msg.exec_()
# making an instance of the window and running the program
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec_())