generated from hyperjiang/go-tpl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsa.go
334 lines (283 loc) · 7.98 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package rsa
import (
"bytes"
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"math/big"
"strings"
)
// EncryptByPublicKey encrypts by public key.
func EncryptByPublicKey(src, publicKey []byte) (dst []byte, err error) {
dst = []byte("")
if len(src) == 0 {
return
}
pub, err := ParsePublicKey(publicKey)
if err != nil {
err = invalidPublicKeyError()
return
}
buffer := bytes.NewBufferString("")
for _, chunk := range bytesSplit(src, pub.Size()-11) {
dst, err = rsa.EncryptPKCS1v15(rand.Reader, pub, chunk)
buffer.Write(dst)
}
dst = buffer.Bytes()
return
}
// DecryptByPrivateKey decrypts by private key.
func DecryptByPrivateKey(src, privateKey []byte) (dst []byte, err error) {
dst = []byte("")
if len(src) == 0 {
return
}
pri, err := ParsePrivateKey(privateKey)
if err != nil {
err = invalidPrivateKeyError()
return
}
buffer := bytes.NewBufferString("")
for _, chunk := range bytesSplit(src, pri.Size()) {
dst, err = rsa.DecryptPKCS1v15(rand.Reader, pri, chunk)
buffer.Write(dst)
}
dst = buffer.Bytes()
return
}
// EncryptByPrivateKey encrypts by private key.
func EncryptByPrivateKey(src, privateKey []byte) (dst []byte, err error) {
dst = []byte("")
if len(src) == 0 {
return
}
pri, err := ParsePrivateKey(privateKey)
if err != nil {
err = invalidPrivateKeyError()
return
}
buffer := bytes.NewBufferString("")
for _, chunk := range bytesSplit(src, pri.Size()-11) {
dst, err = rsa.SignPKCS1v15(nil, pri, crypto.Hash(0), chunk)
buffer.Write(dst)
}
dst = buffer.Bytes()
return
}
// DecryptByPublicKey decrypts by public key.
func DecryptByPublicKey(src, publicKey []byte) (dst []byte, err error) {
dst = []byte("")
if len(src) == 0 {
return
}
pub, err := ParsePublicKey(publicKey)
if err != nil {
err = invalidPublicKeyError()
return
}
buffer := bytes.NewBufferString("")
bigInt := new(big.Int)
for _, chunk := range bytesSplit(src, pub.Size()) {
bigInt.Exp(new(big.Int).SetBytes(chunk), big.NewInt(int64(pub.E)), pub.N)
dst = leftUnPad(leftPad(bigInt.Bytes(), pub.Size()))
buffer.Write(dst)
}
dst = buffer.Bytes()
return
}
// SignByPrivateKey signs by private key.
func SignByPrivateKey(src, privateKey []byte, hash crypto.Hash) (dst []byte, err error) {
dst = []byte("")
pri, err := ParsePrivateKey(privateKey)
if err != nil {
err = invalidPrivateKeyError()
return
}
hasher := hash.New()
hasher.Write(src)
hashed := hasher.Sum(nil)
dst, err = rsa.SignPKCS1v15(rand.Reader, pri, hash, hashed)
return
}
// VerifyByPublicKey verify by public key.
func VerifyByPublicKey(src, sign, publicKey []byte, hash crypto.Hash) (err error) {
pub, err := ParsePublicKey(publicKey)
if err != nil {
err = invalidPublicKeyError()
return
}
hasher := hash.New()
hasher.Write(src)
hashed := hasher.Sum(nil)
return rsa.VerifyPKCS1v15(pub, hash, hashed, sign)
}
// GenKeyPair generates key pair.
func GenKeyPair(pkcs KeyFormat, bits int) (publicKey, privateKey []byte) {
pri, _ := rsa.GenerateKey(rand.Reader, bits)
if pkcs == PKCS1 {
privateBytes := x509.MarshalPKCS1PrivateKey(pri)
privateKey = pem.EncodeToMemory(&pem.Block{
Type: PKCS1PrivateKey,
Bytes: privateBytes,
})
publicBytes := x509.MarshalPKCS1PublicKey(&pri.PublicKey)
publicKey = pem.EncodeToMemory(&pem.Block{
Type: PKCS1PublicKey,
Bytes: publicBytes,
})
} else if pkcs == PKCS8 {
privateBytes, _ := x509.MarshalPKCS8PrivateKey(pri)
privateKey = pem.EncodeToMemory(&pem.Block{
Type: PKCS8PrivateKey,
Bytes: privateBytes,
})
publicBytes, _ := x509.MarshalPKIXPublicKey(&pri.PublicKey)
publicKey = pem.EncodeToMemory(&pem.Block{
Type: PKCS8PublicKey,
Bytes: publicBytes,
})
}
return
}
// VerifyKeyPair verify key pairs.
func VerifyKeyPair(publicKey, privateKey []byte) bool {
pub, err := ExportPublicKey(privateKey)
if err != nil {
return false
}
if bytes2string(publicKey) == bytes2string(pub) {
return true
}
return false
}
// ParsePublicKey parses public key.
func ParsePublicKey(publicKey []byte) (*rsa.PublicKey, error) {
block, _ := pem.Decode(publicKey)
if block == nil {
return nil, invalidPublicKeyError()
}
if block.Type == PKCS1PublicKey {
pub, err := x509.ParsePKCS1PublicKey(block.Bytes)
if err != nil {
return nil, invalidPublicKeyError()
}
return pub, nil
}
if block.Type == PKCS8PublicKey {
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, invalidPublicKeyError()
}
return pub.(*rsa.PublicKey), err
}
return nil, invalidPublicKeyError()
}
// ParsePrivateKey parses private key.
func ParsePrivateKey(privateKey []byte) (*rsa.PrivateKey, error) {
block, _ := pem.Decode(privateKey)
if block == nil {
return nil, invalidPrivateKeyError()
}
if block.Type == PKCS1PrivateKey {
pri, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, invalidPrivateKeyError()
}
return pri, err
}
if block.Type == PKCS8PrivateKey {
pri, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, invalidPrivateKeyError()
}
return pri.(*rsa.PrivateKey), err
}
return nil, invalidPrivateKeyError()
}
// ExportPublicKey exports public key from private key.
func ExportPublicKey(privateKey []byte) (publicKey []byte, err error) {
block, _ := pem.Decode(privateKey)
if block == nil {
err = invalidPrivateKeyError()
return
}
pri, err := ParsePrivateKey(privateKey)
if err != nil {
return
}
blockType, blockBytes := "", []byte("")
if block.Type == PKCS1PrivateKey {
blockType = PKCS1PublicKey
blockBytes = x509.MarshalPKCS1PublicKey(&pri.PublicKey)
} else if block.Type == PKCS8PrivateKey {
blockType = PKCS8PublicKey
blockBytes, err = x509.MarshalPKIXPublicKey(&pri.PublicKey)
}
publicKey = pem.EncodeToMemory(&pem.Block{
Type: blockType,
Bytes: blockBytes,
})
return
}
// IsPublicKey checks whether is public key.
func IsPublicKey(publicKey []byte) bool {
block, _ := pem.Decode(publicKey)
if block == nil {
return false
}
return block.Type == PKCS1PublicKey || block.Type == PKCS8PublicKey
}
// IsPrivateKey checks whether is private key.
func IsPrivateKey(privateKey []byte) bool {
block, _ := pem.Decode(privateKey)
if block == nil {
return false
}
return block.Type == PKCS1PrivateKey || block.Type == PKCS8PrivateKey
}
// CompressKey compresses key, removes the head, tail and newline character.
func CompressKey(key []byte) ([]byte, error) {
str := strings.Replace(bytes2string(key), "\n", "", -1)
if IsPublicKey(key) {
str = strings.Replace(str, PKCS1PublicHeader, "", -1)
str = strings.Replace(str, PKCS1PublicTail, "", -1)
str = strings.Replace(str, PKCS8PublicHeader, "", -1)
str = strings.Replace(str, PKCS8PublicTail, "", -1)
} else if IsPrivateKey(key) {
str = strings.Replace(str, PKCS1PrivateHeader, "", -1)
str = strings.Replace(str, PKCS1PrivateTail, "", -1)
str = strings.Replace(str, PKCS8PrivateHeader, "", -1)
str = strings.Replace(str, PKCS8PrivateTail, "", -1)
} else {
return nil, invalidRSAKeyError()
}
return string2bytes(str), nil
}
// FormatPublicKey formats public key, adds header, tail and newline character.
func FormatPublicKey(pkcs KeyFormat, publicKey []byte) []byte {
var keyHeader, keyTail string
if pkcs == PKCS1 {
keyHeader = PKCS1PublicHeader + "\n"
keyTail = PKCS1PublicTail + "\n"
} else if pkcs == PKCS8 {
keyHeader = PKCS8PublicHeader + "\n"
keyTail = PKCS8PublicTail + "\n"
}
keyBody := stringSplit(strings.Replace(bytes2string(publicKey), "\n", "", -1), 64)
return string2bytes(keyHeader + keyBody + keyTail)
}
// FormatPrivateKey formats private key, adds header, tail and newline character.
func FormatPrivateKey(pkcs KeyFormat, privateKey []byte) []byte {
var keyHeader, keyTail string
if pkcs == PKCS1 {
keyHeader = PKCS1PrivateHeader + "\n"
keyTail = PKCS1PrivateTail + "\n"
} else if pkcs == PKCS8 {
keyHeader = PKCS8PrivateHeader + "\n"
keyTail = PKCS8PrivateTail + "\n"
}
keyBody := stringSplit(strings.Replace(bytes2string(privateKey), "\n", "", -1), 64)
return string2bytes(keyHeader + keyBody + keyTail)
}