The following demonstrates how to use Delphi language to download random data from a AlphaRNG device when used with AlphaRNG Entropy Server for Windows. The following code will only work when running AlphaRNG Entropy Server. Before using the following example, make sure the AlphaRNG device is plugged into one of USB ports available and entropy-server.exe is running on same system.

A Delphi sample source code for retrieving random values from AlphaRNG device when using AlphaRNG Entropy Server for Windows.


//
//
// A sample Delphi program that demonstrates how to retrieve random bytes from
// AlphaRNG device using Entropy Server for Windows.
//
//

program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  SysUtils,
  Classes;

var
 EntropyServerStream: TFileStream;
 ByteCount: LongWord;
 RandomByteArray: array[0..4] of Byte;
 RequestByteArray: array[0..7] of Byte;

begin
  EntropyServerStream := TFileStream.Create('\\.\pipe\AlphaRNG', fmOpenReadWrite);
  try
    // How many random bytes to retrieve
    ByteCount := 5;


    // Prepare the entropy server request.
    // For more information visit https://tectrolabs.com/docs/alpharng/entropy-server-api/
    Move(ByteCount, RequestByteArray[4], SizeOf(ByteCount));

    // Send request to the entropy server.
    // Request bytes should be sent at once with no delay in between.
    EntropyServerStream.WriteBuffer(RequestByteArray, SizeOf(RequestByteArray));

    // Retrieve random bytes from the entropy server.
    // Random bytes should be received at once with no delay in between.
    EntropyServerStream.ReadBuffer(RandomByteArray, SizeOf(RandomByteArray));


  finally
    EntropyServerStream.Free;
  end;

  // Print all random bytes retrieved
  Writeln('random byte 0: ', RandomByteArray[0]);
  Writeln('random byte 1: ', RandomByteArray[1]);
  Writeln('random byte 2: ', RandomByteArray[2]);
  Writeln('random byte 3: ', RandomByteArray[3]);
  Writeln('random byte 4: ', RandomByteArray[4]);

end.



A Delphi sample source code for retrieving random values from AlphaRNG device when using AlphaRNG Entropy Server for Windows and AlphaRNG-32.dll

//
//
// A sample Delphi program that demonstrates how to retrieve random bytes from
// AlphaRNG Entropy Server using AlphaRNG-32.dll
// This example is a modified version of Zac's code sample.
//

program Project1;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  System.SysUtils;

const
  entropyByteCount = 10; // How many entropy bytes to retrieve

type
  RandomBuffer = array [0 .. (entropyByteCount - 1)] of byte;
  xbuffer = ^RandomBuffer;


var
  i, z: integer;
  zbuffer: randombuffer;

Function getEntropy(ybuffer: xbuffer; y: longword): integer; stdcall; external 'C:\tools\alrng-windows-binaries\windows-x86-vs-2019\AlphaRNG-32.dll';

begin
  try
    z := getEntropy(@zbuffer, entropyByteCount);
    writeln('status = ', z);

     if z=0 then begin

        for i:=0 to entropyByteCount-1 do
          writeln('randombyte[',i,'] = ',zbuffer[i]);
        end

      else
        writeln('error occurred in AlphaRNG');


    readln;
  except
    on E: Exception do
      writeln(E.ClassName, ': ', E.Message);
  end;

end.