Tests SQLCipher passphrase. Returns true if passphrase is correct, i.e. the database is new or can be unlocked with this passphrase, and false if the database is already encrypted with another passphrase or corrupted. Fails if database is already open.
(&self, passphrase: String)
| 83 | /// |
| 84 | /// Fails if database is already open. |
| 85 | pub async fn check_passphrase(&self, passphrase: String) -> Result<bool> { |
| 86 | if self.is_open().await { |
| 87 | bail!("Database is already opened."); |
| 88 | } |
| 89 | |
| 90 | // Hold the lock to prevent other thread from opening the database. |
| 91 | let _lock = self.pool.write().await; |
| 92 | |
| 93 | // Test that the key is correct using a single connection. |
| 94 | let connection = Connection::open(&self.dbfile)?; |
| 95 | if !passphrase.is_empty() { |
| 96 | connection |
| 97 | .pragma_update(None, "key", &passphrase) |
| 98 | .context("Failed to set PRAGMA key")?; |
| 99 | } |
| 100 | let key_is_correct = connection |
| 101 | .query_row("SELECT count(*) FROM sqlite_master", [], |_row| Ok(())) |
| 102 | .is_ok(); |
| 103 | |
| 104 | Ok(key_is_correct) |
| 105 | } |
| 106 | |
| 107 | /// Checks if there is currently a connection to the underlying Sqlite database. |
| 108 | pub async fn is_open(&self) -> bool { |