From da5eaff18268316c1aa9621d7f45849f5a4c9922 Mon Sep 17 00:00:00 2001 From: hashlag Date: Fri, 23 Jan 2026 21:31:04 +0300 Subject: [PATCH] Introduce stateful DesCrypt::Encryptor class, remove static DesCrypt::EncryptBlock() --- Chaos/Cipher/Des/DesCrypt.hpp | 32 +++++++++++++++++++---------- ChaosTests/Cipher/DesCryptTests.cpp | 15 +++++++++----- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/Chaos/Cipher/Des/DesCrypt.hpp b/Chaos/Cipher/Des/DesCrypt.hpp index 374225a..6d0f9a4 100644 --- a/Chaos/Cipher/Des/DesCrypt.hpp +++ b/Chaos/Cipher/Des/DesCrypt.hpp @@ -207,22 +207,32 @@ public: Inner_::RawKey Key_; }; - template - static void EncryptBlock(OutputIt out, InputIt inBegin, InputIt inEnd, const Key & key) + class Encryptor { - RawBlockArray block; + public: + Encryptor(const Key & key) + : Schedule_(key.Key_) + { } - int_fast8_t i = 0; - for (InputIt in = inBegin; i < block.Size() && in != inEnd; ++i, ++in) + template + void EncryptBlock(OutputIt out, InputIt inBegin, InputIt inEnd) { - block[i] = *in; + RawBlockArray block; + + int_fast8_t i = 0; + for (InputIt in = inBegin; i < block.Size() && in != inEnd; ++i, ++in) + { + block[i] = *in; + } + + Block encrypted = DesCrypt::EncryptBlock(Inner_::Bitwise::PackUInt64(block.Begin(), block.End()), Schedule_); + + Inner_::Bitwise::CrunchUInt64(out, encrypted); } - Inner_::KeySchedule schedule(key.Key_); - Block encrypted = EncryptBlock(Inner_::Bitwise::PackUInt64(block.Begin(), block.End()), schedule); - - Inner_::Bitwise::CrunchUInt64(out, encrypted); - } + private: + Inner_::KeySchedule Schedule_; + }; private: using Block = uint64_t; diff --git a/ChaosTests/Cipher/DesCryptTests.cpp b/ChaosTests/Cipher/DesCryptTests.cpp index 73828ec..d5c6b61 100644 --- a/ChaosTests/Cipher/DesCryptTests.cpp +++ b/ChaosTests/Cipher/DesCryptTests.cpp @@ -48,7 +48,8 @@ TEST(DesCryptTests, EncryptTest) result.fill(0); DesCrypt::Key desKey(key.begin(), key.end()); - DesCrypt::EncryptBlock(result.begin(), data.begin(), data.end(), desKey); + DesCrypt::Encryptor enc(desKey); + enc.EncryptBlock(result.begin(), data.begin(), data.end()); return result; } @@ -95,7 +96,8 @@ TEST(DesCryptTests, EncryptShortDataTest) result.resize(8, 0); DesCrypt::Key desKey(key.begin(), key.end()); - DesCrypt::EncryptBlock(result.begin(), data.begin(), data.end(), desKey); + DesCrypt::Encryptor enc(desKey); + enc.EncryptBlock(result.begin(), data.begin(), data.end()); return result; } @@ -128,7 +130,8 @@ TEST(DesCryptTests, EncryptLongDataTest) result.resize(8, 0); DesCrypt::Key desKey(key.begin(), key.end()); - DesCrypt::EncryptBlock(result.begin(), data.begin(), data.end(), desKey); + DesCrypt::Encryptor enc(desKey); + enc.EncryptBlock(result.begin(), data.begin(), data.end()); return result; } @@ -203,7 +206,8 @@ TEST(DesCryptTests, OutIteratorUsageTest) OutputItMock it(asteriskCalls, incrementCalls); DesCrypt::Key desKey(key.begin(), key.end()); - DesCrypt::EncryptBlock(it, data.begin(), data.end(), desKey); + DesCrypt::Encryptor enc(desKey); + enc.EncryptBlock(it, data.begin(), data.end()); ASSERT_EQ(8, asteriskCalls); ASSERT_EQ(8, incrementCalls); @@ -218,7 +222,8 @@ TEST(DesCryptTests, OutIteratorUsageTest) OutputItMock it(asteriskCalls, incrementCalls); DesCrypt::Key desKey(key.begin(), key.end()); - DesCrypt::EncryptBlock(it, data.begin(), data.end(), desKey); + DesCrypt::Encryptor enc(desKey); + enc.EncryptBlock(it, data.begin(), data.end()); ASSERT_EQ(8, asteriskCalls); ASSERT_EQ(8, incrementCalls);