feat: Remove generate key command, each command will generate its own key.
This commit is contained in:
1 parent
76854f4e4b
commit
bf3ca1d0bf
1 file changed
-54
@@ -1,54 +0,0 @@
|
||||
package hmpki
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"os"
|
||||
|
||||
"git.handmadecities.com/HMC/shhh/pkg/pki"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func init() {
|
||||
generateCmd.AddCommand(generateKeyCmd)
|
||||
}
|
||||
|
||||
var generateKeyCmd = &cobra.Command{
|
||||
Use: "key",
|
||||
Short: "Generate a private key and save it to a file",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
der, err := x509.MarshalPKCS8PrivateKey(priv)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pemBytes := pem.EncodeToMemory(&pem.Block{
|
||||
Type: pki.PEMTypePrivateKey,
|
||||
Bytes: der,
|
||||
})
|
||||
|
||||
kf, err := os.OpenFile(
|
||||
Output,
|
||||
// Write only, and fail if it alreay exists.
|
||||
os.O_WRONLY|os.O_CREATE|os.O_EXCL,
|
||||
pki.PrivateKeyFilePerms,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer kf.Close()
|
||||
if _, err = kf.Write(pemBytes); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
Reference in new issue
Block a user