forked from Craig-Macomber/election
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateElection.go
96 lines (86 loc) · 2.15 KB
/
createElection.go
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
package main
import (
"code.google.com/p/goprotobuf/proto"
"crypto/rand"
"crypto/rsa"
"github.com/Craig-Macomber/election/keys"
"github.com/Craig-Macomber/election/msg"
"github.com/Craig-Macomber/election/msg/msgs"
"io/ioutil"
"os"
"runtime"
"sync"
)
func doFile(path string, data []byte) {
// open output file
fo, err := os.Create(path)
if err != nil {
panic(err)
}
// close fo on exit and check for its returned error
defer func() {
if err := fo.Close(); err != nil {
panic(err)
}
}()
_, err = fo.Write(data)
if err != nil {
panic(err)
}
}
func makeServer(publicDst **msgs.Server, privatePath string) {
wg.Add(1)
go func() {
var server msgs.Server
*publicDst = &server
address := ("localhost" + msg.Service)
server.Address = &address
ballotKey, _ := rsa.GenerateKey(rand.Reader, 2048)
k := keys.PackPrivateKey(ballotKey)
server.Key = k.PublicKey
private, err := proto.Marshal(k)
if err != nil {
panic(err)
}
doFile(privatePath, private)
wg.Done()
}()
}
var wg sync.WaitGroup
func main() {
runtime.GOMAXPROCS(4)
electionPath := "demoElection/"
voterPublicKeyDir := electionPath + "voterPublicKeys/"
privateOutput := electionPath + "serverPrivate/"
configDst := electionPath + "config"
var config msgs.ElectionConfig
// Generate server keys
makeServer(&config.BallotServer, privateOutput+"ballotKey")
makeServer(&config.VoteServer, privateOutput+"voteKey")
makeServer(&config.VoterListServer, privateOutput+"voterListKey")
makeServer(&config.FinalVoteSetServer, privateOutput+"finalVoteSetKey")
makeServer(&config.FinalSignatureRequestSetServer, privateOutput+"finalSignatureRequestSetKey")
// Fill in voters
infos, err := ioutil.ReadDir(voterPublicKeyDir)
if err != nil {
panic(err)
}
for _, info := range infos {
name := info.Name()
var v msgs.Voter
v.Key = keys.LoadKey(voterPublicKeyDir + name)
v.Name = &name
config.Voters = append(config.Voters, &v)
}
// Fill in description
s := "A test election"
config.BallotDescription = &s
// Wait for server keys
wg.Wait()
// Write out file
configData, err := proto.Marshal(&config)
if err != nil {
panic(err)
}
doFile(configDst, configData)
}