Introduce stateful DesCrypt::Encryptor class, remove static DesCrypt::EncryptBlock()

This commit is contained in:
hashlag
2026-01-23 21:31:04 +03:00
parent 3a2a665031
commit da5eaff182
2 changed files with 31 additions and 16 deletions

View File

@@ -207,22 +207,32 @@ public:
Inner_::RawKey Key_;
};
template<typename OutputIt, typename InputIt>
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<typename OutputIt, typename InputIt>
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;

View File

@@ -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);