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} 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 = { | ||||||
|   | |||||||
| @@ -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'), | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user