UNPKG

1.47 kBMarkdownView Raw
1# `sodium-javascript`
2
3[![Build Status](https://travis-ci.org/sodium-friends/sodium-javascript.svg?branch=master)](https://travis-ci.org/sodium-friends/sodium-javascript)
4
5> WIP - a pure javascript version of [sodium-native](https://github.com/sodium-friends/sodium-native).
6Based on tweetnacl
7
8## Usage
9
10``` js
11const sodium = require('sodium-javascript')
12
13const key = Buffer.alloc(sodium.crypto_secretbox_KEYBYTES)
14const nonce = Buffer.alloc(sodium.crypto_secretbox_NONCEBYTES)
15
16sodium.randombytes_buf(key)
17sodium.randombytes_buf(nonce)
18
19const message = Buffer.from('Hello, World!')
20const cipher = Buffer.alloc(message.length + sodium.crypto_secretbox_MACBYTES)
21
22sodium.crypto_secretbox_easy(cipher, message, nonce, key)
23
24console.log('Encrypted:', cipher)
25
26const plainText = Buffer.alloc(cipher.length - sodium.crypto_secretbox_MACBYTES)
27
28sodium.crypto_secretbox_open_easy(plainText, cipher, nonce, key)
29
30console.log('Plaintext:', plainText.toString())
31```
32
33## API
34
35See [sodium-native](https://github.com/sodium-friends/sodium-native).
36This is a work in progress so all functions are not implemented yet.
37
38This module is organised into individual submodules which can be required
39independently for smaller bundles in the browser. To leverage automatic
40switching between `sodium-javascript` and `sodium-native`, see
41[`sodium-universal`](https://github.com/sodium-friends/sodium-universal).
42
43## Install
44
45```
46npm install sodium-javascript
47```
48
49## License
50
51[MIT](LICENSE)