Skip to content

Commit

Permalink
core: allow using encryption key to decrypt backup
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodrr committed Mar 4, 2024
1 parent 6e135cd commit 68de3cc
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions packages/core/src/database/backup.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,10 @@ export default class Backup {
/**
*
* @param {any} backup the backup data
* @param {string} [password]
* @param {string} [key]
*/
async import(backup, password) {
async import(backup, password, key) {
if (!backup) return;

if (!this._validate(backup)) throw new Error("Invalid backup.");
Expand All @@ -188,12 +190,14 @@ export default class Backup {
let db = backup.data;
const isEncrypted = db.salt && db.iv && db.cipher;
if (backup.encrypted || isEncrypted) {
if (!password)
if (!password && !key)
throw new Error(
"Please provide a password to decrypt this backup & restore it."
"Please provide a password or an encryption key to decrypt this backup & restore it."
);

const key = await this._db.storage.generateCryptoKey(password, db.salt);
key = key
? { key, salt: db.salt }
: await this._db.storage.generateCryptoKey(password, db.salt);
if (!key)
throw new Error("Could not generate encryption key for backup.");

Expand Down

0 comments on commit 68de3cc

Please sign in to comment.