| 
									
										
										
										
											2024-08-10 18:23:49 +02:00
										 |  |  | import crypto from 'crypto'; | 
					
						
							|  |  |  | import sql from './sql.js'; | 
					
						
							|  |  |  | import decryptService from './decrypt.js'; | 
					
						
							| 
									
										
										
										
											2022-02-10 23:37:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-10 18:23:49 +02:00
										 |  |  | function getDataKey(password: any) { | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-08-10 18:23:49 +02:00
										 |  |  |     catch (e: any) { | 
					
						
							| 
									
										
										
										
											2022-02-12 22:20:15 +01:00
										 |  |  |         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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-10 18:23:49 +02:00
										 |  |  | function getPasswordDerivedKey(password: any) { | 
					
						
							| 
									
										
										
										
											2022-02-10 23:37:25 +01:00
										 |  |  |     const salt = getOption('passwordDerivedKeySalt'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return getScryptHash(password, salt); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-10 18:23:49 +02:00
										 |  |  | function getScryptHash(password: any, salt: any) { | 
					
						
							| 
									
										
										
										
											2022-02-10 23:37:25 +01:00
										 |  |  |     const hashed = crypto.scryptSync(password, salt, 32, | 
					
						
							| 
									
										
										
										
											2024-08-10 18:23:49 +02:00
										 |  |  |         { N: 16384, r: 8, p: 1 }); | 
					
						
							| 
									
										
										
										
											2022-02-10 23:37:25 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return hashed; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-10 18:23:49 +02:00
										 |  |  | function getOption(name: string) { | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-10 18:23:49 +02:00
										 |  |  | export default { | 
					
						
							| 
									
										
										
										
											2022-02-10 23:37:25 +01:00
										 |  |  |     getDataKey | 
					
						
							|  |  |  | }; |