The following C# sample code downloads random data from a SwiftRNG device on 64-bit Windows.
Before using the examples, make sure your C# project targets the 64-bit Windows platform and the SwiftRNG device is plugged into an available USB port. A thread-safe and process-safe 64-bit SwiftRNG.dll is included in the software kit.
Note: the following methods are deprecated. Use the alternative methods described in C# sample code for the Windows entropy server.
Retrieving one byte
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);
}
}
}
}
Retrieving an array of bytes
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]);
}
}
}
}
}
Retrieving primitive-type random values
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);
}
}
}
}
Disabling post-processing
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);
}
}
}
}