diff --git a/index.js b/index.js index 45b9a3d..5ff6a4d 100644 --- a/index.js +++ b/index.js @@ -177,9 +177,10 @@ function encrypt_aes (text, pass) { * * @param {string} ciphertext encrypted text * @param {string} pass password + * @param {boolean} rethrow rethrow exceptions instead of returning null * @returns {string} plaintext */ -function decrypt_aes (ciphertext, pass) { +function decrypt_aes (ciphertext, pass, rethrow = false) { try { let buf = Buffer.from (ciphertext, 'base64'); const salt = buf.slice (0, encryption.salt_size); @@ -202,8 +203,10 @@ function decrypt_aes (ciphertext, pass) { .toString ('utf-8'); } catch (e) { - return null; + if (rethrow) + throw e; } + return null; } module.exports = { diff --git a/test/encryption.js b/test/encryption.js index 33edb03..af0e2ad 100644 --- a/test/encryption.js +++ b/test/encryption.js @@ -22,6 +22,13 @@ test ('fail decryption', (t) => { t.is (dec, null); }); +test ('rethrow decryption', (t) => { + const enc = crypto.encrypt_aes ('foo', 'bar'); + t.throws (() => { + const dec = crypto.decrypt_aes (enc, 'baz', true); + }); +}); + test ('unique crypto strings', (t) => { const enc = [ crypto.encrypt_aes ('foo', 'bar'),