# sodium-encryption

> Sodium encryption that works in node and in the browser

Latest version **1.2.3** (published 2017-02-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install sodium-encryption
pnpm add sodium-encryption
yarn add sodium-encryption
bun add sodium-encryption
```

## 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 | 1.2.3 |
| Published | 2017-02-17 |
| First published | 2016-03-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 47 |
| Author | Mathias Buus |
| Maintainers | mafintosh |

## Links

- npm: https://www.npmjs.com/package/sodium-encryption
- Repository: https://github.com/mafintosh/sodium-encryption
- Issues: https://github.com/mafintosh/sodium-encryption/issues
- npm.io page: https://npm.io/package/sodium-encryption

## Dependencies (2)

- [tweetnacl](https://npm.io/package/tweetnacl.md) ^0.14.3
- [sodium-native](https://npm.io/package/sodium-native.md) ^1.5.1

## Recent versions

- 1.2.3 (latest) — 2017-02-17
- 1.2.2 — 2016-10-09
- 1.2.1 — 2016-10-08
- 1.2.0 — 2016-10-08
- 1.1.0 — 2016-05-03
- 1.0.1 — 2016-03-10
- 1.0.0 — 2016-03-10

## README

# sodium-encryption

[Sodium encryption](https://github.com/paixaop/node-sodium) that works in node and in the browser

```
npm install sodium-encryption
```

[![build status](http://img.shields.io/travis/mafintosh/sodium-encryption.svg?style=flat)](http://travis-ci.org/mafintosh/sodium-encryption)

## Usage

``` js
var encryption = require('sodium-encryption')

var key = encryption.key()
var nonce = encryption.nonce()
var message = Buffer('a message')

var box = encryption.encrypt(message, nonce, key)

console.log('encrypted message is:', box)
console.log('decrypted message is:', encryption.decrypt(box, nonce, key))
```

## API

#### `key = encryption.key()`

Generate a secret key. Returns a 32 byte buffer.

#### `nonce = encryption.nonce()`

Generate a nonce. Returns a 24 byte buffer.
A nonce can be public and you should use a new one everytime you use a key.

#### `box = encryption.encrypt(message, nonce, key)`

Encrypts a message.

#### `message = encryption.decrypt(box, nonce, key)`

Decrypt a message. Returns `null` is the message cannot be decrypted.

#### `keys = encryption.scalarMultiplicationKeyPair([secretKey])`

Generate a key pair to be used for scalar multiplication. If you don't pass in a secret key, one will be generated for you.
Returns a key pair consisting of a secret key and a public key.

``` js
{
  secretKey: <Buffer>,
  publicKey: <Buffer>
}
```

#### `sharedKey = encryption.scalarMultiplication(secretKey, otherPublicKey)`

Generate a shared key based on a remote public scalar multiplication key and your own secret key.
Returns the same shared key if called with corresponding secret and public key

``` js
var keys = encryption.scalarMultiplicationKeyPair()
var otherKeys = encryption.scalarMultiplicationKeyPair()

console.log('shared key', encryption.scalarMultiplication(keys.secretKey, otherKeys.publicKey))
console.log('shared key', encryption.scalarMultiplication(otherKeys.secretKey, keys.publicKey))
```

## License

MIT

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