· 6 years ago · Jun 01, 2019, 08:02 PM
1from flask import Flask, render_template, request
2from hashlib import sha256
3from sqlite3 import connect
4from datetime import datetime
5
6# Create Flask app
7app = Flask(__name__)
8
9# Configure the app
10app.config["TEMPLATES_AUTO_RELOAD"] = True
11
12# Connect to sqlite
13connection = connect("C:\\Users\\Dhruv\\PycharmProjects\\DocumentValidation\\documents.db", check_same_thread=False)
14cursor = connection.cursor()
15
16# Create table if not already created
17sql = '''CREATE TABLE IF NOT EXISTS Documents
18 (ID INTEGER PRIMARY KEY AUTOINCREMENT,
19 SENDER VARCHAR(100),
20 HASH VARCHAR(64),
21 TIME DATETIME)'''
22cursor.execute(sql)
23
24
25@app.route("/")
26def index():
27 # Query whether the user wants to upload a document or check a document's validity
28 return render_template("index.html")
29
30
31@app.route("/upload", methods=["GET", "POST"])
32def upload():
33 # Prompt the user to enter a file
34 return render_template("upload.html")
35
36
37@app.route("/display_id", methods=["GET", "POST"])
38def display_id():
39 # Display the id of the uploaded document
40
41 if request.method == "POST":
42
43 # Get the required values from the form
44 file = request.files["file"]
45 sender = request.form.get("sender")
46
47 # Check if all input has been provided
48 if not file or not sender:
49 return render_template("apology.html", code="Please provide the necessary inputs")
50
51 # Store the hashcode in the table
52 hashcode = sha256(file.read()).hexdigest()
53 command = "INSERT INTO Documents (SENDER, HASH, TIME) VALUES ('{}', '{}', '{}')".format(sender, hashcode, datetime.now())
54 cursor.execute(command)
55 connection.commit()
56
57 # Get the identity of the last entry
58 cursor.execute("SELECT COUNT(*) FROM Documents")
59 identity = cursor.fetchall()
60
61 # Success
62 return render_template("success.html", code="The id is {}".format(identity[0][0]))
63
64 else:
65 # Access the page via a post request
66 upload()
67
68
69@app.route("/valid", methods=["GET", "POST"])
70def valid():
71 # Prompt the user for the id and the document
72 return render_template("upload1.html")
73
74
75@app.route("/validate", methods=["GET", "POST"])
76def validate():
77 # Output whether the document uploaded is valid or not
78
79 if request.method == "POST":
80
81 # Get the required values from the form
82 file = request.files["file"]
83 key = request.form.get("id")
84
85 # Check if all input has been provided
86 if not file or not key:
87 return render_template("apology.html", code="Please provide the required input fields")
88
89 # Query database
90 hashcode = sha256(file.read()).hexdigest()
91 command = "SELECT * FROM Documents WHERE ID = {} AND HASH = '{}'".format(key, hashcode)
92 cursor.execute(command)
93 result = cursor.fetchall()
94
95 # Display whether the document was tampered or not
96 if not result:
97 return render_template("success.html", code="The document has been modified")
98 else:
99 return render_template("success.html", code="The document has not been modified")
100
101 else:
102 # Access the page via a post request
103 valid()
104
105
106# Run the app
107if __name__ == "__main__":
108 app.run(
109 debug=True
110 )