aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go63
1 files changed, 63 insertions, 0 deletions
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..6297ad9
--- /dev/null
+++ b/main.go
@@ -0,0 +1,63 @@
+// SPDX-FileCopyrightText: © 2021 Grégoire Duchêne <gduchene@awhk.org>
+// SPDX-License-Identifier: ISC
+
+package main
+
+import (
+ "bytes"
+ "encoding/base64"
+ "flag"
+ "fmt"
+ "io/ioutil"
+ "os"
+
+ "github.com/hashicorp/vault/shamir"
+)
+
+var (
+ combine = flag.Bool("c", false, "combine instead of split")
+ parts = flag.Int("p", 10, "parts to split the secret into")
+ thres = flag.Int("t", 2, "threshold required to reconstruct the secret")
+)
+
+func main() {
+ flag.Parse()
+
+ buf, err := ioutil.ReadAll(os.Stdin)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "failed to read the secret:", err)
+ os.Exit(1)
+ }
+
+ if !*combine {
+ parts, err := shamir.Split(buf, *parts, *thres)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "failed to split the secret:", err)
+ os.Exit(1)
+ }
+ for _, part := range parts {
+ fmt.Println(base64.StdEncoding.EncodeToString(part))
+ }
+ return
+ }
+
+ parts := [][]byte{}
+ for _, buf := range bytes.Split(buf, []byte("\n")) {
+ if len(buf) == 0 {
+ continue
+ }
+ part := make([]byte, base64.StdEncoding.DecodedLen(len(buf)))
+ n, err := base64.StdEncoding.Decode(part, buf)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "failed to decode a part:", err)
+ os.Exit(1)
+ }
+ parts = append(parts, part[:n])
+ }
+ sec, err := shamir.Combine(parts)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "failed to combine parts:", err)
+ os.Exit(1)
+ }
+ fmt.Print(string(sec))
+}