Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZeroMQ curve-based security in Node js

I am trying to connect my web app (Node js) to a zmq backend which is written in python and using zmq curve encryption. But I am unable to find any helpful example or another resource about curve encryption in Node js. I tried this code :

      const zmq = require("zeromq");

      async function run(){
          const sock = new zmq.Request;
          sock.curve_publickey =  'wka2b<1234]+x64D%/a?+l0QS*3XRfhn$i!}3lM}'  // Not the original keys. ;-)
          sock.curve_secretkey =  '?frhwU4mFY1g168tXp64(N6sr/nn{=pQdYP**Ej*'
          sock.curve_serverkey =  '4:U}SW6z?:!]uE44]46.TRGV^4z55+(TTX$}C(DC'
          
          sock.connect("tcp://localhost:9090")
          console.log("Connected to Server")
          
          await sock.send(`Request=GenerateModels|UserId=${user}|FileName=${req.params.name}`)
          const [result] = await sock.receive()
          console.log(result.toString('utf8'))
      }
      
      run()

But it doesn't connect to the server. I think I am missing the correct syntax for this. Any help or link to the documentation (searched but no luck) to curve security in zmq (Node js) would be really helpful.

like image 223
Tink3r-t Avatar asked Jul 02 '26 07:07

Tink3r-t


2 Answers

Server:

s.setsockopt(zmq.ZMQ_CURVE_SERVER, 1);
s.setsockopt(zmq.ZMQ_CURVE_SECRETKEY, new Buffer.from("]W{YFJ^->}wG7*m-o/z8L$yWw}<R%c*]EcU.xDQV"));
s.setsockopt(zmq.ZMQ_CURVE_PUBLICKEY, new Buffer.from("rHLUXzqQG$ex{KD/7B#GcT%)f}Li&wo^Ctd*+?Ir"));

Client:

ckeys = zmq.zmqCurveKeypair();
c.setsockopt(zmq.ZMQ_CURVE_SERVER, 0);
c.setsockopt(zmq.ZMQ_CURVE_SECRETKEY, new Buffer.from(ckeys.secret));
c.setsockopt(zmq.ZMQ_CURVE_PUBLICKEY, new Buffer.from(ckeys.public));
c.setsockopt(zmq.ZMQ_CURVE_SERVERKEY, new Buffer.from("rHLUXzqQG$ex{KD/7B#GcT%)f}Li&wo^Ctd*+?Ir"));
like image 155
uid2532 Avatar answered Jul 04 '26 21:07

uid2532


Here's how you do it with the latest version 6.x.x.

var zmq = require('zeromq');

const serverKeypair = zmq.curveKeyPair();
const clientKeypair = zmq.curveKeyPair();

// SERVER
(async () => {

    const sock = new zmq.Publisher({
        linger: 0,
        curveServer: true,
        curvePublicKey: serverKeypair.publicKey,
        curveSecretKey: serverKeypair.secretKey,
    });

    await sock.bind("tcp://127.0.0.1:3000");
    ...

})();

// CLIENT
(async () => {

    const sock = new zmq.Subscriber({
        linger: 0,
        curveServerKey: serverKeypair.publicKey,
        curvePublicKey: clientKeypair.publicKey,
        curveSecretKey: clientKeypair.secretKey,
    });

    sock.connect("tcp://127.0.0.1:3000")
    ...

})();
like image 26
xpepermint Avatar answered Jul 04 '26 21:07

xpepermint



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!