1
+ import sys
2
+
3
+ from PySide6 .QtWidgets import QApplication , QMainWindow , QLineEdit
4
+
5
+ import buttons
6
+ import password
7
+ from ui .ui_main import Ui_MainWindow
8
+ import ui .resources
9
+
10
+ class PasswordGenerator (QMainWindow ):
11
+ def __init__ (self ):
12
+ super (PasswordGenerator , self ).__init__ ()
13
+ self .ui = Ui_MainWindow ()
14
+ self .ui .setupUi (self )
15
+
16
+ self .connect_slider_to_spinbox ()
17
+ self .set_password ()
18
+ self .do_when_password_edit ()
19
+
20
+ for btn in buttons .GENERATE_PASSWORD :
21
+ getattr (self .ui , btn ).clicked .connect (self .set_password )
22
+
23
+ self .ui .btn_visibility .clicked .connect (self .change_password_visibility )
24
+ self .ui .btn_copy .clicked .connect (self .copy_to_clipboard )
25
+ def connect_slider_to_spinbox (self ) -> None :
26
+ ...
27
+ self .ui .slider_length .valueChanged .connect (self .ui .spinbox_length .setValue )
28
+ self .ui .spinbox_length .valueChanged .connect (self .ui .slider_length .setValue )
29
+ self .ui .spinbox_length .valueChanged .connect (self .set_password )
30
+
31
+ def do_when_password_edit (self ) -> None :
32
+ self .ui .line_password .textEdited .connect (self .set_entropy )
33
+ self .ui .line_password .textEdited .connect (self .set_strength )
34
+
35
+ def get_characters (self ) -> str :
36
+ chars = ""
37
+
38
+ for btn in buttons .Characters :
39
+ if getattr (self .ui , btn .name ).isChecked ():
40
+ chars += btn .value
41
+
42
+ return chars
43
+
44
+ def set_password (self ) -> None :
45
+ try :
46
+ self .ui .line_password .setText (
47
+ password .create_new (
48
+ length = self .ui .slider_length .value (),
49
+ characters = self .get_characters ())
50
+ )
51
+ except IndexError :
52
+ self .ui .line_password .clear ()
53
+
54
+ self .set_entropy ()
55
+ self .set_strength ()
56
+
57
+ def get_character_number (self ) -> int :
58
+ num = 0
59
+
60
+ for btn in buttons .CHARACTER_NUMBER .items ():
61
+ if getattr (self .ui , btn [0 ]).isChecked ():
62
+ num += btn [1 ]
63
+
64
+ return num
65
+
66
+
67
+ def set_entropy (self ) -> None :
68
+ length = len (self .ui .line_password .text ())
69
+ char_num = self .get_character_number ()
70
+
71
+ self .ui .labe_entropy .setText (
72
+ f"Entropy: { password .get_entropy (length , char_num )} bit"
73
+ )
74
+
75
+ def set_strength (self ) -> None :
76
+ length = len (self .ui .line_password .text ())
77
+ char_num = self .get_character_number ()
78
+
79
+ for strength in password .StrengthToEntropy :
80
+ if password .get_entropy (length , char_num ) >= strength .value :
81
+ self .ui .label_strenth .setText (f"Strength: { strength .name } " )
82
+
83
+ def change_password_visibility (self ) -> None :
84
+ if self .ui .btn_visibility .isChecked ():
85
+ self .ui .line_password .setEchoMode (QLineEdit .Normal )
86
+ else :
87
+ self .ui .line_password .setEchoMode (QLineEdit .Password )
88
+
89
+ def copy_to_clipboard (self ) -> None :
90
+ QApplication .clipboard ().setText (self .ui .line_password .text ())
91
+
92
+ if __name__ == "__main__" :
93
+ app = QApplication (sys .argv )
94
+
95
+ window = PasswordGenerator ()
96
+ window .show ()
97
+
98
+ sys .exit (app .exec ())
0 commit comments