2018-04-01 21:27:46 -04:00
|
|
|
const optionService = require('./options');
|
|
|
|
const myScryptService = require('./my_scrypt');
|
2017-11-09 23:25:23 -05:00
|
|
|
const utils = require('./utils');
|
2018-04-01 21:27:46 -04:00
|
|
|
const dataEncryptionService = require('./data_encryption');
|
2017-11-09 23:25:23 -05:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function verifyPassword(password) {
|
|
|
|
const givenPasswordHash = utils.toBase64(myScryptService.getVerificationHash(password));
|
2017-11-09 23:25:23 -05:00
|
|
|
|
2022-01-12 19:32:23 +01:00
|
|
|
const dbPasswordHash = optionService.getOptionOrNull('passwordVerificationHash');
|
2017-11-09 23:25:23 -05:00
|
|
|
|
2022-01-12 19:32:23 +01:00
|
|
|
if (!dbPasswordHash) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-09 23:25:23 -05:00
|
|
|
return givenPasswordHash === dbPasswordHash;
|
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function setDataKey(password, plainTextDataKey) {
|
|
|
|
const passwordDerivedKey = myScryptService.getPasswordDerivedKey(password);
|
2017-11-15 23:39:50 -05:00
|
|
|
|
2019-01-13 00:24:51 +01:00
|
|
|
const newEncryptedDataKey = dataEncryptionService.encrypt(passwordDerivedKey, plainTextDataKey, 16);
|
2017-11-15 23:39:50 -05:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
optionService.setOption('encryptedDataKey', newEncryptedDataKey);
|
2017-11-15 23:39:50 -05:00
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function getDataKey(password) {
|
|
|
|
const passwordDerivedKey = myScryptService.getPasswordDerivedKey(password);
|
2017-11-15 23:39:50 -05:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
const encryptedDataKey = optionService.getOption('encryptedDataKey');
|
2017-11-15 23:39:50 -05:00
|
|
|
|
2019-01-11 23:04:51 +01:00
|
|
|
const decryptedDataKey = dataEncryptionService.decrypt(passwordDerivedKey, encryptedDataKey, 16);
|
2017-11-15 23:39:50 -05:00
|
|
|
|
|
|
|
return decryptedDataKey;
|
|
|
|
}
|
|
|
|
|
2017-11-09 23:25:23 -05:00
|
|
|
module.exports = {
|
|
|
|
verifyPassword,
|
2017-11-18 12:53:17 -05:00
|
|
|
getDataKey,
|
|
|
|
setDataKey
|
2020-06-20 12:31:38 +02:00
|
|
|
};
|