blob: 695176fd4efa94e881bdd07c42393c902c4c46bf [file] [log] [blame]
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +04001package installer
2
3import (
4 "crypto/ed25519"
5 "crypto/rand"
6 "crypto/x509"
7 "encoding/pem"
8
9 "golang.org/x/crypto/ssh"
10)
11
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040012type KeyPair struct {
13 Public string
14 Private string
15}
16
17func NewSSHKeyPair() (KeyPair, error) {
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040018 pub, priv, err := ed25519.GenerateKey(rand.Reader)
19 if err != nil {
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040020 return KeyPair{}, err
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040021 }
22 privEnc, err := x509.MarshalPKCS8PrivateKey(priv)
23 if err != nil {
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040024 return KeyPair{}, err
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040025 }
26 privPem := pem.EncodeToMemory(
27 &pem.Block{
28 Type: "PRIVATE KEY",
29 Bytes: privEnc,
30 },
31 )
32 pubKey, err := ssh.NewPublicKey(pub)
33 if err != nil {
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040034 return KeyPair{}, err
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040035 }
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040036 return KeyPair{
37 Public: string(ssh.MarshalAuthorizedKey(pubKey)),
38 Private: string(privPem),
39 }, nil
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040040}