Fixed some documentation and corrected the encryption detection.

This commit is contained in:
Jorrit Wronski 2016-12-23 07:53:55 +01:00
parent 685ff293c5
commit 02f8fe19c0
2 changed files with 28 additions and 19 deletions

View File

@ -375,25 +375,29 @@ public:
* This is the equivalent of the sqlite3_key call and should thus be called * This is the equivalent of the sqlite3_key call and should thus be called
* directly after opening the database. If the database is unencrypted, * directly after opening the database. If the database is unencrypted,
* this methods encrypts it immediately. * this methods encrypts it immediately.
* Normal database -> call db.key("secret") -> encrypted database, database ready
* Encrypted database -> call db.key("secret") -> database ready
* *
* @param[in] key Key to decode/encode the database * @param[in] aKey Key to decode/encode the database
* *
* @throw SQLite::Exception in case of error * @throw SQLite::Exception in case of error
*/ */
void key(const std::string& aKey) const noexcept; // nothrow void key(const std::string& aKey) const;
/** /**
* @brief Reset the key for the current sqlite database instance. * @brief Reset the key for the current sqlite database instance.
* *
* This is the equivalent of the sqlite3_rekey call and should thus be called * This is the equivalent of the sqlite3_rekey call and should thus be called
* after the database has been opened with a valid key. To decrypt a * after the database has been opened with a valid key. To decrypt a
* database, call this method with a NULL pointer. * database, call this method with an empty string.
* Encrypted database -> call db.key("secret") -> call db.rekey("newsecret") -> change key, database ready
* Encrypted database -> call db.key("secret") -> call db.rekey("") -> decrypted database, database ready
* *
* @param[in] nkey New key to encode the database * @param[in] aNewKey New key to encode the database
* *
* @throw SQLite::Exception in case of error * @throw SQLite::Exception in case of error
*/ */
void rekey(const std::string& aNewKey) const noexcept; // nothrow void rekey(const std::string& aNewKey) const;
/** /**
* @brief Test if a file contains an unencrypted database. * @brief Test if a file contains an unencrypted database.
@ -406,8 +410,10 @@ public:
* @param[in] aFilename path/uri to a file * @param[in] aFilename path/uri to a file
* *
* @return true if the database has the standard header. * @return true if the database has the standard header.
*
* @throw SQLite::Exception in case of error
*/ */
const bool isUnencrypted(const std::string& aFilename) const noexcept; // nothrow const bool isUnencrypted(const std::string& aFilename) const;
private: private:
/// @{ Database must be non-copyable /// @{ Database must be non-copyable

View File

@ -56,7 +56,7 @@ Database::Database(const char* apFilename,
mpSQLite(NULL), mpSQLite(NULL),
mFilename(apFilename) mFilename(apFilename)
{ {
int ret = sqlite3_open_v2(apFilename, &mpSQLite, aFlags, apVfs); const int ret = sqlite3_open_v2(apFilename, &mpSQLite, aFlags, apVfs);
if (SQLITE_OK != ret) if (SQLITE_OK != ret)
{ {
const SQLite::Exception exception(mpSQLite, ret); // must create before closing const SQLite::Exception exception(mpSQLite, ret); // must create before closing
@ -77,7 +77,7 @@ Database::Database(const std::string& aFilename,
mpSQLite(NULL), mpSQLite(NULL),
mFilename(aFilename) mFilename(aFilename)
{ {
int ret = sqlite3_open_v2(aFilename.c_str(), &mpSQLite, aFlags, aVfs.empty() ? NULL : aVfs.c_str()); const int ret = sqlite3_open_v2(aFilename.c_str(), &mpSQLite, aFlags, aVfs.empty() ? NULL : aVfs.c_str());
if (SQLITE_OK != ret) if (SQLITE_OK != ret)
{ {
const SQLite::Exception exception(mpSQLite, ret); // must create before closing const SQLite::Exception exception(mpSQLite, ret); // must create before closing
@ -229,12 +229,12 @@ void Database::loadExtension(const char* apExtensionName, const char *apEntryPoi
} }
//Set the key for the current sqlite database instance. //Set the key for the current sqlite database instance.
void Database::key(const std::string& aKey) const noexcept // nothrow void Database::key(const std::string& aKey) const
{ {
int pass_len = aKey.length(); int pass_len = aKey.length();
#ifdef SQLITE_HAS_CODEC #ifdef SQLITE_HAS_CODEC
if (pass_len > 0) { if (pass_len > 0) {
int ret = sqlite3_key(mpSQLite, aKey.c_str(), pass_len); const int ret = sqlite3_key(mpSQLite, aKey.c_str(), pass_len);
check(ret); check(ret);
} }
#else #else
@ -246,15 +246,15 @@ void Database::key(const std::string& aKey) const noexcept // nothrow
} }
// Reset the key for the current sqlite database instance. // Reset the key for the current sqlite database instance.
void Database::rekey(const std::string& aNewKey) const noexcept // nothrow void Database::rekey(const std::string& aNewKey) const
{ {
#ifdef SQLITE_HAS_CODEC #ifdef SQLITE_HAS_CODEC
int pass_len = aNewKey.length(); int pass_len = aNewKey.length();
if (pass_len > 0) { if (pass_len > 0) {
int ret = sqlite3_rekey(mpSQLite, aNewKey.c_str(), pass_len); const int ret = sqlite3_rekey(mpSQLite, aNewKey.c_str(), pass_len);
check(ret); check(ret);
} else { } else {
int ret = sqlite3_rekey(mpSQLite, nullptr, 0); const int ret = sqlite3_rekey(mpSQLite, nullptr, 0);
check(ret); check(ret);
} }
#else #else
@ -264,9 +264,8 @@ void Database::rekey(const std::string& aNewKey) const noexcept // nothrow
} }
//Test if a file contains an unencrypted database. //Test if a file contains an unencrypted database.
const bool Database::isUnencrypted(const std::string& aFilename) const noexcept // nothrow const bool Database::isUnencrypted(const std::string& aFilename) const
{ {
bool encrypted_db = false;
if (aFilename.length() > 0) { if (aFilename.length() > 0) {
std::ifstream fileBuffer(aFilename, std::ios::in | std::ios::binary); std::ifstream fileBuffer(aFilename, std::ios::in | std::ios::binary);
char header[16]; char header[16];
@ -274,10 +273,14 @@ const bool Database::isUnencrypted(const std::string& aFilename) const noexcept
fileBuffer.seekg(0, std::ios::beg); fileBuffer.seekg(0, std::ios::beg);
fileBuffer.getline(header, 16); fileBuffer.getline(header, 16);
fileBuffer.close(); fileBuffer.close();
} else {
const SQLite::Exception exception("Error opening file: " + aFilename);
throw exception;
} }
encrypted_db = strncmp(header, "SQLite format 3\000", 16) != 0; return strncmp(header, "SQLite format 3\000", 16) == 0;
} }
return encrypted_db; const SQLite::Exception exception("Could not open database, the aFilename parameter was empty.");
throw exception;
} }
} // namespace SQLite } // namespace SQLite