forked from NuVotifier/go-votifier
-
Notifications
You must be signed in to change notification settings - Fork 2
/
protocol_v1.go
57 lines (50 loc) · 1.25 KB
/
protocol_v1.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
package votifier
import (
"crypto/rand"
"crypto/rsa"
"fmt"
"strings"
)
// DecodeV1 decodes the vote from the V1 protocol.
func (v *Vote) DecodeV1(data []byte, key *rsa.PrivateKey) error {
if v == nil {
*v = Vote{}
}
decrypted, err := rsa.DecryptPKCS1v15(rand.Reader, key, data)
if err != nil {
return fmt.Errorf("failed to decrypt vote: %w", err)
}
elements := strings.Split(string(decrypted), "\n")
if len(elements) != 6 {
return fmt.Errorf("invalid element count, wanted 6, got %d", len(elements))
}
if elements[0] != "VOTE" {
return fmt.Errorf("first element is incorrect; expected 'VOTE', got %s", elements[0])
}
v.ServiceName = elements[1]
v.Username = elements[2]
v.Address = elements[3]
v.Timestamp = parseTime(elements[4])
return nil
}
// EncodeV1 encodes the vote to the V1 protocol.
func (v *Vote) EncodeV1(publicKey *rsa.PublicKey) (*[]byte, error) {
if v.Timestamp.IsZero() {
v.Timestamp = timeNow()
}
s := strings.Join([]string{
"VOTE",
v.ServiceName,
v.Username,
v.Address,
formatTimeMillis(v.Timestamp),
"",
}, "\n")
msg := []byte(s)
// Encrypt the v using the supplied public key.
encrypted, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, msg)
if err != nil {
return nil, err
}
return &encrypted, nil
}