CryptographyTest.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include "pch.h"
  2. #include <catch.hpp>
  3. #include <Cryptography.h>
  4. using namespace YumeBot;
  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 == Cryptography::Tea::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, 4), result);
  30. constexpr const nByte expectedResult[] = "\x09\x8f\x6b\xcd\x46\x21\xd3\x73\xca\xde\x4e\x83\x26\x27\xb4\xf6";
  31. REQUIRE(std::memcmp(result, expectedResult, std::size(result)) == 0);
  32. }
  33. }