add rethrow option
This commit is contained in:
		
							
								
								
									
										7
									
								
								index.js
									
									
									
									
									
								
							
							
						
						
									
										7
									
								
								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 = { | ||||
|   | ||||
| @@ -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'), | ||||
|   | ||||
		Reference in New Issue
	
	Block a user