2018-06-04 17:47:55 +00:00
|
|
|
const ece = require('http_ece');
|
|
|
|
require('buffer');
|
|
|
|
|
|
|
|
import assert from 'assert';
|
|
|
|
import { b64ToArray } from '../../../app/utils';
|
2018-06-21 00:05:33 +00:00
|
|
|
import ECE from '../../../app/ece.js';
|
2018-06-04 17:47:55 +00:00
|
|
|
|
|
|
|
const rs = 36;
|
|
|
|
|
|
|
|
const str = 'You are the dancing queen, young and sweet, only seventeen.';
|
|
|
|
const testSalt = 'I1BsxtFttlv3u_Oo94xnmw';
|
|
|
|
const keystr = 'yqdlZ-tYemfogSmv7Ws5PQ';
|
|
|
|
|
|
|
|
const buffer = Buffer.from(str);
|
|
|
|
const params = {
|
|
|
|
version: 'aes128gcm',
|
|
|
|
rs: rs,
|
|
|
|
salt: testSalt,
|
|
|
|
keyid: '',
|
|
|
|
key: keystr
|
|
|
|
};
|
|
|
|
|
|
|
|
const encrypted = ece.encrypt(buffer, params);
|
|
|
|
const decrypted = ece.decrypt(encrypted, params);
|
|
|
|
|
|
|
|
describe('Streaming', function() {
|
|
|
|
//testing against http_ece's implementation
|
|
|
|
describe('ECE', function() {
|
|
|
|
const key = b64ToArray(keystr);
|
|
|
|
const salt = b64ToArray(testSalt).buffer;
|
|
|
|
const blob = new Blob([str], { type: 'text/plain' });
|
|
|
|
|
|
|
|
it('can encrypt', async function() {
|
2018-06-21 00:05:33 +00:00
|
|
|
const encStream = new ECE(blob, key, 'encrypt', rs, salt).stream;
|
|
|
|
const reader = encStream.getReader();
|
2018-06-04 17:47:55 +00:00
|
|
|
|
|
|
|
let result = Buffer.from([]);
|
|
|
|
|
|
|
|
let state = await reader.read();
|
|
|
|
while (!state.done) {
|
|
|
|
result = Buffer.concat([result, state.value]);
|
|
|
|
state = await reader.read();
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.deepEqual(result, encrypted);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can decrypt', async function() {
|
|
|
|
const encBlob = new Blob([encrypted]);
|
2018-06-21 00:05:33 +00:00
|
|
|
const decStream = await new ECE(encBlob, key, 'decrypt', rs).stream;
|
2018-06-04 17:47:55 +00:00
|
|
|
|
2018-06-21 00:05:33 +00:00
|
|
|
const reader = decStream.getReader();
|
2018-06-04 17:47:55 +00:00
|
|
|
let result = Buffer.from([]);
|
|
|
|
|
|
|
|
let state = await reader.read();
|
|
|
|
while (!state.done) {
|
|
|
|
result = Buffer.concat([result, state.value]);
|
|
|
|
state = await reader.read();
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.deepEqual(result, decrypted);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|