Notes/src/services/password_encryption.js

41 lines
1.2 KiB
JavaScript
Raw Normal View History

const optionService = require('./options');
const myScryptService = require('./my_scrypt');
const utils = require('./utils');
const dataEncryptionService = require('./data_encryption');
2020-06-20 12:31:38 +02:00
function verifyPassword(password) {
const givenPasswordHash = utils.toBase64(myScryptService.getVerificationHash(password));
2022-01-12 19:32:23 +01:00
const dbPasswordHash = optionService.getOptionOrNull('passwordVerificationHash');
2022-01-12 19:32:23 +01:00
if (!dbPasswordHash) {
return false;
}
return givenPasswordHash === dbPasswordHash;
}
2020-06-20 12:31:38 +02:00
function setDataKey(password, plainTextDataKey) {
const passwordDerivedKey = myScryptService.getPasswordDerivedKey(password);
const newEncryptedDataKey = dataEncryptionService.encrypt(passwordDerivedKey, plainTextDataKey, 16);
2020-06-20 12:31:38 +02:00
optionService.setOption('encryptedDataKey', newEncryptedDataKey);
}
2020-06-20 12:31:38 +02:00
function getDataKey(password) {
const passwordDerivedKey = myScryptService.getPasswordDerivedKey(password);
2020-06-20 12:31:38 +02:00
const encryptedDataKey = optionService.getOption('encryptedDataKey');
const decryptedDataKey = dataEncryptionService.decrypt(passwordDerivedKey, encryptedDataKey, 16);
return decryptedDataKey;
}
module.exports = {
verifyPassword,
getDataKey,
setDataKey
2020-06-20 12:31:38 +02:00
};