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} ciphertext encrypted text
* @param {string} pass password * @param {string} pass password
* @param {boolean} rethrow rethrow exceptions instead of returning null
* @returns {string} plaintext * @returns {string} plaintext
*/ */
function decrypt_aes (ciphertext, pass) { function decrypt_aes (ciphertext, pass, rethrow = false) {
try { try {
let buf = Buffer.from (ciphertext, 'base64'); let buf = Buffer.from (ciphertext, 'base64');
const salt = buf.slice (0, encryption.salt_size); const salt = buf.slice (0, encryption.salt_size);
@ -202,8 +203,10 @@ function decrypt_aes (ciphertext, pass) {
.toString ('utf-8'); .toString ('utf-8');
} }
catch (e) { catch (e) {
return null; if (rethrow)
throw e;
} }
return null;
} }
module.exports = { module.exports = {

View File

@ -22,6 +22,13 @@ test ('fail decryption', (t) => {
t.is (dec, null); 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) => { test ('unique crypto strings', (t) => {
const enc = [ const enc = [
crypto.encrypt_aes ('foo', 'bar'), crypto.encrypt_aes ('foo', 'bar'),