| 
									
										
										
										
											2022-02-12 22:20:15 +01:00
										 |  |  | const crypto = require("crypto"); | 
					
						
							| 
									
										
										
										
											2024-02-16 22:44:12 +02:00
										 |  |  | const sql = require('./sql'); | 
					
						
							| 
									
										
										
										
											2023-11-22 19:34:48 +01:00
										 |  |  | const decryptService = require('./decrypt.js'); | 
					
						
							| 
									
										
										
										
											2022-02-10 23:37:25 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | function getDataKey(password) { | 
					
						
							| 
									
										
										
										
											2022-02-12 22:20:15 +01:00
										 |  |  |     if (!password) { | 
					
						
							|  |  |  |         return null; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-02-10 23:37:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-12 22:20:15 +01:00
										 |  |  |     try { | 
					
						
							|  |  |  |         const passwordDerivedKey = getPasswordDerivedKey(password); | 
					
						
							| 
									
										
										
										
											2022-02-10 23:37:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-12 22:20:15 +01:00
										 |  |  |         const encryptedDataKey = getOption('encryptedDataKey'); | 
					
						
							| 
									
										
										
										
											2022-02-10 23:37:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-12 22:20:15 +01:00
										 |  |  |         const decryptedDataKey = decryptService.decrypt(passwordDerivedKey, encryptedDataKey, 16); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return decryptedDataKey; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     catch (e) { | 
					
						
							|  |  |  |         throw new Error(`Cannot read data key, the entered password might be wrong. The underlying error: '${e.message}', stack:\n${e.stack}`); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-02-10 23:37:25 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function getPasswordDerivedKey(password) { | 
					
						
							|  |  |  |     const salt = getOption('passwordDerivedKeySalt'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return getScryptHash(password, salt); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function getScryptHash(password, salt) { | 
					
						
							|  |  |  |     const hashed = crypto.scryptSync(password, salt, 32, | 
					
						
							|  |  |  |         {N: 16384, r:8, p:1}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return hashed; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function getOption(name) { | 
					
						
							| 
									
										
										
										
											2022-02-12 22:20:15 +01:00
										 |  |  |     return sql.getValue("SELECT value FROM options WHERE name = ?", [name]); | 
					
						
							| 
									
										
										
										
											2022-02-10 23:37:25 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  |     getDataKey | 
					
						
							|  |  |  | }; |