crypto-helper/test/encryption.js

104 lines
2.3 KiB
JavaScript
Raw Normal View History

2020-03-25 17:02:59 +01:00
/*
* Copyright (C) Sapphirecode - All Rights Reserved
* This file is part of Crypto-Helper which is released under MIT.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, March 2020
*/
2020-03-04 15:35:04 +01:00
/* eslint-disable no-magic-numbers */
// @ts-nocheck
2020-03-04 14:51:22 +01:00
'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');
});
2020-03-09 08:04:16 +01:00
test ('encryption 128', (t) => {
const enc = crypto.encrypt_aes (
'foo',
'bar',
2020-03-09 10:12:40 +01:00
crypto.encryption_mode_cbc_128
2020-03-09 08:04:16 +01:00
);
t.is (typeof enc, 'string');
});
test ('decryption 128', (t) => {
const enc = crypto.encrypt_aes (
'foo',
'bar',
2020-03-09 10:12:40 +01:00
crypto.encryption_mode_cbc_128
2020-03-09 08:04:16 +01:00
);
const dec = crypto.decrypt_aes (
enc,
'bar',
2020-03-09 10:12:40 +01:00
crypto.encryption_mode_cbc_128
2020-03-09 08:04:16 +01:00
);
t.is (dec, 'foo');
});
2020-03-11 09:21:33 +01:00
test ('encryption 256_quick', (t) => {
const enc = crypto.encrypt_aes (
'foo',
'bar',
crypto.encryption_mode_cbc_256_quick
);
t.is (typeof enc, 'string');
});
test ('decryption 256_quick', (t) => {
const enc = crypto.encrypt_aes (
'foo',
'bar',
crypto.encryption_mode_cbc_256_quick
);
const dec = crypto.decrypt_aes (
enc,
'bar',
crypto.encryption_mode_cbc_256_quick
);
t.is (dec, 'foo');
});
2020-03-04 14:51:22 +01:00
test ('fail decryption', (t) => {
const enc = crypto.encrypt_aes ('foo', 'bar');
const dec = crypto.decrypt_aes (enc, 'baz');
t.is (dec, null);
});
2020-03-04 15:35:04 +01:00
2020-03-05 10:24:27 +01:00
test ('rethrow decryption', (t) => {
const enc = crypto.encrypt_aes ('foo', 'bar');
t.throws (() => {
2020-03-05 10:39:00 +01:00
crypto.decrypt_aes (
enc,
'baz',
crypto.encryption_mode_cbc_256,
true
);
2020-03-05 10:24:27 +01:00
});
});
2020-03-04 15:35:04 +01:00
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);
});