-
Notifications
You must be signed in to change notification settings - Fork 0
/
cryptography.test.js
302 lines (290 loc) · 15.6 KB
/
cryptography.test.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
import Cryptography from './cryptography.js';
const salts = [null, '', 'abc123'];
const algorithms = [null, 'aes-128-ctr', 'aes-192-ctr', 'aes-256-ctr'];
const hashAlgorithms = [null, 'ripemd160', 'sha256', 'sha384', 'sha512'];
const encodings = [null, 'utf8', 'ascii'];
const outputEncodings = [null, 'base64', 'hex'];
const passwords = ['short', 'longerpassword', 'c0MPl3x_PAS5W0rd:%@&'];
test('Cryptography: constructs ok.', () => {
expect(() => { new Cryptography(); }).not.toThrow();
for (let algorithm of algorithms) {
for (let hashAlgorithm of hashAlgorithms) {
for (let encoding of encodings) {
for (let outputEncoding of outputEncodings) {
expect(() => { new Cryptography(algorithm, hashAlgorithm, encoding, outputEncoding); }).not.toThrow();
let c = new Cryptography(algorithm, hashAlgorithm, encoding, outputEncoding);
expect(c.algorithm).toBe(algorithm || 'aes-256-ctr');
expect(c.encoding).toBe(encoding || 'utf8');
expect(c.outputEncoding).toBe(outputEncoding || 'base64');
}
}
}
}
});
describe('buffer encryption & decryption', () => {
let data = [Buffer.from('test 1234')];
for (let algorithm of algorithms) {
if (algorithm !== null) {
for (let encoding of encodings) {
if (encoding !== null) {
for (let outputEncoding of outputEncodings) {
if (outputEncoding !== null) {
it(`using the "${algorithm}" algorithm, "${encoding}" encoding, and "${outputEncoding}" output encoding.`, async () => {
let c = new Cryptography(algorithm, null, encoding, outputEncoding);
for (let password of passwords) {
for (let salt of salts) {
for (let d of data) {
let enc = await c.encryptBuffer(d, password, salt);
await expect(c.decryptBuffer(enc, password, salt)).resolves.toBeInstanceOf(Buffer);
let dec = await c.decryptBuffer(enc, password, salt);
expect(d.equals(dec)).toBe(true);
}
}
}
});
}
}
}
}
}
}
});
describe('text encryption & decryption', () => {
let data = ['', 'test123'];
for (let algorithm of algorithms) {
if (algorithm !== null) {
for (let encoding of encodings) {
if (encoding !== null) {
for (let outputEncoding of outputEncodings) {
if (outputEncoding !== null) {
it(`using the "${algorithm}" algorithm, "${encoding}" encoding, and "${outputEncoding}" output encoding.`, async () => {
let c = new Cryptography(algorithm, null, encoding, outputEncoding);
for (let password of passwords) {
for (let salt of salts) {
for (let d of data) {
let enc = await c.encryptText(d, password, salt);
await expect(c.decryptText(enc, password, salt)).resolves.not.toBeNull();
let dec = await c.decryptText(enc, password, salt);
expect(dec).toEqual(d);
}
}
}
});
}
}
}
}
}
}
});
describe('buffer hashing', () => {
for (let hashAlgorithm of hashAlgorithms) {
if (hashAlgorithm !== null) {
for (let encoding of encodings) {
if (encoding !== null) {
for (let outputEncoding of outputEncodings) {
if (outputEncoding !== null) {
let c = new Cryptography(null, hashAlgorithm, encoding, outputEncoding);
it(`reliable digest using the "${hashAlgorithm}" hash algorithm, "${encoding}" encoding, and "${outputEncoding}" output encoding.`, async () => {
let digest = await c.hashBuffer(Buffer.from('test123', encoding), 'saltABC');
let digest2 = await c.hashBuffer(Buffer.from('test123', encoding), 'saltABC');
expect(Buffer.compare(digest, digest2)).toBe(0);
digest = await c.hashBuffer(Buffer.from('test123', encoding), '');
digest2 = await c.hashBuffer(Buffer.from('test123', encoding), null);
expect(Buffer.compare(digest, digest2)).toBe(0);
});
}
}
}
}
}
}
});
describe('text hashing', () => {
for (let hashAlgorithm of hashAlgorithms) {
if (hashAlgorithm !== null) {
for (let encoding of encodings) {
if (encoding !== null) {
for (let outputEncoding of outputEncodings) {
if (outputEncoding !== null) {
let c = new Cryptography(null, hashAlgorithm, encoding, outputEncoding);
it(`reliable digest using the "${hashAlgorithm}" hash algorithm, "${encoding}" encoding, and "${outputEncoding}" output encoding.`, async () => {
let digest = await c.hashText('test123', 'saltABC');
let digest2 = await c.hashText('test123', 'saltABC');
expect(digest).toEqual(digest2);
digest = await c.hashText('test123', '');
digest2 = await c.hashText('test123', null);
expect(digest).toEqual(digest2);
});
}
}
}
}
}
}
});
describe('file hashing', () => {
for (let hashAlgorithm of hashAlgorithms) {
if (hashAlgorithm !== null) {
for (let encoding of encodings) {
if (encoding !== null) {
for (let outputEncoding of outputEncodings) {
if (outputEncoding !== null) {
let c = new Cryptography(null, hashAlgorithm, encoding, outputEncoding);
it(`reliable digest using the "${hashAlgorithm}" hash algorithm, "${encoding}" encoding, and "${outputEncoding}" output encoding.`, async () => {
let digest = await c.hashFile('test/crypto-test.json', 'saltABC');
let digest2 = await c.hashFile('test/crypto-test.json', 'saltABC');
expect(digest).toEqual(digest2);
digest = await c.hashFile('test/crypto-test.json', '');
digest2 = await c.hashFile('test/crypto-test.json', null);
expect(digest).toEqual(digest2);
});
}
}
}
}
}
}
});
describe('symmetric buffer signature and verification', () => {
for (let hashAlgorithm of hashAlgorithms) {
if (hashAlgorithm !== null) {
for (let encoding of encodings) {
if (encoding !== null) {
for (let outputEncoding of outputEncodings) {
if (outputEncoding !== null) {
let c = new Cryptography(null, hashAlgorithm, encoding, outputEncoding);
let buf = Buffer.from('test123', encoding);
let key = 'keyABC';
it(`generates signature using "${hashAlgorithm}" hash algorithm, "${encoding}" encoding, and "${outputEncoding}" output encoding.`, async () => {
let sig = c.signBuffer(buf, key);
expect(Buffer.isBuffer(sig)).toBe(true);
});
it(`verifies signature using "${hashAlgorithm}" hash algorithm, "${encoding}" encoding, and "${outputEncoding}" output encoding.`, async () => {
let sig = c.signBuffer(buf, key);
let verified = c.verifyBuffer(buf, key, sig);
expect(verified).toBe(true);
verified = c.verifyBuffer(buf, 'bad_key987', sig);
expect(verified).toBe(false);
verified = c.verifyBuffer(buf, key, Buffer.from('hello'));
expect(verified).toBe(false);
});
}
}
}
}
}
}
});
describe('symmetric text signature and verification', () => {
for (let hashAlgorithm of hashAlgorithms) {
if (hashAlgorithm !== null) {
for (let encoding of encodings) {
if (encoding !== null) {
for (let outputEncoding of outputEncodings) {
if (outputEncoding !== null) {
let c = new Cryptography(null, hashAlgorithm, encoding, outputEncoding);
let text = 'test123';
let key = 'keyABC';
it(`generates signature using "${hashAlgorithm}" hash algorithm, "${encoding}" encoding, and "${outputEncoding}" output encoding.`, async () => {
let sig = c.signText(text, key);
expect(typeof sig).toBe('string');
expect(sig.length).toBeGreaterThan(0);
});
it(`verifies signature using "${hashAlgorithm}" hash algorithm, "${encoding}" encoding, and "${outputEncoding}" output encoding.`, async () => {
let sig = c.signText(text, key);
let verified = c.verifyText(text, key, sig);
expect(verified).toBe(true);
verified = c.verifyText(text, 'bad_key987', sig);
expect(verified).toBe(false);
verified = c.verifyText(text, key, 'hello');
expect(verified).toBe(false);
});
}
}
}
}
}
}
});
describe('asymmetric buffer signature and verification', () => {
for (let hashAlgorithm of hashAlgorithms) {
if (hashAlgorithm !== null) {
for (let encoding of encodings) {
if (encoding !== null) {
for (let outputEncoding of outputEncodings) {
if (outputEncoding !== null) {
let keypair = Cryptography.keypair('ed25519');
// console.log(keypair.privateKey.export({type: 'pkcs8', format:'pem'}), keypair.publicKey.export({type: 'spki', format:'pem'}));
let c = new Cryptography(null, hashAlgorithm, encoding, outputEncoding);
let buf = Buffer.from('test123', encoding);
it(`generates signature using "${hashAlgorithm}" hash algorithm, "${encoding}" encoding, and "${outputEncoding}" output encoding.`, async () => {
let sig = c.signBuffer(buf, keypair.privateKey, true);
expect(Buffer.isBuffer(sig)).toBe(true);
});
it(`verifies signature using "${hashAlgorithm}" hash algorithm, "${encoding}" encoding, and "${outputEncoding}" output encoding.`, async () => {
let sig = c.signBuffer(buf, keypair.privateKey, true);
let verified = c.verifyBuffer(buf, keypair.publicKey, sig, true);
expect(verified).toBe(true);
verified = c.verifyBuffer(buf, 'bad_key987', sig, true);
expect(verified).toBe(false);
verified = c.verifyBuffer(buf, keypair.publicKey, Buffer.from('hello'), true);
expect(verified).toBe(false);
});
}
}
}
}
}
}
});
describe('asymmetric text signature and verification', () => {
for (let hashAlgorithm of hashAlgorithms) {
if (hashAlgorithm !== null) {
for (let encoding of encodings) {
if (encoding !== null) {
for (let outputEncoding of outputEncodings) {
if (outputEncoding !== null) {
let keypair = Cryptography.keypair('ed25519');
let c = new Cryptography(null, hashAlgorithm, encoding, outputEncoding);
let text = 'test123';
it(`generates signature using "${hashAlgorithm}" hash algorithm, "${encoding}" encoding, and "${outputEncoding}" output encoding.`, async () => {
let sig = c.signText(text, keypair.privateKey, true);
expect(typeof sig).toBe('string');
expect(sig.length).toBeGreaterThan(0);
});
it(`verifies signature using "${hashAlgorithm}" hash algorithm, "${encoding}" encoding, and "${outputEncoding}" output encoding.`, async () => {
let sig = c.signText(text, keypair.privateKey, true);
let verified = c.verifyText(text, keypair.publicKey, sig, true);
expect(verified).toBe(true);
verified = c.verifyText(text, 'bad_key987', sig, true);
expect(verified).toBe(false);
verified = c.verifyText(text, keypair.publicKey, 'hello', true);
expect(verified).toBe(false);
});
}
}
}
}
}
}
});
describe('random buffers', () => {
let c = new Cryptography();
it('generates a random buffer of data between the specified byte length', async () => {
for (let x = 0; x < 1000; x++) {
let r = c.randomBuffer(x, 1001);
expect(Buffer.isBuffer(r)).toBe(true);
expect(r.byteLength).toBeGreaterThanOrEqual(x);
expect(r.byteLength).toBeLessThanOrEqual(1001);
}
});
it('generates random text between the specified length', async () => {
for (let x = 0; x < 1000; x++) {
let r = c.randomText(x, 1001);
expect(typeof r).toBe('string');
expect(r.length).toBeGreaterThanOrEqual(x);
expect(r.length).toBeLessThanOrEqual(1001);
}
});
});