# noise-peer

> Simple end-to-end encrypted, secure channels using Noise Protocol Framework and libsodium secretstream

Latest version **2.1.1** (published 2020-12-14) · ISC license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.1 |
| Published | 2020-12-14 |
| First published | 2018-10-11 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 29 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 147 |
| Author | Emil Bay |
| Maintainers | emilbayes |

## Links

- npm: https://www.npmjs.com/package/noise-peer
- Repository: https://github.com/emilbayes/noise-peer
- Homepage: https://github.com/emilbayes/noise-peer#readme
- Issues: https://github.com/emilbayes/noise-peer/issues
- npm.io page: https://npm.io/package/noise-peer

## Dependencies (4)

- [sodium-native](https://npm.io/package/sodium-native.md) ^3.1.1
- [readable-stream](https://npm.io/package/readable-stream.md) ^3.0.6
- [simple-handshake](https://npm.io/package/simple-handshake.md) ^3.0.0
- [secretstream-stream](https://npm.io/package/secretstream-stream.md) ^2.0.0

## Recent versions

- 2.1.1 (latest) — 2020-12-14
- 2.1.0 — 2020-05-24
- 1.1.0 — 2019-07-23
- 1.0.1 — 2018-12-03
- 1.0.0 — 2018-11-14
- 0.3.0 — 2018-11-14
- 0.2.2 — 2018-11-09
- 0.2.0 — 2018-10-16
- 0.1.1 — 2018-10-11

## README

# `noise-peer`

[![Build Status](https://travis-ci.org/emilbayes/noise-peer.svg?branch=master)](https://travis-ci.org/emilbayes/noise-peer)

> Simple end-to-end encrypted, secure channels using Noise Protocol Framework and libsodium secretstream

## Usage

Below is an example of a secure UPPERCASE echo server.

Server:

```js
var peer = require('noise-peer')
var through = require('through2')
var pump = require('pump')
var net = require('net')

var server = net.createServer(function (rawStream) {
  var sec = peer(rawStream, false)

  pump(sec, through(function (buf, _, cb) {
    cb(null, buf.toString().toUpperCase())
  }), sec)
})

server.listen(5000)
```

Client:

```js
var peer = require('noise-peer')
var pump = require('pump')
var net = require('net')

var rawStream = net.connect(5000)

var sec = peer(rawStream, true)

pump(sec, process.stdout)
sec.end('beep boop\n')
```

More examples are available in [examples](examples)

## API

### `var {publicKey, secretKey} = peer.keygen()`

Generate a new key pair for use with `noiseOpts`. See the Handshake Pattern
examples

### `var secureStream = peer(rawStream, isInitiator, [noiseOpts])`

Create a new peer, performing handshaking transparently. Note that all messages
are chunked to ~64kb size due to a 2 byte length header. By default the Noise
`NN` pattern is used, which simply creates a
[forward secret](https://en.wikipedia.org/wiki/Forward_secrecy) channel.
This does not authenticate either party. See below for other handshake pattern
exapmles.

### `secureStream.initiator`

Boolean indicating whether the stream is a client/initiator or a
server/responder, as given by the `isInitiator` constructor argument.

### `secureStream.rawStream`

Access to the `rawStream` passed in the constructor

### `secureStream.end([chunk][, encoding][, callback])`

Special mention, as this also sends a `FINISH` message to the other party, which
signals that the stream is to end and no more messages are to be expected. This
is important to know that an active adversary did not truncate the stream.

### `secureStream.setTimeout(timeout[, callback])`

Call `setTimeout` on the underlying `rawStream` if supported. This function will
be undefined if not supported

### `secureStream.on('timeout')`

Bubble `timeout` event from the underlying `rawStream` if supported. You should
call either `.end()` (which sends a `FINISH` message to the other peer) or
`.destroy()` the stream manually (like eg. `net` specifies).

### `secureStream.on('handshake', {remoteStaticKey, remoteEphemeralKey, handshakeHash})`

Emitted when the handshaking has succeeded. `remoteStaticKey` may be `null` if
you're using a `pattern` which does not use or receive a remote static key. All
will be Buffers, with keys be cleared immediately after the handshake event.
The `handshakeHash` can be used for channel binding as described in the
[Noise Specficiation](https://noiseprotocol.org/noise.html#channel-binding)

### `secureSteam.on('connected')`

Emitted when the secure connection is fully established

### Handshake Pattern examples

To have mutual authentication use the `XX` pattern, add a static keypair and
provide a `onstatickey` function on both sides:

```js
var opts = {
  pattern: 'XX',
  staticKeyPair: peer.keygen(),
  onstatickey: function (remoteKey, done) {
    if (remoteKey.equals(someSavedKey)) return done()

    return done(new Error('Unauthorized key'))
  }
}
```

To have have client authentication but use a preshared server public key use the
`XK` pattern, add a static keypair on both sides, a remote static key on the
client and provide a `onstatickey` function on the server:

```js
var serverKeys = peer.keygen()

var clientOpts = {
  pattern: 'XK',
  staticKeyPair: peer.keygen(),
  remoteStaticKey: serverKeys.publicKey
}

var serverOpts = {
  pattern: 'XK',
  staticKeyPair: serverKeys,
  onstatickey: function (remoteKey, done) {
    if (remoteKey.equals(someSavedClientKey)) return done()

    return done(new Error('Unauthorized key'))
  }
}
```

## Install

```sh
npm install noise-peer
```

## License

[ISC](LICENSE)

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