-
Notifications
You must be signed in to change notification settings - Fork 26
/
from.go
419 lines (314 loc) · 9.2 KB
/
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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package sm2
import (
"io"
"errors"
"strings"
"math/big"
"crypto/rand"
"github.com/deatil/go-cryptobin/gm/sm2"
cryptobin_tool "github.com/deatil/go-cryptobin/tool"
)
// 使用自定义数据生成密钥对
func (this SM2) GenerateKeyWithSeed(reader io.Reader) SM2 {
privateKey, err := sm2.GenerateKey(reader)
if err != nil {
return this.AppendError(err)
}
this.privateKey = privateKey
this.publicKey = &privateKey.PublicKey
return this
}
// 使用自定义数据生成密钥对
func GenerateKeyWithSeed(reader io.Reader) SM2 {
return defaultSM2.GenerateKeyWithSeed(reader)
}
// 生成密钥对
func (this SM2) GenerateKey() SM2 {
return this.GenerateKeyWithSeed(rand.Reader)
}
// 生成密钥对
func GenerateKey() SM2 {
return defaultSM2.GenerateKey()
}
// ==========
// 私钥
func (this SM2) FromPrivateKey(key []byte) SM2 {
privateKey, err := this.ParsePKCS8PrivateKeyFromPEM(key)
if err == nil {
this.privateKey = privateKey
return this
}
privateKey, err = this.ParsePKCS1PrivateKeyFromPEM(key)
if err != nil {
return this.AppendError(err)
}
this.privateKey = privateKey
return this
}
// 私钥
func FromPrivateKey(key []byte) SM2 {
return defaultSM2.FromPrivateKey(key)
}
// 私钥带密码
func (this SM2) FromPrivateKeyWithPassword(key []byte, password string) SM2 {
privateKey, err := this.ParsePrivateKeyFromPEMWithPassword(key, password)
if err == nil {
this.privateKey = privateKey
return this
}
privateKey, err = this.ParsePKCS8PrivateKeyFromPEMWithPassword(key, password)
if err == nil {
this.privateKey = privateKey
return this
}
privateKey, err = this.ParsePKCS1PrivateKeyFromPEMWithPassword(key, password)
if err != nil {
return this.AppendError(err)
}
this.privateKey = privateKey
return this
}
// 私钥带密码
func FromPrivateKeyWithPassword(key []byte, password string) SM2 {
return defaultSM2.FromPrivateKeyWithPassword(key, password)
}
// ==========
// PKCS1 私钥
func (this SM2) FromPKCS1PrivateKey(key []byte) SM2 {
privateKey, err := this.ParsePKCS1PrivateKeyFromPEM(key)
if err != nil {
return this.AppendError(err)
}
this.privateKey = privateKey
return this
}
// PKCS1 私钥
func FromPKCS1PrivateKey(key []byte) SM2 {
return defaultSM2.FromPKCS1PrivateKey(key)
}
// PKCS1 私钥带密码
func (this SM2) FromPKCS1PrivateKeyWithPassword(key []byte, password string) SM2 {
privateKey, err := this.ParsePKCS1PrivateKeyFromPEMWithPassword(key, password)
if err != nil {
return this.AppendError(err)
}
this.privateKey = privateKey
return this
}
// PKCS1 私钥带密码
func FromPKCS1PrivateKeyWithPassword(key []byte, password string) SM2 {
return defaultSM2.FromPKCS1PrivateKeyWithPassword(key, password)
}
// ==========
// PKCS8 私钥
func (this SM2) FromPKCS8PrivateKey(key []byte) SM2 {
privateKey, err := this.ParsePKCS8PrivateKeyFromPEM(key)
if err != nil {
return this.AppendError(err)
}
this.privateKey = privateKey
return this
}
// PKCS8 私钥
func FromPKCS8PrivateKey(key []byte) SM2 {
return defaultSM2.FromPKCS8PrivateKey(key)
}
// PKCS8 私钥带密码
func (this SM2) FromPKCS8PrivateKeyWithPassword(key []byte, password string) SM2 {
var err error
var privateKey *sm2.PrivateKey
if privateKey, err = this.ParsePrivateKeyFromPEMWithPassword(key, password); err != nil {
if privateKey, err = this.ParsePKCS8PrivateKeyFromPEMWithPassword(key, password); err != nil {
return this.AppendError(err)
}
}
this.privateKey = privateKey
return this
}
// PKCS8 私钥带密码
func FromPKCS8PrivateKeyWithPassword(key []byte, password string) SM2 {
return defaultSM2.FromPKCS8PrivateKeyWithPassword(key, password)
}
// ==========
// 公钥,默认只有一种类型
func (this SM2) FromPublicKey(key []byte) SM2 {
publicKey, err := this.ParsePublicKeyFromPEM(key)
if err != nil {
return this.AppendError(err)
}
this.publicKey = publicKey
return this
}
// 公钥,默认只有一种类型
func FromPublicKey(key []byte) SM2 {
return defaultSM2.FromPublicKey(key)
}
// ==========
// PKCS1 编码 DER 私钥
func (this SM2) FromPKCS1PrivateKeyDer(der []byte) SM2 {
key := cryptobin_tool.EncodeDerToPem(der, "SM2 PRIVATE KEY")
privateKey, err := this.ParsePKCS1PrivateKeyFromPEM(key)
if err != nil {
return this.AppendError(err)
}
this.privateKey = privateKey
return this
}
// PKCS8 编码 DER 私钥
func (this SM2) FromPKCS8PrivateKeyDer(der []byte) SM2 {
key := cryptobin_tool.EncodeDerToPem(der, "PRIVATE KEY")
privateKey, err := this.ParsePKCS8PrivateKeyFromPEM(key)
if err != nil {
return this.AppendError(err)
}
this.privateKey = privateKey
return this
}
// DER 公钥
func (this SM2) FromPublicKeyDer(der []byte) SM2 {
key := cryptobin_tool.EncodeDerToPem(der, "PUBLIC KEY")
publicKey, err := this.ParsePublicKeyFromPEM(key)
if err != nil {
return this.AppendError(err)
}
this.publicKey = publicKey
return this
}
// ==========
// 公钥 x,y 16进制字符对
// [xString: xHexString, yString: yHexString]
func (this SM2) FromPublicKeyXYString(xString, yString string) SM2 {
x, _ := new(big.Int).SetString(xString[:], 16)
y, _ := new(big.Int).SetString(yString[:], 16)
this.publicKey = &sm2.PublicKey{
Curve: sm2.P256(),
X: x,
Y: y,
}
return this
}
// 公钥 x,y 字节对
func (this SM2) FromPublicKeyXYBytes(XBytes, YBytes []byte) SM2 {
x := new(big.Int).SetBytes(XBytes)
y := new(big.Int).SetBytes(YBytes)
this.publicKey = &sm2.PublicKey{
Curve: sm2.P256(),
X: x,
Y: y,
}
return this
}
// ==========
// 公钥明文未压缩 (hexStringX + hexStringY)
// public-key: 047c********.
func (this SM2) FromPublicKeyUncompressString(keyString string) SM2 {
if len(keyString) == 130 && strings.HasPrefix(keyString, "04") {
keyString = strings.TrimPrefix(keyString, "04")
}
x, _ := new(big.Int).SetString(keyString[:64], 16)
y, _ := new(big.Int).SetString(keyString[64:], 16)
this.publicKey = &sm2.PublicKey{
Curve: sm2.P256(),
X: x,
Y: y,
}
return this
}
// 公钥明文压缩
// public-key: 027c******** || 036c********
// 0333B01B61D94A775DA72A0BFF9AB324DE672EA0977584D23AF34F8150223305B0
// 023613B13F252F6FB2374A85D93C7FFE9CCAD1231BE866F5FE69255312CE85B9FF
func (this SM2) FromPublicKeyCompressString(key string) SM2 {
if len(key) != 66 || (!strings.HasPrefix(key, "02") && !strings.HasPrefix(key, "03")) {
err := errors.New("Compress PublicKey prefix is 02 or 03.")
return this.AppendError(err)
}
d, _ := new(big.Int).SetString(key[:], 16)
publicKey, err := sm2.Decompress(d.Bytes())
if err != nil {
return this.AppendError(err)
}
this.publicKey = publicKey
return this
}
// 公钥明文
func (this SM2) FromPublicKeyString(key string) SM2 {
if len(key) == 66 {
return this.FromPublicKeyCompressString(key)
}
return this.FromPublicKeyUncompressString(key)
}
// 私钥明文
// private-key: 07e4********;
func (this SM2) FromPrivateKeyString(keyString string) SM2 {
c := sm2.P256()
k, _ := new(big.Int).SetString(keyString[:], 16)
priv := new(sm2.PrivateKey)
priv.PublicKey.Curve = c
priv.D = k
priv.PublicKey.X, priv.PublicKey.Y = c.ScalarBaseMult(k.Bytes())
this.privateKey = priv
return this
}
// ==========
// 公钥明文, hex 或者 base64 解码后
func (this SM2) FromPublicKeyBytes(pubBytes []byte) SM2 {
key := cryptobin_tool.HexEncode(pubBytes)
return this.FromPublicKeyString(key)
}
// 私钥明文, hex 或者 base64 解码后
func (this SM2) FromPrivateKeyBytes(priBytes []byte) SM2 {
c := sm2.P256()
k := new(big.Int).SetBytes(priBytes)
priv := new(sm2.PrivateKey)
priv.PublicKey.Curve = c
priv.D = k
priv.PublicKey.X, priv.PublicKey.Y = c.ScalarBaseMult(k.Bytes())
this.privateKey = priv
return this
}
// ==========
// 字节
func (this SM2) FromBytes(data []byte) SM2 {
this.data = data
return this
}
// 字节
func FromBytes(data []byte) SM2 {
return defaultSM2.FromBytes(data)
}
// 字符
func (this SM2) FromString(data string) SM2 {
this.data = []byte(data)
return this
}
// 字符
func FromString(data string) SM2 {
return defaultSM2.FromString(data)
}
// Base64数据
func (this SM2) FromBase64String(data string) SM2 {
newData, err := cryptobin_tool.Base64Decode(data)
if err != nil {
return this.AppendError(err)
}
this.data = newData
return this
}
// Base64数据
func FromBase64String(data string) SM2 {
return defaultSM2.FromBase64String(data)
}
// 16进制数据
func (this SM2) FromHexString(data string) SM2 {
newData, err := cryptobin_tool.HexDecode(data)
if err != nil {
return this.AppendError(err)
}
this.data = newData
return this
}
// 16进制数据
func FromHexString(data string) SM2 {
return defaultSM2.FromHexString(data)
}