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