Compare commits

...

1 Commits

Author SHA1 Message Date
hashlag
2a3185406b Add PKCS#7 padding implementation.
All checks were successful
Chaos Ci / test-and-benchmark (push) Successful in 1m58s
2026-02-08 23:05:57 +03:00
3 changed files with 159 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
#ifndef CHAOS_PADDING_PADPKCS7_HPP
#define CHAOS_PADDING_PADPKCS7_HPP
#include <cstdint>
#include <iterator>
#include <limits>
#include "Service/ChaosException.hpp"
namespace Chaos::Padding
{
class PadPkcs7
{
public:
template<typename OutputIt>
static void Pad(OutputIt begin, OutputIt end)
{
auto dist = std::distance(begin, end);
if (dist >= 0 && dist <= std::numeric_limits<uint8_t>::max())
{
for (OutputIt it = begin; it != end; ++it)
{
*it = static_cast<uint8_t>(dist);
}
}
else
{
throw Service::ChaosException("PadPkcs7::Pad(): invalid range");
}
}
};
} // namespace Chaos::Padding
#endif // CHAOS_PADDING_PADPKCS7_HPP

View File

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

View File

@@ -0,0 +1,121 @@
#include <gtest/gtest.h>
#include <array>
#include <cstdint>
#include <vector>
#include "Padding/PadPkcs7.hpp"
#include "Service/ChaosException.hpp"
using namespace Chaos::Padding;
TEST(PadPkcs7Tests, PadTest)
{
{
std::array<uint8_t, 1> fact = {};
std::array<uint8_t, 1> expected = { 0x01 };
PadPkcs7::Pad(fact.begin(), fact.end());
ASSERT_EQ(expected, fact);
}
{
std::array<uint8_t, 7> fact = {};
std::array<uint8_t, 7> expected =
{
0x07, 0x07, 0x07, 0x07, 0x07,
0x07, 0x07
};
PadPkcs7::Pad(fact.begin(), fact.end());
ASSERT_EQ(expected, fact);
}
{
std::array<uint8_t, 10> fact = {};
std::array<uint8_t, 10> expected =
{
0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
0x0a, 0x0a, 0x0a, 0x0a, 0x0a
};
PadPkcs7::Pad(fact.begin(), fact.end());
ASSERT_EQ(expected, fact);
}
for (int i = 0; i < 256; ++i)
{
std::vector<uint8_t> fact(i, 0x00);
PadPkcs7::Pad(fact.begin(), fact.end());
ASSERT_EQ(std::vector<uint8_t>(i, i), fact);
}
}
TEST(PadPkcs7Tests, PadInvalidRangeTest)
{
{
std::array<uint8_t, 256> out = {};
ASSERT_THROW(PadPkcs7::Pad(out.begin(), out.end()), Chaos::Service::ChaosException);
}
{
std::array<uint8_t, 500> out = {};
ASSERT_THROW(PadPkcs7::Pad(out.begin(), out.end()), Chaos::Service::ChaosException);
}
{
std::array<uint8_t, 50> out = {};
ASSERT_THROW(PadPkcs7::Pad(out.end(), out.begin()), Chaos::Service::ChaosException);
}
}
TEST(PadPkcs7Tests, PadOutIteratorUsageTest)
{
{
std::array<uint8_t, 28> fact = {};
std::array<uint8_t, 28> expected =
{
0x00, 0x00, 0x00,
0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16,
0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16,
0x00, 0x00, 0x00
};
PadPkcs7::Pad(fact.begin() + 3, fact.end() - 3);
ASSERT_EQ(expected, fact);
}
{
std::array<uint8_t, 39> fact = {};
std::array<uint8_t, 39> expected =
{
0x00, 0x00, 0x00,
0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21,
0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21,
0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21,
0x00, 0x00, 0x00
};
PadPkcs7::Pad(fact.begin() + 3, fact.end() - 3);
ASSERT_EQ(expected, fact);
}
{
std::array<uint8_t, 10> fact =
{
0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
0xbb, 0xbb, 0xbb, 0xbb, 0xbb
};
std::array<uint8_t, 10> expected =
{
0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
0xbb, 0xbb, 0xbb, 0xbb, 0xbb
};
PadPkcs7::Pad(fact.begin() + 5, fact.begin() + 5);
ASSERT_EQ(expected, fact);
}
}