-
Notifications
You must be signed in to change notification settings - Fork 52
/
rsa_test.go
104 lines (94 loc) · 3.29 KB
/
rsa_test.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
package gorsa
import (
"testing"
)
func init() {
}
var Pubkey = `-----BEGIN 公钥-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAk+89V7vpOj1rG6bTAKYM
56qmFLwNCBVDJ3MltVVtxVUUByqc5b6u909MmmrLBqS//PWC6zc3wZzU1+ayh8xb
UAEZuA3EjlPHIaFIVIz04RaW10+1xnby/RQE23tDqsv9a2jv/axjE/27b62nzvCW
eItu1kNQ3MGdcuqKjke+LKhQ7nWPRCOd/ffVqSuRvG0YfUEkOz/6UpsPr6vrI331
hWRB4DlYy8qFUmDsyvvExe4NjZWblXCqkEXRRAhi2SQRCl3teGuIHtDUxCskRIDi
aMD+Qt2Yp+Vvbz6hUiqIWSIH1BoHJer/JOq2/O6X3cmuppU4AdVNgy8Bq236iXvr
MQIDAQAB
-----END 公钥-----
`
var Pirvatekey = `-----BEGIN 私钥-----
MIIEpAIBAAKCAQEAk+89V7vpOj1rG6bTAKYM56qmFLwNCBVDJ3MltVVtxVUUByqc
5b6u909MmmrLBqS//PWC6zc3wZzU1+ayh8xbUAEZuA3EjlPHIaFIVIz04RaW10+1
xnby/RQE23tDqsv9a2jv/axjE/27b62nzvCWeItu1kNQ3MGdcuqKjke+LKhQ7nWP
RCOd/ffVqSuRvG0YfUEkOz/6UpsPr6vrI331hWRB4DlYy8qFUmDsyvvExe4NjZWb
lXCqkEXRRAhi2SQRCl3teGuIHtDUxCskRIDiaMD+Qt2Yp+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NRUbHsfQtv+wqnzoyPpHZgVQeQBhcoXJSm+qV3cdGjLU6OM
HgqeaQKBgQCnmL5SX7GSAeB0rSNugPp2GezAQj0H4OCc8kNrHK8RUvXIU9B2zKA2
z/QUKFb1gIGcKxYr+LqQ25/+TGvINjuf6P3fVkHL0U8jOG0IqpPJXO3Vl9B8ewWL
cFQVB/nQfmaMa4ChK0QEUe+Mqi++MwgYbRHx1lIOXEfUJO+PXrMekw==
-----END 私钥-----
`
func Test_SetPublicKey(t *testing.T) {
if err := RSA.SetPublicKey(Pubkey); err != nil {
t.Error(err)
}
}
func Test_SetPrivateKey(t *testing.T) {
if err := RSA.SetPrivateKey(Pirvatekey); err != nil {
t.Error(err)
}
}
// 公钥加密私钥解密
func Test_PubENCTYPTPriDECRYPT(t *testing.T) {
if err := RSA.SetPublicKey(Pubkey); err != nil {
t.Error(err)
}
if err := RSA.SetPrivateKey(Pirvatekey); err != nil {
t.Error(err)
}
pubenctypt, err := RSA.PubKeyENCTYPT([]byte(`hello world`))
if err != nil {
t.Error(err)
}
pridecrypt, err := RSA.PriKeyDECRYPT(pubenctypt)
if err != nil {
t.Error(err)
}
if string(pridecrypt) != `hello world` {
t.Error(`不符合预期`)
}
}
// 公钥解密私钥加密
func Test_PriENCTYPTPubDECRYPT(t *testing.T) {
if err := RSA.SetPublicKey(Pubkey); err != nil {
t.Error(err)
}
if err := RSA.SetPrivateKey(Pirvatekey); err != nil {
t.Error(err)
}
prienctypt, err := RSA.PriKeyENCTYPT([]byte(`hello world`))
if err != nil {
t.Error(err)
}
pubdecrypt, err := RSA.PubKeyDECRYPT(prienctypt)
if err != nil {
t.Error(err)
}
if string(pubdecrypt) != `hello world` {
t.Error(`不符合预期`)
}
}