-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRSA.test.js
25 lines (23 loc) · 1.13 KB
/
RSA.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
const RSA = require("./RSA.js");
const fs = require('fs');
const sss = require('shamirs-secret-sharing');
const tmp = require('tmp-promise');
const path = require('path');
test('Asserts the decrypted text is equal to the original text', async () => {
//creates temporary directory to run the test which is later deleted
const dir = await tmp.dir();
//runs the exported generateKeys function from RSA.js
await RSA.generateKeys(dir.path, 5);
const text = "testing"
//encrypts random plain text using the public key found in 'public.txt'
const enc = await RSA.encrypt(text, path.join(dir.path, 'public.txt'));
function shardPath(num) {
return path.join(dir.path, 'Shard[' + num + '].txt');
}
//recovers the private key using shards 2 and 5 found in Shard[2].txt and Shard[5].txt
recovered = sss.combine([fs.readFileSync(shardPath(2)), fs.readFileSync(shardPath(5))]);
//decrypts the encrypted text using the recovered key
const dec = RSA.decrypt(enc, recovered);
//test if the decrypted plain text is equal to the original plain text
expect(dec).toBe(text);
})