-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
66 lines (58 loc) · 1.58 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
package main
import (
"encoding/base64"
"fmt"
"github.com/hashicorp/vault/shamir"
"log"
"os"
"strings"
"flag"
)
func handleError(err error) {
if err != nil {
log.Fatalln(err)
}
}
var encoder *base64.Encoding = base64.URLEncoding
var secret, shardsCommaSep string
var k, t int
func init() {
flag.StringVar(&secret, "split", "", "The secret to split into shards")
flag.StringVar(&shardsCommaSep, "combine", "", "The shards to combine to get the secret separated by commas")
flag.IntVar(&k, "k", 3, "The number of total key shards")
flag.IntVar(&t, "t", 2, "The min number of shards needed to re-assemble the secret")
flag.Parse()
}
func main() {
if len(secret) > 0 && len(shardsCommaSep) > 0 {
fmt.Println("ERROR: Cannot both split and combine in the same action")
flag.Usage()
os.Exit(1)
} else if len(secret) == 0 && len(shardsCommaSep) == 0 {
flag.Usage()
os.Exit(1)
}
// Split Command selected
if len(secret) > 0 {
keys, err := shamir.Split([]byte(secret), k, t)
if err != nil {
log.Fatalln(err)
}
for _, shard := range keys {
// RFC 4648 Encoding
encoded := encoder.EncodeToString(shard)
fmt.Println(encoded)
}
} else {
shards := strings.Split(shardsCommaSep, ",")
shardBytes := make([][]byte, len(shards))
for i, shard := range shards {
decodedShard, err := base64.URLEncoding.DecodeString(shard)
handleError(err)
shardBytes[i] = []byte(decodedShard)
}
decodedSecret, err := shamir.Combine(shardBytes)
handleError(err)
fmt.Println(decodedSecret)
}
}