Commit 930f5a7

Karn Wong <[email protected]>
2023-12-28 17:03:53
openssh compatible
1 parent d00e51e
cmd/ssh/ssh_create_ssh_key.go
@@ -1,19 +1,22 @@
 package ssh
 
 import (
+	"crypto"
 	"crypto/ed25519"
-	"crypto/rand"
 	"encoding/base64"
+	"encoding/pem"
 	"fmt"
 	"log"
 	"os"
 
+	"golang.org/x/crypto/ssh"
+
 	"github.com/fatih/color"
 	"github.com/spf13/cobra"
 )
 
 // helpers
-func writeStringToFile(filePath, data string) {
+func writeStringToFile(filePath, data string, permission os.FileMode) {
 	file, err := os.Create(filePath)
 	if err != nil {
 		log.Fatal(err)
@@ -23,34 +26,40 @@ func writeStringToFile(filePath, data string) {
 	if err != nil {
 		log.Fatal(err)
 	}
-}
 
-func writePrivateKey(privateKey ed25519.PrivateKey) {
-	privateKeyStr := fmt.Sprintf("-----BEGIN OPENSSH PRIVATE KEY-----\n%s\n-----END OPENSSH PRIVATE KEY-----\n", base64.StdEncoding.EncodeToString(privateKey))
-
-	writeStringToFile("key.pem", privateKeyStr)
-}
-
-func writePublicKey(publicKey ed25519.PublicKey) {
-	publicKeyStr := fmt.Sprintf("ssh-ed25519 %s", base64.StdEncoding.EncodeToString(publicKey))
-
-	writeStringToFile("key.pub", publicKeyStr)
+	err = file.Chmod(permission)
+	if err != nil {
+		fmt.Println("Error setting file permissions:", err)
+		return
+	}
 }
 
 // main
-func createSSHKeyEDSA() string {
+func createSSHKeyEDSA(fileName string) {
 	// Generate a new Ed25519 private key
-	publicKey, privateKey, err := ed25519.GenerateKey(rand.Reader)
+	//// If rand is nil, crypto/rand.Reader will be used
+	pub, priv, err := ed25519.GenerateKey(nil)
+	if err != nil {
+		panic(err)
+	}
+	p, err := ssh.MarshalPrivateKey(crypto.PrivateKey(priv), "")
 	if err != nil {
-		fmt.Println("Error generating private key:", err)
-		os.Exit(1)
+		panic(err)
 	}
 
-	// Write key
-	writePrivateKey(privateKey)
-	writePublicKey(publicKey)
+	// private key
+	privateKeyPem := pem.EncodeToMemory(p)
+	privateKeyString := string(privateKeyPem)
 
-	return "foo"
+	writeStringToFile(fmt.Sprintf("%s.pem", fileName), privateKeyString, 0600)
+
+	// public key
+	publicKey, err := ssh.NewPublicKey(pub)
+	if err != nil {
+		panic(err)
+	}
+	publicKeyString := "ssh-ed25519" + " " + base64.StdEncoding.EncodeToString(publicKey.Marshal())
+	writeStringToFile(fmt.Sprintf("%s.pub", fileName), publicKeyString, 0644)
 }
 
 var createSSHKey = &cobra.Command{
@@ -60,7 +69,9 @@ var createSSHKey = &cobra.Command{
 	Run: func(cmd *cobra.Command, args []string) {
 		color.Green("SSH: create-ssh-key")
 
-		fmt.Printf("\tSSH key created at: %s\n", createSSHKeyEDSA())
+		fileName := "foo"
+		createSSHKeyEDSA(fileName)
+		fmt.Printf("\tSSH key created at: %s\n", fileName)
 	},
 }
 
go.mod
@@ -5,6 +5,7 @@ go 1.20
 require (
 	github.com/fatih/color v1.15.0
 	github.com/spf13/cobra v1.7.0
+	golang.org/x/crypto v0.17.0
 )
 
 require (
@@ -12,5 +13,5 @@ require (
 	github.com/mattn/go-colorable v0.1.13 // indirect
 	github.com/mattn/go-isatty v0.0.17 // indirect
 	github.com/spf13/pflag v1.0.5 // indirect
-	golang.org/x/sys v0.13.0 // indirect
+	golang.org/x/sys v0.15.0 // indirect
 )
go.sum
@@ -13,8 +13,11 @@ github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
 github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
 github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
 github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
+golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
+golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
 golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
-golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
-golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
+golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
+golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=