forked from wechatpay-apiv3/wechatpay-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsa_crypto_test.go
145 lines (129 loc) · 6.21 KB
/
rsa_crypto_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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2021 Tencent Inc. All rights reserved.
package utils
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/x509"
"encoding/base64"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
testRSACryptoUtilPublicKeyStr = `-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2VCTd91fnUn73Xy9DLvt
/V62TVxRTEEstVdeRaZ3B3leO0pldE806mXO4RwdHXagHQ4vGeZN0yqm++rDsGK+
U3AH7kejyD2pXshNP9Cq5YwbptiLGtjcquw4HNxJQUOmDeJf2vg6byms9RUipiq4
SzbJKqJFlUpbuIPDpSpWz10PYmyCNeDGUUK65E5h2B834uxl1zNLYQCrkdBzb8oU
xwYeP5a2DNxmjL5lsJML7DGr5znsevnoqGRwTm9fxCGfy8wus7hwKz6clt3Whmmd
a7UAdb1c08hEQFVRbF14AR73xbnd8N0obCWJPCbzMCtkaSef4FdEEgEXJiw0VAJT
8wIDAQAB
-----END PUBLIC KEY-----`
testRSACryptoUtilPrivateKeyStr = `-----BEGIN TESTING KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEA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zbJKqJFlUpbuIPDpSpWz10PYmyCNeDGUUK65E5h2B83
4uxl1zNLYQCrkdBzb8oUxwYeP5a2DNxmjL5lsJML7DGr5znsevnoqGRwTm9fxCGf
y8wus7hwKz6clt3Whmmda7UAdb1c08hEQFVRbF14AR73xbnd8N0obCWJPCbzMCtk
aSef4FdEEgEXJiw0VAJT8wIDAQABo1AwTjAdBgNVHQ4EFgQUT1c7nd/SUO76HSoZ
umNUJv1R5PwwHwYDVR0jBBgwFoAUT1c7nd/SUO76HSoZumNUJv1R5PwwDAYDVR0T
BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAfTjxKRQMzNB/U6ZoCUS+BSNfa2Oh
0plMN6ZuzwiVVZwg1jywvv5yv04koS7Pd4i9E4gt9ZBUQXlpq+A3oOCEEHNRR6b2
kyazGRM7s0OP5X21WrbpSmKmU6K7hkfx30yYs08LVs/Q8DIhvaj1FCFeJzUCzYn/
fHMq4tsbKO0dKAeydPM/nrUZBmaYQVKMVOORGLFjFKVO7JV6Kq/R86ouhjEPgJOe
2xulNBUcjicqtZlBdEh/PWCYP2SpGVDclKm8jeo175T3EVAkdKzzmfpxtMmnMlmq
cTJOU9TxuGvNASMtjj7pYIerTx+xgZDXEVBWFW9PjJ0TV06tCRsgSHItgg==
-----END CERTIFICATE-----`
testRSACryptoUtilPrivateKey *rsa.PrivateKey
testRSACryptoUtilPublicKey *rsa.PublicKey
testRSACryptoUtilCertificate *x509.Certificate
)
func init() {
var err error
testRSACryptoUtilPrivateKey, err = LoadPrivateKey(testingKey(testRSACryptoUtilPrivateKeyStr))
if err != nil {
panic(fmt.Errorf("fail to load the private key:%s", err.Error()))
}
testRSACryptoUtilPublicKey, err = LoadPublicKey(testRSACryptoUtilPublicKeyStr)
if err != nil {
panic(fmt.Errorf("fail to load the public key:%s", err.Error()))
}
testRSACryptoUtilCertificate, err = LoadCertificate(testRSACryptoUtilMchCertificateStr)
if err != nil {
panic(fmt.Errorf("fail to load the certificate key:%s", err.Error()))
}
}
func TestOAEPCrypto(t *testing.T) {
const message = "hello world"
// 使用OAEP padding方式对证书加密
ciphertext, err := EncryptOAEPWithCertificate(message, testRSACryptoUtilCertificate)
require.NoError(t, err)
// 使用OAEP padding方式用公有库直接进行私钥解密,以验证加密正确
decodedCiphertext, err := base64.StdEncoding.DecodeString(ciphertext)
require.NoError(t, err)
decryptMessageBytes, err := rsa.DecryptOAEP(
sha1.New(), rand.Reader, testRSACryptoUtilPrivateKey, decodedCiphertext, nil)
require.NoError(t, err)
assert.Equal(t, message, string(decryptMessageBytes))
// 使用OAEP padding方式直接公钥加密
ciphertext, err = EncryptOAEPWithPublicKey(message, testRSACryptoUtilPublicKey)
require.NoError(t, err)
// 使用OAEP padding方式私钥解密
decryptMessage, err := DecryptOAEP(ciphertext, testRSACryptoUtilPrivateKey)
require.NoError(t, err)
assert.Equal(t, message, decryptMessage)
}
func TestPKCS1v15Crypto(t *testing.T) {
const message = "hello world"
// 使用PKCS1 padding对证书加密
ciphertext, err := EncryptPKCS1v15WithCertificate(message, testRSACryptoUtilCertificate)
require.NoError(t, err)
// 使用PKCS1 padding对用公有库直接进行私钥解密,以验证加密正确
decodedCiphertext, err := base64.StdEncoding.DecodeString(ciphertext)
require.NoError(t, err)
decryptMessageBytes, err := rsa.DecryptPKCS1v15(rand.Reader, testRSACryptoUtilPrivateKey, decodedCiphertext)
require.NoError(t, err)
assert.Equal(t, message, string(decryptMessageBytes))
// 使用PKCS1 padding进行公钥加密
ciphertext, err = EncryptPKCS1v15WithPublicKey(message, testRSACryptoUtilPublicKey)
require.NoError(t, err)
// 使用PKCS1 padding进行私钥解密
decryptMessage, err := DecryptPKCS1v15(ciphertext, testRSACryptoUtilPrivateKey)
require.NoError(t, err)
assert.Equal(t, message, decryptMessage)
}