peer-to-peer encrypted streams using public key cryptography and signing

Usage no npm install needed!

<script type="module">
  import securePeer from '';



Create encrypted peer-to-peer streams using public key cryptography and signing.

No certificates, no authorities. Each side of the connection has the same kind of keys so it doesn't matter which side initiates the connection.

build status


First generate some public/private keypairs with rsa-json:

$ rsa-json > a.json
$ rsa-json > b.json
var secure = require('secure-peer');
var peer = secure(require('./a.json'));
var through = require('through');

var net = require('net');
var server = net.createServer(function (rawStream) {
    var sec = peer(function (stream) {
        stream.pipe(through(function (buf) {
            this.emit('data', String(buf).toUpperCase());
var secure = require('secure-peer');
var peer = secure(require('./b.json'));

var net = require('net');
var rawStream = net.connect(5000);

var sec = peer(function (stream) {
    stream.end('beep boop\n');

sec.on('identify', function (id) {
    // you can asynchronously verify that the key matches the known value here

For extra security, you should keep a file around with known hosts to verify that the public key you receive on the first connection doesn't change later on like how ~/.ssh/known_hosts works.

Maintaining a known hosts file is outside the scope of this module.


var secure = require('secure-peer')

var peer = secure(keys)

Return a function to create streams given the keys supplied.

keys.private should be a private PEM string and keys.public should be a public PEM string.

You can generate keypairs with rsa-json.

var sec = peer(cb)

Create a new duplex stream sec


sec.on('connection', function (stream) {})

Emitted when the secure connection has been established successfully. is the identify object from the 'identify' event.

sec.on('identify', function (id) {})

Emitted when the connection identifies with its public key, id.key.

Each listener must call either id.accept() or id.reject().

The connection won't be accepted until all listeners call id.accept(). If any listener calls id.reject(), the connection will be aborted.


Accept the connection. This function must be called for every listener on the 'identify' event for the connection to succeed.


Reject the connection. The connection will not succeed even if id.accept() was called in another listener.

sec.on('header', function (header) {})

Emitted when the remote side provides a signed header.payload json string signed with its private key in header.hash.


With npm do:

npm install secure-peer