Compare commits
4 Commits
6fd21e4e36
...
e470b68ea5
Author | SHA1 | Date | |
---|---|---|---|
e470b68ea5 | |||
59476a8087 | |||
c603fccd6f | |||
91f7d66dbb |
6
index.js
6
index.js
@ -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)
|
||||
|
@ -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');
|
||||
|
@ -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) => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user