diff --git a/Chaos/Cipher/Block/Des/DesCrypt.hpp b/Chaos/Cipher/Block/Des/DesCrypt.hpp index 66cdb21..b6d72db 100644 --- a/Chaos/Cipher/Block/Des/DesCrypt.hpp +++ b/Chaos/Cipher/Block/Des/DesCrypt.hpp @@ -233,7 +233,7 @@ public: Inner_::RawKey Key_; }; - class Encryptor : public Chaos::Cipher::Block::Encryptor + class Encryptor { public: using Block = DesCrypt::Block; @@ -279,6 +279,8 @@ public: Inner_::KeySchedule Schedule_; }; + static_assert(Chaos::Cipher::Block::Encryptor); + class Decryptor : public Chaos::Cipher::Block::Decryptor { public: diff --git a/Chaos/Cipher/Block/Encryptor.hpp b/Chaos/Cipher/Block/Encryptor.hpp index ba3f9f5..3c8ada7 100644 --- a/Chaos/Cipher/Block/Encryptor.hpp +++ b/Chaos/Cipher/Block/Encryptor.hpp @@ -1,44 +1,27 @@ #ifndef CHAOS_CIPHER_BLOCK_ENCRYPTOR_HPP #define CHAOS_CIPHER_BLOCK_ENCRYPTOR_HPP +#include +#include +#include + namespace Chaos::Cipher::Block { template -class Encryptor +concept Encryptor = requires(T encryptor, + typename T::Block block, + uint8_t * outBegin, uint8_t * outEnd, + uint8_t * inBegin, uint8_t * inEnd) { -public: - template - void EncryptBlock(OutputIt outBegin, OutputIt outEnd, - InputIt inBegin, InputIt inEnd) const - { - Impl().EncryptBlock(outBegin, outEnd, inBegin, inEnd); - } - - template - auto EncryptBlock(Block block) const - { - return Impl().EncryptBlock(block); - } - - auto GetBlockSize() const - { - return Impl().GetBlockSize(); - } - -protected: - Encryptor() = default; - -private: - const T & Impl() const - { - return static_cast(*this); - } - - T & Impl() - { - return static_cast(*this); - } + typename T::Block; + typename T::Key; + requires std::constructible_from; + requires std::unsigned_integral>; + requires std::unsigned_integral>; + encryptor.EncryptBlock(outBegin, outEnd, inBegin, inEnd); + { encryptor.EncryptBlock(block) } -> std::same_as; + { encryptor.GetBlockSize() } -> std::unsigned_integral; }; } // namespace Chaos::Cipher::Block diff --git a/ChaosTests/Cipher/Block/Des/DesCryptTests.cpp b/ChaosTests/Cipher/Block/Des/DesCryptTests.cpp index 417f765..d525671 100644 --- a/ChaosTests/Cipher/Block/Des/DesCryptTests.cpp +++ b/ChaosTests/Cipher/Block/Des/DesCryptTests.cpp @@ -486,9 +486,9 @@ TEST(DesCryptTests, OutIteratorUsageDecryptTest) } } -template -static std::vector EncryptThroughBase(const Encryptor & enc, - InputIt begin, InputIt end) +template +static std::vector EncryptGeneric(const EncryptorImpl & enc, + InputIt begin, InputIt end) { std::vector result; result.resize(enc.GetBlockSize(), 0); @@ -497,7 +497,7 @@ static std::vector EncryptThroughBase(const Encryptor & enc, return result; } -TEST(DesCryptTests, EncryptThroughBaseTest) +TEST(DesCryptTests, EncryptGenericTest) { std::array key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 }; @@ -507,16 +507,16 @@ TEST(DesCryptTests, EncryptThroughBaseTest) DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Encryptor enc(desKey); - ASSERT_EQ(expected, EncryptThroughBase(enc, data.begin(), data.end())); + ASSERT_EQ(expected, EncryptGeneric(enc, data.begin(), data.end())); } -template -static uint64_t EncryptUInt64BlockThroughBase(const Encryptor & enc, uint64_t block) +template +static uint64_t EncryptUInt64BlockGeneric(const EncryptorImpl & enc, uint64_t block) { return enc.EncryptBlock(block); } -TEST(DesCryptTests, EncryptUInt64BlockThroughBaseTest) +TEST(DesCryptTests, EncryptUInt64BlockGenericTest) { std::array key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 }; @@ -526,7 +526,7 @@ TEST(DesCryptTests, EncryptUInt64BlockThroughBaseTest) DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Encryptor enc(desKey); - ASSERT_EQ(expected, EncryptUInt64BlockThroughBase(enc, data)); + ASSERT_EQ(expected, EncryptUInt64BlockGeneric(enc, data)); } template