Introduce BlockAdapter.
Chaos Ci / test (push) Successful in 3m23s
Chaos Ci / benchmark (push) Successful in 1m33s

This commit is contained in:
hashlag committed 2026-10-12 01:05:14 +03:00
1 parent 8401612e20
commit 4d613d8015
3 files changed
+168

No files matched your search

+53
View File
@@ -0,0 +1,53 @@
#ifndef CHAOS_CIPHER_BLOCK_BLOCKADAPTER_HPP
#define CHAOS_CIPHER_BLOCK_BLOCKADAPTER_HPP
#include <cstdint>
#include <vector>
namespace Chaos::Cipher::Block
{
template<typename ModeT>
class BlockAdapter
{
public:
class Key
{
friend class BlockAdapter;
public:
template<typename InputIt>
Key(InputIt keyBegin, InputIt keyEnd)
: Key_(keyBegin, keyEnd)
{ }
template<typename Byte>
Key(const std::vector<Byte> & key)
: Key_(key.cbegin(), key.cend())
{ }
private:
ModeT::Cipher::Key Key_;
};
template<typename Byte>
static std::vector<Byte> Encrypt(const Key & key, const std::vector<Byte> & plaintext)
{
typename ModeT::Encryptor encryptor(key.Key_);
std::vector<Byte> result;
const uint64_t maxResultSize = encryptor.PredictMaxUpdateOutput(plaintext.size()) +
encryptor.PredictMaxFinishOutput();
result.resize(maxResultSize);
uint64_t written = encryptor.Update(result.begin(), result.end(),
plaintext.begin(), plaintext.end());
written += encryptor.Finish(result.begin() + written, result.end());
result.resize(written);
return result;
}
};
} // namespace Chaos::Cipher::Block
#endif // CHAOS_CIPHER_BLOCK_BLOCKADAPTER_HPP