sodium-javascript/README.md

52 lines
1.4 KiB
Markdown
Raw Permalink Normal View History

2020-06-24 12:49:05 +00:00
# `sodium-javascript`
2017-01-24 10:41:06 +00:00
2020-06-24 12:49:05 +00:00
[![Build Status](https://travis-ci.org/sodium-friends/sodium-javascript.svg?branch=master)](https://travis-ci.org/sodium-friends/sodium-javascript)
2017-01-24 10:41:06 +00:00
2020-06-24 13:06:48 +00:00
> WIP - a pure javascript version of [sodium-native](https://github.com/sodium-friends/sodium-native).
2020-06-24 12:49:05 +00:00
Based on tweetnacl
2017-01-24 10:41:06 +00:00
## Usage
``` js
2020-06-24 12:49:05 +00:00
const sodium = require('sodium-javascript')
2017-01-24 10:41:06 +00:00
2020-06-24 12:49:05 +00:00
const key = Buffer.alloc(sodium.crypto_secretbox_KEYBYTES)
const nonce = Buffer.alloc(sodium.crypto_secretbox_NONCEBYTES)
2017-01-24 10:41:06 +00:00
sodium.randombytes_buf(key)
sodium.randombytes_buf(nonce)
2020-06-24 12:49:05 +00:00
const message = Buffer.from('Hello, World!')
const cipher = Buffer.alloc(message.length + sodium.crypto_secretbox_MACBYTES)
2017-01-24 10:41:06 +00:00
sodium.crypto_secretbox_easy(cipher, message, nonce, key)
console.log('Encrypted:', cipher)
2020-06-24 12:49:05 +00:00
const plainText = Buffer.alloc(cipher.length - sodium.crypto_secretbox_MACBYTES)
2017-01-24 10:41:06 +00:00
sodium.crypto_secretbox_open_easy(plainText, cipher, nonce, key)
console.log('Plaintext:', plainText.toString())
```
## API
2020-06-24 13:06:48 +00:00
See [sodium-native](https://github.com/sodium-friends/sodium-native).
2017-01-24 10:41:06 +00:00
This is a work in progress so all functions are not implemented yet.
2020-06-24 13:06:54 +00:00
This module is organised into individual submodules which can be required
independently for smaller bundles in the browser. To leverage automatic
switching between `sodium-javascript` and `sodium-native`, see
[`sodium-universal`](https://github.com/sodium-friends/sodium-universal).
2020-06-24 12:49:05 +00:00
## Install
```
npm install sodium-javascript
```
2017-01-24 10:41:06 +00:00
## License
2020-06-24 12:49:05 +00:00
[MIT](LICENSE)