Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ add_executable(
monitor.cpp
utilities.cpp
timers.cpp
curve.cpp
)

target_include_directories(unit_tests PUBLIC ${CATCH_MODULE_PATH})
Expand Down
29 changes: 29 additions & 0 deletions tests/curve.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <catch2/catch_all.hpp>
#include <zmq.hpp>

TEST_CASE("curve keypair", "[curve]")
{
auto [public_key, secret_key] = zmq::curve::keypair();
CHECK(!public_key.empty());
CHECK(!secret_key.empty());
}

TEST_CASE("curve public_", "[curve]")
{
auto secret_key = "D:)Q[IlAW!ahhC2ac:9*A}h:p?([4%wOTJ%JR%cs";
auto public_key = zmq::curve::public_(secret_key);
CHECK(public_key == "Yne@$w-vo<fVvi]a<NY6T1ed:M$fCG*[IaLV{hID");
}

TEST_CASE("curve z85 encode", "[curve]")
{
std::vector<uint8_t> data{1,2,3,4,5,6,7,8};
auto encoded = zmq::curve::z85::encode(data);
CHECK(encoded == "0rJua1Qkhq");
}

TEST_CASE("curve z85 decode", "[curve]")
{
auto decoded = zmq::curve::z85::decode("0rJua1Qkhq");
CHECK(decoded == std::vector<uint8_t>{1,2,3,4,5,6,7,8});
}
52 changes: 52 additions & 0 deletions zmq.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@

#include <cassert>
#include <cstring>
#include <cstdint>

#include <type_traits>
#include <algorithm>
Expand Down Expand Up @@ -2874,6 +2875,57 @@ class timers

#endif // defined(ZMQ_CPP11) && defined(ZMQ_HAVE_TIMERS)

namespace curve {

inline std::pair<std::string, std::string> keypair()
{
char public_key_buffer[41];
char secret_key_buffer[41];
int rc = zmq_curve_keypair(public_key_buffer, secret_key_buffer);
if (rc == -1)
throw zmq::error_t();
return std::pair{public_key_buffer, secret_key_buffer};
}

inline std::string public_(const std::string& secret)
{
if (secret.size() != 40)
throw std::runtime_error("Invalid secret string size");
char public_key_buffer[41];
int rc = zmq_curve_public(public_key_buffer, secret.c_str());
if (rc == -1)
throw zmq::error_t();
return public_key_buffer;
}

namespace z85 {

inline std::string encode(const std::vector<uint8_t>& data)
{
size_t buffer_size = static_cast<double>(data.size()) * 1.25 + 1;
char *buffer = new char[buffer_size];
auto *result = zmq_z85_encode(buffer, data.data(), data.size());
if (result == nullptr)
throw zmq::error_t();
std::string dest(result);
delete [] buffer;
return dest;
}

inline std::vector<uint8_t> decode(const std::string& encoded)
{
size_t dest_size = static_cast<double>(encoded.size()) * 0.8;
std::vector<uint8_t> dest(dest_size);
auto *result = zmq_z85_decode(dest.data(), encoded.c_str());
if (result == nullptr)
throw zmq::error_t();
return dest;
}

}

}

} // namespace zmq

#endif // __ZMQ_HPP_INCLUDED__
Loading