transform.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * Based on
  3. * 1. OpenSSL lib
  4. * 2. PurpleI2P source code
  5. * 3. cppcodec lib
  6. *
  7. * PUBLIC DOMAIN C++ WRAPPER
  8. * acetone, 2022
  9. */
  10. #ifndef TRANSFORM_H
  11. #define TRANSFORM_H
  12. #include <vector>
  13. #include <array>
  14. using uint8_t = unsigned char;
  15. namespace FriendlyCrypto {
  16. namespace Transform {
  17. template<size_t T>
  18. std::vector<uint8_t> arrayToVector (const std::array<uint8_t, T> &array) noexcept
  19. {
  20. std::vector<uint8_t> result;
  21. for (const auto& byte: array)
  22. {
  23. result.push_back(byte);
  24. }
  25. return result;
  26. }
  27. template <size_t T>
  28. std::array<uint8_t, T> vectorToArray (const std::vector<uint8_t>& vector) noexcept
  29. {
  30. static_assert (T<512, "FriendlyCrypto::Transform::vectorToArray size of array too big (>512B). "
  31. "For sure using a std::array which uses the stack (not the heap) is a bad solution for you.");
  32. std::array<uint8_t, T> result {0};
  33. for (size_t i = 0; i < T and i < vector.size(); ++i)
  34. {
  35. result[i] = vector[i];
  36. }
  37. return result;
  38. }
  39. } // namespace Transform
  40. } // namespace FriendlyCrypto
  41. #endif // TRANSFORM_H