Introduce BlockAdapter.
This commit is contained in:
1 parent
8a82d33f39
commit
779bd6c568
3 files changed
+168
No files matched your search
@@ -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
|
||||
Reference in new issue
Block a user