2
0

CryptographyTest.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <catch.hpp>
  2. #include <Cryptography.h>
  3. using namespace YumeBot;
  4. using namespace NatsuLib;
  5. TEST_CASE("Cryptography", "[Utility][Cryptography]")
  6. {
  7. using namespace Cryptography;
  8. SECTION("Tea")
  9. {
  10. using namespace Tea;
  11. constexpr const char text[] = "123456789123456789";
  12. constexpr const char key[] = "00000000000000000000000000000000";
  13. char result[CalculateOutputSize(std::size(text))]{};
  14. const auto formattedKey = FormatKey(Utility::ToByteSpan(key));
  15. // 0x30303030 == { '0', '0', '0', '0' }
  16. REQUIRE(std::all_of(std::cbegin(formattedKey), std::cend(formattedKey), [](std::uint32_t value) constexpr { return value == 0x30303030; }));
  17. const auto resultSize = Encrypt(Utility::ToByteSpan(text), Utility::ToByteSpan(result), formattedKey);
  18. char decryptResult[std::size(result)]{};
  19. const auto decryptResultSize = Decrypt(Utility::ToByteSpan(result), Utility::ToByteSpan(decryptResult), formattedKey);
  20. REQUIRE(resultSize == CalculateOutputSize(std::size(text)));
  21. REQUIRE(decryptResultSize == std::size(text));
  22. REQUIRE(std::memcmp(decryptResult, text, std::size(text)) == 0);
  23. }
  24. SECTION("Md5")
  25. {
  26. using namespace Md5;
  27. constexpr const char test[] = "test";
  28. std::byte result[16];
  29. Calculate(Utility::ToByteSpan(test).subspan(0, std::size(test) - 1), result);
  30. constexpr const std::uint8_t expectedResult[] = "\x09\x8f\x6b\xcd\x46\x21\xd3\x73\xca\xde\x4e\x83\x26\x27\xb4\xf6";
  31. constexpr const auto expectedResultStr = u8"098f6bcd4621d373cade4e832627b4f6"_nv;
  32. REQUIRE(std::memcmp(result, expectedResult, std::size(result)) == 0);
  33. Md5ToHexString(result, [&](const char* begin, const char* end)
  34. {
  35. REQUIRE(nStrView{ begin, end } == expectedResultStr);
  36. });
  37. }
  38. }