Replace Decryptor CRTP base with a concept.
This commit is contained in:
@@ -1,44 +1,27 @@
|
||||
#ifndef CHAOS_CIPHER_BLOCK_DECRYPTOR_HPP
|
||||
#define CHAOS_CIPHER_BLOCK_DECRYPTOR_HPP
|
||||
|
||||
#include <concepts>
|
||||
#include <cstdint>
|
||||
#include <type_traits>
|
||||
|
||||
namespace Chaos::Cipher::Block
|
||||
{
|
||||
|
||||
template<typename T>
|
||||
class Decryptor
|
||||
concept Decryptor = requires(T decryptor,
|
||||
typename T::Block block,
|
||||
uint8_t * outBegin, uint8_t * outEnd,
|
||||
uint8_t * inBegin, uint8_t * inEnd)
|
||||
{
|
||||
public:
|
||||
template<typename OutputIt, typename InputIt>
|
||||
void DecryptBlock(OutputIt outBegin, OutputIt outEnd,
|
||||
InputIt inBegin, InputIt inEnd) const
|
||||
{
|
||||
Impl().DecryptBlock(outBegin, outEnd, inBegin, inEnd);
|
||||
}
|
||||
|
||||
template<typename Block>
|
||||
auto DecryptBlock(Block block) const
|
||||
{
|
||||
return Impl().DecryptBlock(block);
|
||||
}
|
||||
|
||||
auto GetBlockSize() const
|
||||
{
|
||||
return Impl().GetBlockSize();
|
||||
}
|
||||
|
||||
protected:
|
||||
Decryptor() = default;
|
||||
|
||||
private:
|
||||
const T & Impl() const
|
||||
{
|
||||
return static_cast<const T &>(*this);
|
||||
}
|
||||
|
||||
T & Impl()
|
||||
{
|
||||
return static_cast<T &>(*this);
|
||||
}
|
||||
typename T::Block;
|
||||
typename T::Key;
|
||||
requires std::constructible_from<T, typename T::Key>;
|
||||
requires std::unsigned_integral<std::remove_cvref_t<decltype(T::BlockSize)>>;
|
||||
requires std::unsigned_integral<std::remove_cvref_t<decltype(T::KeySize)>>;
|
||||
decryptor.DecryptBlock(outBegin, outEnd, inBegin, inEnd);
|
||||
{ decryptor.DecryptBlock(block) } -> std::same_as<typename T::Block>;
|
||||
{ decryptor.GetBlockSize() } -> std::unsigned_integral;
|
||||
};
|
||||
|
||||
} // namespace Chaos::Cipher::Block
|
||||
|
||||
@@ -281,7 +281,7 @@ public:
|
||||
|
||||
static_assert(Chaos::Cipher::Block::Encryptor<Encryptor>);
|
||||
|
||||
class Decryptor : public Chaos::Cipher::Block::Decryptor<Decryptor>
|
||||
class Decryptor
|
||||
{
|
||||
public:
|
||||
using Block = DesCrypt::Block;
|
||||
@@ -327,6 +327,8 @@ public:
|
||||
Inner_::KeySchedule Schedule_;
|
||||
};
|
||||
|
||||
static_assert(Chaos::Cipher::Block::Decryptor<Decryptor>);
|
||||
|
||||
private:
|
||||
using BlockHalf = uint32_t;
|
||||
using RawBlockArray = Service::SeArray<uint8_t, 8>;
|
||||
|
||||
@@ -529,9 +529,9 @@ TEST(DesCryptTests, EncryptUInt64BlockGenericTest)
|
||||
ASSERT_EQ(expected, EncryptUInt64BlockGeneric(enc, data));
|
||||
}
|
||||
|
||||
template<typename Impl, typename InputIt>
|
||||
static std::vector<uint8_t> DecryptThroughBase(const Decryptor<Impl> & dec,
|
||||
InputIt begin, InputIt end)
|
||||
template<Decryptor DecryptorImpl, typename InputIt>
|
||||
static std::vector<uint8_t> DecryptGeneric(const DecryptorImpl & dec,
|
||||
InputIt begin, InputIt end)
|
||||
{
|
||||
std::vector<uint8_t> result;
|
||||
result.resize(dec.GetBlockSize(), 0);
|
||||
@@ -540,7 +540,7 @@ static std::vector<uint8_t> DecryptThroughBase(const Decryptor<Impl> & dec,
|
||||
return result;
|
||||
}
|
||||
|
||||
TEST(DesCryptTests, DecryptThroughBaseTest)
|
||||
TEST(DesCryptTests, DecryptGenericTest)
|
||||
{
|
||||
std::array<uint8_t, DesCrypt::KeySize> key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 };
|
||||
|
||||
@@ -550,16 +550,16 @@ TEST(DesCryptTests, DecryptThroughBaseTest)
|
||||
DesCrypt::Key desKey(key.begin(), key.end());
|
||||
DesCrypt::Decryptor dec(desKey);
|
||||
|
||||
ASSERT_EQ(expected, DecryptThroughBase(dec, data.begin(), data.end()));
|
||||
ASSERT_EQ(expected, DecryptGeneric(dec, data.begin(), data.end()));
|
||||
}
|
||||
|
||||
template<typename Impl>
|
||||
static uint64_t DecryptUInt64BlockThroughBase(const Decryptor<Impl> & dec, uint64_t block)
|
||||
template<Decryptor DecryptorImpl>
|
||||
static uint64_t DecryptUInt64BlockGeneric(const DecryptorImpl & dec, uint64_t block)
|
||||
{
|
||||
return dec.DecryptBlock(block);
|
||||
}
|
||||
|
||||
TEST(DesCryptTests, DecryptUInt64BlockThroughBaseTest)
|
||||
TEST(DesCryptTests, DecryptUInt64BlockGenericTest)
|
||||
{
|
||||
std::array<uint8_t, DesCrypt::KeySize> key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 };
|
||||
|
||||
@@ -569,5 +569,5 @@ TEST(DesCryptTests, DecryptUInt64BlockThroughBaseTest)
|
||||
DesCrypt::Key desKey(key.begin(), key.end());
|
||||
DesCrypt::Decryptor dec(desKey);
|
||||
|
||||
ASSERT_EQ(expected, DecryptUInt64BlockThroughBase(dec, data));
|
||||
ASSERT_EQ(expected, DecryptUInt64BlockGeneric(dec, data));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user