0
# Random Number Generation
1
2
Random number generation utilities for cryptographic operations. Note: These functions are deprecated and provided for backward compatibility only.
3
4
## Capabilities
5
6
### Random Number Seeding (Deprecated)
7
8
Functions for seeding the OpenSSL random number generator. These are deprecated as modern OpenSSL versions handle random seeding automatically.
9
10
```python { .api }
11
@deprecated
12
def add(buffer: bytes, entropy: int) -> None:
13
"""
14
Add random bytes to OpenSSL's entropy pool.
15
16
Parameters:
17
- buffer: Random bytes to add to entropy pool
18
- entropy: Estimate of entropy in buffer (in bytes)
19
20
Note: This function is deprecated. Modern OpenSSL versions
21
automatically seed the random number generator.
22
"""
23
24
@deprecated
25
def status() -> int:
26
"""
27
Check if random number generator has been seeded.
28
29
Returns:
30
1 if PRNG has been seeded with enough data, 0 otherwise
31
32
Note: This function is deprecated and may not provide
33
meaningful results on modern systems.
34
"""
35
```
36
37
## Usage Examples
38
39
### Basic Random Seeding (Deprecated)
40
41
```python
42
from OpenSSL import rand
43
import warnings
44
45
# Note: These functions are deprecated
46
with warnings.catch_warnings():
47
warnings.simplefilter("ignore", DeprecationWarning)
48
49
# Check if random generator is seeded
50
if rand.status():
51
print("Random generator is seeded")
52
else:
53
print("Random generator needs seeding")
54
55
# Add some entropy (deprecated approach)
56
import os
57
random_bytes = os.urandom(32)
58
rand.add(random_bytes, 32)
59
60
print(f"Random generator status: {rand.status()}")
61
```
62
63
### Modern Approach (Recommended)
64
65
```python
66
# Instead of using OpenSSL.rand, use Python's secure random functions
67
import secrets
68
import os
69
70
# Generate cryptographically secure random bytes
71
secure_random = secrets.token_bytes(32)
72
print(f"Generated {len(secure_random)} secure random bytes")
73
74
# Or use os.urandom for system randomness
75
system_random = os.urandom(32)
76
print(f"Generated {len(system_random)} system random bytes")
77
78
# Modern OpenSSL automatically handles seeding, so manual
79
# seeding with OpenSSL.rand is not necessary
80
```
81
82
## Deprecation Notice
83
84
The `OpenSSL.rand` module is deprecated because:
85
86
1. Modern OpenSSL versions automatically seed the random number generator
87
2. The system provides sufficient entropy sources
88
3. Python's `secrets` and `os.urandom` provide better interfaces for secure randomness
89
4. Manual entropy seeding is rarely needed in modern applications
90
91
For new code, use Python's built-in secure random functions instead of this module.