aboutsummaryrefslogtreecommitdiff
path: root/sign_input.go
blob: e2c23201bdac63738d0894a399e795b217d6f87d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
}