2018-04-18 18:59:48 +02:00
|
|
|
import { NodeCryptoFunctionService } from '../../../src/services/nodeCryptoFunction.service';
|
|
|
|
|
2018-04-20 16:59:55 +02:00
|
|
|
import { Utils } from '../../../src/misc/utils';
|
2018-04-18 18:59:48 +02:00
|
|
|
|
|
|
|
describe('NodeCrypto Function Service', () => {
|
|
|
|
describe('aesEncrypt', () => {
|
|
|
|
it('should successfully aes encrypt data', async () => {
|
|
|
|
const nodeCryptoFunctionService = new NodeCryptoFunctionService();
|
|
|
|
const iv = makeStaticByteArray(16);
|
|
|
|
const key = makeStaticByteArray(32);
|
2018-04-20 16:59:55 +02:00
|
|
|
const data = Utils.fromUtf8ToArray('EncryptMe!');
|
2018-04-18 18:59:48 +02:00
|
|
|
const encValue = await nodeCryptoFunctionService.aesEncrypt(data.buffer, iv.buffer, key.buffer);
|
2018-04-20 16:59:55 +02:00
|
|
|
expect(Utils.fromBufferToB64(encValue)).toBe('ByUF8vhyX4ddU9gcooznwA==');
|
2018-04-18 18:59:48 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('aesDecryptSmall', () => {
|
|
|
|
it('should successfully aes decrypt data', async () => {
|
|
|
|
const nodeCryptoFunctionService = new NodeCryptoFunctionService();
|
|
|
|
const iv = makeStaticByteArray(16);
|
|
|
|
const key = makeStaticByteArray(32);
|
2018-04-20 16:59:55 +02:00
|
|
|
const data = Utils.fromB64ToArray('ByUF8vhyX4ddU9gcooznwA==');
|
|
|
|
const decValue = await nodeCryptoFunctionService.aesDecryptSmall(data.buffer, iv.buffer, key.buffer);
|
|
|
|
expect(Utils.fromBufferToUtf8(decValue)).toBe('EncryptMe!');
|
2018-04-18 18:59:48 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('aesDecryptLarge', () => {
|
|
|
|
it('should successfully aes decrypt data', async () => {
|
|
|
|
const nodeCryptoFunctionService = new NodeCryptoFunctionService();
|
|
|
|
const iv = makeStaticByteArray(16);
|
|
|
|
const key = makeStaticByteArray(32);
|
2018-04-20 16:59:55 +02:00
|
|
|
const data = Utils.fromB64ToArray('ByUF8vhyX4ddU9gcooznwA==');
|
2018-04-18 18:59:48 +02:00
|
|
|
const decValue = await nodeCryptoFunctionService.aesDecryptLarge(data.buffer, iv.buffer, key.buffer);
|
2018-04-20 16:59:55 +02:00
|
|
|
expect(Utils.fromBufferToUtf8(decValue)).toBe('EncryptMe!');
|
2018-04-18 18:59:48 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('randomBytes', () => {
|
|
|
|
it('should make a value of the correct length', async () => {
|
|
|
|
const nodeCryptoFunctionService = new NodeCryptoFunctionService();
|
|
|
|
const randomData = await nodeCryptoFunctionService.randomBytes(16);
|
|
|
|
expect(randomData.byteLength).toBe(16);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not make the same value twice', async () => {
|
|
|
|
const nodeCryptoFunctionService = new NodeCryptoFunctionService();
|
|
|
|
const randomData = await nodeCryptoFunctionService.randomBytes(16);
|
|
|
|
const randomData2 = await nodeCryptoFunctionService.randomBytes(16);
|
|
|
|
expect(randomData.byteLength === randomData2.byteLength && randomData !== randomData2).toBeTruthy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function makeStaticByteArray(length: number) {
|
|
|
|
const arr = new Uint8Array(length);
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
arr[i] = i;
|
|
|
|
}
|
|
|
|
return arr;
|
|
|
|
}
|