Suggestion for encrypting data with ZeroMQ

Viewed 1534

I'm exercising with ØMQ and have developed a simple publisher/subscribers using cppzmq with reference to zguide. Please find code snippet of the same as below, I'm able to successfully publish and subscribe data and memory footprint also quite impressive. Since this is the distributed messaging system I would like to use it in my project.

publisher.cpp

int main (int argc, char * argv[]) {
//  Prepare our context and publisher
zmq::context_t context(1);
zmq::socket_t publisher(context, ZMQ_PUB);

std::string addr("tcp://*:");
addr += ((argc > 1) ? std::string(argv[1]) : "5563");
std::string tappend((argc > 2) ? std::string(argv[2]) : "");
publisher.bind(addr.c_str());

struct timeval timeofday;

for(unsigned int idx = 0; idx < Total_Topics; ++idx)
{
    Topics.emplace(std::make_pair(idx, std::string("temperature/celsius" + tappend + std::to_string(idx))));
}

std::this_thread::sleep_for (std::chrono::seconds(2));

for(unsigned int NoOfTimes = 0; NoOfTimes < 20000; ++NoOfTimes)
{
    for(unsigned int count = 0; count < Topics.size(); ++count)
    {
      {
         Quote quote = {};
         quote.ticker = "BHELL";
         gettimeofday(&timeofday,NULL);
         quote.timestampus = timeofday.tv_usec;
         quote.timestamps = timeofday.tv_sec;

         std::stringstream ss;

         quote.value *= 0.1;
         quote.data = std::to_string(NoOfTimes) + std::to_string(count);

         ss << quote;

         s_sendmore(publisher, Topics[count]);
         std::cout << "Publish "<< Topics[count] << std::endl;
         s_send(publisher, ss.str());
      }
    }

    std::this_thread::sleep_for (std::chrono::seconds(8));
    }

    return 0;
}

subscriber.cpp

int main (int argc, char * argv[]) {
//  Prepare our context and subscriber
zmq::context_t context(1);
zmq::socket_t subscriber (context, ZMQ_SUB);

std::string addr("tcp://localhost:");
addr += ((argc > 1) ? std::string(argv[1]) : "5563");
std::string tappend((argc > 2) ? std::string(argv[2]) : "");
subscriber.connect(addr.c_str());

struct timeval timeofday;

std::string topic("");

for(unsigned int idx = 0; idx < Total_Topics; ++idx)
{
    topic = std::string("temperature/celsius" + tappend + std::to_string(idx));
    subscriber.setsockopt( ZMQ_SUBSCRIBE, topic.c_str(), topic.length());
}

while(1)
{
    {
        Quote quote;
        std::stringstream ss;

        std::string address = s_recv (subscriber);
        std::cout<<"Msg Rcvd" << std::endl;
        std::cout<<"Topic: " << address << std::endl;

        std::string contents = s_recv (subscriber);

        ss << contents;
        ss >> quote;

        gettimeofday(&timeofday,NULL);
        std::cout << "us diff  "  << (timeofday.tv_usec - quote.timestampus) << std::endl;
        std::cout << "Sec diff "  << (timeofday.tv_sec - quote.timestamps) << std::endl;
    }
}
    return 0;
}

Now I would like to know which security technique should be used with zeroMQ. I understand there are curveZMQ and czmq. But I'm not sure which one should I use. Anybody already has used any of these or you have anyother libraries for ZeroMQ security. Please advise.

1 Answers

After digging up further on the zeroMQ encryption, I found that libcurve (C version) and libzmq (C++ version) supports the curve security. By default libzmq supports the curve and libcurve requires libsodium, libzmq, and czmq.

To find out what the difference between libcurve and libzmq curve implementation, you can through the discussion here.

Please find for sample PUB/SUB implementation with libcurve here.

Related