-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.py
70 lines (60 loc) · 2.53 KB
/
logic.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
# import passGen
import string
import random
from tkinter import messagebox
import pyperclip as clipboard
from cryptography.fernet import Fernet
key = b'4b6LWAOo3Y1mgjTIjOMSAdz2LPaKFNvCfdKv1Vzg-Lc='
token = Fernet(key)
def generatePassword(self):
if self.val.get() == 1:
randomUpper = random.choices(string.ascii_uppercase, k=5)
else:randomUpper = []
if self.special.get() == 1:
randomSpecial = random.choices("-_@!?$#%^&*.", k=2)
else:randomSpecial=[]
if self.num.get()== 1:
randomNum = random.choices(string.digits, k=5)
else:randomNum =[]
randomGen = random.sample(randomUpper + randomSpecial + randomNum + random.choices(string.ascii_lowercase, k=12), k = int(self.lenght.get()))
#populate textbox
genetated_password = "".join(randomGen)
self.textBox.config(state='normal')
self.textBox.delete(1.0,'end')
self.textBox.insert(1.0, genetated_password)
self.textBox.config(state='disabled')
#encrypt generated password and save it on file
encrypted = token.encrypt(genetated_password.encode())
with open('password.txt','ab') as file:
file.write(encrypted + b'\n') #add password to file
def getHistory(self):
self.textBox.config(state='normal')
self.textBox.delete(1.0, 'end')
with open('password.txt','r')as text:
for line in text:
if line != '\n':
readEncrypted = token.decrypt(line.encode())
self.textBox.insert(1.0, readEncrypted)
self.textBox.insert(1.0,'\n')
self.textBox.config(state='disabled')
def copyPassword(self):
#message
msg = 'Password copied succesfuly'
msg_generate = 'First generate password'
self.textBox.config(state='normal')
if self.textBox.get(1.0,'end')=='\n':
messagebox.showwarning('Warning',msg_generate)
elif len(self.textBox.get(1.0,'end'))>=1 and len(self.textBox.get(1.0,'end'))<=13:
#to copy password need to: pip install pyperclip then import pyperclip
clipboard.copy(self.textBox.get(1.0,'end'))
messagebox.showinfo('message', msg)
self.textBox.delete(1.0,'end')
else:
with open('password.txt','r') as text:
for line in text:# read all file and copy just last password from for iteration
pass
readEncrypted = token.decrypt(line.encode())
clipboard.copy(readEncrypted.decode('utf-8'))
messagebox.showinfo('message', msg)
self.textBox.delete(1.0,'end')
self.textBox.config(state='disabled')