-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
75 lines (65 loc) · 2.57 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
from flask import Flask, render_template, request, Response, make_response, jsonify
from hashlib import sha256
import os
import pymongo
from pymongo import MongoClient
from datetime import datetime
import json
from bson import json_util
from bson.objectid import ObjectId
import wget
from image_to_ascii import ImageToAscii
cluster = MongoClient("mongodb+srv://Angel:dailyTasksAppPass@cluster0.a5gk4.mongodb.net/myFirstDatabase?retryWrites=true&w=majority")
db = cluster["PhotoCrypt-DB"]
collection = db["UserCredentials"]
app = Flask(__name__)
def getCode():
ImageToAscii(imagePath="pure.png", outputFile="ascii.txt")
f = open("ascii.txt", "r")
text = f.read()
os.remove("pure.png")
os.remove("ascii.txt")
return sha256(text.encode()).hexdigest()
@app.route('/', methods=['GET', 'POST'])
def index():
if request.form.get("username", False) != False:
username = request.form['username']
image = request.files['file']
image.save("pure.png")
encrypted_string = getCode()
existingUser = list(collection.find({'username':username}))
if(existingUser):
if(existingUser[0]['password'] == encrypted_string):
return render_template('landingPage.html')
else:
return render_template('index.html', invalid = True)
else:
return render_template('index.html', invalid = True)
return render_template('index.html')
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.form.get("username", False) != False:
username = request.form['username']
image = request.files['file']
image.save("pure.png")
encrypted_string = getCode()
exists = list(collection.find({'username':username}))
if(exists):
return render_template('register.html', usernameTaken = True)
else:
collection.insert_one({"username":username, "password":encrypted_string})
return render_template('index.html',createdUserLogin=username)
return render_template('register.html')
@app.route('/<path:url>/<string:caps>/<string:chars>/<int:length>',methods=['GET'])
def landingPage(url, caps, chars, length):
image = wget.download(str(url), out = "pure.png")
encrypted_string = getCode()[:length]
if caps == "true":
encrypted_string = encrypted_string[1:] + "A"
if chars == "true":
encrypted_string = "!"+ encrypted_string[1:]
resp = make_response(jsonify(
code=encrypted_string
))
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp