-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.py
403 lines (313 loc) · 12.1 KB
/
cli.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import os
import pickle
import sys
from pwinput import pwinput
from tools.locker import Locker, LockerError
def newLocker(file, key):
print('Creating new Locker...')
locker = Locker(key)
locker.save(file)
clrscr()
openLocker(file, key)
return
def openLocker(file, key):
print("Opening the Locker...")
with open(file, 'rb') as f:
locker: Locker = pickle.load(f)
clrscr()
if locker.unlock(key):
passmode(locker)
else:
raise LockerError('Wrong password')
def passmode(locker: Locker):
global file
while True:
print("---- {0} ----\n--Password Section--".format(os.path.basename(file)))
if not locker.pwd_dict:
print('\nNo password added yet...')
else:
print("\nSites for which passwords have been found -")
n=0
for site in locker.pwd_dict.keys():
n += 1
print(f'{n}. {site}')
del n
print()
if locker.pwd_dict:
print('Enter the site number to view/change/delete the password stored for that site.')
print("Type 'add' to add a new password.\nType 'f' to go to Files Section.")
print("Type 'change password' to change locker password.\nType 'exit' to close the locker.")
choice = input().lower()
if choice == 'f':
clrscr()
file_closed = filemode(locker)
if file_closed == True:
return
else:
continue
elif choice == 'exit':
return
elif choice == 'change password':
new_pwd = pwinput("\nEnter a new password (Leave empty to cancel): ")
if not new_pwd:
clrscr()
continue
if len(new_pwd) < 5:
clrscr()
print("Password should be at least 5 characters long!\nPassword change failed!!\n")
continue
cnew_pwd = pwinput("Confirm password: ")
if cnew_pwd != new_pwd:
clrscr()
print("Password Mismatch!\nPassword change failed!!\n")
continue
locker.change_password(new_pwd)
locker.save(file)
clrscr()
print("Password changed!\n")
elif choice == 'add':
site = input("Enter a new site name to store password for (Leave empty to cancel): ")
if not site:
clrscr()
continue
if site in locker.pwd_dict:
print('Password for this site already exists!\n')
continue
newPass = pwinput('Enter the password to store (Leave empty to cancel): ')
clrscr()
if not newPass:
continue
locker.pwd_dict[site] = newPass
locker.save(file)
print('New password added successfully...\n')
continue
elif locker.pwd_dict and choice.isnumeric() and int(choice) <= len(locker.pwd_dict.keys()):
site = list(locker.pwd_dict.keys())[int(choice) - 1]
password = locker.pwd_dict[site]
print(f"\nPassword for '{site}' is:\n{password}")
print("Type 'change' to change this password.")
print("Type 'delete' to delete this password. Password can't be retrieved once deleted.")
print("Enter 'b' to go back to site selection menu.")
choice = input().lower()
clrscr()
if choice == 'b':
clrscr()
continue
elif choice == 'change':
newPass = pwinput("Enter new password (Leave empty to cancel): ")
clrscr()
if not newPass:
continue
locker.pwd_dict[site] = newPass
locker.save(file)
print('Password updated successfully...\n')
continue
elif choice == 'delete':
del locker.pwd_dict[site]
locker.save(file)
print("Password deleted successfully...\n")
continue
else:
print('Invalid input!\n')
continue
else:
clrscr()
print('Invalid input!\n')
continue
def filemode(locker: Locker):
global file
print("This locker is designed to encrypt important documents. Since encryption is very resource intensive,")
print("it is not advised to put huge files in the locker. Locker will take more time to encrypt bigger files.\n")
print("If you want to store large number of files, you can zip those files and store it here.\n")
while True:
print("---- {0} ----\n--Files Section --".format(os.path.basename(file)))
if not locker.files:
print('\nNo Files found in the locker...')
else:
print("\nFollowing files have been found in the locker -")
n=0
for filename in locker.files.keys():
n += 1
print(f'{n}. {filename}')
del n
print()
if locker.files:
print('Enter the file number to extract/rename/delete that file.')
print("Type 'add' to add a new file.\nType 'p' to go to Password Section.\nType 'exit' to close the locker.")
choice = input().lower()
if choice == 'p':
clrscr()
return False
elif choice == 'add':
filename = input("Enter path of the file to add (Leave empty to cancel): ")
if not filename:
clrscr()
continue
try:
if os.path.getsize(filename) > 2*1024*1024*1024: # 2 GB = 2*1024*1024*1024 bytes
clrscr()
print('File too large to add. Size limit per file is set to 2 GB for performance purposes!!\n')
continue
if (os.path.getsize(file) + os.path.getsize(filename)) > 3*1024*1024*1024:
clrscr()
print("Adding this file will exceed the locker limit (3 GB)!!\n")
continue
with open(filename, 'rb') as f:
print("\nProcessing the file...")
locker.add_file(os.path.basename(filename), f.read())
except FileNotFoundError:
clrscr()
print(filename)
print("File not found!\n")
continue
locker.save(file)
clrscr()
print("File added successfully...\nFrom " + filename +'\n')
continue
elif choice == 'exit':
return True
elif locker.files and choice.isnumeric() and int(choice) <= len(locker.files):
filename = list(locker.files.keys())[int(choice) - 1]
print(f"\n--- {filename} ---\n")
print("Type 'extract' to extract the file.\nType 'rename' to rename it.")
print("Type 'delete' to remove the file from the locker.\nEnter 'b' to go back to the files list.")
choice = input().lower()
if choice == "b":
clrscr()
continue
elif choice == "rename":
newName = input("Enter new name for the file (Leave empty to cancel): ")
if not newName:
clrscr()
continue
print("\nRenaming the file...")
locker.rename_file(filename, newName)
locker.save(file)
clrscr()
print("File renamed successfully...\n")
continue
elif choice == "delete":
print("\nDeleting the file...")
locker.remove_file(filename)
locker.save(file)
clrscr()
print("File deleted successfully...\n")
continue
elif choice == "extract":
extractpath = input("\nEnter the location to extract the file ('!' to cancel):\n")
if extractpath == '!':
clrscr()
continue
extractpath = os.path.join(extractpath, filename)
try:
print("\nExtracting the file...")
with open(extractpath, 'wb') as f:
f.write(locker.get_file(filename))
except FileNotFoundError:
clrscr()
print(extractpath)
print('No such folder exists!\n')
continue
except PermissionError:
clrscr()
print(extractpath)
print("No Permission to write this folder!!\n")
continue
clrscr()
print("File successfully extracted...\nTo " + extractpath +'\n')
continue
else:
clrscr()
print('Invalid input!\n')
continue
else:
clrscr()
print('Invalid input!\n')
continue
# Micro Functions----------------------------------------------------------------------------------------------
def clrscr():
if os.name == 'nt':
os.system('cls')
else:
os.system('clear')
# Main Program--------------------------------------------------------------------------------------------------
def main():
global file
if len(sys.argv) >= 2:
dir, file = os.path.split(sys.argv[1])
if dir: os.chdir(dir)
key = pwinput("Enter password: ")
clrscr()
try:
openLocker(file, key)
except LockerError:
print('Wrong password!!')
input('Press Enter to exit...\n')
sys.exit()
except:
clrscr()
print("Something went wrong...")
input('Press Enter to exit...\n')
sys.exit()
sys.exit()
while True:
print('What should we do?\n1. Open a locker\n2. Create new locker\n3. Exit')
choice = input('Enter your choice no.: ')
if choice == '1':
file = input("Enter path for .lkr file (Leave empty to cancel): ")
if not file:
clrscr()
continue
if not file.endswith('.lkr'):
file = file + '.lkr'
if not os.path.exists(file):
clrscr()
print(file)
print(f'This file does not exist!\n')
continue
key = pwinput("Enter password (Leave empty to cancel): ")
clrscr()
if not key:
clrscr()
continue
try:
openLocker(file, key)
clrscr()
except LockerError:
print('Wrong password!!\n')
continue
except:
clrscr()
print('Something went wrong!!\n')
continue
elif choice == '2':
file = input("Enter name and path for the new Locker file (Leave empty to cancel): ")
if not file:
clrscr()
continue
pwd = pwinput("Enter password for the locker (Leave empty to cancel): ")
if not pwd:
clrscr()
continue
if len(pwd) < 5:
clrscr()
print("Password should be at least 5 characters long!\nLocker creation failed!!\n")
continue
cpwd = pwinput("Confirm password: ")
if cpwd != pwd:
clrscr()
print("Password Mismatch!\nLocker creation failed!!\n")
continue
if not file.endswith('.lkr'):
file = file + '.lkr'
if os.path.dirname(file):
os.makedirs(os.path.dirname(file), exist_ok=True)
newLocker(file, pwd)
clrscr()
elif choice == '3' or choice.lower() == 'exit':
sys.exit()
else:
clrscr()
print("Invaild input!\n")
if __name__ == '__main__':
main()