add rethrow option

This commit is contained in:
Timo Hocker 2020-03-05 10:24:27 +01:00
parent 44cfbc0b9b
commit fedc497b97
2 changed files with 12 additions and 2 deletions

View File

@ -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 = {

View File

@ -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'),