forked from travist/jsencrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.rsa.js
321 lines (235 loc) · 12.7 KB
/
test.rsa.js
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// import {JSEncrypt} from "../src/JSEncrypt";
var keySizes = [128, 256, 512, 1024, 2048];
var pbkeys = [
"-----BEGIN PUBLIC KEY-----\n" +
"MCwwDQYJKoZIhvcNAQEBBQADGwAwGAIRAMfE82X6tlpNK7Bxbhg6nEECAwEAAQ==\n" +
"-----END PUBLIC KEY-----",
"-----BEGIN PUBLIC KEY-----\n" +
"MDwwDQYJKoZIhvcNAQEBBQADKwAwKAIhAMLw0mRGv5KF+P0LsgNvfrM5AJdVBWqr\n" +
"Q6Bf2gES5gwPAgMBAAE=\n" +
"-----END PUBLIC KEY-----",
"-----BEGIN PUBLIC KEY-----\n" +
"MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKEpu21RDTXxEly55HdkVV9SlFL3Hgpl\n" +
"i6+IohAsnaqFnApsKi1R7fAd3tBLmeHV2tlxYIogtxpzfpcc+QBVDx8CAwEAAQ==\n" +
"-----END PUBLIC KEY-----",
"-----BEGIN PUBLIC KEY-----\n" +
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC5LO5xVlO9g4PL1xdWudnihIAP\n" +
"bMsixr396bIbBIwKBul98UWQ3UALbqByq2bXVuoIbl48UokxOVstenGCyyo026NF\n" +
"h3Fg6Cnvj9ptvbmqk2i3eTOBrt+e26Z1sepsnQL5OojiVIbrWwS6v1pFCXpnnLLv\n" +
"yy6GPt/kftbhazH3oQIDAQAB\n" +
"-----END PUBLIC KEY-----",
"-----BEGIN PUBLIC KEY-----\n" +
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtKrsFSnzYl19m5wTwYdu\n" +
"/r1UVZJV+zkAFud6+XTInAy8HbCR9n59H9+54P+Af/fUE6rvEPc4H09Z63vQzIGM\n" +
"iL6GlqzMmptv/KRDIhj7Mk3MXomvEVfUsXrz5IpO0lf6NSeGhz4PGZUkHZ30VRx3\n" +
"Jd/a0KIhgftZHxzmMsh8iB/n781B18pCP2eOPTF+5gRCaW+0fVPBlb/mBlg8MJrd\n" +
"ScGCAReQ9NfTq8slJ0aO1NWaaRRANPQcCMljnTIK1ssyXBaSHKfoWeGx141mWMRx\n" +
"/LxyZ13Zc3lqgmICiKFqMrQl5UeV1IUXYpj5hO9f60LGpZVHDqqo/JdF3+VAheaf\n" +
"QwIDAQAB\n" +
"-----END PUBLIC KEY-----"
];
var prkeys = [
"-----BEGIN RSA PRIVATE KEY-----\n" +
"MGMCAQACEQDHxPNl+rZaTSuwcW4YOpxBAgMBAAECEQCqk6mhsmpyv17fK1dPeD3h\n" +
"AgkA9Lo1aGRom0sCCQDQ+JpqE6KDIwIJAKstyIfBnA3rAggOsWwqCTdkAQIIOP95\n" +
"RV9y2iQ=\n" +
"-----END RSA PRIVATE KEY-----\n",
"-----BEGIN RSA PRIVATE KEY-----\n" +
"MIGqAgEAAiEAwvDSZEa/koX4/QuyA29+szkAl1UFaqtDoF/aARLmDA8CAwEAAQIh\n" +
"AME2Z5Ez/hR/7PUBboKxM2U7hSaavytvocBdQjLvOUWhAhEA8HgiLHRk9KjJ2hp0\n" +
"5q3BfQIRAM+H7dYUXRnKXjYoqiKueXsCEGnaaCirf/lXB6vzs3wMBr0CEHT2Xwzw\n" +
"nSgT7dUIRhsVylECEFQRGFtZcKRmL8lqTBwECWI=\n" +
"-----END RSA PRIVATE KEY-----\n",
"-----BEGIN RSA PRIVATE KEY-----\n" +
"MIIBOQIBAAJBAKEpu21RDTXxEly55HdkVV9SlFL3Hgpli6+IohAsnaqFnApsKi1R\n" +
"7fAd3tBLmeHV2tlxYIogtxpzfpcc+QBVDx8CAwEAAQJAFn0VS07JEiLelhPWfpaA\n" +
"lzmVuvICvh6nXEormygupBGiIPSXfIsTFid26yxt9wu4JHeRF0lq+Ozo55XpBQED\n" +
"4QIhAM0E7ikuEa2bDsR2hQJhIz3SvzzyhE5dJcqFjRtKtMQvAiEAyT0C0gUyqCdN\n" +
"YuRON1T7FUffarMdQXR+8tgRkhoCeBECID+ZKfAoVF+QXDJhub0VOQNyntRfPt+4\n" +
"UYLTjwRKVm0NAiBuOCtuSoiHTxd0naU1aycmbboxn67bZeoOKkfdZL+LcQIgK6Xh\n" +
"1wb9I/sNYv9ByJEGBNJRwtUEZrk5babLEdkUq90=\n" +
"-----END RSA PRIVATE KEY-----\n",
"-----BEGIN RSA PRIVATE KEY-----\n" +
"MIICXgIBAAKBgQC5LO5xVlO9g4PL1xdWudnihIAPbMsixr396bIbBIwKBul98UWQ\n" +
"3UALbqByq2bXVuoIbl48UokxOVstenGCyyo026NFh3Fg6Cnvj9ptvbmqk2i3eTOB\n" +
"rt+e26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M64VAoGAbajAW1skN/tAL8r48dl9WWo4x8mZvJLX36z9\n" +
"6q1dGzVF8FPz8EPIJW51B8n7keQlBedC5CElo0KRz/OK7LfI87La+Hd4LbuKCEmv\n" +
"g8qZVLpALtWaUbD9bHxCWLfFVPOtqOcV+AVKdXdSZEFaK7j0yzM2Un/Ce07CgB+X\n" +
"0c23mO8CgYAOqnUR/uPIzkvj/eIbO7pnhHoKZ4Ji2TrIBqjskzaFd0Tox9i3SWKa\n" +
"cRdQciRIT1wkMdywnHFrJT1rwYXxcgfQXAku/vnYqAfvIzY7TyoL3pWX55O0Zrs7\n" +
"05R9mA5TZmzUU9m/PzUrRjasOGYSKkCz4Y2qGlrKI3H0aE+p+R56kQ==\n" +
"-----END RSA PRIVATE KEY-----\n"
];
keySizes.forEach(function(keySize, index){
var jse = new JSEncrypt({default_key_size:keySize});
var openssl_public_key = pbkeys[index];
var openssl_private_key = prkeys[index];
describe('JSEncrypt - '+keySize+' bit', function(){
//this.timeout(0); //Timout for test cases, zero means infinite. Needed for key sizes > 1024
describe('#getKey()', function(){
it('should be '+keySize+' bit long', function () {
var key = jse.getKey();
var length = key.n.bitLength();
length = length%2===0 ? length: length+1;
expect(length).to.equal(keySize);
});
});
describe('#getKey() async', function(done) {
it('should be '+keySize+' bit long', function () {
jse.getKey(function () {
var key = jse.getKey();
var length = key.n.bitLength();
length = length%2===0 ? length: length+1;
expect(length).to.equal(keySize);
done();
});
});
});
describe('#encrypt() | #decrypt()', function(){
//Tom Wu's RSA Object use paddingpkcs #1, type 2
var maxLength = (((jse.getKey().n.bitLength()+7)>>3)-11);
var maxLengthBit = maxLength << 3;
it('should encrypt/decrypt up to '+maxLengthBit+' bit', function () {
var test = [];
for (var i=0; i<maxLength;i++)
test.push('a');
test = test.join('');
var encrypted = jse.encrypt(test);
expect(encrypted).to.be.ok();
var decrypted = jse.decrypt(encrypted);
expect(decrypted).to.be(test);
});
it('should fail to encrypt more than '+maxLengthBit+' bit', function(){
var test = [];
for (var i=0; i<(maxLength+1);i++)
test.push('a');
test = test.join('');
var encrypted = jse.encrypt(test);
expect(encrypted).to.not.be.ok();
});
});
describe('#getPublicKey()', function(){
var pkey = jse.getPublicKey();
it('should be a non-empty string', function(){
expect(pkey).to.be.a('string');
expect(pkey).to.not.be.empty();
});
it('should contain public header and footer and be wrapped at 64 chars', function(){
//For small bit keys, the public key may contain only one line
var regex = /-----BEGIN PUBLIC KEY-----\n((.{64}\n)+(.{1,64}\n)?)|(.{1,64}\n)-----END PUBLIC KEY-----/;
expect(pkey).to.match(regex);
});
});
describe('#getPrivateKey()', function(){
var pkey = jse.getPrivateKey();
it('should be a non-empty string', function(){
expect(pkey).to.be.a('string');
expect(pkey).to.not.be.empty();
});
it('should contain private header and footer and be wrapped at 64 chars', function(){
var regex = /-----BEGIN RSA PRIVATE KEY-----\n(.{64}\n)+(.{1,64}\n)?-----END RSA PRIVATE KEY-----/;
expect(pkey).to.match(regex);
});
});
describe('#getPublicKeyB64()', function(){
var pkey = jse.getPublicKeyB64();
it('should be a non-empty string', function(){
expect(pkey).to.be.a('string');
expect(pkey).to.not.be.empty();
});
it('should not contain public header and footer,one line, base64 encoded', function(){
//regex to match base64 encoded string, reference:
//http://stackoverflow.com/a/5885097/1446321
var regex = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$/;
expect(pkey).to.match(regex);
});
});
describe('#getPrivateKeyB64()', function(){
var pkey = jse.getPrivateKeyB64();
it('should be a non-empty string', function(){
expect(pkey).to.be.a('string');
expect(pkey).to.not.be.empty();
});
it('should not contain private header and footer,one line, base64 encoded', function(){
//regex to match base64 encoded string, reference:
//http://stackoverflow.com/a/5885097/1446321
var regex = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$/;
expect(pkey).to.match(regex);
});
});
describe('#setPrivateKey()', function(){
var tmp = new JSEncrypt();
tmp.setPrivateKey(openssl_private_key);
it('should correctly set the private key parameters',function(){
var params = ['n', 'e', 'd', 'p', 'q', 'dmp1', 'dmq1', 'coeff'];
expect(tmp.key).to.be.ok();
expect(tmp.key).to.have.keys(params);
params.forEach(function(value,index){
expect(tmp.key[value]).to.be.ok();
});
});
it('should both encrypt and decrypt',function(){
var test = 'test';
var enc = tmp.encrypt(test);
expect(enc).to.be.ok();
var dec = tmp.decrypt(enc);
expect(dec).to.be(test);
});
});
describe('#setPublicKey()', function(){
var tmp = new JSEncrypt();
tmp.setPublicKey(openssl_public_key);
it('should correctly set the public key parameters',function(){
var pub_params = ['n', 'e'];
var priv_params = ['d', 'p', 'q', 'dmp1', 'dmq1', 'coeff'];
expect(tmp.key).to.be.ok();
expect(tmp.key).to.have.keys(pub_params);
pub_params.forEach(function(value,index){
expect(tmp.key[value]).to.be.ok();
});
priv_params.forEach(function(value,index){
expect(tmp.key[value]).to.not.be.ok();
});
});
it('should only encrypt',function(){
var test = 'test';
var enc = tmp.encrypt(test);
var dec = tmp.decrypt(enc);
expect(enc).to.be.ok();
expect(dec).to.not.be.ok();
});
});
});
});