-
Notifications
You must be signed in to change notification settings - Fork 0
/
pbtypes_ext.go
153 lines (127 loc) · 3.5 KB
/
pbtypes_ext.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
//go:generate protoc -I $GOPATH/src --go_out=$GOPATH/src $GOPATH/src/github.com/libreoscar/crypto/crypto.proto
package crypto
import (
"bytes"
"crypto/ecdsa"
"crypto/rand"
"encoding/hex"
"fmt"
"github.com/golang/protobuf/proto"
)
//--------------------------- PublicKey256 --------------------------------------------------------
func (pubkey *PublicKey256) AsMapKey() string {
return string(pubkey.Data)
}
func (pubkey *PublicKey256) ToText() string {
return hex.EncodeToString(pubkey.Data)
}
func TextToPublicKey256(text string) (*PublicKey256, error) {
if len(text) != 256/8*2 {
return nil, fmt.Errorf("invalid input")
} else {
bytes, err := hex.DecodeString(text)
if err != nil {
return nil, err
}
return &PublicKey256{bytes}, nil
}
}
func (pubkey *PublicKey256) DebugString() string {
return pubkey.ToText()
}
//--------------------------- PrivateKey ----------------------------------------------------------
func (key *PrivateKey) GetPublicKey() *PublicKey256 {
if key.Type == Type_P256 {
ecPriv := p256NewEcPrivKey(key.Data)
return p256ToPubKey256(ecPriv.X, ecPriv.Y)
} else {
panic(fmt.Errorf("Unsupported type: %s", key.Type))
}
}
func (key *PrivateKey) Sign(digest *Digest256) *Signature {
if key.Type == Type_P256 {
ecPriv := p256NewEcPrivKey(key.Data)
r, s, err := ecdsa.Sign(rand.Reader, ecPriv, digest.Data)
if err == nil {
return p256ToSignature(ecPriv.X, ecPriv.Y, r, s)
} else {
panic(err)
}
} else {
panic(fmt.Errorf("Unsupported type: %s", key.Type))
}
}
func (key *PrivateKey) DebugString() string {
return fmt.Sprintf("Type: %s, Data: %s", key.Type, hex.EncodeToString(key.Data[:8]))
}
func (key *PrivateKey) ToText() string {
data, err := proto.Marshal(key)
if err != nil {
panic(err)
}
return hex.EncodeToString(data)
}
func TextToPrivateKey(text string) (*PrivateKey, error) {
bytes, err := hex.DecodeString(text)
if err != nil {
return nil, err
}
privKey := &PrivateKey{}
err = proto.Unmarshal(bytes, privKey)
if err != nil {
return nil, err
} else {
return privKey, nil
}
}
//--------------------------- Signature -----------------------------------------------------------
func NewP256Signature(bytes []byte) (*Signature, error) {
sig := &Signature{Type: Type_P256, Data: bytes}
_, _, _, _, err := p256ParseSignature(sig)
if err != nil {
return nil, err
} else {
return sig, nil
}
}
func (s *Signature) DebugString() string {
return fmt.Sprintf("Type: %s, Data: %s", s.Type, hex.EncodeToString(s.Data[:8]))
}
//--------------------------- Digest256 -----------------------------------------------------------
func (digest *Digest256) EqualTo(d *Digest256) bool {
return bytes.Equal(digest.Data, d.Data)
}
func (digest *Digest256) AsMapKey() string {
return string(digest.Data)
}
func (digest *Digest256) ToText() string {
return hex.EncodeToString(digest.Data)
}
func (digest *Digest256) IsValid() bool {
return len(digest.Data) == 256/8
}
func TextToDigest256(text string) (digest *Digest256, err error) {
bytes, err := hex.DecodeString(text)
if err != nil {
return
}
digest = &Digest256{bytes}
if !digest.IsValid() {
err = fmt.Errorf("Invalid digest: %v", text)
digest = nil
}
return
}
func NewDigest256(bytes []byte) (digest *Digest256, err error) {
cp := make([]byte, len(bytes))
copy(cp, bytes)
digest = &Digest256{cp}
if !digest.IsValid() {
err = fmt.Errorf("Invalid digest: %v", bytes)
digest = nil
}
return
}
func (digest *Digest256) DebugString() string {
return hex.EncodeToString(digest.Data[:4]) + "..."
}