- Notifications
You must be signed in to change notification settings - Fork569
Android SQLite API based on SQLCipher
sqlcipher/android-database-sqlcipher
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Theandroid-database-sqlcipher
project has beenofficially deprecated. The long-term replacement issqlcipher-android
. Instructions for migrating fromandroid-database-sqlcipher
tosqlcipher-android
may be foundhere.
The latest AAR binary package information can behere, the source can be foundhere.
SQLCipher for Android runs on Android from 5.0 (API 21), forarmeabi-v7a
,x86
,x86_64
, andarm64_v8a
architectures.
We welcome contributions, to contribute to SQLCipher for Android, acontributor agreement needs to be submitted. All submissions should be based on themaster
branch.
A typical SQLite database in unencrypted, and visually parseable even as encoded text. The following example shows the difference between hexdumps of a standard SQLite database and one implementing SQLCipher.
~ sjlombardo$ hexdump -C sqlite.db00000000 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 |SQLite format 3.|…000003c0 65 74 32 74 32 03 43 52 45 41 54 45 20 54 41 42 |et2t2.CREATE TAB|000003d0 4c 45 20 74 32 28 61 2c 62 29 24 01 06 17 11 11 |LE t2(a,b)$…..|…000007e0 20 74 68 65 20 73 68 6f 77 15 01 03 01 2f 01 6f | the show…./.o|000007f0 6e 65 20 66 6f 72 20 74 68 65 20 6d 6f 6e 65 79 |ne for the money|~ $ sqlite3 sqlcipher.dbsqlite> PRAGMA KEY=’test123′;sqlite> CREATE TABLE t1(a,b);sqlite> INSERT INTO t1(a,b) VALUES (‘one for the money’, ‘two for the show’);sqlite> .quit~ $ hexdump -C sqlcipher.db00000000 84 d1 36 18 eb b5 82 90 c4 70 0d ee 43 cb 61 87 |.?6.?..?p.?C?a.|00000010 91 42 3c cd 55 24 ab c6 c4 1d c6 67 b4 e3 96 bb |.B?..?|00000bf0 8e 99 ee 28 23 43 ab a4 97 cd 63 42 8a 8e 7c c6 |..?(#C??.?cB..|?|~ $ sqlite3 sqlcipher.dbsqlite> SELECT * FROM t1;Error: file is encrypted or is not a database
(example courtesy of SQLCipher)
You have a two main options for using SQLCipher for Android in your app:
Using it with Room or other consumers of the
androidx.sqlite
APIUsing the native SQLCipher for Android classes
In both cases, you will need to add a dependency onnet.zetetic:android-database-sqlcipher
,such as having the following line in your module'sbuild.gradle
dependencies
closure:
implementation"net.zetetic:android-database-sqlcipher:4.5.3"implementation"androidx.sqlite:sqlite:2.1.0"
(replacing4.5.3
with the version you want)
SQLCipher for Android has aSupportFactory
class in thenet.sqlcipher.database
packagethat can be used to configure Room to use SQLCipher for Android.
There are threeSupportFactory
constructors:
SupportFactory(byte[] passphrase)
SupportFactory(byte[] passphrase, SQLiteDatabaseHook hook)
SupportFactory(byte[] passphrase, SQLiteDatabaseHook hook, boolean clearPassphrase)
All three take abyte[]
to use as the passphrase (if you have achar[]
, useSQLiteDatabase.getBytes()
to get a suitablebyte[]
to use).
Two offer aSQLiteDatabaseHook
parameter that you can usefor executing SQL statements before or after the passphrase is used to keythe database.
The three-parameter constructor also offersclearPassphrase
, which defaultstotrue
in the other two constructors. IfclearPassphrase
is set totrue
,this will zero out the bytes of thebyte[]
after we open the database. Thisis safest from a security standpoint, but it does mean that theSupportFactory
instance is a single-use object. Attempting to reuse theSupportFactory
instance later will result in being unable to open the database, because thepassphrase will be wrong. If you think that you might need to reuse theSupportFactory
instance, passfalse
forclearPassphrase
.
Then, pass yourSupportFactory
toopenHelperFactory()
on yourRoomDatabase.Builder
:
finalbyte[]passphrase =SQLiteDatabase.getBytes(userEnteredPassphrase);finalSupportFactoryfactory =newSupportFactory(passphrase);finalSomeDatabaseroom =Room.databaseBuilder(activity,SomeDatabase.class,DB_NAME) .openHelperFactory(factory) .build();
Now, Room will make all of its database requests using SQLCipher for Android insteadof the framework copy of SQLCipher.
Note thatSupportFactory
should work with other consumers of theandroidx.sqlite
API;Room is merely a prominent example.
If you have existing SQLite code using classes likeSQLiteDatabase
andSQLiteOpenHelper
,converting your code to use SQLCipher for Android mostly is a three-step process:
Replace all
android.database.sqlite.*
import
statements with ones thatusenet.sqlcipher.database.*
(e.g., convertandroid.database.sqlite.SQLiteDatabase
tonet.sqlcipher.database.SQLiteDatabase
)Before attempting to open a database, call
SQLiteDatabase.loadLibs()
, passingin aContext
(e.g., add this toonCreate()
of yourApplication
subclass, usingtheApplication
itself as theContext
)When opening a database (e.g.,
SQLiteDatabase.openOrCreateDatabase()
), passin the passphrase as achar[]
orbyte[]
The rest of your code may not need any changes.
An article covering both integration of SQLCipher into an Android application as well as building the source can be foundhere.
For applications which utilize ProGuard, a few additional rules must be included when using SQLCipher for Android. These rules instruct ProGuard to omit the renaming of the internal SQLCipher classes which are used via lookup from the JNI layer. It is worth noting that since SQLCipher or Android is based on open source code there is little value in obfuscating the library anyway. The more important use of ProGuard is to protect your application code and business logic.
-keep,includedescriptorclasses class net.sqlcipher.** { *; }-keep,includedescriptorclasses interface net.sqlcipher.** { *; }
In order to buildandroid-database-sqlcipher
from source you will need both the Android SDK, Gradle, Android NDK, SQLCipher core source directory, and an OpenSSL source directory. We currently recommend using Android NDK LTS version23.0.7599858
.
To complete themake
command, theANDROID_NDK_HOME
environment variable must be defined which should point to your NDK root. Once you have cloned the repo, change directory into the root of the repository and run the following commands:
SQLCIPHER_ROOT=/some/path/to/sqlcipher-folder \OPENSSL_ROOT=/some/path/to/openssl-folder \SQLCIPHER_CFLAGS="-DSQLITE_HAS_CODEC -DSQLITE_TEMP_STORE=2" \SQLCIPHER_ANDROID_VERSION="4.5.3" \make build-release
You may specify other build flags/features withinSQLCIPHER_CFLAGS
, however, specifying-DSQLITE_HAS_CODEC
and-DSQLITE_TEMP_STORE
is necessary in the list of flags.
The Android support libraries are licensed under Apache 2.0, in line with the Android OS code on which they are based. The SQLCipher code itself is licensed under a BSD-style license from Zetetic LLC. Finally, the original SQLite code itself is in the public domain.
About
Android SQLite API based on SQLCipher
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors15
Uh oh!
There was an error while loading.Please reload this page.