2022-12-08 13:05:19 -03:00

70 lines
1.6 KiB
Go

// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License.AGPL.txt in the project root for license information.
package cmd
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"os"
"github.com/spf13/cobra"
)
// genkeyCmd represents the genkey command
var genkeyCmd = &cobra.Command{
Use: "genkey",
Short: "Generates a public/private key for signing licenses",
RunE: func(cmd *cobra.Command, args []string) error {
priv, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return err
}
privf, err := os.Create("private_key.pem")
if err != nil {
return err
}
defer privf.Close()
err = pem.Encode(privf, &pem.Block{
Type: "PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(priv),
})
if err != nil {
return err
}
pubf, err := os.Create("public_key.pem")
if err != nil {
return err
}
defer pubf.Close()
err = pem.Encode(pubf, &pem.Block{
Type: "PUBLIC KEY",
Bytes: x509.MarshalPKCS1PublicKey(&priv.PublicKey),
})
if err != nil {
return err
}
return nil
},
}
func init() {
rootCmd.AddCommand(genkeyCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// genkeyCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// genkeyCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}