crypto-helper/test/encryption.js

74 lines
1.6 KiB
JavaScript

/* eslint-disable no-magic-numbers */
// @ts-nocheck
'use strict';
const test = require ('ava');
const crypto = require ('../index');
test ('encryption', (t) => {
const enc = crypto.encrypt_aes ('foo', 'bar');
t.is (typeof enc, 'string');
});
test ('decryption', (t) => {
const enc = crypto.encrypt_aes ('foo', 'bar');
const dec = crypto.decrypt_aes (enc, 'bar');
t.is (dec, 'foo');
});
test ('encryption 128', (t) => {
const enc = crypto.encrypt_aes (
'foo',
'bar',
crypto.encryption_mode_cbc_128,
);
t.is (typeof enc, 'string');
});
test ('decryption 128', (t) => {
const enc = crypto.encrypt_aes (
'foo',
'bar',
crypto.encryption_mode_cbc_128,
);
const dec = crypto.decrypt_aes (
enc,
'bar',
crypto.encryption_mode_cbc_128,
);
t.is (dec, 'foo');
});
test ('fail decryption', (t) => {
const enc = crypto.encrypt_aes ('foo', 'bar');
const dec = crypto.decrypt_aes (enc, 'baz');
t.is (dec, null);
});
test ('rethrow decryption', (t) => {
const enc = crypto.encrypt_aes ('foo', 'bar');
t.throws (() => {
crypto.decrypt_aes (
enc,
'baz',
crypto.encryption_mode_cbc_256,
true
);
});
});
test ('unique crypto strings', (t) => {
const enc = [
crypto.encrypt_aes ('foo', 'bar'),
crypto.encrypt_aes ('foo', 'bar'),
crypto.encrypt_aes ('foo', 'bar'),
crypto.encrypt_aes ('foo', 'bar'),
crypto.encrypt_aes ('foo', 'bar'),
crypto.encrypt_aes ('foo', 'bar'),
crypto.encrypt_aes ('foo', 'bar'),
crypto.encrypt_aes ('foo', 'bar')
];
const unique = enc.filter ((v, i) => enc.indexOf (v) === i).length;
t.is (unique, 8);
});