forked from anshB1998/CertiCheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
154 lines (129 loc) · 5.39 KB
/
views.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
import os
from app import app
from flask import Flask, flash, request, redirect, render_template, session, jsonify
from werkzeug.utils import secure_filename
import csv
# Mugdha imports for Merkle Tree
import hashlib
from math import sqrt
from merkletools import MerkleTools
import json
import ast
# Prachiti Resume Parser
from resumeParser import ResumeParser
# Jainam Blockchain
from blockchain import Blockchain
UNIVERSITY_ALLOWED_EXTENSIONS = set(['csv'])
VERIFIER_ALLOWED_EXTENSIONS = set(['pdf', 'json'])
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in UNIVERSITY_ALLOWED_EXTENSIONS
def allowed_verification_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in VERIFIER_ALLOWED_EXTENSIONS
@app.route('/login', methods=['GET', 'POST'])
def login():
universities = ['VJTI', 'IITB', 'KJSCE']
if request.method == 'POST':
username = request.form['username']
session['logged_in'] = True
session['username'] = username
return render_template('upload.html', universities=universities)
return render_template('login.html')
@app.route('/', methods=['GET', 'POST'])
def home():
return render_template('home.html')
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
universities = ['VJTI', 'IITB', 'KJSCE']
if request.method == 'POST':
if 'file' not in request.files:
flash("No file selected", "danger")
error = "No file selected"
return render_template('upload.html', universities=universities, error = error)
file = request.files['file']
if file.filename == '':
flash("No file selected", "error")
return render_template('upload.html', universities=universities)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['CSV_FOLDER'], filename))
#Merkle Tree init
mt = MerkleTools(hash_type='sha3_256')
count = 0
with open(os.path.join(app.config['CSV_FOLDER'], filename)) as File:
reader = csv.reader(File)
for row in reader:
#studentID CPI Name batch college
data = str(row[0]) + str(row[2]) + str(row[1]) + str(row[3]) + session['username']
batch = str(row[3])
mt.add_leaf(data, do_hash= True)
count += 1
mt.make_tree()
# if mt.get_leaf_count() != count:
# print("not equal")
# print(count, mt.get_leaf_count())
if mt.get_tree_ready_state:
merkleRoot = mt.get_merkle_root()
institute = session['username']
bc = Blockchain(institute)
print("Adding to the blockchain: ", session['username'], batch, merkleRoot)
try:
bc.addBatchMerkleRoot(batch, merkleRoot)
except:
error = "Institute not registered!"
return render_template('upload.html', universities=universities, error=error)
itr = 0
with open(os.path.join(app.config['CSV_FOLDER'], filename)) as File:
reader = csv.reader(File)
for row in reader:
data={}
data["cpi"] = str(row[2])
data["name"] = str(row[1])
data["year"] = str(row[3])
data["studentId"] = str(row[0])
data["institution"] = session['username']
data["merklePath"] = mt.get_proof(itr)
itr += 1
filename = str(row[1]) + '.json'
with open(os.path.join(app.config['RECEIPT_FOLDER'], filename), 'w') as json_file:
json_file.write(json.dumps(data))
flash("Successfully uploaded to blockchain!", "success")
return render_template('upload.html', universities=universities)
@app.route('/verify', methods=['GET', 'POST'])
def verify():
if request.method == 'POST':
if 'json' not in request.files or 'pdf' not in request.files:
flash("All files not selected", "danger")
return render_template('verify.html')
jsonFile = request.files['json']
pdf = request.files['pdf']
if jsonFile.filename == '' or pdf.filename == '':
flash("All files not selected", "danger")
return render_template('verify.html')
if jsonFile and allowed_verification_file(jsonFile.filename) and pdf and allowed_verification_file(pdf.filename):
json_name = secure_filename(jsonFile.filename)
jsonFile.save(os.path.join(app.config['JSON_FOLDER'], json_name))
pdf_name = secure_filename(pdf.filename)
pdf.save(os.path.join(app.config['RESUME_FOLDER'], pdf_name))
resumeJsonData = ResumeParser.parse(os.path.join(app.config['RESUME_FOLDER'], pdf_name))
with open(os.path.join(app.config['JSON_FOLDER'], json_name)) as receiptJson:
receiptJsonData = json.loads(receiptJson.read())
if resumeJsonData["cpi"] != receiptJsonData["cpi"] or resumeJsonData["name"] != receiptJsonData["name"] or resumeJsonData["year"] != receiptJsonData["year"]:
flash("Details don't match", "danger")
else:
mt = MerkleTools(hash_type='sha3_256')
data = receiptJsonData['studentId'] + receiptJsonData['cpi'] + receiptJsonData['name'] + receiptJsonData['year'] + receiptJsonData['institution']
data = data.encode()
data = hashlib.sha3_256(data).hexdigest()
# print(mt.validate_proof(receiptJsonData['merklePath'], data))
merkleRoot = mt.validate_proof(receiptJsonData['merklePath'], data)
res = False
try:
bc = Blockchain("VJTI")
res = bc.verifyBatchMerkleRoot(receiptJsonData["institution"], receiptJsonData["year"], merkleRoot)
except:
print("Error occurred")
if res is True:
flash("Details verified successfully using the Blockchain", "success")
else:
flash("Details don't match", "danger")
return render_template('verify.html')