The following examples show how to use Python to download random data from an AlphaRNG device through the AlphaRNG Entropy Server for Windows. The code only works while the AlphaRNG Entropy Server is running. Before using the examples, make sure you are running 64-bit Python on Windows and an AlphaRNG device is plugged into an available USB port. AlphaRNG-64.dll is included in the software kit.
Retrieving a random byte through AlphaRNG-64.dll
import os
import sys
import ctypes
dll = ctypes.WinDLL("C:\\tools\\alrng-windows-binaries\\windows-x64-vs-2019\\AlphaRNG-64.dll")
func = dll.getEntropyAsByte
func.restype = ctypes.c_int
random = func()
if random > 255:
raise Exception("Could not retreieve entropy byte. Is entropy server running?")
else:
print ("Random byte: ", random)
Retrieving an array of random bytes through AlphaRNG-64.dll
import os
import sys
import ctypes
byte_count = 10
byte_arr = ctypes.c_byte * byte_count
byte_arr_p = ctypes.POINTER(ctypes.c_byte)
dll = ctypes.WinDLL("C:\\tools\\alrng-windows-binaries\\windows-x64-vs-2019\\AlphaRNG-64.dll")
func = dll.getEntropy
func.restype = ctypes.c_int
func.argtypes = [byte_arr_p, ctypes.c_long]
entropy_bytes = byte_arr()
status = func(entropy_bytes, byte_count)
if status != 0:
raise Exception("Could not retreieve entropy bytes. Is entropy server running?")
print("Retrieved random bytes:")
for x in entropy_bytes:
print(x)