diff options
-rw-r--r-- | cmd/sigsum-debug/genkey.go | 19 | ||||
-rw-r--r-- | cmd/sigsum-debug/hashkey.go | 24 | ||||
-rw-r--r-- | cmd/sigsum-debug/main.go | 92 | ||||
-rw-r--r-- | cmd/sigsum-debug/pubkey.go | 27 | ||||
-rw-r--r-- | cmd/sigsum-debug/util.go | 24 |
5 files changed, 186 insertions, 0 deletions
diff --git a/cmd/sigsum-debug/genkey.go b/cmd/sigsum-debug/genkey.go new file mode 100644 index 0000000..87175a5 --- /dev/null +++ b/cmd/sigsum-debug/genkey.go @@ -0,0 +1,19 @@ +package main + +import ( + "crypto/ed25519" + "crypto/rand" + "fmt" + + "git.sigsum.org/sigsum-lib-go/pkg/hex" +) + +func CmdGenKey() error { + _, priv, err := ed25519.GenerateKey(rand.Reader) + if err != nil { + return fmt.Errorf("failed generating key-pair: %v", err) + } + + fmt.Printf("%s\n", hex.Serialize(priv[:])) + return nil +} diff --git a/cmd/sigsum-debug/hashkey.go b/cmd/sigsum-debug/hashkey.go new file mode 100644 index 0000000..d9b37dd --- /dev/null +++ b/cmd/sigsum-debug/hashkey.go @@ -0,0 +1,24 @@ +package main + +import ( + "crypto/ed25519" + "fmt" + + "git.sigsum.org/sigsum-lib-go/pkg/hex" + "git.sigsum.org/sigsum-lib-go/pkg/types" +) + +func CmdHashKey() error { + b, err := decodeHexFromStdin() + if err != nil { + return err + } + if len(b) != ed25519.PublicKeySize { + return fmt.Errorf("invalid private key: size") + } + pub := ed25519.PublicKey(b) + keyHash := types.HashFn(pub) + + fmt.Printf("%s\n", hex.Serialize(keyHash[:])) + return nil +} diff --git a/cmd/sigsum-debug/main.go b/cmd/sigsum-debug/main.go new file mode 100644 index 0000000..1d9e769 --- /dev/null +++ b/cmd/sigsum-debug/main.go @@ -0,0 +1,92 @@ +// package main provides a tool named sigsum-debug. +// +// Install as follows: +// +// $ go install -ldflags="-X 'main.someVersion=git commit $(git rev-list -1 HEAD)'" +// +// Usage: +// +// $ sigsum-debug help +// +package main + +import ( + "flag" + "fmt" + "log" + "os" +) + +const usage = `sigsum-debug version %s + +Usage: + sigsum-debug help + Outputs a usage message. + + sigsum-debug genkey + Outputs a new private key. + + sigsum-debug pubkey + Reads a private key from stdin and output its public key. + + sigsum-debug hashkey + Reads a public key from stdin and output its key hash. +` + +var ( + someVersion = "unknown" +) + +func main() { + log.SetFlags(0) + + var err error + switch cmd := parseCommand(); cmd.Name() { + case "help": + cmd.Usage() + case "genkey": + err = CmdGenKey() + case "pubkey": + err = CmdPubKey() + case "hashkey": + err = CmdHashKey() + default: + err = fmt.Errorf("invalid command %q, try %q", cmd.Name(), "sigsum help") + } + + if err != nil { + log.Printf("%s", err) + os.Exit(1) + } +} + +func parseCommand() *flag.FlagSet { + args := os.Args + if len(args) < 2 { + args = append(args, "") + } + + fs := flag.NewFlagSet(args[1], flag.ExitOnError) + fs.Usage = func() { + log.Printf(usage, someVersion) + } + registerOptions(fs) + fs.Parse(args[2:]) + return fs +} + +func registerOptions(fs *flag.FlagSet) { + switch cmd := fs.Name(); cmd { + default: + } +} + +func registerStringOption(fs *flag.FlagSet, opt *string, short, long, value string) { + fs.StringVar(opt, short, value, "") + fs.StringVar(opt, long, value, "") +} + +func registerUint64Option(fs *flag.FlagSet, opt *uint64, short, long string, value uint64) { + fs.Uint64Var(opt, short, value, "") + fs.Uint64Var(opt, long, value, "") +} diff --git a/cmd/sigsum-debug/pubkey.go b/cmd/sigsum-debug/pubkey.go new file mode 100644 index 0000000..586d19a --- /dev/null +++ b/cmd/sigsum-debug/pubkey.go @@ -0,0 +1,27 @@ +package main + +import ( + "crypto/ed25519" + "fmt" + + "git.sigsum.org/sigsum-lib-go/pkg/hex" +) + +func CmdPubKey() error { + b, err := decodeHexFromStdin() + if err != nil { + return err + } + if len(b) != ed25519.PrivateKeySize { + return fmt.Errorf("invalid private key: size") + } + + priv := ed25519.PrivateKey(b) + pub, ok := priv.Public().(ed25519.PublicKey) + if !ok { + return fmt.Errorf("failed converting to public key") + } + + fmt.Printf("%s\n", hex.Serialize(pub[:])) + return nil +} diff --git a/cmd/sigsum-debug/util.go b/cmd/sigsum-debug/util.go new file mode 100644 index 0000000..d7ba6a8 --- /dev/null +++ b/cmd/sigsum-debug/util.go @@ -0,0 +1,24 @@ +package main + +import ( + "bytes" + "fmt" + "io/ioutil" + "os" + + "git.sigsum.org/sigsum-lib-go/pkg/hex" +) + +func decodeHexFromStdin() ([]byte, error) { + b, err := ioutil.ReadAll(os.Stdin) + if err != nil { + return nil, fmt.Errorf("failed reading stdin: %v", err) + } + + b, err = hex.Deserialize(string(bytes.TrimSpace(b))) + if err != nil { + return nil, fmt.Errorf("invalid private key: %v", err) + } + + return b, nil +} |