-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsshkeygen.go
53 lines (43 loc) · 1.04 KB
/
sshkeygen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"os"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"golang.org/x/crypto/ssh"
)
func generateSSHKeyPair(publicKeyPath, privateKeyPath string) error {
// Check if Key exists
_, err := os.Stat(privateKeyPath)
// No error means the key exists, do nothing
if err == nil {
return nil
}
// If error isn't "doesn't exist" exit
if !os.IsNotExist(err) {
return err
}
privateKey, err := rsa.GenerateKey(rand.Reader, 1024)
if err != nil {
return err
}
privateKeyFile, err := os.Create(privateKeyPath)
defer privateKeyFile.Close()
if err != nil {
return err
}
privateKeyPEM := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)}
if err := pem.Encode(privateKeyFile, privateKeyPEM); err != nil {
return err
}
if err := os.Chmod(privateKeyPath, 0600); err != nil {
return err
}
// generate and write public key
pub, err := ssh.NewPublicKey(&privateKey.PublicKey)
if err != nil {
return err
}
return os.WriteFile(publicKeyPath, ssh.MarshalAuthorizedKey(pub), 0644)
}