Compare commits

...

2 Commits

Author SHA1 Message Date
hashlag
797a5428cc Add Padder<>. A CRTP base for padding algorithms.
All checks were successful
Chaos Ci / test-and-benchmark (push) Successful in 1m59s
2026-02-08 23:44:51 +03:00
hashlag
5baede0a1d Rename PadPkcs7 --> PadderPkcs7. 2026-02-08 23:31:24 +03:00
4 changed files with 73 additions and 16 deletions

34
Chaos/Padding/Padder.hpp Normal file
View File

@@ -0,0 +1,34 @@
#ifndef CHAOS_PADDING_PADDER_HPP
#define CHAOS_PADDING_PADDER_HPP
namespace Chaos::Padding
{
template<typename T>
class Padder
{
public:
template<typename OutputIt>
void Pad(OutputIt begin, OutputIt end) const
{
Impl().Pad(begin, end);
}
protected:
Padder() = default;
private:
const T & Impl() const
{
return static_cast<const T &>(*this);
}
T & Impl()
{
return static_cast<T &>(*this);
}
};
} // namespace Chaos::Padding
#endif // CHAOS_PADDING_PADDER_HPP

View File

@@ -1,16 +1,17 @@
#ifndef CHAOS_PADDING_PADPKCS7_HPP #ifndef CHAOS_PADDING_PADDERPKCS7_HPP
#define CHAOS_PADDING_PADPKCS7_HPP #define CHAOS_PADDING_PADDERPKCS7_HPP
#include <cstdint> #include <cstdint>
#include <iterator> #include <iterator>
#include <limits> #include <limits>
#include "Padding/Padder.hpp"
#include "Service/ChaosException.hpp" #include "Service/ChaosException.hpp"
namespace Chaos::Padding namespace Chaos::Padding
{ {
class PadPkcs7 class PadderPkcs7 : public Padder<PadderPkcs7>
{ {
public: public:
template<typename OutputIt> template<typename OutputIt>
@@ -34,4 +35,4 @@ public:
} // namespace Chaos::Padding } // namespace Chaos::Padding
#endif // CHAOS_PADDING_PADPKCS7_HPP #endif // CHAOS_PADDING_PADDERPKCS7_HPP

View File

@@ -18,7 +18,7 @@ set(ChaosTests_SOURCE Hash/Md4HasherTests.cpp
Cipher/Arc4GenTests.cpp Cipher/Arc4GenTests.cpp
Cipher/Arc4CryptTests.cpp Cipher/Arc4CryptTests.cpp
Cipher/DesCryptTests.cpp Cipher/DesCryptTests.cpp
Padding/PadPkcs7Tests.cpp Padding/PadderPkcs7Tests.cpp
Service/SeArrayTests.cpp Service/SeArrayTests.cpp
Service/ChaosExceptionTests.cpp) Service/ChaosExceptionTests.cpp)

View File

@@ -3,7 +3,7 @@
#include <cstdint> #include <cstdint>
#include <vector> #include <vector>
#include "Padding/PadPkcs7.hpp" #include "Padding/PadderPkcs7.hpp"
#include "Service/ChaosException.hpp" #include "Service/ChaosException.hpp"
using namespace Chaos::Padding; using namespace Chaos::Padding;
@@ -14,7 +14,7 @@ TEST(PadPkcs7Tests, PadTest)
std::array<uint8_t, 1> fact = {}; std::array<uint8_t, 1> fact = {};
std::array<uint8_t, 1> expected = { 0x01 }; std::array<uint8_t, 1> expected = { 0x01 };
PadPkcs7::Pad(fact.begin(), fact.end()); PadderPkcs7::Pad(fact.begin(), fact.end());
ASSERT_EQ(expected, fact); ASSERT_EQ(expected, fact);
} }
@@ -26,7 +26,7 @@ TEST(PadPkcs7Tests, PadTest)
0x07, 0x07 0x07, 0x07
}; };
PadPkcs7::Pad(fact.begin(), fact.end()); PadderPkcs7::Pad(fact.begin(), fact.end());
ASSERT_EQ(expected, fact); ASSERT_EQ(expected, fact);
} }
@@ -38,7 +38,7 @@ TEST(PadPkcs7Tests, PadTest)
0x0a, 0x0a, 0x0a, 0x0a, 0x0a 0x0a, 0x0a, 0x0a, 0x0a, 0x0a
}; };
PadPkcs7::Pad(fact.begin(), fact.end()); PadderPkcs7::Pad(fact.begin(), fact.end());
ASSERT_EQ(expected, fact); ASSERT_EQ(expected, fact);
} }
@@ -46,7 +46,7 @@ TEST(PadPkcs7Tests, PadTest)
{ {
std::vector<uint8_t> fact(i, 0x00); std::vector<uint8_t> fact(i, 0x00);
PadPkcs7::Pad(fact.begin(), fact.end()); PadderPkcs7::Pad(fact.begin(), fact.end());
ASSERT_EQ(std::vector<uint8_t>(i, i), fact); ASSERT_EQ(std::vector<uint8_t>(i, i), fact);
} }
} }
@@ -56,19 +56,19 @@ TEST(PadPkcs7Tests, PadInvalidRangeTest)
{ {
std::array<uint8_t, 256> out = {}; std::array<uint8_t, 256> out = {};
ASSERT_THROW(PadPkcs7::Pad(out.begin(), out.end()), Chaos::Service::ChaosException); ASSERT_THROW(PadderPkcs7::Pad(out.begin(), out.end()), Chaos::Service::ChaosException);
} }
{ {
std::array<uint8_t, 500> out = {}; std::array<uint8_t, 500> out = {};
ASSERT_THROW(PadPkcs7::Pad(out.begin(), out.end()), Chaos::Service::ChaosException); ASSERT_THROW(PadderPkcs7::Pad(out.begin(), out.end()), Chaos::Service::ChaosException);
} }
{ {
std::array<uint8_t, 50> out = {}; std::array<uint8_t, 50> out = {};
ASSERT_THROW(PadPkcs7::Pad(out.end(), out.begin()), Chaos::Service::ChaosException); ASSERT_THROW(PadderPkcs7::Pad(out.end(), out.begin()), Chaos::Service::ChaosException);
} }
} }
@@ -84,7 +84,7 @@ TEST(PadPkcs7Tests, PadOutIteratorUsageTest)
0x00, 0x00, 0x00 0x00, 0x00, 0x00
}; };
PadPkcs7::Pad(fact.begin() + 3, fact.end() - 3); PadderPkcs7::Pad(fact.begin() + 3, fact.end() - 3);
ASSERT_EQ(expected, fact); ASSERT_EQ(expected, fact);
} }
@@ -99,7 +99,7 @@ TEST(PadPkcs7Tests, PadOutIteratorUsageTest)
0x00, 0x00, 0x00 0x00, 0x00, 0x00
}; };
PadPkcs7::Pad(fact.begin() + 3, fact.end() - 3); PadderPkcs7::Pad(fact.begin() + 3, fact.end() - 3);
ASSERT_EQ(expected, fact); ASSERT_EQ(expected, fact);
} }
@@ -115,7 +115,29 @@ TEST(PadPkcs7Tests, PadOutIteratorUsageTest)
0xbb, 0xbb, 0xbb, 0xbb, 0xbb 0xbb, 0xbb, 0xbb, 0xbb, 0xbb
}; };
PadPkcs7::Pad(fact.begin() + 5, fact.begin() + 5); PadderPkcs7::Pad(fact.begin() + 5, fact.begin() + 5);
ASSERT_EQ(expected, fact);
}
}
template<typename Impl, typename OutputIt>
void PadThroughBase(const Padder<Impl> & padder, OutputIt begin, OutputIt end)
{
padder.Pad(begin, end);
}
TEST(PadPkcs7Tests, PadThroughBaseTest)
{
{
std::array<uint8_t, 5> fact = {};
std::array<uint8_t, 5> expected =
{
0x05, 0x05, 0x05, 0x05, 0x05
};
const PadderPkcs7 padder;
PadThroughBase(padder, fact.begin(), fact.end());
ASSERT_EQ(expected, fact); ASSERT_EQ(expected, fact);
} }
} }