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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//! # Secure Random Number Generation
//!
//! This module provides functions for generating cryptographically secure random numbers
//! and bytes. It uses libsodium's random number generator, which is designed to be
//! suitable for cryptographic operations.
//!
//! ## Security Considerations
//!
//! - The random number generator is automatically seeded with sufficient entropy during
//! library initialization.
//! - The implementation is designed to be resistant to side-channel attacks.
//! - The generator is suitable for generating cryptographic keys, nonces, and other
//! security-sensitive values.
//!
//! ## Available Functions
//!
//! - [`bytes`]: Generate a vector of random bytes
//! - [`fill_bytes`]: Fill an existing buffer with random bytes
//! - [`u32()`]: Generate a random 32-bit unsigned integer
//! - [`uniform`]: Generate a random 32-bit unsigned integer within a range
//!
//! ## Example
//!
//! ```rust
//! use libsodium_rs as sodium;
//! use sodium::random;
//! use sodium::ensure_init;
//!
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//! ensure_init()?;
//!
//! // Generate 32 random bytes (suitable for a key)
//! let random_bytes = random::bytes(32);
//! assert_eq!(random_bytes.len(), 32);
//!
//! // Fill an existing buffer with random bytes
//! let mut buffer = [0u8; 16];
//! random::fill_bytes(&mut buffer);
//!
//! // Generate a random 32-bit unsigned integer
//! let random_u32 = random::u32();
//!
//! // Generate a random integer between 0 and 99 (inclusive)
//! let dice_roll = random::uniform(100);
//! assert!(dice_roll < 100);
//!
//! Ok(())
//! }
//! ```
//!
use libsodium_sys;
/// Generate random bytes
/// Fill a buffer with random bytes
///
/// This function fills the provided buffer with random bytes.
/// It cannot fail and does not return a Result.
/// Generate a random 32-bit unsigned integer
/// Generate a random 32-bit unsigned integer between 0 and upper_bound (exclusive)