/
keys.go
51 lines (43 loc) · 1.19 KB
/
keys.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
// Package sshkey provides public and private key pair for ssh usage
package sshkey
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"fmt"
"golang.org/x/crypto/ssh"
)
const (
// This is a default value for private key block
privateKeyType = "RSA PRIVATE KEY"
// while generating ssh key,
// 2048 bit size is the default size.
// as optional it can be 1024 or 4096 etc..
bitSize = 2048
)
// Generate creates public and private key pairs for using as ssh keys uses
// RSA for generating keys
func Generate() (pubKey string, privKey string, err error) {
// genereate key pair
privateKey, err := rsa.GenerateKey(rand.Reader, bitSize)
if err != nil {
return "", "", err
}
/// convert to private key block
privateKeyBlock := pem.Block{
Type: privateKeyType,
Headers: nil,
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
}
// convert to public key
pub, err := ssh.NewPublicKey(&privateKey.PublicKey)
if err != nil {
return "", "", err
}
// standart github ssh preferences
pubKey = fmt.Sprintf("ssh-rsa %v", base64.StdEncoding.EncodeToString(pub.Marshal()))
privKey = string(pem.EncodeToMemory(&privateKeyBlock))
return pubKey, privKey, nil
}