/
rsakeypair.go
73 lines (63 loc) · 1.71 KB
/
rsakeypair.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
package common
import (
"crypto/rsa"
"fmt"
"io/ioutil"
jwt "github.com/dgrijalva/jwt-go"
)
type RsaKeyPair struct {
Directory string
PrivateKey *rsa.PrivateKey
PrivateBytes []byte
PublicKey *rsa.PublicKey
PublicBytes []byte
KeyPair
}
func NewRsaKeyPair(ctx Context) (KeyPair, error) {
dir := fmt.Sprintf("%s/%s", ctx.GetAppRoot(), "keys")
return CreateRsaKeyPair(ctx, dir)
}
func CreateRsaKeyPair(ctx Context, directory string) (KeyPair, error) {
logger := ctx.GetLogger()
privateKeyBytes, err := ioutil.ReadFile(fmt.Sprintf("%s/%s", directory, "rsa.key"))
if err != nil {
logger.Errorf("[RsaKeyPair] %s", err.Error())
return nil, err
}
privateKey, err := jwt.ParseRSAPrivateKeyFromPEM(privateKeyBytes)
if err != nil {
logger.Errorf("[RsaKeyPair] %s", err.Error())
return nil, err
}
publicKeyBytes, err := ioutil.ReadFile(fmt.Sprintf("%s/%s", directory, "rsa.pub"))
if err != nil {
logger.Errorf("[RsaKeyPair] %s", err.Error())
return nil, err
}
publicKey, err := jwt.ParseRSAPublicKeyFromPEM(publicKeyBytes)
if err != nil {
logger.Errorf("[RsaKeyPair] %s", err.Error())
return nil, err
}
return &RsaKeyPair{
Directory: directory,
PrivateKey: privateKey,
PrivateBytes: privateKeyBytes,
PublicKey: publicKey,
PublicBytes: publicKeyBytes}, nil
}
func (keypair *RsaKeyPair) GetDirectory() string {
return keypair.Directory
}
func (keypair *RsaKeyPair) GetPrivateKey() *rsa.PrivateKey {
return keypair.PrivateKey
}
func (keypair *RsaKeyPair) GetPrivateBytes() []byte {
return keypair.PrivateBytes
}
func (keypair *RsaKeyPair) GetPublicKey() *rsa.PublicKey {
return keypair.PublicKey
}
func (keypair *RsaKeyPair) GetPublicBytes() []byte {
return keypair.PublicBytes
}