-
Notifications
You must be signed in to change notification settings - Fork 25
/
create.go
231 lines (185 loc) · 5.45 KB
/
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
package ecdh
import (
"errors"
"crypto/rand"
"crypto/x509"
"encoding/pem"
cryptobin_ecdh "github.com/deatil/go-cryptobin/ecdh"
cryptobin_ecdh_key "github.com/deatil/go-cryptobin/ecdh/key"
cryptobin_pkcs8 "github.com/deatil/go-cryptobin/pkcs8"
)
type (
// 配置
Opts = cryptobin_pkcs8.Opts
// PBKDF2 配置
PBKDF2Opts = cryptobin_pkcs8.PBKDF2Opts
// Scrypt 配置
ScryptOpts = cryptobin_pkcs8.ScryptOpts
)
var (
// 获取 Cipher 类型
GetCipherFromName = cryptobin_pkcs8.GetCipherFromName
// 获取 hash 类型
GetHashFromName = cryptobin_pkcs8.GetHashFromName
)
// 生成私钥 pem 数据
// 使用:
// obj := New().SetCurve("P256").GenerateKey()
// priKey := obj.CreatePrivateKey().ToKeyString()
func (this ECDH) CreatePrivateKey() ECDH {
if this.privateKey == nil {
err := errors.New("ecdh: privateKey error.")
return this.AppendError(err)
}
privateKey, err := x509.MarshalPKCS8PrivateKey(this.privateKey)
if err != nil {
return this.AppendError(err)
}
privateBlock := &pem.Block{
Type: "PRIVATE KEY",
Bytes: privateKey,
}
this.keyData = pem.EncodeToMemory(privateBlock)
return this
}
// 生成 PKCS8 私钥带密码 pem 数据
// CreatePrivateKeyWithPassword("123", "AES256CBC", "SHA256")
func (this ECDH) CreatePrivateKeyWithPassword(password string, opts ...any) ECDH {
if this.privateKey == nil {
err := errors.New("ecdh: privateKey error.")
return this.AppendError(err)
}
opt, err := cryptobin_pkcs8.ParseOpts(opts...)
if err != nil {
return this.AppendError(err)
}
// 生成私钥
privateKey, err := x509.MarshalPKCS8PrivateKey(this.privateKey)
if err != nil {
return this.AppendError(err)
}
// 生成加密数据
privateBlock, err := cryptobin_pkcs8.EncryptPEMBlock(
rand.Reader,
"ENCRYPTED PRIVATE KEY",
privateKey,
[]byte(password),
opt,
)
if err != nil {
return this.AppendError(err)
}
this.keyData = pem.EncodeToMemory(privateBlock)
return this
}
// 生成公钥 pem 数据
func (this ECDH) CreatePublicKey() ECDH {
if this.publicKey == nil {
err := errors.New("ecdh: publicKey error.")
return this.AppendError(err)
}
publicKeyBytes, err := x509.MarshalPKIXPublicKey(this.publicKey)
if err != nil {
return this.AppendError(err)
}
publicBlock := &pem.Block{
Type: "PUBLIC KEY",
Bytes: publicKeyBytes,
}
this.keyData = pem.EncodeToMemory(publicBlock)
return this
}
// =======================
// 生成私钥 pem 数据, 库自使用的 asn1 格式
func (this ECDH) CreateECDHPrivateKey() ECDH {
if this.privateKey == nil {
err := errors.New("ecdh: privateKey error.")
return this.AppendError(err)
}
priv, err := cryptobin_ecdh.FromPrivateKey(this.privateKey)
if err != nil {
return this.AppendError(err)
}
privateKey, err := cryptobin_ecdh_key.MarshalPrivateKey(priv)
if err != nil {
return this.AppendError(err)
}
privateBlock := &pem.Block{
Type: "PRIVATE KEY",
Bytes: privateKey,
}
this.keyData = pem.EncodeToMemory(privateBlock)
return this
}
// 生成 PKCS8 私钥带密码 pem 数据, 库自使用的 asn1 格式
func (this ECDH) CreateECDHPrivateKeyWithPassword(password string, opts ...any) ECDH {
if this.privateKey == nil {
err := errors.New("ecdh: privateKey error.")
return this.AppendError(err)
}
opt, err := cryptobin_pkcs8.ParseOpts(opts...)
if err != nil {
return this.AppendError(err)
}
priv, err := cryptobin_ecdh.FromPrivateKey(this.privateKey)
if err != nil {
return this.AppendError(err)
}
// 生成私钥
privateKey, err := cryptobin_ecdh_key.MarshalPrivateKey(priv)
if err != nil {
return this.AppendError(err)
}
// 生成加密数据
privateBlock, err := cryptobin_pkcs8.EncryptPEMBlock(
rand.Reader,
"ENCRYPTED PRIVATE KEY",
privateKey,
[]byte(password),
opt,
)
if err != nil {
return this.AppendError(err)
}
this.keyData = pem.EncodeToMemory(privateBlock)
return this
}
// 生成公钥 pem 数据, 库自使用的 asn1 格式
func (this ECDH) CreateECDHPublicKey() ECDH {
if this.publicKey == nil {
err := errors.New("ecdh: publicKey error.")
return this.AppendError(err)
}
pub, err := cryptobin_ecdh.FromPublicKey(this.publicKey)
if err != nil {
return this.AppendError(err)
}
publicKeyBytes, err := cryptobin_ecdh_key.MarshalPublicKey(pub)
if err != nil {
return this.AppendError(err)
}
publicBlock := &pem.Block{
Type: "PUBLIC KEY",
Bytes: publicKeyBytes,
}
this.keyData = pem.EncodeToMemory(publicBlock)
return this
}
// =======================
// 根据公钥和私钥生成对称密钥
func (this ECDH) CreateSecretKey() ECDH {
if this.privateKey == nil {
err := errors.New("ecdh: privateKey error.")
return this.AppendError(err)
}
if this.publicKey == nil {
err := errors.New("ecdh: publicKey error.")
return this.AppendError(err)
}
secretKey, err := this.privateKey.ECDH(this.publicKey)
if err != nil {
return this.AppendError(err)
}
this.secretData = secretKey
return this
}