-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
310 lines (219 loc) · 9.04 KB
/
app.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
import os
import installLib
from form.authenForm import LoginForm, RegisterForm
from form.changeInfoForm import ChangeInfoForm
from form.uploadFileForm import UploadFileForm
from Crypto.PublicKey import RSA
from werkzeug.utils import secure_filename
import authen
import cryptography
import changeInfo
from dotenv import load_dotenv
from flask import Flask, redirect, render_template, request, url_for, session
import json
from flask_bootstrap import Bootstrap
from pymongo import MongoClient
from Crypto.PublicKey import RSA
load_dotenv() # take environment variables from .env.
UPLOAD_FOLDER = "uploads"
DOWNLOAD_FOLDER = "download"
SALT_LENGTH = int(os.getenv("SALT_LENGTH"))
SECRET_KEY = os.getenv("SECRET_KEY")
MONGO_URI = os.getenv("MONGO_URI")
MONGO_PORT = os.getenv("MONGO_PORT")
APP_PORT = os.getenv("APP_PORT")
basedir = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
# Flask-WTF requires an encryption key - the string can be anything
app.config['SECRET_KEY'] = SECRET_KEY
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['DOWNLOAD_FOLDER'] = DOWNLOAD_FOLDER
# create "uploads" folder
def create_uploads_folder():
os.path.exists(UPLOAD_FOLDER) or os.mkdir(UPLOAD_FOLDER)
def create_download_folder():
os.path.exists(DOWNLOAD_FOLDER) or os.mkdir(DOWNLOAD_FOLDER)
# connect to mongoDB
try:
client = MongoClient(MONGO_URI, int(MONGO_PORT))
db = client.myDatabase # database name
except:
app.logger.error("Can not connect MongoDB")
Bootstrap(app)
def authorize():
if not 'user' in session:
return redirect(url_for('login'))
user = json.loads(session["user"])
return user
@app.route('/')
def index():
return redirect(url_for('login'))
@app.route('/register', methods=['GET', 'POST'])
def register():
form = RegisterForm()
if request.method == "POST":
passwd = request.form.get("password")
passphase = authen.salt_hash256(passwd)
# check if email exists
if (authen.check_email_exists(request.form.get("email"))):
return render_template("register.html", form=form, error="Email exist.")
public_key, private_key = cryptography.gen_user_RSA_key_pem(
passphase)
user = {
"avatar": 'https://avatars.dicebear.com/api/human/{}.svg'.format(
form.name.data),
"email": form.email.data,
"name": form.name.data,
"phone": form.phone.data,
"address": form.address.data,
"passphase": passphase,
"pass": passwd, # temp
"public_key": public_key,
"private_key": private_key,
}
db.users.insert_one(user)
return redirect(url_for('login'))
return render_template('register.html', form=form)
@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if request.method == "POST":
email = request.form.get("email")
passwd = request.form.get("password")
app.logger.info("Login..... email: %s - pass: %s", email, passwd)
# check if email exists
user = db.users.find_one({"email": email})
if user is None:
return render_template("login.html", form=form, error="Email does not exist.")
if authen.verify_password(passwd, user["passphase"]):
user = json.dumps(user, default=str)
session["user"] = user
return redirect(url_for('home'))
else:
return render_template("login.html", form=form, error="Password is incorrect.")
return render_template('login.html', form=form)
@app.route("/logout")
def logout():
session.pop('user', None)
return redirect(url_for('login'))
@ app.route('/home', methods=['GET', 'POST'])
def home():
# authorize user
user = authorize()
form = ChangeInfoForm()
upload_form = UploadFileForm()
sharedFiles = db.shared_file.find({"email": user['email']})
files = []
for file in sharedFiles:
files.append(file['name'])
return render_template('home.html', form=form, upload_form=upload_form, user=user, files=files)
@app.route("/change-info", methods=['GET', 'POST'])
def change_info():
user = authorize()
print("user", user)
if request.method == "POST":
new_info = {
"email": request.form.get("email"),
"name": request.form.get("name"),
"phone": request.form.get("phone"),
"address": request.form.get("address"),
"passphase": request.form.get("password")
}
app.logger.info("----- CHANGE INFO --------")
app.logger.info("new info: %s", new_info)
changeInfo.change_info(new_info, user)
return redirect(url_for('home'))
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
# check if email exist
email = request.form.get("email")
receiver = db.users.find_one({"email": email})
uploaded_file = request.files['file']
if receiver is None:
return redirect(url_for('home', status="fail", content="This user does not exist!"))
if not uploaded_file:
return redirect(url_for('home', status="fail", content="Please select 1 file"))
# read content file
filename = secure_filename(uploaded_file.filename)
content = uploaded_file.read()
if len(content) >= 15728640: # 15mb
return redirect(url_for('home', status="fail", content="File too large (>15MB)"))
# gen Ksession and encrypted it with RSA
ksession, en_ksession = cryptography.gen_session_key(
receiver["public_key"])
cipher_text = cryptography.AES_encrypt(content, ksession)
encrypted = b''.join([en_ksession, b'[+++++]', cipher_text])
new_file = {
"name": filename,
"email": email,
"content": encrypted
}
db.shared_file.insert_one(new_file)
return redirect(url_for('home', status="success", content="File has been uploaded"))
@app.route('/decrypt', methods=['GET', 'POST'])
def decrypt_file():
create_download_folder()
user = authorize()
#
select = request.form.get('select')
file = db.shared_file.find_one({"name": select})
if file is None:
return redirect(url_for('home', status="fail", content="Please select 1 file"))
encrypted = file["content"].split(b'[+++++]')
en_ksession, cipher_text = encrypted[0], encrypted[1]
# get private key
priv_key_pem = cryptography.AES_decrypt(
user["private_key"], user["passphase"])
ksession = cryptography.RSA_decrypt(en_ksession, priv_key_pem)
content = cryptography.AES_decrypt(cipher_text.decode(), ksession)
file_path = os.path.join(basedir, app.config['DOWNLOAD_FOLDER'], select)
open(file_path, 'wb').write(content)
return redirect(url_for('home', status="success", content="File has been decrypted"))
@app.route('/sign-on', methods=['GET', 'POST'])
def sign_on_file():
create_uploads_folder()
user = authorize()
try:
uploaded_file = request.files['file']
filename = secure_filename(uploaded_file.filename)
content = uploaded_file.read()
# save
uploaded_file.stream.seek(0)
uploaded_file.save(os.path.join(
basedir, app.config['UPLOAD_FOLDER'], filename))
hashed_file = int(cryptography.sha256(content), 16)
user["private_key"] = cryptography.AES_decrypt(
user["private_key"], user["passphase"])
private_key = RSA.importKey(user["private_key"])
signature = pow(hashed_file, private_key.d, private_key.n)
signed_file_path = os.path.join(
basedir, app.config['UPLOAD_FOLDER'], filename + ".sig")
open(signed_file_path, "w").write(str(signature))
except:
return redirect(url_for('home', status="fail", content="No file uploaded!"))
return redirect(url_for('home', status="success", content="File is signed."))
@ app.route('/verify', methods=['GET', 'POST'])
def verify_signed_file():
user = authorize()
file = request.files['file']
sign_file = request.files['sign_file']
filename = secure_filename(file.filename)
sign_filename = secure_filename(sign_file.filename)
signature = int(sign_file.read().decode())
hashed_file = int(cryptography.sha256(file.read()), 16)
users = db.users.find()
for user in users:
public_key = RSA.importKey(user["public_key"])
hashFromSignature = pow(signature,
public_key.e, public_key.n)
if hashFromSignature == hashed_file:
return redirect(url_for('home', status="success", content="File is verified."))
if filename == "":
return redirect(url_for('home', status="fail", content="No file uploaded!"))
elif sign_filename == "":
return redirect(url_for('home', status="fail", content="No file Signature uploaded!"))
elif sign_filename.split(".")[-1] != "sig":
return redirect(url_for('home', status="fail", content="Wrong file Signature format!"))
return redirect(url_for('home', status="fail", content="Verify failed!"))
if __name__ == '__main__':
app.run(host='0.0.0.0', port=APP_PORT, threaded=True, debug=True)