-
Notifications
You must be signed in to change notification settings - Fork 0
/
keypair.go
65 lines (54 loc) · 1.32 KB
/
keypair.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 tls
import (
"github.com/openshift/installer/pkg/asset"
"github.com/pkg/errors"
)
// KeyPairInterface contains a private key and a public key.
type KeyPairInterface interface {
// Private returns the private key.
Private() []byte
// Public returns the public key.
Public() []byte
}
// KeyPair contains a private key and a public key.
type KeyPair struct {
Pvt []byte
Pub []byte
FileList []*asset.File
}
// Generate generates the rsa private / public key pair.
func (k *KeyPair) Generate(filenameBase string) error {
key, err := PrivateKey()
if err != nil {
return errors.Wrap(err, "failed to generate private key")
}
pubkeyData, err := PublicKeyToPem(&key.PublicKey)
if err != nil {
return errors.Wrap(err, "failed to get public key data from private key")
}
k.Pvt = PrivateKeyToPem(key)
k.Pub = pubkeyData
k.FileList = []*asset.File{
{
Filename: assetFilePath(filenameBase + ".key"),
Data: k.Pvt,
},
{
Filename: assetFilePath(filenameBase + ".pub"),
Data: k.Pub,
},
}
return nil
}
// Public returns the public key.
func (k *KeyPair) Public() []byte {
return k.Pub
}
// Private returns the private key.
func (k *KeyPair) Private() []byte {
return k.Pvt
}
// Files returns the files generated by the asset.
func (k *KeyPair) Files() []*asset.File {
return k.FileList
}