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

Support for SQLCipher #6

Closed
greenrobot opened this issue Dec 9, 2011 · 28 comments
Closed

Support for SQLCipher #6

greenrobot opened this issue Dec 9, 2011 · 28 comments

Comments

@greenrobot
Copy link
Owner

SQLCipher has its own native libs and classes.
http://sqlcipher.net/sqlcipher-for-android/

@DayS
Copy link

DayS commented Jul 6, 2012

I had to use SQLCipher and GreenDAO together in one of my project. I just finish to implement the encryption library into greenDAO.

The main problem was that SQLCipher use it's own version of SQLite classes instead of extending Android's version... I was thinking about new classes named SQLiteXXXXEncrypted wich extends of the SQLiteXXXX Android class and override all methods to use SQLite's methods. And to use encryption database, a new class should have mad to work great. But SQLiteDatabase has a private constructor, so this solution is not possible.

However, I choose an other option : wrappers. The idea is almost the same : create new classes names SQLiteXXXXWrapper wich don't extends any other class. But the implementation of each method will just call SQLCipher's or Android's version according to the encryption configuration. One of the side effect was to change the reference to SQLiteXXXX classes everywhere in greenDAO.

Exemple (in SQLiteDatabaseWrapper) :

public void beginTransaction() {
    if (isCypheredDb())
        sqlCypheredDatabase.beginTransaction();
    else
        sqliteDatabase.beginTransaction();
}

I'll made a pull request soon for interested person :)

@negue
Copy link

negue commented Nov 22, 2012

Would be great if SQLCipher-Support was integrated in git-Version :)

Or is can use DayS-Fork and then simple update with master Version without any problem?

@mindofgautam
Copy link

Great work!

I am trying to use SQLCipher and GreenDAO in my android applciation. Can you I fork your android project ?

Waiting for your positive reply. Thanks and advance.

@nitinattarnea
Copy link

Hi @DayS ,
Thanks for sharing your git forked code, I was curious about your implementation. Earlier today I was thinking of manually changing the sqlitedatabase and other database related android objects in my copy of the GreenDAO source code manually . and that was the time i had come across this thread and your implementation of the same thing I was doing. I was curious about the approach. I understand perhaps why you had to use a wrapper to give you that much more flexibility. In my case I have been using greenDAO objects for a while in my application. And there are too many things using them. If I did resort to changing the references I have a feeling I might run into some trouble in one place or another.

That being said did you have to also modify the source of the standard DAO generator you had to use? Did you have to modify the template(*.ftl file) ?

@DayS
Copy link

DayS commented Jul 3, 2013

Hi,

In my PR I wanted to allow the developer to choose between encrypted database and non-encrypted database. The main idea was to simply replace the entry point SQLiteOpenHelper by a wrapper which will do the job by using a common super-class or interface in both Android's and SQLCipher's SQLiteDatabase classes.
But, as SQLCipher made a total fork of android.database.sqlite classes with their own package it wasn't possible. This is why I had to add more wrapper and use them everywhere the original ones were used (including in *.ftl files).

So, if you want to change database related object and the new objects have something in common with theses original classes, you should be able to do it easily. I Hope this help.

@nitinattarnea
Copy link

Hi,
Thanks for getting back. Your wrapper implementation sure makes it a whole lot easier to deal with it. Cause as I was sitting down to change the reference to all the classes I realized how much there was to be changed manually. But as I had to run it I had to make a few changes in the sense that the DAOmaster class and column-dao files generated would have different method signatures in most cases due to wrapper names. But that was not such a big problem. In any case. Thanks a lot Damian.

@yangblocker
Copy link

I rewrited greenDao to support SQLCipher, but it is not complete(you can use it comfortable yet), https://github.com/ywenblocker/GreenDao-SQLCipher

@nitinattarnea
Copy link

thanks a lot ywen, I had actually forked daneils work earlier and reworked
it into my project. I liked your approach . its more thorough. thanks
yangwen.

On Tue, Feb 11, 2014 at 1:11 PM, yangwen notifications@github.com wrote:

I rewrited greenDao to support SQLCipher, but it is not complete(you can
use it comfortable yet), https://github.com/ywenblocker/GreenDao-SQLCipher

Reply to this email directly or view it on GitHubhttps://github.com//issues/6#issuecomment-34732277
.

@yangblocker
Copy link

hi there, I have rewrite greenDao to support SQLCipher completely, you can use it from here:https://github.com/ywenblocker/GreenDao-SQLCipher. I wish greenDao can update latest greenDao project code to Let GreenDao-SQLCipher update to the latest.

@Hukke
Copy link

Hukke commented Mar 25, 2014

Hey,
my project for university is almost finish.
We used greenDAO to setUp our database and now we've got the trouble to encrypt the db.
So I found your solution but I dont get it, what to change.

How can I use your Solution?
Do I have to create new .jar from your fork?
Or what i have to do?
Please help us

@yangblocker
Copy link

hi, there: you don't need to fork my code,you can just download the DaoExample, read the demo code, replace the old greenDao_.jar with the new greenDao_.jar that I had put it on the libs dir on GitHub, it's easy to encrypt the db.
I hope this will help you. good luck.

------------------ 原始邮件 ------------------
发件人: "Hukke";
发送时间: 2014年3月25日(星期二) 晚上6:18
收件人: "greenrobot/greenDAO";
抄送: "yang.wen.blocker";
主题: Re: [greenDAO] Support for SQLCipher (#6)

Hey,
my project for university is almost finish.
We used greenDAO to setUp our database and now we've got the trouble to encrypt the db.
So I found your solution but I dont get it, what to change.

How can I use your Solution?
Do I have to create new .jar from your fork?
Or what i have to do?
Please help us


Reply to this email directly or view it on GitHub.

@Hukke
Copy link

Hukke commented Mar 25, 2014

Hey,
thank you very much for your fast answer.
So I don't have to recreate my DB-Schema with the Generator.
I only have to change the GreenDao*.jar file in my existing project?

@dragonboyorg
Copy link

nice!

@McPo
Copy link

McPo commented Jul 2, 2014

@ywenblocker Cheers for the changes,
Although it appears something might be amiss.

When using greendao from maven, I get no errors.
However using your fork causes the console to fill up with errors relevant to cursor lifecycle.

Finalizing a Cursor that has not been deactivated or closed. database = /data/data/com._._/databases/app_database, table = null, query = SELECT T.'_id',T.'ID',T.'NAME' FROM LIST T WHERE T.'NAME' LIKE ?
net.sqlcipher.database.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here

Releasing statement in a finalizer. Please ensure that you explicitly call close() on your cursor: SELECT T.'_id',T.'ID',T.'NAME' FROM LIST T WHERE T.'NAME' LIKE ?
net.sqlcipher.database.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here

Any ideas ?

This was referenced Jun 27, 2015
@greenrobot
Copy link
Owner Author

For everybody interested in SQLCipher, there's working code in this branch: https://github.com/greenrobot/greenDAO/tree/DbAbstraction

@sevar83
Copy link

sevar83 commented Aug 19, 2015

Do you plan to merge with master? Would be much easier for us to use it in our Android projects.

@mauricegavin
Copy link

Appreciate that there is an up-to-date branch for this. +1 for support in the main branch.

@McPo
Copy link

McPo commented Aug 25, 2015

For those attempting to implement this, You can no longer use the DaoMaster.OpenHelper. As it provides no way to pass a key in. Instead if your using SQLCipher you have to use the SQLCipher OpenHelper and methods etc. Then wrap the SQLCipher DB created by the openHelper in a:

new de.greenrobot.dao.database.SQLCipherDatabase(db)

Before passing it into DAOMaster. Below is an example

import net.sqlcipher.SQLException;
import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteOpenHelper;    
import de.greenrobot.dao.database.SQLCipherDatabase;

/////////////////////////////////////

    SQLiteDatabase.loadLibs(context);

    SQLiteOpenHelper helper = new SQLiteOpenHelper(context, "dbname", null, DaoMaster.SCHEMA_VERSION) {

        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {
            DaoMaster.createAllTables(new SQLCipherDatabase(sqLiteDatabase), false);
        }

        @Override
        public void onUpgrade(SQLiteDatabase dbIn, int currentVersion, int lastestVersion) {
            SQLCipherDatabase db = new SQLCipherDatabase(dbIn);
            // Do what you need to here
        }

    };

    SQLiteDatabase db = helper.getWritableDatabase(key);

/////////////////////////////////////

Great work, it would be good to get this into master. You won't believe the things I had to do to get SQLCipher with GreenDAO working for production and robolectric unit tests, this feature has removed the nastiness required. Was about to jump ship to Realm as soon as they got Unit Tests working but this change has significantly reduced the need to jump ship.

Cheers

@marvinmarnold
Copy link

@McPo Can you give some more detailed instructions about how to make the changes you are talking about? I just tried to generate dao using the DbAbstraction branch but I'm getting a bunch of errors in the generated code.

@McPo
Copy link

McPo commented Sep 2, 2015

@marvinmarnold Make sure your using the new DAOGenerator JAR as well and that you've regenerated your DAOs. If you intend to use SQLCipher it is not integrated with this branch, its only supported (You still need to get SQLCipher from https://www.zetetic.net/sqlcipher/), although that shouldn't cause any errors in the DAOS.

Im not sure how much more detail can be given, the code above should be good to copy and paste (I might get around to converting the sample app, but I wont get round to it for some time). Id also make sure your using the right imports. If you post some of the errors that your given, I might be able to assist more, although Im not sure if this is the right place for it.

@marvinmarnold
Copy link

Thanks @McPo . I think you are right that my issue is off topic. After digging deeper, I think the problem is trying to build the project from source* as I'm not using JARs. Is there somewhere I can find up-to-date JARs for this branch?

* Android dependencies aren't loading. Probably related to DaoCore/gradle.build#L31, provided 'com.google.android:android:4.1.1.4', but I'm not sure of resolution.

@McPo
Copy link

McPo commented Sep 4, 2015

Heres a zip containing the two JARs (Lib/Generator)
https://drive.google.com/file/d/0B0KW2Rx0YisGWHBHd0ZqMXNHYzQ/view?usp=sharing

Id advise you to make an issue on StackOverflow regarding your other issue, but it looks like you need to install Google/Support repository from the SDK manager.

@marvinmarnold
Copy link

Awesome @McPo, you are the man. I got a working demo up here. I still need to do some sanity checks and make sure that the DB is actually being encrypted but its all compiling fine now thanks to your JARs.

I opened up a StackOverflow question regarding the other issue.

@greenrobot
Copy link
Owner Author

Not sure to merge this branch into master. There seems a slight overhead produced by the abstraction (~10%).

@Lyndon2021
Copy link

Can It encrypt the database?and how to solve the problem ...

2 similar comments
@Lyndon2021
Copy link

Can It encrypt the database?and how to solve the problem ...

@Lyndon2021
Copy link

Can It encrypt the database?and how to solve the problem ...

@greenrobot
Copy link
Owner Author

Has been shipped with version 2.2: http://greenrobot.org/release/greendao-2-2-with-database-encryption/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests