-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcryptutils.py
66 lines (52 loc) · 1.72 KB
/
cryptutils.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from hashlib import md5
from datetime import datetime
import pyme.core, pyme.constants.sig
import yaml
class CryptUtilException(Exception):
pass
def getPassphrase(hint, desc, prev_bad):
print "Passphrase Callback! %s %s %s" % (hint, desc, prev_bad)
sys.stdout.write("Enter passphrase: ")
return sys.stdin.readline().strip()
c = pyme.core.Context()
c.set_armor(1)
c.set_passphrase_cb(getPassphrase)
def createVote (voter, topicid, authorization, vote):
c.op_keylist_start(voter, 0)
votekey = c.op_keylist_next()
if not votekey:
raise CryptUtilException("Voter key not found.")
voterid = votekey.subkeys[0].fpr
#CONFIRM AGAIN THAT VOTER IS AUTHORIZED TO VOTE ON THIS TOPIC (or this set of topics)
# raise CryptUtilException("Voter not authorized to vote on this topic.")
# c.op_keylist_start('Authority', 0)
# authkey = c.op_keylist_next()
#
# if not authkey:
# print ("No such key found.")
# sys.exit(1)
cont = {} #it's a container. I'm running out of descriptive variable names.
cont['type'] = 'vote'
cont['path'] = topicid
cont['voterid'] = voterid
cont['id'] = md5(cont['path']+cont['voterid']).hexdigest()
cont['vote'] = vote # a list of proposalids
voteblob = pyme.core.Data(yaml.dump(cont))
#export voter key
keyblob = pyme.core.Data()
c.op_export(voter, 0, keyblob)
#sign actual vote with voter key
votesig = pyme.core.Data()
c.signers_add(votekey)
c.op_sign(voteblob, votesig, pyme.constants.sig.mode.CLEAR)
c.signers_clear()
data = {}
data['auth'] = authorization
keyblob.seek(0,0)
data['key'] = keyblob.read()
votesig.seek(0,0)
data['sig'] = votesig.read()
return cont, data