-
Notifications
You must be signed in to change notification settings - Fork 2
/
keygen.go
129 lines (109 loc) · 2.94 KB
/
keygen.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
package go_hotstuff
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"encoding/pem"
"errors"
"io/ioutil"
"os"
)
/*
Generate private and public keys for hotstuff (ECDSA)
Deprecated, PLEASE SEE thresholdkeygen.go
*/
const (
PRIVATEKEYFILETYPE = "HOTSTUFF PRIVATE KEY"
PUBLICKEYFILETYPE = "HOTSTUFF PUBLIC KEY"
)
// GeneratePrivateKey 使用ecdsa生成私钥
func GeneratePrivateKey() (privateKey *ecdsa.PrivateKey, err error) {
curve := elliptic.P256()
privateKey, err = ecdsa.GenerateKey(curve, rand.Reader)
return
}
// WritePrivateKeyToFile 将私钥写入磁盘
func WritePrivateKeyToFile(privateKey *ecdsa.PrivateKey, filePath string) error {
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
if err != nil {
return errors.New("cannot open private key file")
}
defer file.Close()
keyBytes, err := x509.MarshalECPrivateKey(privateKey)
if err != nil {
return errors.New("cannot marshal private key")
}
b := &pem.Block{
Type: PRIVATEKEYFILETYPE,
Bytes: keyBytes,
}
err = pem.Encode(file, b)
if err != nil {
return errors.New("write private key to file failed")
}
return nil
}
// WritePublicKeyToFile 将公钥写入磁盘
func WritePublicKeyToFile(publicKey *ecdsa.PublicKey, filePath string) error {
file, err := os.OpenFile(filePath, os.O_TRUNC|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return errors.New("open public key file failed")
}
defer file.Close()
keyBytes, err := x509.MarshalPKIXPublicKey(publicKey)
if err != nil {
return errors.New("marshal public key failed")
}
b := &pem.Block{
Type: PUBLICKEYFILETYPE,
Bytes: keyBytes,
}
err = pem.Encode(file, b)
if err != nil {
return errors.New("write public key to file failed")
}
return nil
}
// ReadPrivateKeyFromFile 从文件中读取私钥
func ReadPrivateKeyFromFile(filePath string) (*ecdsa.PrivateKey, error) {
file, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, errors.New("find private key failed")
}
b, _ := pem.Decode(file)
if b == nil {
return nil, errors.New("private key did not exist")
}
if b.Type != PRIVATEKEYFILETYPE {
return nil, errors.New("file type did not match")
}
privateKey, err := x509.ParseECPrivateKey(b.Bytes)
if err != nil {
return nil, errors.New("parse private key failed")
}
return privateKey, nil
}
// ReadPublicKeyFromFile 从硬盘中读取公钥
func ReadPublicKeyFromFile(filePath string) (*ecdsa.PublicKey, error) {
file, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, errors.New("find public key failed")
}
b, _ := pem.Decode(file)
if b == nil {
return nil, errors.New("public key did not exist")
}
if b.Type != PUBLICKEYFILETYPE {
return nil, errors.New("file type did not match")
}
k, err := x509.ParsePKIXPublicKey(b.Bytes)
if err != nil {
return nil, errors.New("parse private key failed")
}
key, ok := k.(*ecdsa.PublicKey)
if !ok {
return nil, errors.New("key was of wrong type")
}
return key, nil
}