0
# Random Number Generation
1
2
Cryptographically secure random number generation for keys, nonces, salts, and other random values in sodium-native.
3
4
## Capabilities
5
6
### Random Buffer Generation
7
8
Fills a buffer with cryptographically secure random bytes.
9
10
```javascript { .api }
11
/**
12
* Fill buffer with cryptographically secure random bytes
13
* @param buffer - Buffer to fill with random data
14
*/
15
function randombytes_buf(buffer: Buffer): void;
16
```
17
18
**Usage Example:**
19
20
```javascript
21
const sodium = require('sodium-native');
22
23
// Generate random key
24
const key = Buffer.alloc(32);
25
sodium.randombytes_buf(key);
26
27
// Generate random nonce
28
const nonce = Buffer.alloc(24);
29
sodium.randombytes_buf(nonce);
30
31
// Generate random salt
32
const salt = Buffer.alloc(16);
33
sodium.randombytes_buf(salt);
34
```
35
36
### Deterministic Random Generation
37
38
Generates deterministic random bytes from a seed for reproducible randomness.
39
40
```javascript { .api }
41
/**
42
* Fill buffer with deterministic random bytes derived from seed
43
* @param buffer - Buffer to fill with deterministic random data
44
* @param seed - Seed buffer for deterministic generation
45
*/
46
function randombytes_buf_deterministic(buffer: Buffer, seed: Buffer): void;
47
```
48
49
**Usage Example:**
50
51
```javascript
52
const sodium = require('sodium-native');
53
54
// Create a seed
55
const seed = Buffer.alloc(sodium.randombytes_SEEDBYTES);
56
sodium.randombytes_buf(seed);
57
58
// Generate deterministic random data
59
const deterministicData1 = Buffer.alloc(32);
60
const deterministicData2 = Buffer.alloc(32);
61
62
sodium.randombytes_buf_deterministic(deterministicData1, seed);
63
sodium.randombytes_buf_deterministic(deterministicData2, seed);
64
65
// deterministicData1 and deterministicData2 will be identical
66
console.log(deterministicData1.equals(deterministicData2)); // true
67
```
68
69
## Constants
70
71
```javascript { .api }
72
// Size of seed for deterministic random generation
73
const randombytes_SEEDBYTES: number;
74
```
75
76
## Common Patterns
77
78
### Key Generation
79
80
```javascript
81
const sodium = require('sodium-native');
82
83
// Generate different types of keys
84
const secretboxKey = Buffer.alloc(sodium.crypto_secretbox_KEYBYTES);
85
const signSecretKey = Buffer.alloc(sodium.crypto_sign_SECRETKEYBYTES);
86
const hashKey = Buffer.alloc(sodium.crypto_generichash_KEYBYTES);
87
88
sodium.randombytes_buf(secretboxKey);
89
sodium.randombytes_buf(signSecretKey);
90
sodium.randombytes_buf(hashKey);
91
```
92
93
### Nonce Generation
94
95
```javascript
96
const sodium = require('sodium-native');
97
98
// Generate nonces for different operations
99
const secretboxNonce = Buffer.alloc(sodium.crypto_secretbox_NONCEBYTES);
100
const boxNonce = Buffer.alloc(sodium.crypto_box_NONCEBYTES);
101
const streamNonce = Buffer.alloc(sodium.crypto_stream_NONCEBYTES);
102
103
sodium.randombytes_buf(secretboxNonce);
104
sodium.randombytes_buf(boxNonce);
105
sodium.randombytes_buf(streamNonce);
106
```
107
108
### Salt Generation
109
110
```javascript
111
const sodium = require('sodium-native');
112
113
// Generate salts for password hashing
114
const pwhashSalt = Buffer.alloc(sodium.crypto_pwhash_SALTBYTES);
115
const scryptSalt = Buffer.alloc(sodium.crypto_pwhash_scryptsalsa208sha256_SALTBYTES);
116
117
sodium.randombytes_buf(pwhashSalt);
118
sodium.randombytes_buf(scryptSalt);
119
```