2018-04-02 09:27:46 +08:00
|
|
|
const optionService = require('./options');
|
|
|
|
const myScryptService = require('./my_scrypt');
|
2017-11-10 12:25:23 +08:00
|
|
|
const utils = require('./utils');
|
2018-04-02 09:27:46 +08:00
|
|
|
const dataEncryptionService = require('./data_encryption');
|
2017-11-10 12:25:23 +08:00
|
|
|
|
|
|
|
async function verifyPassword(password) {
|
2018-04-02 09:27:46 +08:00
|
|
|
const givenPasswordHash = utils.toBase64(await myScryptService.getVerificationHash(password));
|
2017-11-10 12:25:23 +08:00
|
|
|
|
2018-04-03 09:47:46 +08:00
|
|
|
const dbPasswordHash = await optionService.getOption('passwordVerificationHash');
|
2017-11-10 12:25:23 +08:00
|
|
|
|
|
|
|
return givenPasswordHash === dbPasswordHash;
|
|
|
|
}
|
|
|
|
|
2017-11-29 06:24:08 +08:00
|
|
|
async function setDataKey(password, plainTextDataKey) {
|
2018-04-02 09:27:46 +08:00
|
|
|
const passwordDerivedKey = await myScryptService.getPasswordDerivedKey(password);
|
2017-11-16 12:39:50 +08:00
|
|
|
|
2019-01-13 07:24:51 +08:00
|
|
|
const newEncryptedDataKey = dataEncryptionService.encrypt(passwordDerivedKey, plainTextDataKey, 16);
|
2017-11-16 12:39:50 +08:00
|
|
|
|
2018-04-03 09:47:46 +08:00
|
|
|
await optionService.setOption('encryptedDataKey', newEncryptedDataKey);
|
2017-11-16 12:39:50 +08:00
|
|
|
}
|
|
|
|
|
2017-11-19 01:53:17 +08:00
|
|
|
async function getDataKey(password) {
|
2018-04-02 09:27:46 +08:00
|
|
|
const passwordDerivedKey = await myScryptService.getPasswordDerivedKey(password);
|
2017-11-16 12:39:50 +08:00
|
|
|
|
2018-04-03 09:47:46 +08:00
|
|
|
const encryptedDataKey = await optionService.getOption('encryptedDataKey');
|
2017-11-16 12:39:50 +08:00
|
|
|
|
2019-01-12 06:04:51 +08:00
|
|
|
const decryptedDataKey = dataEncryptionService.decrypt(passwordDerivedKey, encryptedDataKey, 16);
|
2017-11-16 12:39:50 +08:00
|
|
|
|
|
|
|
return decryptedDataKey;
|
|
|
|
}
|
|
|
|
|
2017-11-10 12:25:23 +08:00
|
|
|
module.exports = {
|
|
|
|
verifyPassword,
|
2017-11-19 01:53:17 +08:00
|
|
|
getDataKey,
|
|
|
|
setDataKey
|
2017-11-10 12:25:23 +08:00
|
|
|
};
|