aboutsummaryrefslogtreecommitdiffstats
path: root/library/sodium-plus/docs/SodiumPlus/stream-ciphers.md
blob: 3dc8d47b129cc70e48475bfb459dbf9304c97a28 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
## Stream Ciphers

> **See also**: [Libsodium's documentation on its stream cipher features](https://download.libsodium.org/doc/advanced/stream_ciphers/xsalsa20).

### crypto_stream

Obtain an arbitrary-length stream of pseudorandom bytes from a given 
nonce and key.

**Parameters and their respective types**:

1. `{number}` length
2. `{Buffer}` nonce (must be 24 bytes)
3. `{CryptographyKey}` key

Returns a `Promise` that resolves to a `Buffer` containing
the pseudorandom bytes.

### crypto_stream_xor

Encrypt a message with a given nonce and key.

> [**Danger: Unauthenticated encryption!**](https://tonyarcieri.com/all-the-crypto-code-youve-ever-written-is-probably-broken)
> Without a subsequent message authentication strategy, this is vulnerable to
> chosen-ciphertext attacks. Proceed with caution!

**Parameters and their respective types**:

1. `{string|Buffer}` plaintext
2. `{Buffer}` nonce (must be 24 bytes)
3. `{CryptographyKey}` key

Returns a `Promise` that resolves to a `Buffer` containing
the encrypted bytes.

### Example for crypto_stream

```javascript
const { SodiumPlus } = require('sodium-plus');

let sodium;
(async function () {
    if (!sodium) sodium = await SodiumPlus.auto();
    let key = await sodium.crypto_stream_keygen();
    let iv = await sodium.randombytes_buf(24);
    let output = await sodium.crypto_stream(64, iv, key);
    console.log(output);

    iv = await sodium.randombytes_buf(24);
    let plaintext = 'This is a secret message';
    let ciphertext = await sodium.crypto_stream_xor(plaintext, iv, key);
    let decrypted =  await sodium.crypto_stream_xor(ciphertext, iv, key);
    console.log(decrypted.toString());
})();
```