forked from Craig-Macomber/election
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateVoter.go
60 lines (53 loc) · 1.22 KB
/
createVoter.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
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/msgs"
"os"
)
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 makeVoter(publicPath, privatePath, name string) {
var voter msgs.VoterData
voter.Name = &name
ballotKey, _ := rsa.GenerateKey(rand.Reader, 2048)
k := keys.PackPrivateKey(ballotKey)
voter.Key = k
private, err := proto.Marshal(k.PublicKey)
if err != nil {
panic(err)
}
doFile(publicPath, private)
// Write out file
voterData, err := proto.Marshal(&voter)
if err != nil {
panic(err)
}
doFile(privatePath, voterData)
}
func main() {
electionPath := "demoElection/"
voterPublicKeyDir := electionPath + "voterPublicKeys/"
privateOutput := electionPath + "voterPrivate/"
names := []string{"TestVoter1", "TestVoter2", "TestVoter3"}
for _, name := range names {
makeVoter(voterPublicKeyDir+name, privateOutput+name, name)
}
}