-
Notifications
You must be signed in to change notification settings - Fork 0
/
func.py
318 lines (266 loc) · 8.93 KB
/
func.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# -*- coding:utf-8 -*-
from dao import *
from gui import myGui
import config, util
import datetime
class Func:
def __init__(self):
'''
constructor
'''
def getConnection(self):
conn = sqlite.connect(config.DB_PATH)
return conn
class MasterFunc(Func):
'''
function about master password
'''
def __init__(self):
'''
constructor
'''
def getMasterPwd(self):
'''
get master password (md5 encrypted)
'''
conn = self.getConnection()
mDao = MasterPwdDao(conn)
res = mDao.getMasterPwd()
conn.close()
return res
def authenticate(self, pwd):
md5String = util.md5Encoding(pwd)
md5Pwd = self.getMasterPwd()
return True if md5String == md5Pwd else False
def changeMasterPwd(self, newPwd):
oldPwd = config.getMasterPwd()
conn = self.getConnection()
mDao = MasterPwdDao(conn)
pDao = PwdDao(conn)
# 1st re-encrypt all passwords with the new master password
accountList = pDao.getAllPwd()
currentDate = unicode(datetime.datetime.today())
for account in accountList:
dePwd = util.decrypt(oldPwd, account.pwd)
enPwd = util.encrypt(newPwd, dePwd)
if account.secret:
deSecret = util.decrypt(oldPwd, account.secret)
enSecret = util.encrypt(newPwd, deSecret)
else:
enSecret = ''
deUsername = util.decrypt(oldPwd, account.username)
enUsername = util.encrypt(newPwd, deUsername)
pDao.updateAccount(account.id, account.title, account.description, enUsername, enPwd, enSecret, currentDate)
# 2nd get md5 of new master password, update the masterpassword table
newMD5String = util.md5Encoding(newPwd)
mDao.updateMasterPwd(newMD5String)
# 3rd update master password in config module
config.setMasterPwd(newPwd)
conn.commit()
conn.close()
class PwdFunc(Func):
'''
function about account (password item)
'''
def __init__(self):
'''
constructor
'''
def decryptUsername(self, pwd):
if pwd.username:
pwd.username = util.decrypt(config.getMasterPwd(), pwd.username)
return pwd
def getAllPwd(self):
conn = self.getConnection()
pDao = PwdDao(conn)
res = pDao.getAllPwd()
# decrypt username
res = [self.decryptUsername(pwd) for pwd in res]
conn.close()
return res
def getPwdByID(self, pwdID):
conn = self.getConnection()
pDao = PwdDao(conn)
res = self.decryptUsername(pDao.getPwdByID(pwdID))
conn.close()
return res
def getAllPwdCnt(self):
res = 0
conn = self.getConnection()
pDao = PwdDao(conn)
res = pDao.getAllPwdCnt()
conn.close()
return res
def getPwdListFromTagID(self, tagID):
conn = self.getConnection()
pDao = PwdDao(conn)
res = []
if tagID == myGui.ID_TAG_ALL: # all selected
res = pDao.getAllPwd()
elif tagID == myGui.ID_TAG_TRASH: # trash selected
res = pDao.getPwdListInTrash()
else:
res = pDao.getPwdListFromTagID(tagID)
# decrypt username
res = [self.decryptUsername(pwd) for pwd in res]
conn.close()
return res
def updateAccount(self, id, title, description, username, password, secret, tagIDs):
'''
update account
@param id: account id
@param title: account title
@param description: account description
@param username: account username
@param password: account password, it will not be updated if value is None
@param secret: secret text from user
@param tagIDs: a list of related tagIDs
'''
conn = self.getConnection()
pDao = PwdDao(conn)
pwdObj = pDao.getPwdByID(id)
mPwd = config.getMasterPwd()
eUsername = util.encrypt(mPwd, username) if username else ''
ePassword = util.encrypt(mPwd, password) if password else pwdObj.pwd
eSecret = util.encrypt(mPwd, secret) if secret else ''
currentDate = datetime.datetime.today()
pDao.updateAccount(id, title, description, eUsername, ePassword, eSecret, currentDate)
pDao.updateAccountTags(id, tagIDs)
conn.commit()
conn.close()
def addAccount(self, title, description, username, password, secret, tagIDs):
'''
add a user-inputed account to database
@param title: account title
@param description: account description
@param username: account username
@param password: account password
@param secret: secret text from user
@param tagIDs: a list of related tagIDs
'''
conn = self.getConnection()
pDao = PwdDao(conn)
# encrypt username & password
mPwd = config.getMasterPwd()
eUsername = util.encrypt(mPwd, username) if username else ''
ePassword = util.encrypt(mPwd, password)
eSecret = util.encrypt(mPwd, secret) if secret else ''
currentDate = datetime.datetime.today()
id = pDao.getAvailablePwdID()
# insert
pDao.insertAccount(id, title, description, eUsername, ePassword, eSecret, currentDate)
# add tag to the new account if there is
if len(tagIDs) > 0: pDao.setTagsOnAccount(id, tagIDs)
conn.commit()
conn.close()
def getPwdCntInTrash(self):
res = 0
conn = self.getConnection()
pDao = PwdDao(conn)
res = pDao.getPwdCntInTrash()
conn.close()
return res
def getSearchResult(self, keyword):
conn = self.getConnection()
pDao = PwdDao(conn)
res = pDao.getSearchResult(keyword)
# decrypt username
res = [self.decryptUsername(pwd) for pwd in res]
conn.close()
return res
def recoverFromTrash(self, pwdID):
conn = self.getConnection()
pDao = PwdDao(conn)
pDao.recoverFromTrash(pwdID)
conn.commit()
conn.close()
def moveToTrash(self, pwdID):
conn = self.getConnection()
pDao = PwdDao(conn)
pDao.moveToTrash(pwdID)
conn.commit()
conn.close()
def deleteAccount(self, pwdID):
conn = self.getConnection()
pDao = PwdDao(conn)
pDao.deleteAccount(pwdID)
conn.commit()
conn.close()
def emptyTrash(self):
conn = self.getConnection()
pDao = PwdDao(conn)
pDao.emptyTrash()
conn.commit()
conn.close()
def isTitleNameValid(self, name, ID=-1):
conn = self.getConnection()
pDao = PwdDao(conn)
res = pDao.isTitleNameValid(name, ID)
conn.close()
return res
class TagFunc(Func):
'''
function about tags
'''
def __init__(self):
'''
constructor
'''
def getAllTags(self):
conn = self.getConnection()
tDao = TagDao(conn)
res = tDao.getAllTags()
conn.close()
return res
def getPwdCntByTagID(self, tagID):
conn = self.getConnection()
tDao = TagDao(conn)
res = tDao.getPwdCntByID(tagID)
conn.close()
return res
def addNewTag(self, name):
conn = self.getConnection()
tDao = TagDao(conn)
tDao.insertTag(name)
conn.commit()
conn.close()
def editTag(self, tagID, name):
conn = self.getConnection()
tDao = TagDao(conn)
tDao.updateTag(tagID, name)
conn.commit()
conn.close()
def removeTag(self, tagID):
conn = self.getConnection()
tDao = TagDao(conn)
tDao.deleteTag(tagID)
conn.commit()
conn.close()
def removeTagInUse(self, tagID):
conn = self.getConnection()
tDao = TagDao(conn)
tDao.removeTagFromAccount(tagID)
tDao.deleteTag(tagID)
conn.commit()
conn.close()
def getTagByID(self, tagID):
conn = self.getConnection()
tDao = TagDao(conn)
res = tDao.getTagByID(tagID)
conn.close()
return res
def isTagNameValid(self, name, id=-1):
conn = self.getConnection()
tDao = TagDao(conn)
res = tDao.isTagNameValid(name, id)
conn.close()
return res
def getTagNameString(self, tags):
tagStr = ''
for tag in tags:
if tag:
tagStr += tag.name
if tag != tags[-1]:
tagStr += '> <'
if tagStr != '': tagStr = '<' +tagStr + '>'
return tagStr