-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rsa.go
150 lines (118 loc) · 4.07 KB
/
rsa.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package encryption
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
)
type RSAHelper struct{}
func (h RSAHelper) Encode(privateKey *rsa.PrivateKey, publicKey *rsa.PublicKey) (string, string) {
x509Encoded := x509.MarshalPKCS1PrivateKey(privateKey)
pemEncoded := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: x509Encoded})
x509EncodedPub := x509.MarshalPKCS1PublicKey(publicKey)
pemEncodedPub := pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: x509EncodedPub})
return string(pemEncoded), string(pemEncodedPub)
}
func (h RSAHelper) Decode(pemEncoded string, pemEncodedPub string) (*rsa.PrivateKey, *rsa.PublicKey) {
block, _ := pem.Decode([]byte(pemEncoded))
x509Encoded := block.Bytes
privateKey, err := x509.ParsePKCS1PrivateKey(x509Encoded)
if err != nil {
println(err.Error())
}
blockPub, _ := pem.Decode([]byte(pemEncodedPub))
x509EncodedPub := blockPub.Bytes
genericPublicKey, err := x509.ParsePKIXPublicKey(x509EncodedPub)
if err != nil {
println(err.Error())
}
publicKey := genericPublicKey.(*rsa.PublicKey)
return privateKey, publicKey
}
func (h RSAHelper) DecodePrivateKeyFromPem(pemEncoded string) *rsa.PrivateKey {
block, _ := pem.Decode([]byte(pemEncoded))
x509Encoded := block.Bytes
privateKey, err := x509.ParsePKCS1PrivateKey(x509Encoded)
if err != nil {
logger.Error("There was an error decoding the private key from pem: %v", err.Error())
return nil
}
return privateKey
}
func (h RSAHelper) DecodePublicKeyFromPem(pemEncoded string) *rsa.PublicKey {
block, _ := pem.Decode([]byte(pemEncoded))
x509Encoded := block.Bytes
publicKey, err := x509.ParsePKCS1PublicKey(x509Encoded)
if err != nil {
logger.Error("There was an error decoding the private key from pem: %v", err.Error())
return nil
}
return publicKey
}
func (h RSAHelper) DecodePrivateKeyFromBase64(bas64Encoded string) *rsa.PrivateKey {
x509Encoded, err := base64.URLEncoding.DecodeString(bas64Encoded)
if err != nil {
logger.Error("There was an error decoding the private key from base64: %v", err.Error())
return nil
}
privateKey, err := x509.ParsePKCS1PrivateKey(x509Encoded)
if err != nil {
logger.Error("There was an error getting the private key from base64: %v", err.Error())
return nil
}
return privateKey
}
func (h RSAHelper) DecodePublicKeyFromBase64(bas64Encoded string) *rsa.PublicKey {
x509Encoded, err := base64.URLEncoding.DecodeString(bas64Encoded)
if err != nil {
logger.Error("There was an error decoding the private key from base64: %v", err.Error())
return nil
}
publicKey, err := x509.ParsePKCS1PublicKey(x509Encoded)
if err != nil {
logger.Error("There was an error getting the private key from base64: %v", err.Error())
return nil
}
return publicKey
}
func (h RSAHelper) DecodePrivateKeyFromBase64Pem(bas64Encoded string) *rsa.PrivateKey {
pemEncoded, err := base64.URLEncoding.DecodeString(bas64Encoded)
if err != nil {
logger.Error("There was an error decoding the private key from base64: %v", err.Error())
return nil
}
privateKey := h.DecodePrivateKeyFromPem(string(pemEncoded))
if privateKey == nil {
logger.Error("There was an error getting the private key from base64: %v", err.Error())
return nil
}
return privateKey
}
func (h RSAHelper) DecodePublicKeyFromBase64Pem(bas64Encoded string) *rsa.PublicKey {
pemEncoded, err := base64.URLEncoding.DecodeString(bas64Encoded)
if err != nil {
logger.Error("There was an error decoding the private key from base64: %v", err.Error())
return nil
}
publicKey := h.DecodePublicKeyFromPem(string(pemEncoded))
if publicKey == nil {
logger.Error("There was an error getting the private key from base64: %v", err.Error())
return nil
}
return publicKey
}
func (h RSAHelper) GeneratePrivateKey(size EncryptionKeySize) *rsa.PrivateKey {
priv, err := rsa.GenerateKey(rand.Reader, int(size))
if err != nil {
return nil
}
return priv
}
func (h RSAHelper) GenerateKeys(size EncryptionKeySize) (*rsa.PrivateKey, *rsa.PublicKey) {
priv := h.GeneratePrivateKey(size)
if priv == nil {
return nil, nil
}
return priv, &priv.PublicKey
}