-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
170 lines (144 loc) · 4.18 KB
/
main.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Package main allows you to generate and verify ring signatures.
package main
import (
crand "crypto/rand"
"fmt"
"os"
"github.com/t-bast/ring-signatures/ring"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.EnableBashCompletion = true
app.Name = "ring-signatures"
app.Usage = "generate and verify ring signatures."
app.Version = "0.1.0"
app.Commands = []cli.Command{
{
Name: "generate",
Aliases: []string{"g"},
Usage: "generate a public and private key",
UsageText: "ring-signatures generate",
Action: generate,
},
{
Name: "sign",
Aliases: []string{"s"},
Usage: "sign a message with a ring",
UsageText: "Alice has private key \"Pr1v4T3k3y\", public key \"4l1c3\" and wants to sign the message \"hello!\".\n" +
" She wants to use Bob and Carol's public keys to form a ring.\n" +
" Bob's public key is \"b0b\" and Carol's public key is \"c4r0l\".\n" +
" Alice can form the ring [c4r0l, 4l1c3, b0b] and hide herself in that ring with the following command:\n" +
" ring-signatures sign --message \"hello!\" --private-key 4l1c3" +
" --ring-index 1 --ring c4r0l --ring 4l1c3 --ring b0b",
Action: sign,
Flags: []cli.Flag{
cli.StringFlag{
Name: "message, m",
Usage: "message to sign or verify",
},
cli.StringFlag{
Name: "private-key, k",
Usage: "private key to use for signing",
},
cli.IntFlag{
Name: "ring-index, i",
Usage: "index of your private key in the signing ring",
},
cli.StringSliceFlag{
Name: "ring, r",
Usage: "comma-separated list of public keys to use as ring",
},
},
},
{
Name: "verify",
Aliases: []string{"v"},
Usage: "verify a message signature",
UsageText: "ring-signatures verify --message \"hello!\" --signature s1GN4tUr3",
Action: verify,
Flags: []cli.Flag{
cli.StringFlag{
Name: "message, m",
Usage: "message to sign or verify",
},
cli.StringFlag{
Name: "signature, s",
Usage: "signature to verify",
},
},
},
}
app.Run(os.Args)
}
func generate(c *cli.Context) error {
fmt.Println("Generating your public and private key...")
pk, sk := ring.Generate(crand.Reader)
fmt.Printf("Public key: %s\n", ring.ConfigEncodeKey(pk))
fmt.Printf("Private key: %s\n", ring.ConfigEncodeKey(sk))
fmt.Println("You can (should) share your public key with the world, but make sure you secure your private key.")
return nil
}
func sign(c *cli.Context) error {
r := c.StringSlice("ring")
if len(r) == 0 {
return cli.NewExitError("you need to specify a ring to use for signing", 1)
}
var ringKeys []ring.PublicKey
for _, key := range r {
pkBytes, err := ring.ConfigDecodeKey(key)
if err != nil {
return cli.NewExitError(fmt.Sprintf("invalid public key: %s", key), 1)
}
ringKeys = append(ringKeys, ring.PublicKey(pkBytes))
}
m := c.String("message")
if len(m) == 0 {
return cli.NewExitError("you need to specify a message to sign", 1)
}
i := c.Int("ring-index")
if i < 0 {
return cli.NewExitError("invalid index", 1)
}
pk := c.String("private-key")
if len(pk) == 0 {
return cli.NewExitError("you need to specify the private key to use for signing", 1)
}
privKeyBytes, err := ring.ConfigDecodeKey(pk)
if err != nil {
return cli.NewExitError("invalid private key", 1)
}
privKey := ring.PrivateKey(privKeyBytes)
fmt.Println("Signing message...")
sig, err := privKey.Sign(crand.Reader, []byte(m), ringKeys, i)
if err != nil {
return cli.NewExitError(err, 1)
}
sigStr, err := sig.Encode()
if err != nil {
return cli.NewExitError(err, 1)
}
fmt.Println(sigStr)
return nil
}
func verify(c *cli.Context) error {
sigStr := c.String("signature")
if len(sigStr) == 0 {
return cli.NewExitError("you need to specify the signature to verify", 1)
}
m := c.String("message")
if len(m) == 0 {
return cli.NewExitError("you need to specify the signed message", 1)
}
sig := &ring.Signature{}
err := sig.Decode(sigStr)
if err != nil {
return cli.NewExitError("invalid signature", 1)
}
valid := sig.Verify([]byte(m))
if !valid {
return cli.NewExitError("invalid signature", 1)
}
fmt.Println("Signature is valid.")
return nil
}