-
Notifications
You must be signed in to change notification settings - Fork 55
/
key.go
230 lines (188 loc) · 4.05 KB
/
key.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
/**
* @Author: fuxiao
* @Email: 576101059@qq.com
* @Date: 2022/11/1 12:50 上午
* @Desc: TODO
*/
package ecc
import (
"bytes"
"crypto/ecdsa"
"crypto/rand"
"crypto/x509"
"encoding/pem"
"github.com/dobyte/due/errors"
"github.com/dobyte/due/utils/xconv"
"github.com/dobyte/due/utils/xpath"
"github.com/ethereum/go-ethereum/crypto/ecies"
"io"
"io/ioutil"
"os"
"path"
)
type Key struct {
prv *ecdsa.PrivateKey
}
// GenerateKey 生成秘钥
func GenerateKey(curve Curve) (*Key, error) {
prv, err := ecdsa.GenerateKey(curve.New(), rand.Reader)
if err != nil {
return nil, err
}
return &Key{prv: prv}, nil
}
// PublicKey 获取公钥
func (k *Key) PublicKey() *ecdsa.PublicKey {
return &k.prv.PublicKey
}
// PrivateKey 获取私钥
func (k *Key) PrivateKey() *ecdsa.PrivateKey {
return k.prv
}
// MarshalPublicKey 编码公钥
func (k *Key) MarshalPublicKey() ([]byte, error) {
buf := bytes.NewBuffer(nil)
err := k.marshalPublicKey(buf)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// 编码私钥
func (k *Key) marshalPublicKey(out io.Writer) error {
derText, err := x509.MarshalPKIXPublicKey(k.PublicKey())
if err != nil {
return err
}
return pem.Encode(out, &pem.Block{
Type: "ECDSA PUBLIC KEY",
Bytes: derText,
})
}
// MarshalPrivateKey 编码私钥
func (k *Key) MarshalPrivateKey() ([]byte, error) {
buf := bytes.NewBuffer(nil)
err := k.marshalPrivateKey(buf)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// 编码私钥
func (k *Key) marshalPrivateKey(out io.Writer) error {
derText, err := x509.MarshalECPrivateKey(k.PrivateKey())
if err != nil {
return err
}
return pem.Encode(out, &pem.Block{
Type: "ECDSA PRIVATE KEY",
Bytes: derText,
})
}
// SaveKeyPair 保存秘钥对
func (k *Key) SaveKeyPair(dir string, file string) (err error) {
if !xpath.IsDir(dir) {
err = os.MkdirAll(dir, os.ModePerm)
if err != nil {
return
}
}
err = k.savePublicKey(dir, file)
if err != nil {
return
}
return k.savePrivateKey(dir, file)
}
// 保存公钥
func (k *Key) savePrivateKey(dir string, file string) (err error) {
filepath := path.Join(dir, file)
defer func() {
if err != nil {
_ = os.Remove(filepath)
}
}()
f, err := os.Create(filepath)
if err != nil {
return
}
return k.marshalPrivateKey(f)
}
// 保存公钥
func (k *Key) savePublicKey(dir string, file string) (err error) {
base, _, name, ext := xpath.Split(file)
if ext != "" {
file = name + ".pub." + ext
} else {
file = name + ".pub"
}
filepath := path.Join(dir, base, file)
defer func() {
if err != nil {
_ = os.Remove(filepath)
}
}()
f, err := os.Create(filepath)
if err != nil {
return
}
return k.marshalPublicKey(f)
}
func loadKey(key string) (*pem.Block, error) {
var (
err error
buffer []byte
)
if xpath.IsFile(key) {
buffer, err = ioutil.ReadFile(key)
if err != nil {
return nil, err
}
} else {
buffer = xconv.StringToBytes(key)
}
block, _ := pem.Decode(buffer)
return block, nil
}
func parseECIESPublicKey(publicKey string) (*ecies.PublicKey, error) {
pub, err := parseECDSAPublicKey(publicKey)
if err != nil {
return nil, err
}
return ecies.ImportECDSAPublic(pub), nil
}
func parseECIESPrivateKey(privateKey string) (*ecies.PrivateKey, error) {
prv, err := parseECDSAPrivateKey(privateKey)
if err != nil {
return nil, err
}
return ecies.ImportECDSA(prv), nil
}
func parseECDSAPublicKey(publicKey string) (*ecdsa.PublicKey, error) {
black, err := loadKey(publicKey)
if err != nil {
return nil, err
}
if black == nil {
return nil, errors.New("invalid public key")
}
pub, err := x509.ParsePKIXPublicKey(black.Bytes)
if err != nil {
return nil, err
}
switch key := pub.(type) {
case *ecdsa.PublicKey:
return key, nil
default:
return nil, errors.New("invalid public key")
}
}
func parseECDSAPrivateKey(privateKey string) (*ecdsa.PrivateKey, error) {
black, err := loadKey(privateKey)
if err != nil {
return nil, err
}
if black == nil {
return nil, errors.New("invalid private key")
}
return x509.ParseECPrivateKey(black.Bytes)
}