-
Notifications
You must be signed in to change notification settings - Fork 22
/
ecdsa_create.go
245 lines (201 loc) · 5.42 KB
/
ecdsa_create.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
package ecdsa
import (
"errors"
"crypto/rand"
"crypto/ecdsa"
"crypto/x509"
"encoding/pem"
"github.com/deatil/go-cryptobin/pkcs8"
cryptobin_tool "github.com/deatil/go-cryptobin/tool"
)
type (
// 配置
Opts = pkcs8.Opts
// PBKDF2 配置
PBKDF2Opts = pkcs8.PBKDF2Opts
// Scrypt 配置
ScryptOpts = pkcs8.ScryptOpts
)
// Cipher 列表
var CipherMap = map[string]pkcs8.CipherBlock{
"DESCBC": pkcs8.DESCBC,
"DESEDE3CBC": pkcs8.DESEDE3CBC,
"AES128CBC": pkcs8.AES128CBC,
"AES192CBC": pkcs8.AES192CBC,
"AES256CBC": pkcs8.AES256CBC,
}
// 私钥
func (this Ecdsa) CreatePrivateKey() Ecdsa {
if this.privateKey == nil {
this.Error = errors.New("privateKey error.")
return this
}
x509PrivateKey, err := x509.MarshalECPrivateKey(this.privateKey)
if err != nil {
this.Error = err
return this
}
privateBlock := &pem.Block{
Type: "EC PRIVATE KEY",
Bytes: x509PrivateKey,
}
this.keyData = pem.EncodeToMemory(privateBlock)
return this
}
// 私钥带密码
// CreatePrivateKeyWithPassword("123", "AES256CBC")
func (this Ecdsa) CreatePrivateKeyWithPassword(password string, opts ...string) Ecdsa {
if this.privateKey == nil {
this.Error = errors.New("privateKey error.")
return this
}
// DESCBC | DESEDE3CBC | AES128CBC
// AES192CBC | AES256CBC
opt := "AES256CBC"
if len(opts) > 0 {
opt = opts[0]
}
// 具体方式
cipher, ok := PEMCiphers[opt]
if !ok {
this.Error = errors.New("PEMCipher not exists.")
return this
}
// 生成私钥
x509PrivateKey, err := x509.MarshalECPrivateKey(this.privateKey)
if err != nil {
this.Error = err
return this
}
// 生成加密数据
privateBlock, err := x509.EncryptPEMBlock(
rand.Reader,
"EC PRIVATE KEY",
x509PrivateKey,
[]byte(password),
cipher,
)
if err != nil {
this.Error = err
return this
}
this.keyData = pem.EncodeToMemory(privateBlock)
return this
}
// PKCS8 私钥
func (this Ecdsa) CreatePKCS8PrivateKey() Ecdsa {
if this.privateKey == nil {
this.Error = errors.New("privateKey error.")
return this
}
x509PrivateKey, err := x509.MarshalPKCS8PrivateKey(this.privateKey)
if err != nil {
this.Error = err
return this
}
privateBlock := &pem.Block{
Type: "PRIVATE KEY",
Bytes: x509PrivateKey,
}
this.keyData = pem.EncodeToMemory(privateBlock)
return this
}
// PKCS8 私钥带密码
// CreatePKCS8PrivateKeyWithPassword("123", "AES256CBC", "SHA256")
func (this Ecdsa) CreatePKCS8PrivateKeyWithPassword(password string, opts ...any) Ecdsa {
if this.privateKey == nil {
this.Error = errors.New("privateKey error.")
return this
}
opt, err := parseOpt(opts...)
if err != nil {
this.Error = err
return this
}
// 生成私钥
x509PrivateKey, err := x509.MarshalPKCS8PrivateKey(this.privateKey)
if err != nil {
this.Error = err
return this
}
// 生成加密数据
privateBlock, err := pkcs8.EncryptPKCS8PrivateKey(
rand.Reader,
"ENCRYPTED PRIVATE KEY",
x509PrivateKey,
[]byte(password),
opt,
)
if err != nil {
this.Error = err
return this
}
this.keyData = pem.EncodeToMemory(privateBlock)
return this
}
// 公钥
func (this Ecdsa) CreatePublicKey() Ecdsa {
var publicKey *ecdsa.PublicKey
if this.publicKey == nil {
if this.privateKey == nil {
this.Error = errors.New("privateKey error.")
return this
}
publicKey = &this.privateKey.PublicKey
} else {
publicKey = this.publicKey
}
x509PublicKey, err := x509.MarshalPKIXPublicKey(publicKey)
if err != nil {
this.Error = err
return this
}
publicBlock := &pem.Block{
Type: "PUBLIC KEY",
Bytes: x509PublicKey,
}
this.keyData = pem.EncodeToMemory(publicBlock)
return this
}
// 解析配置
func parseOpt(opts ...any) (pkcs8.Opts, error) {
if len(opts) == 0 {
return pkcs8.DefaultOpts, nil
}
switch newOpt := opts[0].(type) {
case pkcs8.Opts:
return newOpt, nil
case string:
// DESCBC | DESEDE3CBC | AES128CBC
// AES192CBC | AES256CBC
opt := "AES256CBC"
if len(opts) > 0 {
opt = opts[0].(string)
}
// MD5 | SHA1 | SHA224 | SHA256 | SHA384
// SHA512 | SHA512_224 | SHA512_256
encryptHash := "SHA1"
if len(opts) > 1 {
encryptHash = opts[1].(string)
}
// 具体方式
cipher, ok := CipherMap[opt]
if !ok {
return pkcs8.Opts{}, errors.New("PEMCipher not exists.")
}
hmacHash := cryptobin_tool.NewHash().
GetCryptoHash(encryptHash)
// 设置
enOpt := pkcs8.Opts{
Cipher: cipher,
KDFOpts: pkcs8.PBKDF2Opts{
SaltSize: 16,
IterationCount: 10000,
HMACHash: hmacHash,
},
}
return enOpt, nil
default:
return pkcs8.DefaultOpts, nil
}
}