Skip to content

Commit deda3a4

Browse files
committed
initial commit
0 parents  commit deda3a4

22 files changed

+1227
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
venv*
2+
.idea
3+
__pycache__

generator_pass/app.py

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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())

generator_pass/buttons.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from string import ascii_lowercase, ascii_uppercase, digits, punctuation
2+
from enum import Enum
3+
4+
5+
class Characters(Enum):
6+
btn_lower = ascii_lowercase
7+
btn_upper = ascii_uppercase
8+
btn_digits = digits
9+
btn_special = punctuation
10+
11+
12+
CHARACTER_NUMBER = {
13+
'btn_lower': len(Characters.btn_lower.value),
14+
'btn_upper': len(Characters.btn_upper.value),
15+
'btn_digits': len(Characters.btn_digits.value),
16+
'btn_special': len(Characters.btn_special.value)
17+
}
18+
19+
GENERATE_PASSWORD = (
20+
'btn_refresh', 'btn_lower', 'btn_upper', 'btn_digits', 'btn_special'
21+
)

generator_pass/password.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from enum import IntEnum
2+
from math import log2
3+
import secrets
4+
5+
6+
class StrengthToEntropy(IntEnum):
7+
Pathetic = 0
8+
Weak = 30
9+
Good = 50
10+
Strong = 70
11+
Excellent = 120
12+
13+
14+
def create_new(length: int, characters: str) -> str:
15+
return ''.join(secrets.choice(characters) for _ in range(length))
16+
17+
18+
def get_entropy(length: int, character_number: int) -> float:
19+
try:
20+
entropy = length * log2(character_number)
21+
except ValueError:
22+
return 0.0
23+
24+
return round(entropy, 2)

generator_pass/ui/css/btn_refresh.css

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
QPushButton {
2+
margin-right: 0;
3+
margin-left: 0;
4+
}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
QPushButton {
2+
border: none;
3+
margin: 0;
4+
background-color: transparent;
5+
}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
QFrame {
2+
border: 2px solid gray;
3+
border-radius: 5px;
4+
margin-right: 0;
5+
}
6+
7+
QFrame:hover {
8+
border-color: #090;
9+
}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
QLineEdit {
2+
border: none;
3+
margin: 0;
4+
font-size: 20pt;
5+
}

generator_pass/ui/css/qmain_win.css

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
QWidget {
2+
background-color: #121212;
3+
color: white;
4+
font-family: Verdana;
5+
font-size: 16pt;
6+
margin: 10px;
7+
}
8+
9+
QPushButton {
10+
border: 2px solid gray;
11+
border-radius: 5px;
12+
}
13+
14+
QPushButton#btn_lower,
15+
#btn_upper,
16+
#btn_digits,
17+
#btn_special {
18+
padding: 10px;
19+
}
20+
21+
QPushButton:hover {
22+
border-color: #090;
23+
}
24+
25+
QPushButton:pressed {
26+
border: 4px solid #090;
27+
border-radius: 5px;
28+
}
29+
30+
QPushButton:checked {
31+
background-color: #006300;
32+
border-color: #090;
33+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
QSlider::groove:horizontal {
2+
background-color: transparent;
3+
height: 5px;
4+
}
5+
6+
QSlider::sub-page:horizontal {
7+
background-color: #090;
8+
}
9+
QSlider::add-page:horizontal {
10+
background-color: gray;
11+
}
12+
QSlider::handle:horizontal {
13+
background-color: white;
14+
width: 22px;
15+
border-radius: 10px;
16+
margin-top: -8px;
17+
margin-bottom: -8px;
18+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
QSpinBox {
2+
border: 2px solid gray;
3+
border-radius: 5px;
4+
background: transparent;
5+
padding: 2px;
6+
}
7+
8+
QSpinBox:hover {
9+
border-color: #009900;
10+
}
Loading

generator_pass/ui/icons/lock.svg

+1
Loading
Binary file not shown.
Loading
Loading
Loading

0 commit comments

Comments
 (0)