MCPcopy Create free account
hub / github.com/chatmail/core / check_passphrase

Method check_passphrase

src/sql.rs:85–105  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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 {

Callers 2

test_check_passphraseFunction · 0.45

Calls 3

query_rowMethod · 0.80
is_openMethod · 0.45
is_emptyMethod · 0.45

Tested by 2

test_check_passphraseFunction · 0.36