diff options
Diffstat (limited to 'src/leveldbwrapper.h')
-rw-r--r-- | src/leveldbwrapper.h | 53 |
1 files changed, 49 insertions, 4 deletions
diff --git a/src/leveldbwrapper.h b/src/leveldbwrapper.h index c65e842704..f5c463830c 100644 --- a/src/leveldbwrapper.h +++ b/src/leveldbwrapper.h @@ -9,6 +9,7 @@ #include "serialize.h" #include "streams.h" #include "util.h" +#include "utilstrencodings.h" #include "version.h" #include <boost/filesystem/path.hpp> @@ -31,8 +32,14 @@ class CLevelDBBatch private: leveldb::WriteBatch batch; + const std::vector<unsigned char> obfuscate_key; public: + /** + * @param[in] obfuscate_key If passed, XOR data with this key. + */ + CLevelDBBatch(const std::vector<unsigned char>& obfuscate_key) : obfuscate_key(obfuscate_key) { }; + template <typename K, typename V> void Write(const K& key, const V& value) { @@ -44,6 +51,7 @@ public: CDataStream ssValue(SER_DISK, CLIENT_VERSION); ssValue.reserve(ssValue.GetSerializeSize(value)); ssValue << value; + ssValue.Xor(obfuscate_key); leveldb::Slice slValue(&ssValue[0], ssValue.size()); batch.Put(slKey, slValue); @@ -85,8 +93,27 @@ private: //! the database itself leveldb::DB* pdb; + //! a key used for optional XOR-obfuscation of the database + std::vector<unsigned char> obfuscate_key; + + //! the key under which the obfuscation key is stored + static const std::string OBFUSCATE_KEY_KEY; + + //! the length of the obfuscate key in number of bytes + static const unsigned int OBFUSCATE_KEY_NUM_BYTES; + + std::vector<unsigned char> CreateObfuscateKey() const; + public: - CLevelDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory = false, bool fWipe = false); + /** + * @param[in] path Location in the filesystem where leveldb data will be stored. + * @param[in] nCacheSize Configures various leveldb cache settings. + * @param[in] fMemory If true, use leveldb's memory environment. + * @param[in] fWipe If true, remove all existing data. + * @param[in] obfuscate If true, store data obfuscated via simple XOR. If false, XOR + * with a zero'd byte array. + */ + CLevelDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory = false, bool fWipe = false, bool obfuscate = false); ~CLevelDBWrapper(); template <typename K, typename V> @@ -107,6 +134,7 @@ public: } try { CDataStream ssValue(strValue.data(), strValue.data() + strValue.size(), SER_DISK, CLIENT_VERSION); + ssValue.Xor(obfuscate_key); ssValue >> value; } catch (const std::exception&) { return false; @@ -117,7 +145,7 @@ public: template <typename K, typename V> bool Write(const K& key, const V& value, bool fSync = false) throw(leveldb_error) { - CLevelDBBatch batch; + CLevelDBBatch batch(obfuscate_key); batch.Write(key, value); return WriteBatch(batch, fSync); } @@ -144,7 +172,7 @@ public: template <typename K> bool Erase(const K& key, bool fSync = false) throw(leveldb_error) { - CLevelDBBatch batch; + CLevelDBBatch batch(obfuscate_key); batch.Erase(key); return WriteBatch(batch, fSync); } @@ -159,7 +187,7 @@ public: bool Sync() throw(leveldb_error) { - CLevelDBBatch batch; + CLevelDBBatch batch(obfuscate_key); return WriteBatch(batch, true); } @@ -168,6 +196,23 @@ public: { return pdb->NewIterator(iteroptions); } + + /** + * Return true if the database managed by this class contains no entries. + */ + bool IsEmpty(); + + /** + * Accessor for obfuscate_key. + */ + const std::vector<unsigned char>& GetObfuscateKey() const; + + /** + * Return the obfuscate_key as a hex-formatted string. + */ + std::string GetObfuscateKeyHex() const; + }; #endif // BITCOIN_LEVELDBWRAPPER_H + |