The following demonstrates how to use C# language to download random data from SwiftRNG device when used with 64 bit Windows. Before using the following examples, make sure your C# project is targeted for Windows 64 bit platform and the SwiftRNG device is plugged into one of USB ports available. A thread-safe and process-safe 64 bit SwiftRNG.dll is included in the software kit.
Note: The following methods are deprecated. Please use alternative methods as described at this location.
A sample source code for retrieving one byte from SwiftRNG device
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
namespace SwiftRngSampleApp
{
class Program
{
// Include the exact path to the SwiftRNG.dll (it uses additional dependencies from same location) available with the software kit.
[DllImport("C:\\tools\\windows-x64\\x64\\Release\\SwiftRNG.dll", EntryPoint = "swftGetEntropyByteSynchronized")]
public static extern int getRandomByte();
static void Main(string[] args)
{
int rndByte = getRandomByte();
if (rndByte > 255) // Any number greater than 255 will indicate an error
{
Console.Out.WriteLine("Could not retrieve a random byte from SwiftRNG device");
} else
{
Console.Out.WriteLine(rndByte);
}
}
}
}
A sample source code for retrieving an array of bytes from SwiftRNG device
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
namespace SwiftRngSampleApp
{
class Program
{
// Include the exact path to the SwiftRNG.dll (it uses additional dependencies from same location) available with the software kit.
[DllImport("C:\\tools\\windows-x64\\x64\\Release\\SwiftRNG.dll", EntryPoint = "swftGetEntropySynchronized")]
public static extern int getRandomBytes(byte[] bytes, long byteCount);
static void Main(string[] args)
{
byte[] bytes = new byte[10];
int status = getRandomBytes(bytes, bytes.Length);
if (status != 0) // Non zero status indicates an error
{
Console.Out.WriteLine("Could not retrieve an array of random bytes from SwiftRNG device");
}
else
{
for (int i = 0; i < bytes.Length; i++)
{
Console.Out.WriteLine(bytes[i]);
}
}
}
}
}
A sample source code for retrieving primitive type random values from SwiftRNG device
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
namespace SwiftRngSampleApp
{
class Program
{
// Include the exact path to the SwiftRNG.dll (it uses additional dependencies from same location) available with the software kit.
[DllImport("C:\\tools\\windows-x64\\x64\\Release\\SwiftRNG.dll", EntryPoint = "swftGetEntropySynchronized")]
public static extern int getRandomDouble(ref double d, long byteCount);
[DllImport("C:\\tools\\windows-x64\\x64\\Release\\SwiftRNG.dll", EntryPoint = "swftGetEntropySynchronized")]
public static extern int getRandomInt(ref int i, long byteCount);
[DllImport("C:\\tools\\windows-x64\\x64\\Release\\SwiftRNG.dll", EntryPoint = "swftGetEntropySynchronized")]
public static extern int getRandomUInt64(ref UInt64 ui64, 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 from SwiftRNG device");
}
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 device");
}
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 device");
}
else
{
Console.Out.WriteLine("Random Uint64: " + ui64);
}
}
}
}
A sample source code that demonstrates how to disable post processing for SwiftRNG device
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
namespace SwiftRngSampleApp
{
class Program
{
// Include the exact path to the SwiftRNG.dll (it uses additional dependencies from same location) available with the software kit.
[DllImport("C:\\tools\\windows-x64\\x64\\Release\\SwiftRNG.dll", EntryPoint = "swftGetEntropyByteSynchronized")]
public static extern int getRandomByte();
[DllImport("C:\\tools\\windows-x64\\x64\\Release\\SwiftRNG.dll", EntryPoint = "swrngDisableDataPostProcessing")]
public static extern int disablePostProcessing();
static void Main(string[] args)
{
int status = disablePostProcessing(); // call this once
if (status != 0)
{
Console.Out.WriteLine("Could not disable data post processing for SwiftRNG device");
}
int rndByte = getRandomByte();
if (rndByte > 255) // Any number greater than 255 will indicate an error
{
Console.Out.WriteLine("Could not retrieve a random byte from SwiftRNG device");
}
else
{
Console.Out.WriteLine(rndByte);
}
}
}
}