#pragma once #ifndef BLOCKSTORE_IMPLEMENTATIONS_ENCRYPTED_ENCRYPTEDBLOCKSTORE_H_ #define BLOCKSTORE_IMPLEMENTATIONS_ENCRYPTED_ENCRYPTEDBLOCKSTORE_H_ #include "../../interface/BlockStore.h" #include #include #include "Cipher.h" #include "EncryptedBlock.h" namespace blockstore { namespace encrypted { template class EncryptedBlockStore: public BlockStore { public: EncryptedBlockStore(std::unique_ptr baseBlockStore, const typename Cipher::EncryptionKey &encKey); Key createKey() override; std::unique_ptr tryCreate(const Key &key, Data data) override; std::unique_ptr load(const Key &key) override; void remove(std::unique_ptr block) override; uint64_t numBlocks() const override; private: std::unique_ptr _baseBlockStore; typename Cipher::EncryptionKey _encKey; DISALLOW_COPY_AND_ASSIGN(EncryptedBlockStore); }; template EncryptedBlockStore::EncryptedBlockStore(std::unique_ptr baseBlockStore, const typename Cipher::EncryptionKey &encKey) : _baseBlockStore(std::move(baseBlockStore)), _encKey(encKey) { } template Key EncryptedBlockStore::createKey() { return _baseBlockStore->createKey(); } template std::unique_ptr EncryptedBlockStore::tryCreate(const Key &key, Data data) { return EncryptedBlock::TryCreateNew(_baseBlockStore.get(), key, std::move(data), _encKey); } template std::unique_ptr EncryptedBlockStore::load(const Key &key) { auto block = _baseBlockStore->load(key); if (block.get() == nullptr) { return nullptr; } return std::make_unique>(std::move(block), _encKey); } template void EncryptedBlockStore::remove(std::unique_ptr block) { auto baseBlock = cpputils::dynamic_pointer_move>(block)->releaseBlock(); return _baseBlockStore->remove(std::move(baseBlock)); } template uint64_t EncryptedBlockStore::numBlocks() const { return _baseBlockStore->numBlocks(); } } } #endif