-
Notifications
You must be signed in to change notification settings - Fork 24
/
keygen.go
49 lines (43 loc) 路 1.25 KB
/
keygen.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
42
43
44
45
46
47
48
49
package cmd
import (
"fmt"
"path/filepath"
"github.com/ferama/rospo/pkg/utils"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(keygenCmd)
keygenCmd.Flags().BoolP("store", "s", false, "optional store the keys to files")
keygenCmd.Flags().StringP("path", "p", ".", "key pair destination path")
keygenCmd.Flags().StringP("name", "n", "identity", "output file name")
}
var keygenCmd = &cobra.Command{
Use: "keygen",
Short: "Generates private/public key pairs",
Long: `Generates private/public key pairs`,
Example: `
# generates a key pair an store it into identiy and identity.pub files
$ rospo keygen -s
`,
Run: func(cmd *cobra.Command, args []string) {
path, _ := cmd.Flags().GetString("path")
name, _ := cmd.Flags().GetString("name")
storeKeys, _ := cmd.Flags().GetBool("store")
key, err := utils.GeneratePrivateKey()
if err != nil {
panic(err)
}
publicKey, err := utils.GeneratePublicKey(&key.PublicKey)
if err != nil {
panic(err)
}
encodedKey := utils.EncodePrivateKeyToPEM(key)
if storeKeys {
utils.WriteKeyToFile(encodedKey, filepath.Join(path, name))
utils.WriteKeyToFile(publicKey, filepath.Join(path, name+".pub"))
} else {
fmt.Printf("%s", encodedKey)
fmt.Printf("%s", publicKey)
}
},
}