-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprivkey.go
90 lines (68 loc) · 1.62 KB
/
privkey.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
package key25519
import (
"crypto/ed25519"
"crypto/rand"
"encoding/hex"
)
const PrivateKeyLength int = ed25519.PrivateKeySize
type PrivateKey [PrivateKeyLength]byte
func (pk PrivateKey) GetPubKey() PublicKey {
edPrivKey := pk.ToEd25519PrivKey()
edPubKey := edPrivKey.Public()
var pubKey PublicKey
copy(pubKey[:], edPubKey.(ed25519.PublicKey))
return pubKey
}
func (pk PrivateKey) ToEd25519PrivKey() ed25519.PrivateKey {
var edPrivKey = make([]byte, ed25519.PrivateKeySize)
copy(edPrivKey, pk.Bytes())
return edPrivKey
}
func (pk PrivateKey) SignMsg(msg []byte) []byte {
return ed25519.Sign(pk.ToEd25519PrivKey(), msg)
}
func (pk PrivateKey) HexString() string {
return hex.EncodeToString(pk.Bytes())
}
func NewPrivateKey(seed []byte) PrivateKey {
if seed == nil {
seed = randSeed()
}
edPrivKey := ed25519.NewKeyFromSeed(seed)
privKey, _ := bytesToPrivKey(edPrivKey)
return privKey
}
func NewPrivKeyFromEd25119PrivKey(key ed25519.PrivateKey) (PrivateKey, error) {
return bytesToPrivKey(key)
}
func (pk *PrivateKey) LoadFromHex(s string) error {
d, err := hex.DecodeString(s)
if err != nil {
return err
}
return pk.LoadFromBytes(d)
}
func (pk *PrivateKey) LoadFromBytes(d []byte) error {
btp, err := bytesToPrivKey(d)
if err != nil {
return err
}
*pk = btp
return nil
}
func (pk PrivateKey) Bytes() []byte {
return pk[:]
}
func (pk PrivateKey) Seed() []byte {
return pk.ToEd25519PrivKey().Seed()
}
func bytesToPrivKey(d []byte) (PrivateKey, error) {
var privKey PrivateKey
copy(privKey[:], d)
return privKey, nil
}
func randSeed() []byte {
seed := make([]byte, 32)
rand.Read(seed)
return seed
}