title: Is there a correct way to test whether a user-provided SQLCipher password is valid on Android?
body: <p>I'm using <a href="http://sqlcipher.net/sqlcipher-for-android/" rel="noreferrer">SQLCipher for Android</a> and am trying to determine the correct way to test whether a user-provided password is valid.</p>
  
  <p>My first inclination was to just try and open the database with the given password, using the SQLCipher implementation of <code>SQLiteOpenHelper.getReadableDatabase(password)</code>, and then catch the <code>SQLiteException</code> that pops out.</p>
  
  <p>This does work, but the problem is that because the Android API actually wraps the underlying C calls, it does a lot of the work for you - specifically, when you open a database using the Android API, it opens the database, runs the native C-level <code>sqlite3_key</code> method (with the provided password), then tries to set the locale on the database, whether or not the provided password was correct.</p>
  
  <p>At this point, the Android library tries to set the locale, and the underlying database throws the "encrypted or not a database" <code>SQLiteException</code>, which is caught and rethrown; but before it is, an unrelated error is written to the logs, essentially saying the locale cannot be set and the database is being closed (with an included stack trace). Because this is specifically written by the Android library, I cannot suppress it, leaving an ugly error in the logs that is actually unrelated to my original problem, which was simply that I passed in the wrong password.</p>
  
  <p>Because the Android library does not expose the C-level calls, I can't just use the method described in the SQLCipher API documentation regarding <a href="http://sqlcipher.net/sqlcipher-api/#key" rel="noreferrer">Testing the Key</a>, because I don't have access to directly open the database.</p>
  
  <p>I lean towards using the <code>SQLiteDatabaseHook</code>, but as best I can tell, that precludes my use of <code>SQLiteOpenHelper</code>, which does not appear to provide a way to set up a hook.</p>
  
  <p>Does anyone else know any better way to test whether an input passphrase properly decrypts a SQLCipher database through the SQLCipher Android API? I would completely expect to call a method and check for an exception being thrown - what I don't want is for the operation to try and perform extraneous processing (like set locale) on the database and write a completely insuppressible error to my logs.</p>