-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd_generate_key.go
41 lines (36 loc) · 843 Bytes
/
cmd_generate_key.go
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
package main
import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"github.com/azdagron/spire-pipe/codec"
"github.com/spf13/cobra"
)
func GenerateKeyCommand() *cobra.Command {
impl := &generateKey{
outFormat: AllBytesFormats(),
}
cmd := &cobra.Command{
Use: "key",
Args: cobra.ExactArgs(0),
RunE: runOut(impl),
}
cmd.Flags().VarP(&impl.outFormat, "out-format", "", "output format")
return cmd
}
type generateKey struct {
outFormat BytesFormatFlag
}
func (cmd *generateKey) Run(_ context.Context, args []string) ([]byte, error) {
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return nil, err
}
keyBytes, err := x509.MarshalPKCS8PrivateKey(key)
if err != nil {
return nil, err
}
return codec.BytesToBytes(keyBytes, codec.RawBytes(), cmd.outFormat)
}