Skip to content

Commit 3d2e3a9

Browse files
committed
Add curve functions wrappers
1 parent 8bace31 commit 3d2e3a9

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ add_executable(
3131
monitor.cpp
3232
utilities.cpp
3333
timers.cpp
34+
curve.cpp
3435
)
3536

3637
target_include_directories(unit_tests PUBLIC ${CATCH_MODULE_PATH})

tests/curve.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <catch2/catch_all.hpp>
2+
#include <zmq.hpp>
3+
4+
TEST_CASE("curve keypair", "[curve]")
5+
{
6+
auto [public_key, secret_key] = zmq::curve::keypair();
7+
CHECK(!public_key.empty());
8+
CHECK(!secret_key.empty());
9+
}
10+
11+
TEST_CASE("curve public_", "[curve]")
12+
{
13+
auto secret_key = "D:)Q[IlAW!ahhC2ac:9*A}h:p?([4%wOTJ%JR%cs";
14+
auto public_key = zmq::curve::public_(secret_key);
15+
CHECK(public_key == "Yne@$w-vo<fVvi]a<NY6T1ed:M$fCG*[IaLV{hID");
16+
}
17+
18+
TEST_CASE("curve z85 encode", "[curve]")
19+
{
20+
std::vector<uint8_t> data{1,2,3,4,5,6,7,8};
21+
auto encoded = zmq::curve::z85::encode(data);
22+
CHECK(encoded == "0rJua1Qkhq");
23+
}
24+
25+
TEST_CASE("curve z85 decode", "[curve]")
26+
{
27+
auto decoded = zmq::curve::z85::decode("0rJua1Qkhq");
28+
CHECK(decoded == std::vector<uint8_t>{1,2,3,4,5,6,7,8});
29+
}

zmq.hpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107

108108
#include <cassert>
109109
#include <cstring>
110+
#include <cstdint>
110111

111112
#include <type_traits>
112113
#include <algorithm>
@@ -2874,6 +2875,57 @@ class timers
28742875

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

2878+
namespace curve {
2879+
2880+
inline std::pair<std::string, std::string> keypair()
2881+
{
2882+
char public_key_buffer[41];
2883+
char secret_key_buffer[41];
2884+
int rc = zmq_curve_keypair(public_key_buffer, secret_key_buffer);
2885+
if (rc == -1)
2886+
throw zmq::error_t();
2887+
return std::pair{public_key_buffer, secret_key_buffer};
2888+
}
2889+
2890+
inline std::string public_(const std::string& secret)
2891+
{
2892+
if (secret.size() != 40)
2893+
throw std::runtime_error("Invalid secret string size");
2894+
char public_key_buffer[41];
2895+
int rc = zmq_curve_public(public_key_buffer, secret.c_str());
2896+
if (rc == -1)
2897+
throw zmq::error_t();
2898+
return public_key_buffer;
2899+
}
2900+
2901+
namespace z85 {
2902+
2903+
inline std::string encode(const std::vector<uint8_t>& data)
2904+
{
2905+
size_t buffer_size = static_cast<double>(data.size()) * 1.25 + 1;
2906+
char *buffer = new char[buffer_size];
2907+
auto *result = zmq_z85_encode(buffer, data.data(), data.size());
2908+
if (result == nullptr)
2909+
throw zmq::error_t();
2910+
std::string dest(result);
2911+
delete [] buffer;
2912+
return dest;
2913+
}
2914+
2915+
inline std::vector<uint8_t> decode(const std::string& encoded)
2916+
{
2917+
size_t dest_size = static_cast<double>(encoded.size()) * 0.8;
2918+
std::vector<uint8_t> dest(dest_size);
2919+
auto *result = zmq_z85_decode(dest.data(), encoded.c_str());
2920+
if (result == nullptr)
2921+
throw zmq::error_t();
2922+
return dest;
2923+
}
2924+
2925+
}
2926+
2927+
}
2928+
28772929
} // namespace zmq
28782930

28792931
#endif // __ZMQ_HPP_INCLUDED__

0 commit comments

Comments
 (0)