| Giorgi Lekveishvili | 23ef7f8 | 2023-05-26 11:57:48 +0400 | [diff] [blame] | 1 | package installer |
| 2 | |
| 3 | import ( |
| 4 | "crypto/ed25519" |
| 5 | "crypto/rand" |
| 6 | "crypto/x509" |
| 7 | "encoding/pem" |
| 8 | |
| 9 | "golang.org/x/crypto/ssh" |
| 10 | ) |
| 11 | |
| 12 | func GenerateSSHKeys() (string, string, error) { |
| 13 | pub, priv, err := ed25519.GenerateKey(rand.Reader) |
| 14 | if err != nil { |
| 15 | return "", "", err |
| 16 | } |
| 17 | privEnc, err := x509.MarshalPKCS8PrivateKey(priv) |
| 18 | if err != nil { |
| 19 | return "", "", err |
| 20 | } |
| 21 | privPem := pem.EncodeToMemory( |
| 22 | &pem.Block{ |
| 23 | Type: "PRIVATE KEY", |
| 24 | Bytes: privEnc, |
| 25 | }, |
| 26 | ) |
| 27 | pubKey, err := ssh.NewPublicKey(pub) |
| 28 | if err != nil { |
| 29 | return "", "", err |
| 30 | } |
| 31 | return string(ssh.MarshalAuthorizedKey(pubKey)), string(privPem), nil |
| 32 | } |