aboutsummaryrefslogtreecommitdiffstats
path: root/library/sodium-plus/docs/SodiumPlus/randomness.md
blob: 182753c1008bf2db2a38f66aaebfd4702b0d4edf (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
## Randomness

> **See also:** [Libsodium's documentation on its random data features](https://download.libsodium.org/doc/generating_random_data).

### randombytes_buf

Obtain a buffer filled with random bytes.

**Parameters and their respective types**:

1. `{number}` Size of buffer to return

Returns a `Promise` that resolves to a `Buffer`

### randombytes_uniform

Generate an integer between 0 and upperBound (non-inclusive).

For example, randombytes_uniform(10) returns an integer between 0 and 9.

**Parameters and their respective types**:

1. `{number}` Upper bound

Returns a `Promise` that resolves to a `number`.

### Example for randombytes

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

(async function () {
    if (!sodium) sodium = await SodiumPlus.auto();
    
    let someBuf = await sodium.randombytes_buf(32);
    console.log(someBuf.toString('hex'));

    let someInt = await sodium.randombytes_uniform(65536);
    console.log(someInt);
})();
```