CryptographyTest.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <Cryptography.h>
  2. #include <catch2/catch.hpp>
  3. #include <cstring>
  4. #include <iterator>
  5. using namespace YumeBot;
  6. using namespace Cafe;
  7. using namespace Encoding;
  8. using namespace StringLiterals;
  9. TEST_CASE("Cryptography", "[Utility][Cryptography]")
  10. {
  11. using namespace Cryptography;
  12. SECTION("Tea")
  13. {
  14. using namespace Tea;
  15. constexpr const char text[] = "123456789123456789";
  16. constexpr const char key[] = "00000000000000000000000000000000";
  17. char result[CalculateOutputSize(std::size(text))]{};
  18. const auto formattedKey = FormatKey(gsl::as_bytes(gsl::make_span(key)));
  19. // 0x30303030 == { '0', '0', '0', '0' }
  20. REQUIRE(std::all_of(
  21. std::cbegin(formattedKey), std::cend(formattedKey),
  22. [](std::uint32_t value) constexpr { return value == 0x30303030; }));
  23. const auto resultSize = Encrypt(gsl::as_bytes(gsl::make_span(text)),
  24. gsl::as_writeable_bytes(gsl::make_span(result)), formattedKey);
  25. char decryptResult[std::size(result)]{};
  26. const auto decryptResultSize =
  27. Decrypt(gsl::as_bytes(gsl::make_span(result)),
  28. gsl::as_writeable_bytes(gsl::make_span(decryptResult)), formattedKey);
  29. REQUIRE(resultSize == CalculateOutputSize(std::size(text)));
  30. REQUIRE(decryptResultSize == std::size(text));
  31. REQUIRE(std::memcmp(decryptResult, text, std::size(text)) == 0);
  32. }
  33. SECTION("Md5")
  34. {
  35. using namespace Md5;
  36. constexpr const char test[] = "test";
  37. std::byte result[16];
  38. Calculate(gsl::as_bytes(gsl::make_span(test)).subspan(0, std::size(test) - 1), result);
  39. constexpr const std::uint8_t expectedResult[] =
  40. "\x09\x8f\x6b\xcd\x46\x21\xd3\x73\xca\xde\x4e\x83\x26\x27\xb4\xf6";
  41. constexpr const auto expectedResultStr = u8"098f6bcd4621d373cade4e832627b4f6"_sv;
  42. REQUIRE(std::memcmp(result, expectedResult, std::size(result)) == 0);
  43. Md5ToHexString(result, [&](StringView<CodePage::Utf8> const& str) {
  44. REQUIRE(str == expectedResultStr.Trim());
  45. });
  46. }
  47. }