-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
142 lines (116 loc) · 3.26 KB
/
main.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
#!/usr/bin/python3
# Import modules
import os
import sys
import time
import base64
import shutil
import random
import getpass
import hashlib
from cryptography.fernet import Fernet
# Functions
# Clear
def cls():
i = 0
wid = ''
wid_num = 0
try:
os.system('clear')
except Exception:
os.system('cls')
term = os.get_terminal_size()
wid_num = int(term[0]) - 13
for i in range(wid_num):
wid += ' '
print(f'\033[7;38m --PyDiary-- {wid}\n\033[0;0m\n')
return
# List files in the directory
def list_files(directory):
os.system(f'echo " " >> .pydiary/list')
os.remove('.pydiary/list')
try:
os.system(f'ls {directory} >> .pydiary/list')
except Exception:
os.system(f'dir {directory} >> .pydiary/list')
return
# Loading animation
def animate(tasks):
num12=0
while num12<random.randint(4,5):
sys.stdout.write('\r'+tasks+' |')
sys.stdout.flush()
time.sleep(0.1)
sys.stdout.write('\r'+tasks+' /')
sys.stdout.flush()
time.sleep(0.1)
sys.stdout.write('\r'+tasks+' -')
sys.stdout.flush()
time.sleep(0.1)
sys.stdout.write('\r'+tasks+' \\')
sys.stdout.flush()
time.sleep(0.1)
num12+=1
# Define the encrypt function
def encrypt(data, key):
output = Fernet(key).encrypt(data.encode('utf-8'))
return(output)
# Define the decrypt function
def decrypt(data, key):
output = Fernet(key).decrypt(data)
return(output)
# Password check
cls()
try:
open('.pydiary/pass.dat', 'r').read()
password = getpass.getpass('Password:')
except Exception:
print('***Enter your password for encryption***\n')
password = getpass.getpass('Password:')
os.system('mkdir .pydiary/')
os.system('echo "true" >> .pydiary/pass.dat')
key = base64.b64encode((hashlib.md5(password.encode('utf-8')).hexdigest()).encode('utf-8'))
# Check for files
try:
open('.pydiary/check.dat', 'r').read()
except Exception:
os.system('mkdir .pydiary/')
os.system('mkdir .pydiary/entries/')
os.system('echo "true" .pydiary/check.dat')
# Start interface loop
while True:
# Print interface
cls()
choice = input('\n\n [1] Add entry\n [2] View entries\n [3] Wipe\n [q] exit\n\n\n\n> ')
# Add a new entry
if choice == '1':
cls()
title = input('Enter title: ')
entry = input('Entry: ')
file_data = encrypt(entry, key)
os.system(f'echo "text" >> .pydiary/entries/{title}')
open('.pydiary/entries/' + title, 'wb').write(file_data)
input('Entry written!')
# Read an entry
elif choice == '2':
cls()
list_files('.pydiary/entries/')
print(open('.pydiary/list', 'r').read())
title = input('\n\n>')
file_data = open(f'.pydiary/entries/{title}', 'rb').read()
entry = (decrypt(file_data, key)).decode('utf-8')
cls()
print(' --' + title + '--')
input('\n ' + entry + '\n\n\n')
# Erase
elif choice == '3':
cls()
shutil.rmtree('.pydiary/')
print('\n\n')
animate('Wiping...')
input('\n\n\nEntries Wiped\n\n\n')
# Exit
elif choice == 'q':
cls()
print('quitting...')
sys.exit()