-
Notifications
You must be signed in to change notification settings - Fork 25
/
from.go
414 lines (309 loc) · 9.01 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
package ecdsa
import (
"io"
"errors"
"math/big"
"crypto/rand"
"crypto/ecdsa"
"crypto/elliptic"
cryptobin_tool "github.com/deatil/go-cryptobin/tool"
)
// 生成密钥
func (this ECDSA) GenerateKeyWithSeed(reader io.Reader) ECDSA {
privateKey, err := ecdsa.GenerateKey(this.curve, reader)
if err != nil {
return this.AppendError(err)
}
this.privateKey = privateKey
this.publicKey = &privateKey.PublicKey
return this
}
// 生成密钥
// 可选 [P521 | P384 | P256 | P224]
func GenerateKeyWithSeed(reader io.Reader, curve string) ECDSA {
return defaultECDSA.SetCurve(curve).GenerateKeyWithSeed(reader)
}
// 生成密钥
func (this ECDSA) GenerateKey() ECDSA {
return this.GenerateKeyWithSeed(rand.Reader)
}
// 生成密钥
// 可选 [P521 | P384 | P256 | P224]
func GenerateKey(curve string) ECDSA {
return defaultECDSA.SetCurve(curve).GenerateKey()
}
// ==========
// 私钥
func (this ECDSA) FromPrivateKey(key []byte) ECDSA {
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) ECDSA {
return defaultECDSA.FromPrivateKey(key)
}
// 私钥带密码
func (this ECDSA) FromPrivateKeyWithPassword(key []byte, password string) ECDSA {
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) ECDSA {
return defaultECDSA.FromPrivateKeyWithPassword(key, password)
}
// ==========
// 私钥
func (this ECDSA) FromPKCS1PrivateKey(key []byte) ECDSA {
privateKey, err := this.ParsePKCS1PrivateKeyFromPEM(key)
if err != nil {
return this.AppendError(err)
}
this.privateKey = privateKey
return this
}
// 私钥
func FromPKCS1PrivateKey(key []byte) ECDSA {
return defaultECDSA.FromPKCS1PrivateKey(key)
}
// 私钥带密码
func (this ECDSA) FromPKCS1PrivateKeyWithPassword(key []byte, password string) ECDSA {
privateKey, err := this.ParsePKCS1PrivateKeyFromPEMWithPassword(key, password)
if err != nil {
return this.AppendError(err)
}
this.privateKey = privateKey
return this
}
// 私钥带密码
func FromPKCS1PrivateKeyWithPassword(key []byte, password string) ECDSA {
return defaultECDSA.FromPKCS1PrivateKeyWithPassword(key, password)
}
// ==========
// PKCS8 私钥
func (this ECDSA) FromPKCS8PrivateKey(key []byte) ECDSA {
privateKey, err := this.ParsePKCS8PrivateKeyFromPEM(key)
if err != nil {
return this.AppendError(err)
}
this.privateKey = privateKey
return this
}
// PKCS8 私钥
func FromPKCS8PrivateKey(key []byte) ECDSA {
return defaultECDSA.FromPKCS8PrivateKey(key)
}
// Pkcs8WithPassword
func (this ECDSA) FromPKCS8PrivateKeyWithPassword(key []byte, password string) ECDSA {
privateKey, err := this.ParsePKCS8PrivateKeyFromPEMWithPassword(key, password)
if err != nil {
return this.AppendError(err)
}
this.privateKey = privateKey
return this
}
// PKCS8 私钥带密码
func FromPKCS8PrivateKeyWithPassword(key []byte, password string) ECDSA {
return defaultECDSA.FromPKCS8PrivateKeyWithPassword(key, password)
}
// ==========
// 公钥
func (this ECDSA) FromPublicKey(key []byte) ECDSA {
publicKey, err := this.ParsePublicKeyFromPEM(key)
if err != nil {
return this.AppendError(err)
}
this.publicKey = publicKey
return this
}
// 公钥
func FromPublicKey(key []byte) ECDSA {
return defaultECDSA.FromPublicKey(key)
}
// ==========
// DER 私钥
func (this ECDSA) FromPKCS1PrivateKeyDer(der []byte) ECDSA {
key := cryptobin_tool.EncodeDerToPem(der, "EC PRIVATE KEY")
privateKey, err := this.ParsePKCS1PrivateKeyFromPEM(key)
if err != nil {
return this.AppendError(err)
}
this.privateKey = privateKey
return this
}
// DER 私钥
func (this ECDSA) FromPKCS8PrivateKeyDer(der []byte) ECDSA {
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 ECDSA) FromPublicKeyDer(der []byte) ECDSA {
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进制字符对
// 需要设置对应的 curve
// [xString: xHexString, yString: yHexString]
func (this ECDSA) FromPublicKeyXYString(xString string, yString string) ECDSA {
x, _ := new(big.Int).SetString(xString[:], 16)
y, _ := new(big.Int).SetString(yString[:], 16)
this.publicKey = &ecdsa.PublicKey{
Curve: this.curve,
X: x,
Y: y,
}
return this
}
// 公钥字符对,需要设置对应的 curve
func (this ECDSA) FromPublicKeyXYBytes(xBytes, yBytes []byte) ECDSA {
x := new(big.Int).SetBytes(xBytes)
y := new(big.Int).SetBytes(yBytes)
this.publicKey = &ecdsa.PublicKey{
Curve: this.curve,
X: x,
Y: y,
}
return this
}
// ==========
// 公钥明文未压缩
// 需要设置对应的 curve
// public-key hex: 047c********.
func (this ECDSA) FromPublicKeyUncompressString(key string) ECDSA {
k, _ := cryptobin_tool.HexDecode(key)
x, y := elliptic.Unmarshal(this.curve, k)
if x == nil || y == nil {
err := errors.New("ecdsa: publicKey uncompress string error")
return this.AppendError(err)
}
this.publicKey = &ecdsa.PublicKey{
Curve: this.curve,
X: x,
Y: y,
}
return this
}
// 公钥明文压缩
// 需要设置对应的 curve
// public-key hex: 027c******** || 036c********
func (this ECDSA) FromPublicKeyCompressString(key string) ECDSA {
k, _ := cryptobin_tool.HexDecode(key)
x, y := elliptic.UnmarshalCompressed(this.curve, k)
if x == nil || y == nil {
err := errors.New("ecdsa: publicKey compress string error")
return this.AppendError(err)
}
this.publicKey = &ecdsa.PublicKey{
Curve: this.curve,
X: x,
Y: y,
}
return this
}
// 公钥明文,需要设置对应的 curve
func (this ECDSA) FromPublicKeyString(key string) ECDSA {
byteLen := (this.curve.Params().BitSize + 7) / 8
k, _ := cryptobin_tool.HexDecode(key)
if len(k) == 1+byteLen {
return this.FromPublicKeyCompressString(key)
}
return this.FromPublicKeyUncompressString(key)
}
// 私钥明文,需要设置对应的 curve
// private-key: 07e4********;
func (this ECDSA) FromPrivateKeyString(keyString string) ECDSA {
c := this.curve
k, _ := new(big.Int).SetString(keyString[:], 16)
priv := new(ecdsa.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 解码后
// 需要设置对应的 curve
func (this ECDSA) FromPublicKeyBytes(pub []byte) ECDSA {
key := cryptobin_tool.HexEncode(pub)
return this.FromPublicKeyString(key)
}
// 明文私钥生成私钥结构体
// 需要设置对应的 curve
func (this ECDSA) FromPrivateKeyBytes(priByte []byte) ECDSA {
c := this.curve
k := new(big.Int).SetBytes(priByte)
priv := new(ecdsa.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 ECDSA) FromBytes(data []byte) ECDSA {
this.data = data
return this
}
// 字节
func FromBytes(data []byte) ECDSA {
return defaultECDSA.FromBytes(data)
}
// 字符
func (this ECDSA) FromString(data string) ECDSA {
this.data = []byte(data)
return this
}
// 字符
func FromString(data string) ECDSA {
return defaultECDSA.FromString(data)
}
// Base64
func (this ECDSA) FromBase64String(data string) ECDSA {
newData, err := cryptobin_tool.NewEncoding().Base64Decode(data)
this.data = newData
return this.AppendError(err)
}
// Base64
func FromBase64String(data string) ECDSA {
return defaultECDSA.FromBase64String(data)
}
// Hex
func (this ECDSA) FromHexString(data string) ECDSA {
newData, err := cryptobin_tool.NewEncoding().HexDecode(data)
this.data = newData
return this.AppendError(err)
}
// Hex
func FromHexString(data string) ECDSA {
return defaultECDSA.FromHexString(data)
}