Skip to content

Store database key in a file

akwizgran requested to merge 1219-store-db-key-in-file into master

This branch moves the encrypted database key from shared preferences to a file, to avoid problems with vanishing shared preferences.

There are two files, a primary and a backup. When loading the key, we first try to find it in shared preferences. If it's found, we store it in a file and then remove it from shared preferences before returning it. (This will happen before signing in, the first time the user launches Briar after upgrading.) If the key's not in shared preferences we try the primary file and finally the backup file before concluding that no account exists.

When storing the key, we first check whether the backup file exists but the primary doesn't. If so, we rename the backup to the primary. Then we write the key to the backup file, delete the primary, and rename the backup to the primary.

This sequence ensures that once the key has been stored for the first time, we always have a valid copy, even if a crash occurs partway through a write. If the primary file exists then it contains the valid copy; if the primary doesn't exist then the backup contains the valid copy.

Closes #1219 (closed)

Merge request reports