SQLCipher for Android getReadableDatabase () Overherad

I changed my DatabaseHelper class to use the SQLCipher library.

For this I:

  • I copied the assets to my resources folder and libraries (armeabi, x86, commons-codec, guava-r09, sqlcipher) into my libs folder.

  • Changed the import in my class DatabaseHelperto point to import net.sqlcipher.database.*.

  • Call SQLiteDatabase.loadLibs(getApplicationContext());when the application starts.

  • Changed the lines in which I call getReadableDatabase()and getWriteableDatabase()to include the passphrase as a parameter;

Everything works fine, as the data is read / written correctly. My performance problem is because my application can perform database operations at a certain frequency, causing it to become slow (after switching to SQLCipher).

For my DatabaseHelper methods, I believe that I follow standard approaches, for example:

/*
 * Getting all MyObjects
 */
public List<MyObject> getMyObjects() {

    List<MyObject> objects = new ArrayList<MyObject>();

    String selectQuery = "SELECT * FROM " + TABLE_NAME;
    Log.v(LOG, selectQuery);

    // Open
    SQLiteDatabase db = this.getReadableDatabase("...the password...");  
    // I know this passphrase can be figured out by decompiling.

    // Cursor with query
    Cursor c = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (c.moveToFirst()) {
        do {
            MyObject object = createMyObjectFromCursor(c); // Method that builds MyObject from Cursor data
            // adding to list
            objects.add(object);
        } while (c.moveToNext());
    }

    c.close();
    db.close();
    return objects;
}

I am not completely familiar with the internal mechanics of SQLCipher (for example, does it decrypt the entire DB file when called getReadableDatabase()?), But when debugging it seems that the overhead is in getReadableDatabase(password)and getWritableDatabase(password)that makes sense if my assumption above is true.

Would moving these calls to the DatabaseHelper.open () and DatabaseHelper.close () methods, which would be called by the actions whenever they instantiate the DatabaseHelper instead of calling them by each individual method, be bad practice? Share your knowledge on how to solve this problem.

EDIT:

DDMS , , SQLiteOpenHelper.getReadableDatabase() ( ~ 4 ). , , , , .

, , :

SQLiteDatabase.OpenOrCreateDatabaseSqLiteDatabase.openDatabaseSqLiteDatabase.openDatabaseSQLiteDatabase.setLocale

, SQLiteDatabase.setLocale(java.util.Locale) , ~ 4 , getReadableDatabase(). SQLiteDatabase, , native_setLocale(locale.toString(), mFlags) (4 . ) .

, ?

+4
2

, , , SQLCipher. SQLCipher , PBKDF2 (.. SHA1) ( http://sqlcipher.net/design). , setLocale, .

, , . , . , .

, - . SQLCipher PBKDF2 . , . , , . , , KDF:

http://sqlcipher.net/sqlcipher-api/#kdf_iter

+3

getReadableDatabase()?

. (4KB??) , .

, getReadableDatabase (password) getWritableDatabase (password), , .

. , , - .

, , , , .

, .

Traceview, , .

, , - SQLite SQLCipher - . - , .

, SQLCipher Android , . , , , , ; SQLCipher . ( FTS3), .

+2

Source: https://habr.com/ru/post/1530054/


All Articles