The following demonstrates how to use C# language to download random data from a SwiftRNG device when used with SwiftRNG Entropy Server for Windows. The following code will only work when running SwiftRNG Entropy Server. Before using the following example, make sure your C# project is targeted for Windows 64 bit platform and the SwiftRNG device is plugged into one of USB ports available. The SwiftRNG-64.dll is included in the software kit.
A sample source code for retrieving primitive type random values from SwiftRNG device when using Entropy Server for Windows and provided SwiftRNG-64.dll
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
namespace SwiftRngSampleApp2
{
class Program
{
// Include the exact path to the SwiftRNG-64.dll (it uses additional dependencies from same location) available with the software kit.
[DllImport("C:\\tools\\windows-binaries\\windows-x64-vs-2019\\SwiftRNG-64.dll", EntryPoint = "getEntropy")]
public static extern int getRandomDouble(ref double d, long byteCount);
[DllImport("C:\\tools\\windows-binaries\\windows-x64-vs-2019\\SwiftRNG-64.dll", EntryPoint = "getEntropy")]
public static extern int getRandomInt(ref int i, long byteCount);
[DllImport("C:\\tools\\windows-binaries\\windows-x64-vs-2019\\SwiftRNG-64.dll", EntryPoint = "getEntropy")]
public static extern int getRandomUInt64(ref UInt64 ui64, long byteCount);
[DllImport("C:\\tools\\windows-binaries\\windows-x64-vs-2019\\SwiftRNG-64.dll", EntryPoint = "getEntropy")]
public static extern int getRandomByte(ref byte b, long byteCount);
[DllImport("C:\\tools\\windows-binaries\\windows-x64-vs-2019\\SwiftRNG-64.dll", EntryPoint = "getEntropy")]
public static extern int getRandomBytes(byte[] b, long byteCount);
static void Main(string[] args)
{
double d = 0;
int status = getRandomDouble(ref d, sizeof(double));
if (status != 0)
{
Console.Out.WriteLine("Could not retrieve a random double, is Entropy Server running?");
}
else
{
Console.Out.WriteLine("Random double: " + d);
}
int n = 0;
status = getRandomInt(ref n, sizeof(int));
if (status != 0)
{
Console.Out.WriteLine("Could not retrieve a random int from SwiftRNG Entropy Server");
}
else
{
Console.Out.WriteLine("Random int: " + n);
}
UInt64 ui64 = 0;
status = getRandomUInt64(ref ui64, sizeof(UInt64));
if (status != 0)
{
Console.Out.WriteLine("Could not retrieve a random Uint64 from SwiftRNG Entropy Server");
}
else
{
Console.Out.WriteLine("Random Uint64: " + ui64);
}
byte b = 0;
status = getRandomByte(ref b, sizeof(byte));
if (status != 0)
{
Console.Out.WriteLine("Could not retrieve a random byte from SwiftRNG Entropy Server");
}
else
{
Console.Out.WriteLine("Random byte: " + b);
}
byte[] byteArray = new byte[10];
status = getRandomBytes(byteArray, byteArray.Length);
if (status != 0)
{
Console.Out.WriteLine("Could not retrieve a byte array from SwiftRNG Entropy Server");
}
else
{
for (int i = 0; i < byteArray.Length; i++)
{
Console.Out.WriteLine("Random byte [" + i + "] : " + byteArray[i]);
}
}
// For maximum performance, download multiple bytes with one call.
// Can only retrieve up to 100000 bytes at a time.
byte[] maxByteArray = new byte[100000];
status = getRandomBytes(maxByteArray, maxByteArray.Length);
if (status != 0)
{
Console.Out.WriteLine("Could not retrieve " + maxByteArray.Length + " bytes array from SwiftRNG Entropy Server");
}
else
{
Console.Out.WriteLine("Retrieved an array of " + maxByteArray.Length + " bytes successfully");
}
}
}
}