-
Notifications
You must be signed in to change notification settings - Fork 46
/
key.go
65 lines (59 loc) · 1.5 KB
/
key.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
package main
/*
* key.go
* Get or make a key
* By J. Stuart McMurray
* Created 20160515
* Last Modified 20160515
*/
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"io/ioutil"
"golang.org/x/crypto/ssh"
)
/* getKey either gets or makes an SSH key from/in the file named f. generated
will be true if the key was generated during the call. */
func getKey(f string) (key ssh.Signer, generated bool, err error) {
/* Try to read the key the easy way */
b, err := ioutil.ReadFile(f)
if nil == err {
k, err := ssh.ParsePrivateKey(b)
return k, false, err
}
/* Try to make a key */
/* Code stolen from http://stackoverflow.com/questions/21151714/go-generate-an-ssh-public-key */
privateKey, err := rsa.GenerateKey(rand.Reader, 2014)
if err != nil {
return nil, false, err
}
privateKeyDer := x509.MarshalPKCS1PrivateKey(privateKey)
privateKeyBlock := pem.Block{
Type: "RSA PRIVATE KEY",
Headers: nil,
Bytes: privateKeyDer,
}
privateKeyPem := pem.EncodeToMemory(&privateKeyBlock)
/* Write key to the file */
if err := ioutil.WriteFile(f, privateKeyPem, 400); nil != err {
return nil, false, err
}
/* Make a public key, write to file */
pkb := privateKey.PublicKey
pub, err := ssh.NewPublicKey(&pkb)
if nil != err {
return nil, false, err
}
if err := ioutil.WriteFile(
f+".pub",
ssh.MarshalAuthorizedKey(pub),
0644,
); nil != err {
return nil, false, err
}
/* Load it in useable form */
k, err := ssh.ParsePrivateKey(privateKeyPem)
return k, true, err
}