Compare commits

...

4 Commits

3 changed files with 34 additions and 5 deletions

View File

@ -165,7 +165,7 @@ function encrypt_aes (text, pass, mode = encryption_mode_cbc_256) {
const salt = crypto.randomBytes (mode.salt_size);
// eslint-disable-next-line no-sync
const key = crypto.pbkdf2Sync (
Buffer.from (pass, 'utf-8'),
Buffer.from (pass),
salt,
mode.iterations,
mode.key_size,
@ -203,7 +203,7 @@ function decrypt_aes (
buf = buf.slice (mode.salt_size);
// eslint-disable-next-line no-sync
const key = crypto.pbkdf2Sync (
Buffer.from (pass, 'utf-8'),
Buffer.from (pass),
salt,
mode.iterations,
mode.key_size,
@ -216,7 +216,7 @@ function decrypt_aes (
cipher.update (buf),
cipher.final ()
])
.toString ('utf-8');
.toString ();
}
catch (e) {
if (rethrow)

View File

@ -16,6 +16,29 @@ test ('decryption', (t) => {
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');

View File

@ -23,7 +23,10 @@ test ('random_hex with default length', (t) => {
});
test ('random_hex should refuse lenght smaller 1', (t) => {
t.throws (() => (crypto.random_hex (0)));
const error = t.throws (
() => (crypto.random_hex (0))
);
t.is(error.message, 'invalid length');
});
test ('random_string', (t) => {
@ -37,7 +40,10 @@ test ('random_string with default length', (t) => {
});
test ('random_string should refuse lenght smaller 1', (t) => {
t.throws (() => (crypto.random_string (0)));
const error = t.throws (
() => (crypto.random_string (0))
);
t.is(error.message, 'invalid length');
});
test ('hash_sha512', (t) => {