Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLCipher along with DBFlow

I have worked with DBFlow which is quite simple to play around database but i want to ask is there a good example to use SQLCipher with DBFlow

I have Folowed this link for help

Raizlabs/DBFlow

But if any one can provide some simple example will be very helpful to create secure database application with simple steps.

like image 367
farrukh Avatar asked Jun 21 '16 08:06

farrukh


1 Answers

The usage doc linked by ConductedClever doesn't look like much but this is indeed almost all you need. Except for some details which could be mentioned:

I had to add @aar to the dbflow-cipher dependency in build.gradle to get it past gradle in the first place:

// build.gradle
def dbflow_version = "3.1.1"
def sqlcipher_version = "3.5.4"

dependencies {
  annotationProcessor "com.github.Raizlabs.DBFlow:dbflow-processor:${dbflow_version}"
  compile "com.github.Raizlabs.DBFlow:dbflow-core:${dbflow_version}"
  compile "com.github.Raizlabs.DBFlow:dbflow:${dbflow_version}"
  compile "com.github.Raizlabs.DBFlow:dbflow-sqlcipher:${dbflow_version}@aar"
  compile "net.zetetic:android-database-sqlcipher:${sqlcipher_version}@aar"
}

Change the cipher secret as preferred:

// SQLCipherHelperImpl.java
public class SQLCipherHelperImpl extends SQLCipherOpenHelper {

  public SQLCipherHelperImpl(DatabaseDefinition databaseDefinition, DatabaseHelperListener listener) {
    super(databaseDefinition, listener);
  }

  @Override
  protected String getCipherSecret() {
    return "your-cipher-secret";
  }
}

If you followed the dbflow getting started guide and your database is called AppDatabase then this is the class you should pass to new DatabaseConfig.Builder(AppDatabase.class) when initializing DBFlow:

// AppDatabase.java
@Database(name = AppDatabase.NAME, version = AppDatabase.VERSION)
public class AppDatabase {

  public static final String NAME = "AppDatabase";

  public static final int VERSION = 1;
}

// DatabaseApplication.java
public class DatabaseApplication extends Application {
  @Override
  public void onCreate() {
    super.onCreate();
    FlowManager.init(new FlowConfig.Builder(this)
      .addDatabaseConfig(
        new DatabaseConfig.Builder(AppDatabase.class)
          .openHelper(new DatabaseConfig.OpenHelperCreator() {
            @Override
            public OpenHelper createHelper(DatabaseDefinition databaseDefinition, DatabaseHelperListener helperListener) {
              return new SQLCipherHelperImpl(databaseDefinition, helperListener);
            }
          })
          .build())
      .build());
  }
}

Export your database and try to open it in an SQLite Client. This should fail now cause of the encryption.

like image 66
Thomas Avatar answered Oct 18 '22 22:10

Thomas