From 4cf79b61adf93ee06ea6c410292f73ebd1e4576f Mon Sep 17 00:00:00 2001 From: hashlag Date: Sat, 31 Jan 2026 18:12:43 +0300 Subject: [PATCH] Introduce Block::Encryptor<> CRTP base class. Inherit DesEncryptor from it. --- Chaos/Cipher/Block/Des/DesCrypt.hpp | 4 ++- Chaos/Cipher/Block/Encryptor.hpp | 40 +++++++++++++++++++++++++++ ChaosTests/Cipher/DesCryptTests.cpp | 43 +++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 Chaos/Cipher/Block/Encryptor.hpp diff --git a/Chaos/Cipher/Block/Des/DesCrypt.hpp b/Chaos/Cipher/Block/Des/DesCrypt.hpp index c2430cf..d4a2c11 100644 --- a/Chaos/Cipher/Block/Des/DesCrypt.hpp +++ b/Chaos/Cipher/Block/Des/DesCrypt.hpp @@ -7,6 +7,8 @@ #include "Service/ChaosException.hpp" #include "Service/SeArray.hpp" +#include "Cipher/Block/Encryptor.hpp" + namespace Chaos::Cipher::Block::Des::Inner_ { @@ -221,7 +223,7 @@ public: Inner_::RawKey Key_; }; - class DesEncryptor + class DesEncryptor : public Encryptor { public: DesEncryptor(const Key & key) diff --git a/Chaos/Cipher/Block/Encryptor.hpp b/Chaos/Cipher/Block/Encryptor.hpp new file mode 100644 index 0000000..bf15e5c --- /dev/null +++ b/Chaos/Cipher/Block/Encryptor.hpp @@ -0,0 +1,40 @@ +#ifndef CHAOS_CIPHER_BLOCK_ENCRYPTOR_HPP +#define CHAOS_CIPHER_BLOCK_ENCRYPTOR_HPP + +namespace Chaos::Cipher::Block +{ + +template +class Encryptor +{ +public: + template + void EncryptBlock(OutputIt out, InputIt inBegin, InputIt inEnd) + { + Impl().EncryptBlock(out, inBegin, inEnd); + } + + template + auto EncryptBlock(Block block) + { + return Impl().EncryptBlock(block); + } + +protected: + Encryptor() = default; + +private: + const T & Impl() const + { + return static_cast(*this); + } + + T & Impl() + { + return static_cast(*this); + } +}; + +} // namespace Chaos::Cipher::Block + +#endif // CHAOS_CIPHER_BLOCK_ENCRYPTOR_HPP diff --git a/ChaosTests/Cipher/DesCryptTests.cpp b/ChaosTests/Cipher/DesCryptTests.cpp index c10ac14..6b16c80 100644 --- a/ChaosTests/Cipher/DesCryptTests.cpp +++ b/ChaosTests/Cipher/DesCryptTests.cpp @@ -1,8 +1,10 @@ #include #include "Cipher/Block/Des/DesCrypt.hpp" +#include "Cipher/Block/Encryptor.hpp" using namespace Chaos::Cipher::Block::Des; +using namespace Chaos::Cipher::Block; TEST(DesCryptTests, KeyScheduleTest) { @@ -494,3 +496,44 @@ TEST(DesCryptTests, OutIteratorUsageDecryptTest) ASSERT_EQ(8, incrementCalls); } } + +template +static std::array EncryptThroughBase(Encryptor & enc, + InputIt begin, InputIt end) +{ + std::array result; + enc.EncryptBlock(result.begin(), begin, end); + return result; +} + +TEST(DesCryptTests, EncryptThroughBaseTest) +{ + std::array key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 }; + + std::array data = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef }; + std::array expected = { 0x85, 0xe8, 0x13, 0x54, 0x0f, 0x0a, 0xb4, 0x05 }; + + DesCrypt::Key desKey(key.begin(), key.end()); + DesCrypt::DesEncryptor enc(desKey); + + ASSERT_EQ(expected, EncryptThroughBase(enc, data.begin(), data.end())); +} + +template +static uint64_t EncryptUInt64BlockThroughBase(Encryptor & enc, uint64_t block) +{ + return enc.EncryptBlock(block); +} + +TEST(DesCryptTests, EncryptUInt64BlockThroughBaseTest) +{ + std::array key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 }; + + uint64_t data = 0x0123456789abcdef; + uint64_t expected = 0x85e813540f0ab405; + + DesCrypt::Key desKey(key.begin(), key.end()); + DesCrypt::DesEncryptor enc(desKey); + + ASSERT_EQ(expected, EncryptUInt64BlockThroughBase(enc, data)); +}