Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(android): automatically migrate to new cipher #40

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 4 additions & 0 deletions android/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="appcelerator.encrypteddatabase" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="14" />
</manifest>
23 changes: 23 additions & 0 deletions android/encrypteddatabase.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="android" name="Android">
<configuration>
<option name="PROJECT_TYPE" value="1" />
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/gen" isTestSource="false" generated="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="sqlcipher" level="project" />
<orderEntry type="module" module-name="common" scope="PROVIDED" />
<orderEntry type="module" module-name="kroll-apt" scope="PROVIDED" />
<orderEntry type="module" module-name="titanium" scope="PROVIDED" />
</component>
</module>
2 changes: 1 addition & 1 deletion android/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# this is your module manifest and used by Titanium
# during compilation, packaging, distribution, etc.
#
version: 3.0.4
version: 3.0.5
apiversion: 4
architectures: arm64-v8a armeabi-v7a x86
description: Provides transparent, secure 256-bit AES encryption of SQLite database files.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
import org.appcelerator.titanium.TiFileProxy;
import org.appcelerator.titanium.io.TiBaseFile;
import org.appcelerator.titanium.io.TiFileFactory;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiUrl;

import android.app.Activity;
import android.content.Context;

import net.sqlcipher.SQLException;
import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteDatabaseHook;

@Kroll.module(name = "Encrypteddatabase", id = "appcelerator.encrypteddatabase")
public class EncrypteddatabaseModule extends KrollModule {
Expand Down Expand Up @@ -64,6 +64,15 @@ public void setPassword(String value) {
public TiDatabaseProxy open(Object file) {
// Attempt to create/open the given database file/name.
TiDatabaseProxy dbp = null;

// Migrate database if necessary.
final SQLiteDatabaseHook migrationHook = new SQLiteDatabaseHook() {
public void preKey(SQLiteDatabase database) {}
public void postKey(SQLiteDatabase database) {
database.rawExecSQL("PRAGMA cipher_migrate;");
}
};

if (file instanceof TiFileProxy) {
// File support is read-only for now. The NO_LOCALIZED_COLLATORS
// flag means the database doesn't have Android metadata (i.e.
Expand All @@ -73,7 +82,7 @@ public TiDatabaseProxy open(Object file) {
Log.d(TAG, "Opening database from filesystem: " + absolutePath);

SQLiteDatabase db = SQLiteDatabase.openDatabase(absolutePath, getPassword(), null,
SQLiteDatabase.OPEN_READONLY | SQLiteDatabase.NO_LOCALIZED_COLLATORS);
SQLiteDatabase.OPEN_READONLY | SQLiteDatabase.NO_LOCALIZED_COLLATORS, migrationHook);
if (db != null) {
dbp = new TiDatabaseProxy(db);
} else {
Expand All @@ -83,7 +92,7 @@ public TiDatabaseProxy open(Object file) {
String name = (String) file;
File dbPath = TiApplication.getInstance().getDatabasePath(name);
dbPath.getParentFile().mkdirs();
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbPath, getPassword(), null);
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbPath, getPassword(), null, migrationHook);
if (db != null) {
dbp = new TiDatabaseProxy(name, db);
} else {
Expand Down