-
Notifications
You must be signed in to change notification settings - Fork 68
/
rsa_test.go
201 lines (182 loc) · 8.63 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package dongle
import (
"testing"
"github.com/stretchr/testify/assert"
)
var (
rsaInput = "hello world"
pkcs1PublicKey = `-----BEGIN RSA PUBLIC KEY-----
MIGJAoGBAK12MTd84qkCZzp4iLUj8YSUglaFMsFlv9KlIL4+Xts40PK3+wbsXPEw
cujGeUmdgMeZiK7SLLSz8QeE0v7Vs+cGK4Bs4qLtMGCiO6wEuyt10KsafTyBktFn
dk/+gBLr7B/b+9+HaMIIoJUdsFksdAg3cxTSpwVApe98loFNRfqDAgMBAAE=
-----END RSA PUBLIC KEY-----`
pkcs1PrivateKey = `-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQCtdjE3fOKpAmc6eIi1I/GElIJWhTLBZb/SpSC+Pl7bONDyt/sG
7FzxMHLoxnlJnYDHmYiu0iy0s/EHhNL+1bPnBiuAbOKi7TBgojusBLsrddCrGn08
gZLRZ3ZP/oAS6+wf2/vfh2jCCKCVHbBZLHQIN3MU0qcFQKXvfJaBTUX6gwIDAQAB
AoGAFwAfEo56t5JcAcLNzccQVVYj2jkbO820G8hNiSxYA5WLD0QaAx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KveDKPEQ5BD3uKG3DiUN3nAyjh
sg67DUu0x7McLWi62UzrH78EHQFJAgMBAAECgYAeo3nHWzPNURVUsUMcan96U5bE
YA2AugxfQVMNf2HvOGidZ2adh3udWrQY/MglERNcTd5gKriG2rDEH0liBecIrNKs
BL4lV+qHEGRUcnDDdtUBdGInEU8lve5keDgmX+/huXSRJ+3tYA5u9j+32RquVczv
Idtb5XnBLUl61k0osQJBAON5+eJjtw6xpn+pveU92BSHvaJYVyrLHwUjR07aNKb7
GlGVM3MGf1FCa8WQUo9uUzYxGLtg5Qf3sqwOrwPd5UsCQQDAOF/zWqGuY3HfV/1w
giXiWp8rc+S8tanMj5M37QQbYW5YLjUmJImoklVahv3qlgLZdEN5ZSueM5jfoSFt
Nts7AkBKoRDvSiGbi4MBbTHkzLZgfewkH/FxE7S4nctePk553fXTgCyh9ya8BRuQ
dHnxnpNkOxVPHEnnpEcVFbgrf5gjAkB7KmRI4VTiEfRgINhTJAG0VU7SH/N7+4cu
fPzfA+7ywG5c8Fa79wOB0SoB1KeUjcSLo5Ssj2fwea1F9dAeU90LAkBJQFofveaD
a3YlN4EQZOcCvJKmg7xwWuGxFVTZDVVEws7UCQbEOEEXZrNd9x0IF5kpPLR+rxua
RPgUNaDGIh5o
-----END PRIVATE KEY-----`
)
func TestRsa_EncryptWithPublicKy(t *testing.T) {
e1 := Encrypt.FromString(rsaInput).ByRsa(pkcs1PublicKey)
assert.Nil(t, e1.Error)
d11 := Decrypt.FromRawString(e1.ToRawString()).ByRsa(pkcs1PrivateKey)
assert.Nil(t, d11.Error)
assert.Equal(t, rsaInput, d11.ToString())
d12 := Decrypt.FromHexString(e1.ToHexString()).ByRsa(pkcs1PrivateKey)
assert.Nil(t, d12.Error)
assert.Equal(t, rsaInput, d12.ToString())
d13 := Decrypt.FromBase64String(e1.ToBase64String()).ByRsa(pkcs1PrivateKey)
assert.Nil(t, d13.Error)
assert.Equal(t, rsaInput, d13.ToString())
e8 := Encrypt.FromString(rsaInput).ByRsa(pkcs8PublicKey)
assert.Nil(t, e8.Error)
d81 := Decrypt.FromRawString(e8.ToRawString()).ByRsa(pkcs8PrivateKey)
assert.Nil(t, d81.Error)
assert.Equal(t, rsaInput, d11.ToString())
d82 := Decrypt.FromHexString(e8.ToHexString()).ByRsa(pkcs8PrivateKey)
assert.Nil(t, d82.Error)
assert.Equal(t, rsaInput, d12.ToString())
d83 := Decrypt.FromBase64String(e8.ToBase64String()).ByRsa(pkcs8PrivateKey)
assert.Nil(t, d83.Error)
assert.Equal(t, rsaInput, d83.ToString())
}
func TestRsa_EncryptWithPrivateKy(t *testing.T) {
e1 := Encrypt.FromString(rsaInput).ByRsa(pkcs1PrivateKey)
assert.Nil(t, e1.Error)
d11 := Decrypt.FromRawString(e1.ToRawString()).ByRsa(pkcs1PublicKey)
assert.Nil(t, d11.Error)
assert.Equal(t, rsaInput, d11.ToString())
d12 := Decrypt.FromHexString(e1.ToHexString()).ByRsa(pkcs1PublicKey)
assert.Nil(t, d12.Error)
assert.Equal(t, rsaInput, d12.ToString())
d13 := Decrypt.FromBase64String(e1.ToBase64String()).ByRsa(pkcs1PublicKey)
assert.Nil(t, d13.Error)
assert.Equal(t, rsaInput, d13.ToString())
e8 := Encrypt.FromString(rsaInput).ByRsa(pkcs8PrivateKey)
assert.Nil(t, e8.Error)
d81 := Decrypt.FromRawString(e8.ToRawString()).ByRsa(pkcs8PublicKey)
assert.Nil(t, d81.Error)
assert.Equal(t, rsaInput, d81.ToString())
d82 := Decrypt.FromHexString(e8.ToHexString()).ByRsa(pkcs8PublicKey)
assert.Nil(t, d82.Error)
assert.Equal(t, rsaInput, d82.ToString())
d83 := Decrypt.FromBase64String(e8.ToBase64String()).ByRsa(pkcs8PublicKey)
assert.Nil(t, d83.Error)
assert.Equal(t, rsaInput, d83.ToString())
}
func TestRsa_SignWithPrivateKy(t *testing.T) {
s1 := Sign.FromString(rsaInput).ByRsa(pkcs1PrivateKey, SHA224)
assert.Nil(t, s1.Error)
v11 := Verify.FromRawString(s1.ToRawString(), rsaInput).ByRsa(pkcs1PublicKey, SHA224)
assert.Nil(t, v11.Error)
assert.Equal(t, true, v11.ToBool())
v12 := Verify.FromHexString(s1.ToHexString(), rsaInput).ByRsa(pkcs1PublicKey, SHA224)
assert.Nil(t, v12.Error)
assert.Equal(t, true, v12.ToBool())
v13 := Verify.FromBase64String(s1.ToBase64String(), rsaInput).ByRsa(pkcs1PublicKey, SHA224)
assert.Nil(t, v13.Error)
assert.Equal(t, true, v13.ToBool())
s8 := Sign.FromString(rsaInput).ByRsa(pkcs8PrivateKey, SHA256)
assert.Nil(t, s8.Error)
v81 := Verify.FromRawString(s8.ToRawString(), rsaInput).ByRsa(pkcs8PublicKey, SHA256)
assert.Nil(t, v81.Error)
assert.Equal(t, true, v81.ToBool())
v82 := Verify.FromHexString(s8.ToHexString(), rsaInput).ByRsa(pkcs8PublicKey, SHA256)
assert.Nil(t, v82.Error)
assert.Equal(t, true, v12.ToBool())
v83 := Verify.FromBase64String(s8.ToBase64String(), rsaInput).ByRsa(pkcs8PublicKey, SHA256)
assert.Nil(t, v83.Error)
assert.Equal(t, true, v83.ToBool())
}
func TestRsa_Empty_Src(t *testing.T) {
empty := ""
e := Encrypt.FromString(empty).ByRsa(pkcs1PublicKey)
assert.Nil(t, e.Error)
d1 := Decrypt.FromRawString(e.ToRawString()).ByRsa(pkcs1PrivateKey)
assert.Nil(t, d1.Error)
assert.Equal(t, empty, d1.ToString())
d2 := Decrypt.FromHexString(e.ToHexString()).ByRsa(pkcs1PrivateKey)
assert.Nil(t, d2.Error)
assert.Equal(t, empty, d2.ToString())
d3 := Decrypt.FromBase64String(e.ToBase64String()).ByRsa(pkcs1PrivateKey)
assert.Nil(t, d3.Error)
assert.Equal(t, empty, d3.ToString())
s := Sign.FromString(empty).ByRsa(pkcs1PrivateKey, MD5)
assert.Nil(t, s.Error)
v1 := Verify.FromRawString(s.ToRawString(), empty).ByRsa(pkcs1PublicKey, MD5)
assert.Nil(t, v1.Error)
assert.Equal(t, false, v1.ToBool())
v2 := Verify.FromHexString(s.ToHexString(), empty).ByRsa(pkcs1PublicKey, MD5)
assert.Nil(t, v2.Error)
assert.Equal(t, false, v2.ToBool())
v3 := Verify.FromBase64String(s.ToBase64String(), empty).ByRsa(pkcs1PublicKey, MD5)
assert.Nil(t, v1.Error)
assert.Equal(t, false, v3.ToBool())
}
func TestRsa_PublicKey_Error(t *testing.T) {
invalidPublicKey := `-----BEGIN PUBLIC KEY-----
xxxx
-----END PUBLIC KEY-----`
e1 := Encrypt.FromBytes([]byte(rsaInput)).ByRsa([]byte("xxxx"))
assert.Equal(t, invalidRsaPublicKeyError(), e1.Error)
e2 := Encrypt.FromBytes([]byte(rsaInput)).ByRsa([]byte(invalidPublicKey))
assert.Equal(t, invalidRsaPublicKeyError(), e2.Error)
v1 := Verify.FromRawBytes([]byte("xxxx"), []byte(rsaInput)).ByRsa([]byte("xxxx"), SHA1)
assert.Equal(t, invalidRsaPublicKeyError(), v1.Error)
v2 := Verify.FromRawBytes([]byte("xxxx"), []byte(rsaInput)).ByRsa([]byte(invalidPublicKey), SHA224)
assert.Equal(t, invalidRsaPublicKeyError(), v2.Error)
v3 := Verify.FromHexString("xxxx", rsaInput).ByRsa(pkcs8PublicKey, SHA256)
assert.Equal(t, invalidDecodingError("hex"), v3.Error)
v4 := Verify.FromHexBytes([]byte("xxxx"), []byte(rsaInput)).ByRsa(pkcs8PublicKey, SHA384)
assert.Equal(t, invalidDecodingError("hex"), v4.Error)
v5 := Verify.FromBase64String("xxxxxx", rsaInput).ByRsa(pkcs8PublicKey, SHA512)
assert.Equal(t, invalidDecodingError("base64"), v5.Error)
v6 := Verify.FromBase64Bytes([]byte("xxxxxx"), []byte(rsaInput)).ByRsa(pkcs8PublicKey, RIPEMD160)
assert.Equal(t, invalidDecodingError("base64"), v6.Error)
}
func TestRsa_PrivateKey_Error(t *testing.T) {
invalidPrivateKey := `-----BEGIN PRIVATE KEY-----
xxxx
-----END PRIVATE KEY-----`
e := Encrypt.FromBytes([]byte(rsaInput)).ByRsa([]byte(pkcs1PublicKey))
d1 := Decrypt.FromHexBytes(e.ToHexBytes()).ByRsa([]byte("xxxx"))
assert.Equal(t, invalidRsaPrivateKeyError(), d1.Error)
d2 := Decrypt.FromBase64Bytes(e.ToBase64Bytes()).ByRsa([]byte(invalidPrivateKey))
assert.Equal(t, invalidRsaPrivateKeyError(), d2.Error)
s1 := Sign.FromBytes([]byte(rsaInput)).ByRsa([]byte("xxxx"), SHA1)
assert.Equal(t, invalidRsaPrivateKeyError(), s1.Error)
s2 := Sign.FromBytes([]byte(rsaInput)).ByRsa([]byte(invalidPrivateKey), SHA1)
assert.Equal(t, invalidRsaPrivateKeyError(), s2.Error)
}