-
Notifications
You must be signed in to change notification settings - Fork 22
/
sign.go
102 lines (81 loc) · 2.12 KB
/
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
package sm2
import (
"errors"
"crypto/rand"
"github.com/deatil/go-cryptobin/gm/sm2"
)
// 私钥签名
func (this SM2) Sign() SM2 {
if this.privateKey == nil {
err := errors.New("privateKey empty.")
return this.AppendError(err)
}
parsedData, err := this.privateKey.Sign(rand.Reader, this.data, sm2.SignerOpts{
Uid: this.uid,
Hash: this.signHash,
})
if err != nil {
return this.AppendError(err)
}
this.parsedData = parsedData
return this
}
// 公钥验证
// 使用原始数据[data]对比签名后数据
func (this SM2) Verify(data []byte) SM2 {
if this.publicKey == nil {
err := errors.New("publicKey empty.")
return this.AppendError(err)
}
this.verify = this.publicKey.Verify(data, this.data, sm2.SignerOpts{
Uid: this.uid,
Hash: this.signHash,
})
return this
}
// ===============
// 私钥签名 ASN1
func (this SM2) SignASN1() SM2 {
return this.Sign()
}
// 公钥验证 ASN1
// 使用原始数据[data]对比签名后数据
func (this SM2) VerifyASN1(data []byte) SM2 {
return this.Verify(data)
}
// ===============
// 私钥签名 Bytes
// 兼容[招行]
func (this SM2) SignBytes() SM2 {
if this.privateKey == nil {
err := errors.New("privateKey empty.")
return this.AppendError(err)
}
parsedData, err := this.privateKey.SignBytes(rand.Reader, this.data, sm2.SignerOpts{
Uid: this.uid,
Hash: this.signHash,
})
if err != nil {
return this.AppendError(err)
}
this.parsedData = parsedData
return this
}
// 公钥验证 Bytes
// 兼容[招行]
// 使用原始数据[data]对比签名后数据
func (this SM2) VerifyBytes(data []byte) SM2 {
if this.publicKey == nil {
err := errors.New("publicKey empty.")
return this.AppendError(err)
}
if len(this.data) != 64 {
err := errors.New("sig data error.")
return this.AppendError(err)
}
this.verify = this.publicKey.VerifyBytes(data, this.data, sm2.SignerOpts{
Uid: this.uid,
Hash: this.signHash,
})
return this
}