Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,26 @@ public static boolean existsColumnInTable(SQLiteDatabase inDatabase, String inTa

public static boolean isDatabaseEncrypted(Context context, String dbName) {
String appId = MobiComKitClientService.getApplicationKey(AppContextService.getContext(context));

// 💡 DEFENSIVE CHECK: Prevent crash if appId is null
if (appId == null || appId.isEmpty()) {
Log.e("DatabaseCheck", "Application ID is missing. Kommunicate SDK may not be initialized.");
return false;
}

File dbFile = context.getDatabasePath(dbName);
if (!dbFile.exists()) {
return false; // Database file doesn't exist, so it's not encrypted.
}

// Attempt to open the database with the given password
SQLiteDatabase db = null;
try {
// Corrected method call with byte array for password
// Now it's safe to call getBytes()
db = SQLiteDatabase.openDatabase(dbFile.getPath(), appId.getBytes(), null, SQLiteDatabase.OPEN_READONLY, null, null);
db.close();
return true;
} catch (SQLiteException e) { // Updated exception package
// This exception is thrown if the password is wrong or the DB is not encrypted,
// which for this check, means it's not encrypted with the given key.
return true; // Successfully opened with the key, so it's encrypted.
} catch (SQLiteException e) {
// This is expected if the password (appId) is wrong or DB isn't encrypted.
return false;
} finally {
if (db != null && db.isOpen()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ object DatabaseMigrationHelper {
val password =
MobiComKitClientService.getApplicationKey(AppContextService.getContext(context))

// Check added to prevent crash in case of null or empty application key
if (password.isNullOrEmpty()) {
System.err.println("Migration failed: Application Key is missing. Is the SDK initialized?")
return
}

// Load SQLCipher libraries
System.loadLibrary("sqlcipher")

Expand Down
Loading