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

(android) Migrate from android-database-sqlcipher to sqlcipher-android #607

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ dependencies {
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
implementation "androidx.coordinatorlayout:coordinatorlayout:1.2.0"
implementation 'net.zetetic:android-database-sqlcipher:4.5.3'
implementation 'net.zetetic:sqlcipher-android:4.6.1@aar'
implementation "androidx.sqlite:sqlite:2.4.0"
//security library
implementation "androidx.security:security-crypto:1.1.0-alpha06"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.sqlcipher.Cursor;
import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteException;
import net.zetetic.database.sqlcipher.SQLiteCursor;
import net.zetetic.database.sqlcipher.SQLiteDatabase;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -115,7 +114,7 @@ public Database(
* Initialize the SQLCipher Libraries
*/
private void InitializeSQLCipher() {
SQLiteDatabase.loadLibs(_context);
System.loadLibrary("sqlcipher");
}

public SupportSQLiteDatabase getDb() {
Expand Down Expand Up @@ -247,7 +246,7 @@ public void open() throws Exception {
if (_mode.equals("encryption")) {
if (_isEncryption) {
try {
_uCipher.encrypt(_context, _file, SQLiteDatabase.getBytes(password.toCharArray()));
_uCipher.encrypt(_context, _file, password.getBytes());
} catch (Exception e) {
String msg = "Failed in encryption " + e.getMessage();
Log.v(TAG, msg);
Expand All @@ -260,7 +259,7 @@ public void open() throws Exception {
if (_mode.equals("decryption")) {
if (_isEncryption) {
try {
_uCipher.decrypt(_context, _file, SQLiteDatabase.getBytes(password.toCharArray()));
_uCipher.decrypt(_context, _file, password.getBytes());
password = "";
} catch (Exception e) {
String msg = "Failed in decryption " + e.getMessage();
Expand All @@ -273,9 +272,9 @@ public void open() throws Exception {
}
try {
if (!isNCDB() && !this._readOnly) {
_db = SQLiteDatabase.openOrCreateDatabase(_file, password, null);
_db = SQLiteDatabase.openOrCreateDatabase(_file, password, null, null);
} else {
_db = SQLiteDatabase.openDatabase(String.valueOf(_file), password, null, SQLiteDatabase.OPEN_READONLY);
_db = SQLiteDatabase.openDatabase(String.valueOf(_file), password, null, SQLiteDatabase.OPEN_READONLY, null);
}
if (_db != null) {
if (_db.isOpen()) {
Expand All @@ -301,12 +300,6 @@ public void open() throws Exception {
close();
_db = null;
throw new Exception(msg);
} catch (SQLiteException e) {
String msg = "Failed in setVersion " + e.getMessage();
Log.v(TAG, msg);
close();
_db = null;
throw new Exception(msg);
}
if (_version > curVersion && _vUpgObject != null && _vUpgObject.size() > 0) {
// if (_vUpgObject != null && _vUpgObject.size() > 0) {
Expand Down Expand Up @@ -973,12 +966,12 @@ public String deleteSQL(Database mDB, String statement, ArrayList<Object> values
*/
public JSArray selectSQL(String statement, ArrayList<Object> values) throws Exception {
JSArray retArray = new JSArray();
Cursor c = null;
SQLiteCursor c = null;
if (_db == null) {
return retArray;
}
try {
c = (Cursor) _db.query(statement, values.toArray(new Object[0]));
c = (SQLiteCursor) _db.query(statement, values.toArray(new Object[0]));
while (c.moveToNext()) {
JSObject row = new JSObject();
for (int i = 0; i < c.getColumnCount(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.List;
import net.sqlcipher.Cursor;
import net.zetetic.database.sqlcipher.SQLiteCursor;

public class UtilsDrop {

Expand All @@ -20,15 +20,15 @@ public class UtilsDrop {

public List<String> getTablesNames(Database db) throws Exception {
List<String> tables = new ArrayList<String>();
Cursor cursor = null;
SQLiteCursor cursor = null;
String query = "SELECT name FROM sqlite_master WHERE ";
query += "type='table' AND name NOT LIKE 'sync_table' ";
query += "AND name NOT LIKE '_temp_%' ";
query += "AND name NOT LIKE 'sqlite_%' ";
query += "AND name NOT LIKE 'android_%' ";
query += "ORDER BY rootpage DESC;";
try {
cursor = (Cursor) db.getDb().query(query);
cursor = (SQLiteCursor) db.getDb().query(query);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
String tableName = cursor.getString(0);
Expand All @@ -51,12 +51,12 @@ public List<String> getTablesNames(Database db) throws Exception {

public List<String> getViewNames(Database db) throws Exception {
List<String> views = new ArrayList<String>();
Cursor cursor = null;
SQLiteCursor cursor = null;
String query = "SELECT name FROM sqlite_master WHERE ";
query += "type='view' AND name NOT LIKE 'sqlite_%' ";
query += "ORDER BY rootpage DESC;";
try {
cursor = (Cursor) db.getDb().query(query);
cursor = (SQLiteCursor) db.getDb().query(query);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
String viewName = cursor.getString(0);
Expand Down Expand Up @@ -113,10 +113,10 @@ public void dropViews(Database db) throws Exception {

public List<String> getIndexesNames(Database db) {
List<String> indexes = new ArrayList<String>();
Cursor cursor = null;
SQLiteCursor cursor = null;
String query = "SELECT name FROM sqlite_master WHERE ";
query += "type='index' AND name NOT LIKE 'sqlite_%';";
cursor = (Cursor) db.getDb().query(query);
cursor = (SQLiteCursor) db.getDb().query(query);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
String indexName = cursor.getString(0);
Expand Down Expand Up @@ -152,10 +152,10 @@ public void dropIndexes(Database db) throws Exception {

public List<String> getTriggersNames(Database db) {
List<String> triggers = new ArrayList<String>();
Cursor cursor = null;
SQLiteCursor cursor = null;
String query = "SELECT name FROM sqlite_master WHERE ";
query += "type='trigger';";
cursor = (Cursor) db.getDb().query(query);
cursor = (SQLiteCursor) db.getDb().query(query);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
String triggerName = cursor.getString(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteException;
import net.sqlcipher.database.SQLiteStatement;
import net.zetetic.database.sqlcipher.SQLiteDatabase;
import net.zetetic.database.sqlcipher.SQLiteStatement;

public class UtilsSQLCipher {

Expand Down Expand Up @@ -36,12 +35,12 @@ public enum State {
* @return the detected state of the database
*/
public State getDatabaseState(Context ctxt, File dbPath, SharedPreferences sharedPreferences, GlobalSQLite globVar) {
SQLiteDatabase.loadLibs(ctxt);
System.loadLibrary("sqlcipher");
if (dbPath.exists()) {
SQLiteDatabase db = null;

try {
db = SQLiteDatabase.openDatabase(dbPath.getAbsolutePath(), "", null, SQLiteDatabase.OPEN_READONLY);
db = SQLiteDatabase.openDatabase(dbPath.getAbsolutePath(), "", null, SQLiteDatabase.OPEN_READONLY, null);

db.getVersion();

Expand All @@ -50,7 +49,7 @@ public State getDatabaseState(Context ctxt, File dbPath, SharedPreferences share
try {
String passphrase = sharedPreferences.getString("secret", "");
if (passphrase.length() > 0) {
db = SQLiteDatabase.openDatabase(dbPath.getAbsolutePath(), passphrase, null, SQLiteDatabase.OPEN_READONLY);
db = SQLiteDatabase.openDatabase(dbPath.getAbsolutePath(), passphrase, null, SQLiteDatabase.OPEN_READONLY, null);
db.getVersion();
return (State.ENCRYPTED_SECRET);
} else {
Expand All @@ -59,7 +58,14 @@ public State getDatabaseState(Context ctxt, File dbPath, SharedPreferences share
} catch (Exception e1) {
try {
if (globVar.secret.length() > 0) {
db = SQLiteDatabase.openDatabase(dbPath.getAbsolutePath(), globVar.secret, null, SQLiteDatabase.OPEN_READONLY);
db =
SQLiteDatabase.openDatabase(
dbPath.getAbsolutePath(),
globVar.secret,
null,
SQLiteDatabase.OPEN_READONLY,
null
);
db.getVersion();
return (State.ENCRYPTED_GLOBAL_SECRET);
} else {
Expand Down Expand Up @@ -92,11 +98,11 @@ public State getDatabaseState(Context ctxt, File dbPath, SharedPreferences share
* @throws IOException
*/
public void encrypt(Context ctxt, File originalFile, byte[] passphrase) throws IOException {
SQLiteDatabase.loadLibs(ctxt);
System.loadLibrary("sqlcipher");

if (originalFile.exists()) {
File newFile = File.createTempFile("sqlcipherutils", "tmp", ctxt.getCacheDir());
SQLiteDatabase db = SQLiteDatabase.openDatabase(originalFile.getAbsolutePath(), "", null, SQLiteDatabase.OPEN_READWRITE);
SQLiteDatabase db = SQLiteDatabase.openDatabase(originalFile.getAbsolutePath(), "", null, SQLiteDatabase.OPEN_READWRITE, null);
int version = db.getVersion();

db.close();
Expand Down Expand Up @@ -135,7 +141,7 @@ public void encrypt(Context ctxt, File originalFile, byte[] passphrase) throws I
}

public void decrypt(Context ctxt, File originalFile, byte[] passphrase) throws IOException {
SQLiteDatabase.loadLibs(ctxt);
System.loadLibrary("sqlcipher");

if (originalFile.exists()) {
// Create a temporary file for the decrypted database in the cache directory
Expand All @@ -146,15 +152,17 @@ public void decrypt(Context ctxt, File originalFile, byte[] passphrase) throws I
decryptedFile.getAbsolutePath(),
"",
null,
SQLiteDatabase.OPEN_READWRITE
SQLiteDatabase.OPEN_READWRITE,
null
);

// Open the encrypted database with the provided passphrase
SQLiteDatabase encryptedDb = SQLiteDatabase.openDatabase(
originalFile.getAbsolutePath(),
new String(passphrase),
null,
SQLiteDatabase.OPEN_READWRITE
SQLiteDatabase.OPEN_READWRITE,
null
);

int version = encryptedDb.getVersion();
Expand Down Expand Up @@ -196,14 +204,14 @@ public void decrypt(Context ctxt, File originalFile, byte[] passphrase) throws I
}
}

public void changePassword(Context ctxt, File file, String password, String nwpassword) throws IOException {
SQLiteDatabase.loadLibs(ctxt);
public void changePassword(Context ctxt, File file, String password, String nwpassword) throws Exception {
System.loadLibrary("sqlcipher");

if (file.exists()) {
SQLiteDatabase db = SQLiteDatabase.openDatabase(file.getAbsolutePath(), password, null, SQLiteDatabase.OPEN_READWRITE);
SQLiteDatabase db = SQLiteDatabase.openDatabase(file.getAbsolutePath(), password, null, SQLiteDatabase.OPEN_READWRITE, null);

if (!db.isOpen()) {
throw new SQLiteException("database " + file.getAbsolutePath() + " open failed");
throw new Exception("database " + file.getAbsolutePath() + " open failed");
}
db.changePassword(nwpassword);
db.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import net.sqlcipher.Cursor;
import net.zetetic.database.sqlcipher.SQLiteCursor;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand All @@ -19,7 +19,7 @@ public int dbChanges(SupportSQLiteDatabase db) {
String SELECT_CHANGE = "SELECT total_changes()";
Boolean success = true;
int ret = Integer.valueOf(-1);
Cursor cursor = (Cursor) db.query(SELECT_CHANGE, null);
SQLiteCursor cursor = (SQLiteCursor) db.query(SELECT_CHANGE, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
ret = Integer.parseInt(cursor.getString(0));
Expand All @@ -33,7 +33,7 @@ public long dbLastId(SupportSQLiteDatabase db) {
String SELECT_CHANGE = "SELECT last_insert_rowid()";
Boolean success = true;
long ret = (long) -1;
Cursor cursor = (Cursor) db.query(SELECT_CHANGE, null);
SQLiteCursor cursor = (SQLiteCursor) db.query(SELECT_CHANGE, null);
if (cursor.moveToFirst()) {
ret = Long.parseLong(cursor.getString(0));
}
Expand Down