-
Notifications
You must be signed in to change notification settings - Fork 22
/
ecdsa_sign.go
196 lines (144 loc) · 4.4 KB
/
ecdsa_sign.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
package ecdsa
import (
"errors"
"strings"
"math/big"
"crypto/rand"
"crypto/ecdsa"
"encoding/asn1"
cryptobin_tool "github.com/deatil/go-cryptobin/tool"
)
// 私钥签名
func (this Ecdsa) Sign(separator ...string) Ecdsa {
if this.privateKey == nil {
err := errors.New("Ecdsa: [Sign()] privateKey error.")
return this.AppendError(err)
}
hashData := this.DataHash(this.signHash, this.data)
r, s, err := ecdsa.Sign(rand.Reader, this.privateKey, hashData)
if err != nil {
return this.AppendError(err)
}
rt, err := r.MarshalText()
if err != nil {
return this.AppendError(err)
}
st, err := s.MarshalText()
if err != nil {
return this.AppendError(err)
}
sep := "+"
if len(separator) > 0 {
sep = separator[0]
}
signStr := string(rt) + sep + string(st)
this.paredData = []byte(signStr)
return this
}
// 公钥验证
func (this Ecdsa) Verify(data []byte, separator ...string) Ecdsa {
if this.publicKey == nil {
err := errors.New("Ecdsa: [Verify()] publicKey error.")
return this.AppendError(err)
}
hashData := this.DataHash(this.signHash, data)
sep := "+"
if len(separator) > 0 {
sep = separator[0]
}
split := strings.Split(string(this.data), sep)
if len(split) != 2 {
err := errors.New("Ecdsa: [Verify()] sign data is error.")
return this.AppendError(err)
}
rStr := split[0]
sStr := split[1]
rr := new(big.Int)
ss := new(big.Int)
err := rr.UnmarshalText([]byte(rStr))
if err != nil {
return this.AppendError(err)
}
err = ss.UnmarshalText([]byte(sStr))
if err != nil {
return this.AppendError(err)
}
this.verify = ecdsa.Verify(this.publicKey, hashData, rr, ss)
return this
}
// ===============
type ecdsaSignature struct {
R, S *big.Int
}
// 私钥签名
func (this Ecdsa) SignAsn1() Ecdsa {
if this.privateKey == nil {
err := errors.New("Ecdsa: [SignAsn1()] privateKey error.")
return this.AppendError(err)
}
hashData := this.DataHash(this.signHash, this.data)
r, s, err := ecdsa.Sign(rand.Reader, this.privateKey, hashData)
if err != nil {
return this.AppendError(err)
}
paredData, err := asn1.Marshal(ecdsaSignature{r, s})
this.paredData = paredData
return this.AppendError(err)
}
// 公钥验证
// 使用原始数据[data]对比签名后数据
func (this Ecdsa) VerifyAsn1(data []byte) Ecdsa {
if this.publicKey == nil {
err := errors.New("Ecdsa: [VerifyAsn1()] publicKey error.")
return this.AppendError(err)
}
var ecdsaSign ecdsaSignature
_, err := asn1.Unmarshal(this.data, &ecdsaSign)
if err != nil {
return this.AppendError(err)
}
r := ecdsaSign.R
s := ecdsaSign.S
hashData := this.DataHash(this.signHash, data)
this.verify = ecdsa.Verify(this.publicKey, hashData, r, s)
return this
}
// ===============
// 私钥签名
func (this Ecdsa) SignHex() Ecdsa {
if this.privateKey == nil {
err := errors.New("Ecdsa: [SignHex()] privateKey error.")
return this.AppendError(err)
}
hashData := this.DataHash(this.signHash, this.data)
r, s, err := ecdsa.Sign(rand.Reader, this.privateKey, hashData)
if err != nil {
return this.AppendError(err)
}
encoding := cryptobin_tool.NewEncoding()
rHex := encoding.HexEncode(r.Bytes())
sHex := encoding.HexEncode(s.Bytes())
sign := encoding.HexPadding(rHex, 64) + encoding.HexPadding(sHex, 64)
paredData, err := encoding.HexDecode(sign)
this.paredData = paredData
return this.AppendError(err)
}
// 公钥验证
// 使用原始数据[data]对比签名后数据
func (this Ecdsa) VerifyHex(data []byte) Ecdsa {
if this.publicKey == nil {
err := errors.New("Ecdsa: [VerifyHex()] publicKey error.")
return this.AppendError(err)
}
signData := cryptobin_tool.NewEncoding().HexEncode(this.data)
r, _ := new(big.Int).SetString(signData[:64], 16)
s, _ := new(big.Int).SetString(signData[64:], 16)
hashData := this.DataHash(this.signHash, data)
this.verify = ecdsa.Verify(this.publicKey, hashData, r, s)
return this
}
// ===============
// 签名后数据
func (this Ecdsa) DataHash(signHash string, data []byte) []byte {
return cryptobin_tool.NewHash().DataHash(signHash, data)
}