-
Notifications
You must be signed in to change notification settings - Fork 13
/
ssh_key_generator.go
81 lines (67 loc) · 1.94 KB
/
ssh_key_generator.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package types
import (
"crypto/md5"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"golang.org/x/crypto/ssh"
)
const (
sshKeyGeneratorKeyBits = 2048
sshKeyGeneratorHeaderPrivateKey = "RSA PRIVATE KEY"
)
type SSHKeyGenerator struct{}
func NewSSHKeyGenerator() SSHKeyGenerator {
return SSHKeyGenerator{}
}
type SSHKey struct {
PrivateKey string `json:"private_key" yaml:"private_key"`
PublicKey string `json:"public_key" yaml:"public_key"`
PublicKeyFingerprint string `json:"public_key_fingerprint" yaml:"public_key_fingerprint"`
}
func (g SSHKeyGenerator) Generate(parameters interface{}) (interface{}, error) {
priv, pub, err := g.generateRSAKeyPair()
if err != nil {
return nil, fmt.Errorf("Generating RSA key pair: %s", err)
}
sshPubKey, err := ssh.NewPublicKey(pub)
if err != nil {
return nil, err
}
return SSHKey{
PrivateKey: g.privateKeyToPEM(priv),
PublicKey: string(ssh.MarshalAuthorizedKey(sshPubKey)),
PublicKeyFingerprint: g.fingerprintMD5(sshPubKey),
}, nil
}
func (g SSHKeyGenerator) encodePEM(keyBytes []byte, keyType string) string {
block := &pem.Block{
Type: keyType,
Bytes: keyBytes,
}
return string(pem.EncodeToMemory(block))
}
func (g SSHKeyGenerator) generateRSAKeyPair() (*rsa.PrivateKey, *rsa.PublicKey, error) {
private, err := rsa.GenerateKey(rand.Reader, sshKeyGeneratorKeyBits)
if err != nil {
return nil, nil, err
}
public := private.Public().(*rsa.PublicKey)
return private, public, nil
}
func (g SSHKeyGenerator) privateKeyToPEM(privateKey *rsa.PrivateKey) string {
return g.encodePEM(x509.MarshalPKCS1PrivateKey(privateKey), sshKeyGeneratorHeaderPrivateKey)
}
func (g SSHKeyGenerator) fingerprintMD5(key ssh.PublicKey) string {
hash := md5.Sum(key.Marshal())
out := ""
for i := 0; i < len(hash); i++ {
if i > 0 {
out += ":"
}
out += fmt.Sprintf("%02x", hash[i]) // don't forget the leading zeroes
}
return out
}