-
Notifications
You must be signed in to change notification settings - Fork 22
/
rsa_from.go
197 lines (146 loc) · 3.92 KB
/
rsa_from.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
package rsa
import (
"math/big"
"crypto/rsa"
"crypto/rand"
cryptobin_tool "github.com/deatil/go-cryptobin/tool"
)
// 私钥
func (this Rsa) FromPrivateKey(key []byte) Rsa {
privateKey, err := this.ParseRSAPrivateKeyFromPEM(key)
if err != nil {
return this.AppendError(err)
}
this.privateKey = privateKey
return this
}
// 私钥带密码
func (this Rsa) FromPrivateKeyWithPassword(key []byte, password string) Rsa {
privateKey, err := this.ParseRSAPKCS8PrivateKeyFromPEMWithPassword(key, password)
if err == nil {
this.privateKey = privateKey
return this
}
privateKey, err = this.ParseRSAPrivateKeyFromPEMWithPassword(key, password)
if err != nil {
return this.AppendError(err)
}
this.privateKey = privateKey
return this
}
// 公钥
func (this Rsa) FromPublicKey(key []byte) Rsa {
publicKey, err := this.ParseRSAPublicKeyFromPEM(key)
if err != nil {
return this.AppendError(err)
}
this.publicKey = publicKey
return this
}
// 生成密钥
// bits = 512 | 1024 | 2048 | 4096
func (this Rsa) GenerateKey(bits int) Rsa {
privateKey, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return this.AppendError(err)
}
this.privateKey = privateKey
// 生成公钥
this.publicKey = &this.privateKey.PublicKey
return this
}
// ==========
// 模数、指数生成公钥
// 指数默认为 10001
func (this Rsa) FromPublicKeyNE(nString string, e int) Rsa {
n, _ := new(big.Int).SetString(nString[:], 16)
this.publicKey = &rsa.PublicKey{
N: n,
E: e,
}
return this
}
// ==========
// Pkcs1
func (this Rsa) FromPKCS1PrivateKey(key []byte) Rsa {
privateKey, err := this.ParseRSAPrivateKeyFromPEM(key)
if err != nil {
return this.AppendError(err)
}
this.privateKey = privateKey
return this
}
// Pkcs1WithPassword
func (this Rsa) FromPKCS1PrivateKeyWithPassword(key []byte, password string) Rsa {
privateKey, err := this.ParseRSAPrivateKeyFromPEMWithPassword(key, password)
if err != nil {
return this.AppendError(err)
}
this.privateKey = privateKey
return this
}
// Pkcs8
func (this Rsa) FromPKCS8PrivateKey(key []byte) Rsa {
privateKey, err := this.ParseRSAPrivateKeyFromPEM(key)
if err != nil {
return this.AppendError(err)
}
this.privateKey = privateKey
return this
}
// Pkcs8WithPassword
func (this Rsa) FromPKCS8PrivateKeyWithPassword(key []byte, password string) Rsa {
privateKey, err := this.ParseRSAPKCS8PrivateKeyFromPEMWithPassword(key, password)
if err != nil {
return this.AppendError(err)
}
this.privateKey = privateKey
return this
}
// Pkcs12 Cert
func (this Rsa) FromPKCS12Cert(key []byte) Rsa {
privateKey, err := this.ParseRSAPKCS12CertFromPEMWithPassword(key, "")
if err != nil {
return this.AppendError(err)
}
this.privateKey = privateKey
return this
}
// Pkcs12CertWithPassword
func (this Rsa) FromPKCS12CertWithPassword(key []byte, password string) Rsa {
privateKey, err := this.ParseRSAPKCS12CertFromPEMWithPassword(key, password)
if err != nil {
return this.AppendError(err)
}
this.privateKey = privateKey
return this
}
// ==========
// 字节
func (this Rsa) FromBytes(data []byte) Rsa {
this.data = data
return this
}
// 字符
func (this Rsa) FromString(data string) Rsa {
this.data = []byte(data)
return this
}
// Base64
func (this Rsa) FromBase64String(data string) Rsa {
newData, err := cryptobin_tool.NewEncoding().Base64Decode(data)
if err != nil {
return this.AppendError(err)
}
this.data = newData
return this
}
// Hex
func (this Rsa) FromHexString(data string) Rsa {
newData, err := cryptobin_tool.NewEncoding().HexDecode(data)
if err != nil {
return this.AppendError(err)
}
this.data = newData
return this
}