Last active
July 19, 2019 02:15
-
-
Save hkparker/b54d9cd34b5ea442bb4b2b1d7f48e629 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"flag" | |
"bufio" | |
"os" | |
"encoding/hex" | |
log "github.com/sirupsen/logrus" | |
"github.com/hashicorp/vault/shamir" | |
) | |
func main() { | |
var split bool | |
var combine bool | |
var str string | |
var n int | |
var m int | |
flag.BoolVar(&split, "split", false, "shard a string") | |
flag.BoolVar(&combine, "combine", false, "combine shards to reproduce a string") | |
flag.StringVar(&str, "string", "", "string to shard") | |
flag.IntVar(&n, "n", 0, "number of shards required to rebuild secret") | |
flag.IntVar(&m, "m", 0, "total number of shards to generate") | |
flag.Parse() | |
if split && combine { | |
log.Fatal("cannot split and combine at the same time, doesn't make sense") | |
} | |
if !split && !combine { | |
log.Fatal("please choose to either split or combine") | |
} | |
if split { | |
if str == "" { | |
log.Fatal("can't split an empty string, please provide a string argument") | |
} | |
shards, err := shamir.Split([]byte(str), m, n) | |
if err != nil { | |
log.Fatal("error creating shards: " + err.Error()) | |
} | |
for _, shard := range shards { | |
shardStr := hex.EncodeToString(shard) | |
fmt.Println(shardStr) | |
} | |
} else if combine { | |
parts := make([][]byte, 0) | |
scanner := bufio.NewScanner(os.Stdin) | |
recovered := false | |
fmt.Println("Provide shards one at a time:") | |
for !recovered { | |
fmt.Print("> ") | |
scanner.Scan() | |
bytes, err := hex.DecodeString(scanner.Text()) | |
if err != nil { | |
log.Error("error decoding string: " + err.Error()) | |
log.Error("skipping") | |
} else { | |
parts = append(parts, bytes) | |
str, err := shamir.Combine(parts) | |
if err != nil { | |
continue | |
} else { | |
fmt.Println(string(str)) | |
recovered = true | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment