-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsa.go
62 lines (48 loc) · 1.36 KB
/
rsa.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
package piaotong
import (
"crypto"
"fmt"
"github.com/forgoer/openssl"
)
// SignRequest 对向票通发起的请求进行签名
func (c *Client) SignRequest(req *Request) error {
signed, err := openssl.RSASign([]byte(req.SignatureContent()), c.platformPrivateKey, crypto.SHA1)
if err != nil {
return err
}
req.Sign = base64EncodeToString(signed)
return nil
}
// SignResponse 对返回给票通的响应进行签名
func (c *Client) SignResponse(res *Response) error {
signed, err := openssl.RSASign([]byte(res.SignatureContent()), c.platformPrivateKey, crypto.SHA1)
if err != nil {
return err
}
res.Sign = base64EncodeToString(signed)
return nil
}
// VerifyRequest 验证票通请求签名
func (c *Client) VerifyRequest(req *Request) error {
sign, err := base64DecodeString(req.Sign)
if err != nil {
return err
}
err = openssl.RSAVerify([]byte(req.SignatureContent()), sign, c.piaotongPublicKey, crypto.SHA1)
if err != nil {
err = fmt.Errorf("%w: %v", ErrInvalidSignature, err)
}
return err
}
// VerifyResponse 验证票通响应签名
func (c *Client) VerifyResponse(res *Response) error {
sign, err := base64DecodeString(res.Sign)
if err != nil {
return err
}
err = openssl.RSAVerify([]byte(res.SignatureContent()), sign, c.piaotongPublicKey, crypto.SHA1)
if err != nil {
err = fmt.Errorf("%w: %v", ErrInvalidSignature, err)
}
return err
}