-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrsa.go
217 lines (181 loc) · 5.09 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
package crypto4go
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
"strings"
)
const (
kPublicKeyPrefix = "-----BEGIN PUBLIC KEY-----"
kPublicKeySuffix = "-----END PUBLIC KEY-----"
kPKCS1Prefix = "-----BEGIN RSA PRIVATE KEY-----"
KPKCS1Suffix = "-----END RSA PRIVATE KEY-----"
kPKCS8Prefix = "-----BEGIN PRIVATE KEY-----"
KPKCS8Suffix = "-----END PRIVATE KEY-----"
)
var (
ErrPrivateKeyFailedToLoad = errors.New("crypto4go: private key failed to load")
ErrPublicKeyFailedToLoad = errors.New("crypto4go: public key failed to load")
)
func FormatPublicKey(raw string) []byte {
return formatKey(raw, kPublicKeyPrefix, kPublicKeySuffix, 64)
}
func FormatPKCS1PrivateKey(raw string) []byte {
raw = strings.Replace(raw, kPKCS8Prefix, "", 1)
raw = strings.Replace(raw, KPKCS8Suffix, "", 1)
return formatKey(raw, kPKCS1Prefix, KPKCS1Suffix, 64)
}
func FormatPKCS8PrivateKey(raw string) []byte {
raw = strings.Replace(raw, kPKCS1Prefix, "", 1)
raw = strings.Replace(raw, KPKCS1Suffix, "", 1)
return formatKey(raw, kPKCS8Prefix, KPKCS8Suffix, 64)
}
func ParsePKCS1PrivateKey(data []byte) (key *rsa.PrivateKey, err error) {
var block *pem.Block
block, _ = pem.Decode(data)
if block == nil {
return nil, ErrPrivateKeyFailedToLoad
}
key, err = x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
return key, err
}
func ParsePKCS8PrivateKey(data []byte) (key *rsa.PrivateKey, err error) {
var block *pem.Block
block, _ = pem.Decode(data)
if block == nil {
return nil, ErrPrivateKeyFailedToLoad
}
rawKey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
key, ok := rawKey.(*rsa.PrivateKey)
if ok == false {
return nil, ErrPrivateKeyFailedToLoad
}
return key, err
}
func ParsePublicKey(data []byte) (key *rsa.PublicKey, err error) {
var block *pem.Block
block, _ = pem.Decode(data)
if block == nil {
return nil, ErrPublicKeyFailedToLoad
}
var pubInterface interface{}
pubInterface, err = x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
key, ok := pubInterface.(*rsa.PublicKey)
if !ok {
return nil, ErrPublicKeyFailedToLoad
}
return key, err
}
func packageData(originalData []byte, packageSize int) (r [][]byte) {
var src = make([]byte, len(originalData))
copy(src, originalData)
r = make([][]byte, 0)
if len(src) <= packageSize {
return append(r, src)
}
for len(src) > 0 {
var p = src[:packageSize]
r = append(r, p)
src = src[packageSize:]
if len(src) <= packageSize {
r = append(r, src)
break
}
}
return r
}
// RSAEncrypt 使用公钥 key 对数据 src 进行 RSA 加密
func RSAEncrypt(src, key []byte) ([]byte, error) {
pub, err := ParsePublicKey(key)
if err != nil {
return nil, err
}
return RSAEncryptWithKey(src, pub)
}
// RSAEncryptWithKey 使用公钥 key 对数据 src 进行 RSA 加密
func RSAEncryptWithKey(src []byte, key *rsa.PublicKey) ([]byte, error) {
var data = packageData(src, key.N.BitLen()/8-11)
var cipher = make([]byte, 0, 0)
for _, d := range data {
var c, e = rsa.EncryptPKCS1v15(rand.Reader, key, d)
if e != nil {
return nil, e
}
cipher = append(cipher, c...)
}
return cipher, nil
}
// RSADecryptWithPKCS1 使用私钥 key 对数据 cipher 进行 RSA 解密,key 的格式为 pkcs1
func RSADecryptWithPKCS1(cipher, key []byte) ([]byte, error) {
pri, err := ParsePKCS1PrivateKey(key)
if err != nil {
return nil, err
}
return RSADecryptWithKey(cipher, pri)
}
// RSADecryptWithPKCS1 使用私钥 key 对数据 cipher 进行 RSA 解密,key 的格式为 pkcs8
func RSADecryptWithPKCS8(cipher, key []byte) ([]byte, error) {
pri, err := ParsePKCS8PrivateKey(key)
if err != nil {
return nil, err
}
return RSADecryptWithKey(cipher, pri)
}
// RSADecryptWithKey 使用私钥 key 对数据 cipher 进行 RSA 解密
func RSADecryptWithKey(cipher []byte, key *rsa.PrivateKey) ([]byte, error) {
var data = packageData(cipher, key.PublicKey.N.BitLen()/8)
var plainData = make([]byte, 0, 0)
for _, d := range data {
var p, e = rsa.DecryptPKCS1v15(rand.Reader, key, d)
if e != nil {
return nil, e
}
plainData = append(plainData, p...)
}
return plainData, nil
}
func RSASignWithPKCS1(src, key []byte, hash crypto.Hash) ([]byte, error) {
pri, err := ParsePKCS1PrivateKey(key)
if err != nil {
return nil, err
}
return RSASignWithKey(src, pri, hash)
}
func RSASignWithPKCS8(src, key []byte, hash crypto.Hash) ([]byte, error) {
pri, err := ParsePKCS8PrivateKey(key)
if err != nil {
return nil, err
}
return RSASignWithKey(src, pri, hash)
}
func RSASignWithKey(src []byte, key *rsa.PrivateKey, hash crypto.Hash) ([]byte, error) {
var h = hash.New()
h.Write(src)
var hashed = h.Sum(nil)
return rsa.SignPKCS1v15(rand.Reader, key, hash, hashed)
}
func RSAVerify(src, sig, key []byte, hash crypto.Hash) error {
pub, err := ParsePublicKey(key)
if err != nil {
return err
}
return RSAVerifyWithKey(src, sig, pub, hash)
}
func RSAVerifyWithKey(src, sig []byte, key *rsa.PublicKey, hash crypto.Hash) error {
var h = hash.New()
h.Write(src)
var hashed = h.Sum(nil)
return rsa.VerifyPKCS1v15(key, hash, hashed, sig)
}