|
1 | | -from pyCTools.hwrng import ( |
2 | | - get_hardware_random_bytes, |
3 | | - get_hardware_random_bytes_threadsafe, |
4 | | - get_hardware_random_bytes_extended, |
5 | | - hardware_rng_selftest, |
6 | | -) |
| 1 | +import os |
| 2 | +import threading |
| 3 | +import sys |
7 | 4 |
|
8 | | -if __name__ == '__main__': |
9 | | - print("RNG self-test:", "PASS" if hardware_rng_selftest() else "FAIL") |
| 5 | +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) |
| 6 | +from pyCTools.hwrng import MaxRNG |
10 | 7 |
|
| 8 | +rng = MaxRNG() |
| 9 | +rng.dll.maxrng_init() |
| 10 | + |
| 11 | +print("RDRAND supported:", rng.test_rng_available()) |
| 12 | +print("Threading available:", rng.test_threading_available()) |
| 13 | + |
| 14 | +print("Basic RNG 32 bytes:", rng.maxrng(32).hex()) |
| 15 | +print("Ultra RNG 32 bytes (complexity 10):", rng.maxrng_ultra(32, 10).hex()) |
| 16 | +print("Solo Thread-safe RNG 32 bytes:", rng.maxrng_threadsafe(32).hex()) |
| 17 | + |
| 18 | + |
| 19 | +def worker(size, index, results): |
11 | 20 | try: |
12 | | - rb = get_hardware_random_bytes(32) |
13 | | - print("MaxRNG (32 bytes):", rb.hex()) |
14 | | - rb2 = get_hardware_random_bytes_threadsafe(32) |
15 | | - print("MaxRNG_ThreadSafe (32 bytes):", rb2.hex()) |
16 | | - rb3 = get_hardware_random_bytes_extended(32, intensive_level=3) |
17 | | - print("MaxRNG_Extended (32 bytes, level 3):", rb3.hex()) |
18 | | - except Exception as e: |
19 | | - print("Error:", e) |
| 21 | + data = rng.maxrng_threadsafe(size) |
| 22 | + results[index] = data |
| 23 | + except RuntimeError as e: |
| 24 | + results[index] = e |
| 25 | + |
| 26 | + |
| 27 | +num_threads = 5 |
| 28 | +bytes_per_thread = 16 |
| 29 | +results_main = [None] * num_threads |
| 30 | +threads = [] |
| 31 | + |
| 32 | +for i in range(num_threads): |
| 33 | + t = threading.Thread(target=worker, args=(bytes_per_thread, i, results_main)) |
| 34 | + threads.append(t) |
| 35 | + t.start() |
| 36 | + |
| 37 | +for t in threads: |
| 38 | + t.join() |
| 39 | + |
| 40 | +for i, result in enumerate(results_main): |
| 41 | + if isinstance(result, Exception): |
| 42 | + print(f"Thread {i}: RNG failed: {result}") |
| 43 | + else: |
| 44 | + print(f"Thread {i}: got {len(result)} bytes -> {result.hex()}") |
| 45 | + |
| 46 | +print("All threads finished.") |
| 47 | + |
| 48 | + |
| 49 | +print("Example completed successfully.") |
0 commit comments