sqlite_orm icon indicating copy to clipboard operation
sqlite_orm copied to clipboard

Password protection of Database

Open ElephantInTheRoomFirst opened this issue 9 years ago • 6 comments

Good Day, How can be database protected using password? Is there way to do that?

Regards

ElephantInTheRoomFirst avatar Jun 04 '17 12:06 ElephantInTheRoomFirst

Hi. Nice question. Never used encrypted sqlite db, just heard about. Let me read about it and then implement. If you have custom wishes feel free to write. Thanks

fnc12 avatar Jun 04 '17 12:06 fnc12

Looks like there is no encryption shipped in the basic version of libsqlite3. It means one has to use extensions to make db encrypted. There are several encryption extensions for sqlite3. Examples:

  • https://github.com/OlivierJG/botansqlite3/blob/master/test_sqlite.cpp,
  • https://github.com/rindeal/SQLite3-Encryption

It would be silly and impossible to try to add all of them. Better way is to add extension API to sqlite_orm. Details: assume we don't use sqlite_orm but use sqlite3 straight with it's C-lib. If we want db to be encrypted we have to call sqlite3_key function with sqlite3* pointer of database connection right after sqlite3_open. So we can add a member to a storage like

std::function<void(sqlite3*)> on_open;

which will be called right after storage's inner sqlite3_open call (if on_open isn't null of course). So first outer example https://github.com/OlivierJG/botansqlite3/blob/master/test_sqlite.cpp can be implemented like this:

auto storage = ...;
storage.on_open = [&](sqlite3* db){
    auto key = "qwerty";
    sqlite3_key(db, key, strlen(key));
};
//..

but extension with sqlite3_key or whatever function must be added manually.

What do you think? If it's ok I shall add this in the next commit

fnc12 avatar Jun 04 '17 13:06 fnc12

Thank you very much about the reply. I need to do impact analysis to find out best way. Once it is complete, I shall revert back to you.

ElephantInTheRoomFirst avatar Jun 05 '17 08:06 ElephantInTheRoomFirst

It will do . It will be great if you provide an example. This is not urgent issue for me. Thanks

ElephantInTheRoomFirst avatar Jun 05 '17 19:06 ElephantInTheRoomFirst

Hey. I've added on_open public member to the latest d2a4f1bdc6bade4b2880b45dba562447f6c770d3 commit. Please check it out. It is called right after sqlite3_open call (and PRAGMA foreign_key 1 if there are foreign keys exist in a storage). I don't know what kind of example do you expect cause there are a lot of encryption solutions for sqlite and they depend from platform. Which one do you want to use?

fnc12 avatar Aug 10 '17 18:08 fnc12

Thanks. I shall check it later.

ElephantInTheRoomFirst avatar Aug 15 '17 06:08 ElephantInTheRoomFirst