-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from zauguin/sqlcipher
[RFC] Minimal SQLCipher support
- Loading branch information
Showing
6 changed files
with
192 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#pragma once | ||
|
||
#ifndef SQLITE_HAS_CODEC | ||
#define SQLITE_HAS_CODEC | ||
#endif | ||
|
||
#include "../sqlite_modern_cpp.h" | ||
|
||
namespace sqlite { | ||
struct sqlcipher_config : public sqlite_config { | ||
std::string key; | ||
}; | ||
|
||
class sqlcipher_database : public database { | ||
public: | ||
sqlcipher_database(std::string db, const sqlcipher_config &config): database(db, config) { | ||
set_key(config.key); | ||
} | ||
|
||
sqlcipher_database(std::u16string db, const sqlcipher_config &config): database(db, config) { | ||
set_key(config.key); | ||
} | ||
|
||
void set_key(const std::string &key) { | ||
if(auto ret = sqlite3_key(_db.get(), key.data(), key.size())) | ||
exceptions::throw_sqlite_error(ret); | ||
} | ||
|
||
void set_key(const std::string &key, const std::string &db_name) { | ||
if(auto ret = sqlite3_key_v2(_db.get(), db_name.c_str(), key.data(), key.size())) | ||
exceptions::throw_sqlite_error(ret); | ||
} | ||
|
||
void rekey(const std::string &new_key) { | ||
if(auto ret = sqlite3_rekey(_db.get(), new_key.data(), new_key.size())) | ||
exceptions::throw_sqlite_error(ret); | ||
} | ||
|
||
void rekey(const std::string &new_key, const std::string &db_name) { | ||
if(auto ret = sqlite3_rekey_v2(_db.get(), db_name.c_str(), new_key.data(), new_key.size())) | ||
exceptions::throw_sqlite_error(ret); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#ifdef ENABLE_SQLCIPHER_TESTS | ||
#include <iostream> | ||
#include <cstdlib> | ||
#include <unistd.h> | ||
#include <sqlite_modern_cpp/sqlcipher.h> | ||
using namespace sqlite; | ||
using namespace std; | ||
|
||
struct TmpFile | ||
{ | ||
string fname; | ||
|
||
TmpFile() | ||
{ | ||
char f[]="/tmp/sqlite_modern_cpp_test_XXXXXX"; | ||
int fid = mkstemp(f); | ||
close(fid); | ||
|
||
fname = f; | ||
} | ||
|
||
~TmpFile() | ||
{ | ||
unlink(fname.c_str()); | ||
} | ||
}; | ||
|
||
int main() | ||
{ | ||
try | ||
{ | ||
TmpFile file; | ||
sqlcipher_config config; | ||
{ | ||
config.key = "DebugKey"; | ||
sqlcipher_database db(file.fname, config); | ||
|
||
db << "CREATE TABLE foo (a integer, b string);"; | ||
db << "INSERT INTO foo VALUES (?, ?)" << 1 << "hello"; | ||
db << "INSERT INTO foo VALUES (?, ?)" << 2 << "world"; | ||
|
||
string str; | ||
db << "SELECT b from FOO where a=?;" << 2 >> str; | ||
|
||
if(str != "world") | ||
{ | ||
cout << "Bad result on line " << __LINE__ << endl; | ||
exit(EXIT_FAILURE); | ||
} | ||
} | ||
try { | ||
config.key = "DebugKey2"; | ||
sqlcipher_database db(file.fname, config); | ||
db << "INSERT INTO foo VALUES (?, ?)" << 3 << "fail"; | ||
|
||
cout << "Can open with wrong key"; | ||
exit(EXIT_FAILURE); | ||
} catch(exceptions::notadb) { | ||
// Expected, wrong key | ||
} | ||
{ | ||
config.key = "DebugKey"; | ||
sqlcipher_database db(file.fname, config); | ||
db.rekey("DebugKey2"); | ||
} | ||
{ | ||
config.key = "DebugKey2"; | ||
sqlcipher_database db(file.fname, config); | ||
db << "INSERT INTO foo VALUES (?, ?)" << 3 << "fail"; | ||
} | ||
} | ||
catch(sqlite_exception e) | ||
{ | ||
cout << "Unexpected error " << e.what() << endl; | ||
exit(EXIT_FAILURE); | ||
} | ||
catch(...) | ||
{ | ||
cout << "Unknown error\n"; | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
cout << "OK\n"; | ||
exit(EXIT_SUCCESS); | ||
} | ||
#else | ||
int main() { | ||
return 42; //Skip test | ||
} | ||
#endif |