From d01892150eed9d58210eb40b7c005d5fa8e93238 Mon Sep 17 00:00:00 2001 From: Gibheer Date: Sat, 1 Oct 2016 21:56:29 +0200 Subject: rework program flow This commit is a complete rebuild of pkictl. Before everything was all over the place and adding new commands was kind of a hassle. Now each command has its own file and can be adjusted on a command basis. Options are still used by the same name, but can now use different descriptions. --- sign_input.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 sign_input.go (limited to 'sign_input.go') diff --git a/sign_input.go b/sign_input.go new file mode 100644 index 0000000..e2c2320 --- /dev/null +++ b/sign_input.go @@ -0,0 +1,50 @@ +package main + +import ( + "crypto" + "encoding/base64" + "flag" + "io" + "io/ioutil" +) + +func SignInput(args []string) error { + fs := flag.NewFlagSet("pkictl sign-input", flag.ExitOnError) + flagPrivate := fs.String("private-key", "", "path to the private key or read from stdin") + flagInput := fs.String("input", "stdin", "path to the message to sign or stdin") + flagOutput := fs.String("output", "stdout", "write private key to file") + fs.Parse(args) + + pk, err := loadPrivateKey(*flagPrivate) + if err != nil { + return err + } + + out, err := openOutput(*flagOutput) + if err != nil { + return err + } + defer out.Close() + + in, err := openInput(*flagInput) + if err != nil { + return err + } + defer in.Close() + + message, err := ioutil.ReadAll(in) + if err != nil { + return err + } + + signature, err := pk.Sign(message, crypto.SHA256) + if err != nil { + return err + } + + _, err = io.WriteString(out, base64.StdEncoding.EncodeToString(signature)) + if err != nil { + return err + } + return nil +} -- cgit v1.2.3-70-g09d2