-
Notifications
You must be signed in to change notification settings - Fork 25
/
rsa_sign.go
51 lines (37 loc) · 1.15 KB
/
rsa_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
package rsa
import (
"errors"
"crypto/rsa"
"crypto/rand"
"github.com/deatil/go-cryptobin/tool"
)
// 私钥签名
func (this Rsa) Sign() Rsa {
if this.privateKey == nil {
err := errors.New("Rsa: [Sign()] privateKey error.")
return this.AppendError(err)
}
newHash := tool.NewHash()
hasher := newHash.GetCryptoHash(this.signHash)
hashData := newHash.DataCryptoHash(this.signHash, this.data)
paredData, err := rsa.SignPKCS1v15(rand.Reader, this.privateKey, hasher, hashData)
this.paredData = paredData
return this.AppendError(err)
}
// 公钥验证
// 使用原始数据[data]对比签名后数据
func (this Rsa) Verify(data []byte) Rsa {
if this.publicKey == nil {
err := errors.New("Rsa: [Verify()] publicKey error.")
return this.AppendError(err)
}
newHash := tool.NewHash()
hasher := newHash.GetCryptoHash(this.signHash)
hashData := newHash.DataCryptoHash(this.signHash, data)
err := rsa.VerifyPKCS1v15(this.publicKey, hasher, hashData, this.data)
if err != nil {
return this.AppendError(err)
}
this.verify = true
return this
}