# secure-peer

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

Latest version **0.2.1** (published 2013-08-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install secure-peer
pnpm add secure-peer
yarn add secure-peer
bun add secure-peer
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.1 |
| Published | 2013-08-03 |
| First published | 2012-11-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | James Halliday |
| Maintainers | substack |
| Keywords | crypto, diffie-hellman, rsa, encryption |

## Links

- npm: https://www.npmjs.com/package/secure-peer
- Repository: https://github.com/substack/secure-peer
- Issues: https://github.com/substack/secure-peer/issues
- npm.io page: https://npm.io/package/secure-peer

## Dependencies (3)

- [through](https://npm.io/package/through.md) ~1.1.1
- [inherits](https://npm.io/package/inherits.md) ~1.0.0
- [event-stream](https://npm.io/package/event-stream.md) ~3.0.7

## Alternatives

- [@gemini-wallet/core](https://npm.io/package/@gemini-wallet/core.md) — 515.6K weekly downloads
- [utility](https://npm.io/package/utility.md) — 416.6K weekly downloads
- [@primno/dpapi](https://npm.io/package/@primno/dpapi.md) — 7.2K weekly downloads
- [pi-readseek](https://npm.io/package/pi-readseek.md) — 3.7K weekly downloads
- [@emilia-protocol/verify](https://npm.io/package/@emilia-protocol/verify.md) — 1.1K weekly downloads

## Recent versions

- 0.2.1 (latest) — 2013-08-03
- 0.2.0 — 2012-11-13
- 0.1.2 — 2012-11-09
- 0.1.1 — 2012-11-09
- 0.1.0 — 2012-11-09
- 0.0.2 — 2012-11-08
- 0.0.1 — 2012-11-08
- 0.0.0 — 2012-11-08

## README

# secure-peer

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](https://secure.travis-ci.org/substack/secure-peer.png)](http://travis-ci.org/substack/secure-peer)

# example

First generate some public/private keypairs with
[rsa-json](http://github.com/substack/rsa-json):

```
$ rsa-json > a.json
$ rsa-json > b.json
```

``` js
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());
        })).pipe(stream);
    });
    sec.pipe(rawStream).pipe(sec);
});
server.listen(5000);
```

``` js
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.pipe(process.stdout);
    stream.end('beep boop\n');
});
sec.pipe(rawStream).pipe(sec);

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

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.

# methods

``` js
var secure = require('secure-peer')
```

## var peer = secure(keys, opts={})

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](http://github.com/substack/rsa-json).

You can set a preference ordering array of ciphers to use with `opts.ciphers`.
Both sides will use a deterministic ordinal voting algorithm to determine which
cipher to use.
See `openssl list-cipher-algorithms` for the whole list.

## var sec = peer(cb)

Create a new duplex stream `sec` that caries the encrypted contents. This stream
is safe to stream over the wire, including untrusted networks.

`cb` is a shorthand to listen on the `'connection'` event just like
`net.createServer()`.

# events

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

Emitted with the decrypted plaintext stream when the secure connection has been
established successfully.

`stream.id` 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.

### id.accept()

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

### id.reject()

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.

# install

With [npm](https://npmjs.org) do:

```
npm install secure-peer
```

# license

MIT

---
_Source: https://npm.io/package/secure-peer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
