mirror of
https://github.com/cuberite/SQLiteCpp.git
synced 2025-08-04 09:46:02 -04:00
Fixed some documentation and corrected the encryption detection.
This commit is contained in:
parent
685ff293c5
commit
02f8fe19c0
@ -375,25 +375,29 @@ public:
|
||||
* This is the equivalent of the sqlite3_key call and should thus be called
|
||||
* directly after opening the database. If the database is unencrypted,
|
||||
* 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
|
||||
*/
|
||||
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.
|
||||
*
|
||||
* 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
|
||||
* 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
|
||||
*/
|
||||
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.
|
||||
@ -406,8 +410,10 @@ public:
|
||||
* @param[in] aFilename path/uri to a file
|
||||
*
|
||||
* @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:
|
||||
/// @{ Database must be non-copyable
|
||||
|
@ -56,7 +56,7 @@ Database::Database(const char* apFilename,
|
||||
mpSQLite(NULL),
|
||||
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)
|
||||
{
|
||||
const SQLite::Exception exception(mpSQLite, ret); // must create before closing
|
||||
@ -77,7 +77,7 @@ Database::Database(const std::string& aFilename,
|
||||
mpSQLite(NULL),
|
||||
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)
|
||||
{
|
||||
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.
|
||||
void Database::key(const std::string& aKey) const noexcept // nothrow
|
||||
void Database::key(const std::string& aKey) const
|
||||
{
|
||||
int pass_len = aKey.length();
|
||||
#ifdef SQLITE_HAS_CODEC
|
||||
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);
|
||||
}
|
||||
#else
|
||||
@ -246,15 +246,15 @@ void Database::key(const std::string& aKey) const noexcept // nothrow
|
||||
}
|
||||
|
||||
// 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
|
||||
int pass_len = aNewKey.length();
|
||||
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);
|
||||
} else {
|
||||
int ret = sqlite3_rekey(mpSQLite, nullptr, 0);
|
||||
const int ret = sqlite3_rekey(mpSQLite, nullptr, 0);
|
||||
check(ret);
|
||||
}
|
||||
#else
|
||||
@ -264,9 +264,8 @@ void Database::rekey(const std::string& aNewKey) const noexcept // nothrow
|
||||
}
|
||||
|
||||
//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) {
|
||||
std::ifstream fileBuffer(aFilename, std::ios::in | std::ios::binary);
|
||||
char header[16];
|
||||
@ -274,10 +273,14 @@ const bool Database::isUnencrypted(const std::string& aFilename) const noexcept
|
||||
fileBuffer.seekg(0, std::ios::beg);
|
||||
fileBuffer.getline(header, 16);
|
||||
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user