mirror of
https://github.com/zadam/trilium.git
synced 2025-11-04 13:39:01 +01:00
41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
import optionService from "../options.js";
|
|
import myScryptService from "./my_scrypt.js";
|
|
import { toBase64 } from "../utils.js";
|
|
import dataEncryptionService from "./data_encryption.js";
|
|
|
|
function verifyPassword(password: string) {
|
|
const givenPasswordHash = toBase64(myScryptService.getVerificationHash(password));
|
|
|
|
const dbPasswordHash = optionService.getOptionOrNull("passwordVerificationHash");
|
|
|
|
if (!dbPasswordHash) {
|
|
return false;
|
|
}
|
|
|
|
return givenPasswordHash === dbPasswordHash;
|
|
}
|
|
|
|
function setDataKey(password: string, plainTextDataKey: string | Buffer) {
|
|
const passwordDerivedKey = myScryptService.getPasswordDerivedKey(password);
|
|
|
|
const newEncryptedDataKey = dataEncryptionService.encrypt(passwordDerivedKey, plainTextDataKey);
|
|
|
|
optionService.setOption("encryptedDataKey", newEncryptedDataKey);
|
|
}
|
|
|
|
function getDataKey(password: string) {
|
|
const passwordDerivedKey = myScryptService.getPasswordDerivedKey(password);
|
|
|
|
const encryptedDataKey = optionService.getOption("encryptedDataKey");
|
|
|
|
const decryptedDataKey = dataEncryptionService.decrypt(passwordDerivedKey, encryptedDataKey);
|
|
|
|
return decryptedDataKey;
|
|
}
|
|
|
|
export default {
|
|
verifyPassword,
|
|
getDataKey,
|
|
setDataKey
|
|
};
|