117 lines
2.7 KiB
JavaScript
117 lines
2.7 KiB
JavaScript
|
/*
|
||
|
* 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>, May 2020
|
||
|
*/
|
||
|
|
||
|
// @ts-nocheck
|
||
|
'use strict';
|
||
|
|
||
|
const crypto = require ('../../index');
|
||
|
|
||
|
// eslint-disable-next-line max-lines-per-function
|
||
|
describe ('encryption', () => {
|
||
|
it ('encryption', () => {
|
||
|
const enc = crypto.encrypt_aes ('foo', 'bar');
|
||
|
expect (typeof enc)
|
||
|
.toEqual ('string');
|
||
|
});
|
||
|
|
||
|
it ('decryption', () => {
|
||
|
const enc = crypto.encrypt_aes ('foo', 'bar');
|
||
|
const dec = crypto.decrypt_aes (enc, 'bar');
|
||
|
expect (dec)
|
||
|
.toEqual ('foo');
|
||
|
});
|
||
|
|
||
|
it ('encryption 128', () => {
|
||
|
const enc = crypto.encrypt_aes (
|
||
|
'foo',
|
||
|
'bar',
|
||
|
crypto.encryption_mode_cbc_128
|
||
|
);
|
||
|
expect (typeof enc)
|
||
|
.toEqual ('string');
|
||
|
});
|
||
|
|
||
|
it ('decryption 128', () => {
|
||
|
const enc = crypto.encrypt_aes (
|
||
|
'foo',
|
||
|
'bar',
|
||
|
crypto.encryption_mode_cbc_128
|
||
|
);
|
||
|
const dec = crypto.decrypt_aes (
|
||
|
enc,
|
||
|
'bar',
|
||
|
crypto.encryption_mode_cbc_128
|
||
|
);
|
||
|
expect (dec)
|
||
|
.toEqual ('foo');
|
||
|
});
|
||
|
|
||
|
it ('encryption 256_quick', () => {
|
||
|
const enc = crypto.encrypt_aes (
|
||
|
'foo',
|
||
|
'bar',
|
||
|
crypto.encryption_mode_cbc_256_quick
|
||
|
);
|
||
|
expect (typeof enc)
|
||
|
.toEqual ('string');
|
||
|
});
|
||
|
|
||
|
it ('decryption 256_quick', () => {
|
||
|
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
|
||
|
);
|
||
|
expect (dec)
|
||
|
.toEqual ('foo');
|
||
|
});
|
||
|
|
||
|
it ('fail decryption', () => {
|
||
|
const enc = crypto.encrypt_aes ('foo', 'bar');
|
||
|
const dec = crypto.decrypt_aes (enc, 'baz');
|
||
|
expect (dec)
|
||
|
.toEqual (null);
|
||
|
});
|
||
|
|
||
|
it ('rethrow decryption', () => {
|
||
|
const enc = crypto.encrypt_aes ('foo', 'bar');
|
||
|
expect (() => {
|
||
|
crypto.decrypt_aes (
|
||
|
enc,
|
||
|
'baz',
|
||
|
crypto.encryption_mode_cbc_256,
|
||
|
true
|
||
|
);
|
||
|
})
|
||
|
.toThrowError (
|
||
|
// eslint-disable-next-line max-len
|
||
|
'error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt'
|
||
|
);
|
||
|
});
|
||
|
|
||
|
it ('unique crypto strings', () => {
|
||
|
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;
|
||
|
expect (unique)
|
||
|
.toEqual (8);
|
||
|
});
|
||
|
});
|