-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
82 lines (65 loc) · 1.95 KB
/
server.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
from flask import Flask, request, render_template
import crypto
import json
import os
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/setup', methods=['POST'])
def setup():
ballots = []
with open('ballots.csv', 'a+') as file:
file.seek(0)
for line in file:
ballots.append(line.strip())
response = {
'p': crypto.p,
'q': crypto.q,
'g': crypto.g,
'pk': pk,
'ballots': ballots
}
return json.dumps(response)
@app.route('/ballot', methods=['POST'])
def ballot():
data = request.data.decode('utf-8')
data = json.loads(data)
credentials = data['credentials']
cipher = data['cipher']
proof = data['proof']
with open('ballots.csv', 'r') as file:
for line in file:
cred = line.split(",")[0]
if cred == credentials:
return 'Denied', 200
if crypto.verify_vote(pk, cipher, proof):
with open('ballots.csv', 'a') as file:
cipher = [str(s) for s in cipher]
proof = [str(s) for s in proof]
line = '{0},{1},{2}\n'.format(credentials, ','.join(cipher), ','.join(proof))
file.write(line)
return 'Access', 200
return 'Denied', 200
@app.route('/tally', methods=['POST'])
def tally():
ballots = []
with open('ballots.csv', 'r') as file:
for line in file:
line = line.strip().split(',')
ballots.append((int(line[1]), int(line[2])))
a, b = crypto.add(ballots)
yes = crypto.decrypt(sk, a, b)
proof = crypto.correct_decryption_proof(pk, sk, a, b)
response = {
'yes': yes,
'no': len(ballots) - yes,
'cipher': [a, b],
'proof': proof
}
return json.dumps(response)
if __name__ == '__main__':
pk, sk = crypto.generate_keys()
if os.path.isfile('ballots.csv'):
os.remove('ballots.csv')
app.run()