Skip to content

Commit

Permalink
feat: migrate from sqlcipher legacy to latest version (#419)
Browse files Browse the repository at this point in the history
feat: migrate from sqlcipher legacy to latest version
feat: update sqlite version
  • Loading branch information
itsdebs authored May 20, 2024
1 parent eaa282c commit a739a78
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
4 changes: 2 additions & 2 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ dependencies {
compileOnly 'androidx.lifecycle:lifecycle-common:2.6.1'

//sql-cipher
compileOnly "net.zetetic:android-database-sqlcipher:4.5.4"
compileOnly "androidx.sqlite:sqlite:2.3.1"
compileOnly "net.zetetic:sqlcipher-android:4.5.6@aar"
compileOnly "androidx.sqlite:sqlite:2.4.0"

//test
testImplementation('com.android.support.test:rules:1.0.2')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.rudderstack.android.sdk.core.persistence;

import android.app.Application;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;

import androidx.annotation.NonNull;
Expand All @@ -9,8 +10,8 @@
import com.rudderstack.android.sdk.core.ReportManager;
import com.rudderstack.android.sdk.core.RudderLogger;

import net.sqlcipher.Cursor;
import net.sqlcipher.database.SQLiteDatabase;

import net.zetetic.database.sqlcipher.SQLiteDatabase;

import java.io.File;
import java.util.Collections;
Expand All @@ -21,7 +22,6 @@ public class DefaultPersistenceProvider implements PersistenceProvider {
private final Application application;
private final ProviderParams params;


DefaultPersistenceProvider(Application application, ProviderParams params) {
this.application = application;
this.params = params;
Expand Down Expand Up @@ -88,7 +88,7 @@ private EncryptedPersistence createEncryptedObject(@Nullable Persistence.DbCreat

private boolean checkIfEncryptionIsValid(File encryptedDbPath) {
try (SQLiteDatabase database = SQLiteDatabase.openDatabase(encryptedDbPath.getAbsolutePath(),
params.encryptionKey, null, SQLiteDatabase.OPEN_READWRITE)) {
params.encryptionKey, null, SQLiteDatabase.OPEN_READWRITE, null)) {
Cursor cursor = database.rawQuery("PRAGMA cipher_version", null);
cursor.close();
return true;
Expand Down Expand Up @@ -118,12 +118,12 @@ private DefaultPersistence getDefaultPersistence(@Nullable Persistence.DbCreateL

private void createDefaultDatabase() {
File databasePath = application.getDatabasePath(params.dbName);
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(databasePath.getAbsolutePath(), "", null);
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(databasePath.getAbsolutePath(), "",null, null);
database.close();
}

private void initCipheredDatabase() {
SQLiteDatabase.loadLibs(application);
System.loadLibrary("sqlcipher");
}

private void deleteEncryptedDb() {
Expand All @@ -140,7 +140,7 @@ private void migrateToDefaultDatabase(File databasePath) {

File encryptedDb = application.getDatabasePath(params.encryptedDbName);
String encryptedPath = encryptedDb.getAbsolutePath();
SQLiteDatabase database = SQLiteDatabase.openDatabase(encryptedPath, params.encryptionKey, null, SQLiteDatabase.OPEN_READWRITE);
SQLiteDatabase database = SQLiteDatabase.openDatabase(encryptedPath, params.encryptionKey, null, SQLiteDatabase.OPEN_READWRITE, null);
//will throw exception if encryption key is invalid
database.isDatabaseIntegrityOk();
database.rawExecSQL(String.format("ATTACH DATABASE '%s' AS rl_persistence KEY ''",
Expand All @@ -163,11 +163,11 @@ private void migrateToEncryptedDatabase(File encryptedDbPath) {
ReportManager.LABEL_TYPE, ReportManager.LABEL_TYPE_MIGRATE_TO_ENCRYPT
));

SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(encryptedDbPath.getAbsolutePath(), params.encryptionKey, null);
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(encryptedDbPath.getAbsolutePath(), params.encryptionKey,null, null);
database.close();
File decryptedDb = application.getDatabasePath(params.dbName);
String decryptedPath = decryptedDb.getAbsolutePath();
database = SQLiteDatabase.openDatabase(decryptedPath, "", null, SQLiteDatabase.OPEN_READWRITE);
database = SQLiteDatabase.openDatabase(decryptedPath, "", null, SQLiteDatabase.OPEN_READWRITE, null);
database.rawExecSQL(String.format("ATTACH DATABASE '%s' AS rl_persistence_encrypted KEY '%s'",
encryptedDbPath.getAbsolutePath(), params.encryptionKey));
database.rawExecSQL("select sqlcipher_export('rl_persistence_encrypted')");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

import com.rudderstack.android.sdk.core.RudderLogger;

import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteOpenHelper;
import net.zetetic.database.sqlcipher.SQLiteDatabase;
import net.zetetic.database.sqlcipher.SQLiteOpenHelper;

import java.io.File;
import java.util.List;
Expand All @@ -29,21 +29,21 @@
public class EncryptedPersistence extends SQLiteOpenHelper implements Persistence {
private final List<DbCloseListener> dbCloseListeners = new java.util.concurrent.CopyOnWriteArrayList<>();
private final DbCreateListener dbCreateListener;
private final String encryptPassword;
private SQLiteDatabase initialDatabase = null;


EncryptedPersistence(Application application, DbParams params, @Nullable DbCreateListener dbCreateListener) {
super(application, params.dbName, null, params.dbVersion);
this.encryptPassword = params.encryptPassword;
super(application, params.dbName, params.encryptPassword,null, params.dbVersion,
0, null, null, false);
this.dbCreateListener = dbCreateListener;
}

private SQLiteDatabase getWritableDatabase() {
@NonNull
public SQLiteDatabase getWritableDatabase() {
if (initialDatabase != null) {
return initialDatabase;
}
return getWritableDatabase(encryptPassword);
return super.getWritableDatabase();
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion sample-kotlin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ dependencies {
implementation "androidx.work:work-runtime:2.7.1"

//sql-cipher
implementation "net.zetetic:android-database-sqlcipher:4.5.4"
implementation "net.zetetic:sqlcipher-android:4.5.6@aar"
implementation "androidx.sqlite:sqlite:2.3.1"

// required for new life cycle methods
Expand Down

0 comments on commit a739a78

Please sign in to comment.