feat: no need for integrity check for encrypted backups

This commit is contained in:
thecodrr
2021-07-05 21:29:54 +05:00
parent 62de162ba2
commit 7473f63006

View File

@@ -39,22 +39,23 @@ export default class Backup {
);
let data = Object.fromEntries(await this._db.context.readMulti(keys));
let hash = {};
if (encrypt) {
const key = await this._db.user.getEncryptionKey();
data = await this._db.context.encrypt(key, JSON.stringify(data));
} else {
hash = { hash: SparkMD5.hash(JSON.stringify(data)), hash_type: "md5" };
}
// save backup time
await this._db.context.write("lastBackupTime", Date.now());
return JSON.stringify({
version: CURRENT_DATABASE_VERSION,
type,
date: Date.now(),
data,
hash: SparkMD5.hash(JSON.stringify(data)),
hash_type: "md5",
...hash,
});
}
@@ -71,15 +72,13 @@ export default class Backup {
backup = this._migrateBackup(backup);
if (!this._verify(backup))
throw new Error("Backup file has been tempered, aborting...");
let db = backup.data;
//check if we have encrypted data
if (db.salt && db.iv) {
if (!key) key = await this._db.user.getEncryptionKey();
backup.data = JSON.parse(await this._db.context.decrypt(key, db));
}
} else if (!this._verify(backup))
throw new Error("Backup file has been tempered, aborting...");
await this._migrateData(backup);
}