CryptographyTest.cpp 1.4 KB

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