-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsa.go
337 lines (290 loc) · 7.29 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
335
336
337
package encrypt
import (
"bytes"
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"os"
"strings"
)
type optionParams interface{}
type rsaHelper struct{}
func NewRSA() *rsaHelper {
return &rsaHelper{}
}
const (
generatePemPath = 0
)
var generatePemParams = []int{
generatePemPath,
}
// GenerateRSAPem
//
// @param int bits
// @param string filePath
//
// @return publicKeyStr, privateKeyStr string, err error
func (r *rsaHelper) GeneratePem(bits int, opts ...optionParams) (publicKeyStr, privateKeyStr string, err error) {
var isWriteIntoFile = false
var filePath string
if len(opts) > len(generatePemParams) {
panic("Too many params.")
}
for k, v := range opts {
if k == generatePemPath && v.(string) != "" {
isWriteIntoFile = true
filePath = v.(string)
filePath = strings.TrimRight(filePath, "/")
// create pem dir
if isExist(filePath) != true {
mkdirErr := os.Mkdir(filePath, 0755)
if mkdirErr != nil {
err = mkdirErr
return
}
}
}
}
// generate private key
privateKey, genPriKeyErr := rsa.GenerateKey(rand.Reader, bits)
if genPriKeyErr != nil {
return
}
derStream := x509.MarshalPKCS1PrivateKey(privateKey)
block := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: derStream,
}
// write into file
if isWriteIntoFile {
privateKeyFile, createFileErr := os.Create(filePath + "/private_key.pem")
if privateKeyFile != nil {
defer privateKeyFile.Close()
}
if createFileErr != nil {
panic(createFileErr)
}
pemEncodeErr := pem.Encode(privateKeyFile, block)
if pemEncodeErr != nil {
return
}
}
// write into buffer
bufferPrivate := new(bytes.Buffer)
pemEncodeErr := pem.Encode(bufferPrivate, block)
if pemEncodeErr != nil {
return
}
privateKeyStr = bufferPrivate.String()
// generate public key
publicKey := &privateKey.PublicKey
derPkix, err := x509.MarshalPKIXPublicKey(publicKey)
if err != nil {
return
}
block = &pem.Block{
Type: "RSA PUBLIC KEY",
Bytes: derPkix,
}
// write into file
if isWriteIntoFile {
publicKeyFile, createFileErr := os.Create(filePath + "/public_key.pem")
if publicKeyFile != nil {
defer publicKeyFile.Close()
}
if createFileErr != nil {
panic(createFileErr)
}
pemEncodeErr := pem.Encode(publicKeyFile, block)
if pemEncodeErr != nil {
return
}
}
// write into buffer
bufferPublic := new(bytes.Buffer)
pubPemEncodeErr := pem.Encode(bufferPublic, block)
if pubPemEncodeErr != nil {
return
}
publicKeyStr = bufferPublic.String()
return
}
// Encrypt
//
// @param []byte plaintext
// @param []byte pubKeyPem
//
// @return []byte ciphertext
func (r *rsaHelper) Encrypt(plaintext, pubKeyPem []byte) (ciphertext []byte) {
//pem decode
block, _ := pem.Decode(pubKeyPem)
//x509 decode
publicKeyInterface, x509DecodeErr := x509.ParsePKIXPublicKey(block.Bytes)
if x509DecodeErr != nil {
panic(x509DecodeErr)
}
pubKey := publicKeyInterface.(*rsa.PublicKey)
ciphertext, encryptErr := rsa.EncryptPKCS1v15(rand.Reader, pubKey, plaintext)
if encryptErr != nil {
panic(encryptErr)
}
//避免数据过长报错,故分段加密
//partLen := pubKey.N.BitLen() / 8 - 11
//chunks := split(data, partLen)
//buffer := bytes.NewBufferString("")
//for _, chunk := range chunks {
// bytes, err := rsa.EncryptPKCS1v15(rand.Reader, pubKey, chunk)
// if err != nil {
// return nil, err
// }
// buffer.Write(bytes)
//}
//return buffer.Bytes(), nil
//keySize, srcSize := publicKey.(*rsa.PublicKey).Size(), len(src)
//pub := publicKey.(*rsa.PublicKey)
////logs.Debug("密钥长度:", keySize, "\t明文长度:\t", srcSize)
////单次加密的长度需要减掉padding的长度,PKCS1为11
//offSet, once := 0, keySize-11
//buffer := bytes.Buffer{}
//for offSet < srcSize {
// endIndex := offSet + once
// if endIndex > srcSize {
// endIndex = srcSize
// }
// // 加密一部分
// bytesOnce, err := rsa.EncryptPKCS1v15(myrand.Reader, pub, src[offSet:endIndex])
// if err != nil {
// return nil, err
// }
// buffer.Write(bytesOnce)
// offSet = endIndex
//}
//bytesEncrypt = buffer.Bytes()
return
}
// Decrypt
// @param []byte ciphertext
// @param []byte priKeyPem
//
// @return []byte plaintext
func (r *rsaHelper) Decrypt(ciphertext, priKeyPem []byte) (plaintext []byte) {
// pem decode
block, _ := pem.Decode(priKeyPem)
// x509 decode
//privateKey, x509DecodeErr := x509.ParsePKCS8PrivateKey(block.Bytes)
privateKey, x509DecodeErr := x509.ParsePKCS1PrivateKey(block.Bytes)
if x509DecodeErr != nil {
panic(x509DecodeErr)
}
plaintext, decryptErr := rsa.DecryptPKCS1v15(rand.Reader, privateKey, ciphertext)
if decryptErr != nil {
panic(decryptErr)
}
//partLen := priKey.N.BitLen() / 8
//chunks := split(data, partLen)
//buffer := bytes.NewBufferString("")
//for _, chunk := range chunks {
// decrypted, err := rsa.DecryptPKCS1v15(rand.Reader, priKey, chunk)
// if err != nil {
// return nil, err
// }
// buffer.Write(decrypted)
//}
//
//return buffer.Bytes(), nil
//keySize, srcSize := private.Size(), len(src)
////logs.Debug("密钥长度:", keySize, "\t密文长度:\t", srcSize)
//var offSet = 0
//var buffer = bytes.Buffer{}
//for offSet < srcSize {
// endIndex := offSet + keySize
// if endIndex > srcSize {
// endIndex = srcSize
// }
// bytesOnce, err := rsa.DecryptPKCS1v15(myrand.Reader, private, src[offSet:endIndex])
// if err != nil {
// return nil, err
// }
// buffer.Write(bytesOnce)
// offSet = endIndex
//}
//bytesDecrypt = buffer.Bytes()
return
}
// Signature
//
// @param []byte plaintext
// @param []byte priKeyPem
// @param crypto.Hash hashAlg
//
// @return string sign, error err
func (r *rsaHelper) Signature(plaintext, priKeyPem []byte, hashAlg crypto.Hash) (sign string, err error) {
sign = ""
err = nil
block, _ := pem.Decode(priKeyPem)
//privateKey,x509DecodeErr :=x509.ParsePKCS8PrivateKey(block.Bytes)
privateKey, x509DecodeErr := x509.ParsePKCS1PrivateKey(block.Bytes)
if x509DecodeErr != nil {
err = x509DecodeErr
return
}
hashInstance := hashAlg.New()
_, hashWriteErr := hashInstance.Write(plaintext)
if hashWriteErr != nil {
err = hashWriteErr
return
}
hashed := hashInstance.Sum(nil)
signByte, signErr := rsa.SignPKCS1v15(rand.Reader, privateKey, hashAlg, hashed)
if signErr != nil {
err = signErr
return
}
sign = base64.StdEncoding.EncodeToString(signByte)
return
}
// Verify
//
// @param []byte plaintext
// @param string sign
// @param []byte pubKeyPem
// @param crypto.Hash hashAlg
//
// @return error err
func (r *rsaHelper) Verify(plaintext []byte, sign string, pubKeyPem []byte, hashAlg crypto.Hash) (err error) {
signBytes, base64DecodedErr := base64.StdEncoding.DecodeString(sign)
if base64DecodedErr != nil {
err = base64DecodedErr
return
}
block, _ := pem.Decode(pubKeyPem)
publicKey, x509DecodeErr := x509.ParsePKIXPublicKey(block.Bytes)
if x509DecodeErr != nil {
err = x509DecodeErr
return
}
hashInstance := hashAlg.New()
_, hashWriteErr := hashInstance.Write(plaintext)
if hashWriteErr != nil {
err = hashWriteErr
return
}
hashed := hashInstance.Sum(nil)
return rsa.VerifyPKCS1v15(publicKey.(*rsa.PublicKey), hashAlg, hashed, signBytes)
}
func isExist(path string) bool {
_, err := os.Stat(path)
if err != nil {
if os.IsExist(err) {
return true
}
if os.IsNotExist(err) {
return false
}
return false
}
return true
}