-
Notifications
You must be signed in to change notification settings - Fork 25
/
from.go
203 lines (151 loc) · 4.59 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
package ecdh
import (
"io"
"crypto/rand"
"crypto/ecdh"
cryptobin_tool "github.com/deatil/go-cryptobin/tool"
)
// 生成密钥
func (this ECDH) GenerateKeyWithSeed(reader io.Reader) ECDH {
privateKey, err := this.curve.GenerateKey(reader)
if err != nil {
return this.AppendError(err)
}
this.privateKey = privateKey
this.publicKey = privateKey.PublicKey()
return this
}
// 生成密钥
func GenerateKeyWithSeed(reader io.Reader, curve string) ECDH {
return defaultECDH.SetCurve(curve).GenerateKeyWithSeed(reader)
}
// 生成密钥
func (this ECDH) GenerateKey() ECDH {
return this.GenerateKeyWithSeed(rand.Reader)
}
// 生成密钥
func GenerateKey(curve string) ECDH {
return defaultECDH.SetCurve(curve).GenerateKey()
}
// ==========
// 私钥
func (this ECDH) FromPrivateKey(key []byte) ECDH {
parsedKey, err := this.ParsePrivateKeyFromPEM(key)
if err != nil {
return this.AppendError(err)
}
this.privateKey = parsedKey.(*ecdh.PrivateKey)
return this
}
// 私钥
func FromPrivateKey(key []byte) ECDH {
return defaultECDH.FromPrivateKey(key)
}
// 私钥带密码
func (this ECDH) FromPrivateKeyWithPassword(key []byte, password string) ECDH {
parsedKey, err := this.ParsePrivateKeyFromPEMWithPassword(key, password)
if err != nil {
return this.AppendError(err)
}
this.privateKey = parsedKey.(*ecdh.PrivateKey)
return this
}
// 私钥
func FromPrivateKeyWithPassword(key []byte, password string) ECDH {
return defaultECDH.FromPrivateKeyWithPassword(key, password)
}
// 公钥
func (this ECDH) FromPublicKey(key []byte) ECDH {
parsedKey, err := this.ParsePublicKeyFromPEM(key)
if err != nil {
return this.AppendError(err)
}
this.publicKey = parsedKey.(*ecdh.PublicKey)
return this
}
// 公钥
func FromPublicKey(key []byte) ECDH {
return defaultECDH.FromPublicKey(key)
}
// ==========
// DER 私钥
func (this ECDH) FromPrivateKeyDer(der []byte) ECDH {
key := cryptobin_tool.EncodeDerToPem(der, "PRIVATE KEY")
parsedKey, err := this.ParsePrivateKeyFromPEM(key)
if err != nil {
return this.AppendError(err)
}
this.privateKey = parsedKey.(*ecdh.PrivateKey)
return this
}
// DER 公钥
func (this ECDH) FromPublicKeyDer(der []byte) ECDH {
key := cryptobin_tool.EncodeDerToPem(der, "PUBLIC KEY")
parsedKey, err := this.ParsePublicKeyFromPEM(key)
if err != nil {
return this.AppendError(err)
}
this.publicKey = parsedKey.(*ecdh.PublicKey)
return this
}
// ==========
// 私钥, 库自使用的 asn1 格式
func (this ECDH) FromECDHPrivateKey(key []byte) ECDH {
parsedKey, err := this.ParseECDHPrivateKeyFromPEM(key)
if err != nil {
return this.AppendError(err)
}
this.privateKey = parsedKey.(*ecdh.PrivateKey)
return this
}
// 私钥, 库自使用的 asn1 格式
func FromECDHPrivateKey(key []byte) ECDH {
return defaultECDH.FromECDHPrivateKey(key)
}
// 私钥带密码, 库自使用的 asn1 格式
func (this ECDH) FromECDHPrivateKeyWithPassword(key []byte, password string) ECDH {
parsedKey, err := this.ParseECDHPrivateKeyFromPEMWithPassword(key, password)
if err != nil {
return this.AppendError(err)
}
this.privateKey = parsedKey.(*ecdh.PrivateKey)
return this
}
// 私钥, 库自使用的 asn1 格式
func FromECDHPrivateKeyWithPassword(key []byte, password string) ECDH {
return defaultECDH.FromECDHPrivateKeyWithPassword(key, password)
}
// 公钥, 库自使用的 asn1 格式
func (this ECDH) FromECDHPublicKey(key []byte) ECDH {
parsedKey, err := this.ParseECDHPublicKeyFromPEM(key)
if err != nil {
return this.AppendError(err)
}
this.publicKey = parsedKey.(*ecdh.PublicKey)
return this
}
// 公钥, 库自使用的 asn1 格式
func FromECDHPublicKey(key []byte) ECDH {
return defaultECDH.FromECDHPublicKey(key)
}
// ==========
// DER 私钥, 库自使用的 asn1 格式
func (this ECDH) FromECDHPrivateKeyDer(der []byte) ECDH {
key := cryptobin_tool.EncodeDerToPem(der, "PRIVATE KEY")
parsedKey, err := this.ParseECDHPrivateKeyFromPEM(key)
if err != nil {
return this.AppendError(err)
}
this.privateKey = parsedKey.(*ecdh.PrivateKey)
return this
}
// DER 公钥, 库自使用的 asn1 格式
func (this ECDH) FromECDHPublicKeyDer(der []byte) ECDH {
key := cryptobin_tool.EncodeDerToPem(der, "PUBLIC KEY")
parsedKey, err := this.ParseECDHPublicKeyFromPEM(key)
if err != nil {
return this.AppendError(err)
}
this.publicKey = parsedKey.(*ecdh.PublicKey)
return this
}