-
Notifications
You must be signed in to change notification settings - Fork 25
/
sign.go
59 lines (46 loc) · 1.27 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
package elgamal
import (
"errors"
"crypto/rand"
"github.com/deatil/go-cryptobin/elgamal"
)
// 私钥签名
func (this EIGamal) Sign() EIGamal {
if this.privateKey == nil {
err := errors.New("elgamal: privateKey error.")
return this.AppendError(err)
}
hashed, err := this.DataHash(this.signHash, this.data)
if err != nil {
return this.AppendError(err)
}
parsedData, err := elgamal.SignASN1(rand.Reader, this.privateKey, hashed)
if err != nil {
return this.AppendError(err)
}
this.parsedData = parsedData
return this.AppendError(err)
}
// 公钥验证
// 使用原始数据[data]对比签名后数据
func (this EIGamal) Verify(data []byte) EIGamal {
if this.publicKey == nil {
err := errors.New("elgamal: publicKey error.")
return this.AppendError(err)
}
hashed, err := this.DataHash(this.signHash, data)
if err != nil {
return this.AppendError(err)
}
this.verify, err = elgamal.VerifyASN1(this.publicKey, hashed, this.data)
if err != nil {
return this.AppendError(err)
}
return this
}
// 签名后数据
func (this EIGamal) DataHash(fn HashFunc, data []byte) ([]byte, error) {
h := fn()
h.Write(data)
return h.Sum(nil), nil
}