Last active
May 31, 2018 03:30
-
-
Save aaronjanse/6d9ec8bd48ec31bb14efde34bd657390 to your computer and use it in GitHub Desktop.
An easy way to do shamir secret sharing from the command line
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" | |
"io/ioutil" | |
"log" | |
"os" | |
"strings" | |
sssa "github.com/SSSAAS/sssa-golang" | |
"github.com/urfave/cli" | |
) | |
func main() { | |
app := cli.NewApp() | |
app.Name = "shamir" | |
app.Usage = "command line tool for easy Shamir Secret Sharing" | |
app.Commands = []cli.Command{ | |
{ | |
Name: "share", | |
Flags: []cli.Flag{ | |
cli.IntFlag{ | |
Name: "minimum, m", | |
}, | |
cli.IntFlag{ | |
Name: "total, t", | |
}, | |
cli.StringFlag{ | |
Name: "secret, s", | |
}, | |
}, | |
Action: func(c *cli.Context) error { | |
shares, err := sssa.Create(c.Int("minimum"), c.Int("total"), c.String("secret")) | |
if err != nil { | |
return err | |
} | |
fmt.Println(strings.Join(shares, "\n")) | |
return nil | |
}, | |
}, | |
{ | |
Name: "unite", | |
Usage: "shares read from stdin, one per line", | |
Action: func(c *cli.Context) error { | |
stdinBytes, err := ioutil.ReadAll(os.Stdin) | |
if err != nil { | |
return err | |
} | |
stdinText := strings.TrimSpace(string(stdinBytes)) | |
shares := make([]string, 0) | |
for _, line := range strings.Split(stdinText, "\n") { | |
shares = append(shares, line) | |
} | |
secret, err := sssa.Combine(shares) | |
if err != nil { | |
return err | |
} | |
fmt.Println(secret) | |
return nil | |
}, | |
}, | |
} | |
err := app.Run(os.Args) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment