Skip to content

Commit d5b1fe4

Browse files
Refactor RNG methods for internal documentation for clarity (comments)
1 parent 85353f1 commit d5b1fe4

File tree

1 file changed

+80
-75
lines changed

1 file changed

+80
-75
lines changed

pyCTools/hwrng.py

Lines changed: 80 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ class MaxRNG:
114114
random_hex = rng.generate_custom(32, config)
115115
"""
116116

117+
# Internal handlers
117118
def __init__(self):
118119
"""Initialize the MaxRNG wrapper by loading the appropriate DLL."""
119120
# Load the DLL using the load_dll helper with WinDLL loader
@@ -166,6 +167,7 @@ def _setup_advanced_functions(self):
166167
]
167168
self.dll.maxrng_dev.restype = ctypes.c_int
168169

170+
# Availability checks
169171
def is_available(self) -> bool:
170172
"""
171173
Check if the RNG hardware is available.
@@ -184,6 +186,7 @@ def is_threading_available(self) -> bool:
184186
"""
185187
return self.dll.test_threading_available() == 1
186188

189+
# Initialization methods
187190
def init_threading(self) -> None:
188191
"""
189192
Initialize the RNG for thread-safe operations.
@@ -193,80 +196,6 @@ def init_threading(self) -> None:
193196
"""
194197
self.dll.maxrng_init()
195198

196-
def generate(self, size: int) -> bytes:
197-
"""
198-
Generate random bytes using the standard RNG.
199-
200-
Args:
201-
size (int): Number of random bytes to generate.
202-
203-
Returns:
204-
bytes: Random bytes generated.
205-
206-
Raises:
207-
RuntimeError: If the RNG function call fails.
208-
"""
209-
buf = (ctypes.c_ubyte * size)()
210-
success = self.dll.maxrng(buf, size)
211-
if not success:
212-
raise RuntimeError("Failed to generate random data")
213-
return bytes(buf)
214-
215-
def generate_ultra(self, size: int, complexity: int = 5) -> bytes:
216-
"""
217-
Generate high-quality random bytes with specified complexity.
218-
219-
Args:
220-
size (int): Number of random bytes to generate.
221-
complexity (int): Complexity level (1-10), higher is more secure.
222-
223-
Returns:
224-
bytes: Random bytes generated.
225-
226-
Raises:
227-
ValueError: If complexity is out of range.
228-
RuntimeError: If the RNG function call fails.
229-
"""
230-
if not 1 <= complexity <= 10:
231-
raise ValueError("Complexity must be between 1 and 10")
232-
233-
buf = (ctypes.c_ubyte * size)()
234-
success = self.dll.maxrng_ultra(buf, size, complexity)
235-
if not success:
236-
raise RuntimeError("Failed to generate ultra random data")
237-
return bytes(buf)
238-
239-
def generate_threadsafe(self, size: int, complexity: int = 2) -> bytes:
240-
"""
241-
Generate random bytes using thread-safe RNG function.
242-
243-
Args:
244-
size (int): Number of random bytes to generate.
245-
complexity (int): Complexity level (1-5), higher is more secure.
246-
247-
Returns:
248-
bytes: Random bytes generated.
249-
250-
Raises:
251-
ValueError: If complexity is out of range.
252-
RuntimeError: If threading is not available or the RNG call fails.
253-
"""
254-
if not 1 <= complexity <= 5:
255-
raise ValueError("Complexity for thread-safe RNG must be between 1 and 5")
256-
257-
if not self.is_threading_available():
258-
self.init_threading()
259-
if not self.is_threading_available():
260-
raise RuntimeError(
261-
"Threading initialization failed. Ensure the hRng DLL supports thread-safe operations."
262-
)
263-
264-
buf = (ctypes.c_ubyte * size)()
265-
success = self.dll.maxrng_threadsafe(buf, size, complexity)
266-
if not success:
267-
raise RuntimeError("Failed to generate thread-safe random data")
268-
return bytes(buf)
269-
270199
def create_config(self,
271200
security_mode: SecurityMode = SecurityMode.BALANCED,
272201
hash_algo: HashAlgorithm = HashAlgorithm.SHA256,
@@ -348,6 +277,81 @@ def create_config(self,
348277

349278
return config
350279

280+
# All RNG generation methods
281+
def generate(self, size: int) -> bytes:
282+
"""
283+
Generate random bytes using the standard RNG.
284+
285+
Args:
286+
size (int): Number of random bytes to generate.
287+
288+
Returns:
289+
bytes: Random bytes generated.
290+
291+
Raises:
292+
RuntimeError: If the RNG function call fails.
293+
"""
294+
buf = (ctypes.c_ubyte * size)()
295+
success = self.dll.maxrng(buf, size)
296+
if not success:
297+
raise RuntimeError("Failed to generate random data")
298+
return bytes(buf)
299+
300+
def generate_ultra(self, size: int, complexity: int = 5) -> bytes:
301+
"""
302+
Generate high-quality random bytes with specified complexity.
303+
304+
Args:
305+
size (int): Number of random bytes to generate.
306+
complexity (int): Complexity level (1-10), higher is more secure.
307+
308+
Returns:
309+
bytes: Random bytes generated.
310+
311+
Raises:
312+
ValueError: If complexity is out of range.
313+
RuntimeError: If the RNG function call fails.
314+
"""
315+
if not 1 <= complexity <= 10:
316+
raise ValueError("Complexity must be between 1 and 10")
317+
318+
buf = (ctypes.c_ubyte * size)()
319+
success = self.dll.maxrng_ultra(buf, size, complexity)
320+
if not success:
321+
raise RuntimeError("Failed to generate ultra random data")
322+
return bytes(buf)
323+
324+
def generate_threadsafe(self, size: int, complexity: int = 2) -> bytes:
325+
"""
326+
Generate random bytes using thread-safe RNG function.
327+
328+
Args:
329+
size (int): Number of random bytes to generate.
330+
complexity (int): Complexity level (1-5), higher is more secure.
331+
332+
Returns:
333+
bytes: Random bytes generated.
334+
335+
Raises:
336+
ValueError: If complexity is out of range.
337+
RuntimeError: If threading is not available or the RNG call fails.
338+
"""
339+
if not 1 <= complexity <= 5:
340+
raise ValueError("Complexity for thread-safe RNG must be between 1 and 5")
341+
342+
if not self.is_threading_available():
343+
self.init_threading()
344+
if not self.is_threading_available():
345+
raise RuntimeError(
346+
"Threading initialization failed. Ensure the hRng DLL supports thread-safe operations."
347+
)
348+
349+
buf = (ctypes.c_ubyte * size)()
350+
success = self.dll.maxrng_threadsafe(buf, size, complexity)
351+
if not success:
352+
raise RuntimeError("Failed to generate thread-safe random data")
353+
return bytes(buf)
354+
351355
def generate_custom(self,
352356
size: int,
353357
config: Optional[Union[RNGConfig, SecurityMode]] = None,
@@ -408,7 +412,7 @@ def generate_custom(self,
408412
# For HEX and BASE64, return as string
409413
return result.decode('ascii')
410414

411-
# Convenience methods for common use cases
415+
# Convenience methods for common random use cases
412416
def generate_hex(self, size: int, security: SecurityMode = SecurityMode.BALANCED) -> str:
413417
"""Generate random data as a hex string."""
414418
return self.generate_custom(size, security, OutputMode.HEX)
@@ -482,6 +486,7 @@ def generate_range(self, start: int, end: int) -> int:
482486
# Map to the desired range
483487
return start + (value % range_size)
484488

489+
# Convenience methods for common operations
485490
def choose(self, items: List) -> object:
486491
"""
487492
Choose a random item from a list.

0 commit comments

Comments
 (0)