The following VB.NET sample code downloads random data from a SwiftRNG device using SwiftRNG.dll and the 64-bit entropy server for Windows. The code only works while the SwiftRNG entropy server is running.
Before using the examples, make sure your VB.NET 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.
Retrieving one byte
Module Module1
' Include the exact path to the SwiftRNG.dll (it uses additional dependencies from same location) available with the software kit.
Declare Ansi Function getRandomByte Lib "C:\tools\windows-x64\x64\Release\SwiftRNG.dll" Alias "swftGetByteFromEntropyServerSynchronized" () As Integer
Sub Main()
Dim rnd As Integer = getRandomByte()
If rnd < 256 Then
'Successfully generated a random byte value
Console.Write("Random byte: ")
Console.WriteLine(rnd)
Else
'Number greater than 255 means error
'Generator not present or generation error
Console.WriteLine("SwiftRNG Not connected Or device Error")
End If
End Sub
End Module
Retrieving primitive-type random values
Imports System.Runtime.InteropServices
Module Module1
Declare Ansi Function getRandomDouble Lib "C:\tools\windows-x64\x64\Release\SwiftRNG.dll" _
Alias "swftGetEntropyFromEntropyServer" (ByRef d As Double, ByVal byteCount As Integer) As Integer
Declare Ansi Function getRandomInt Lib "C:\tools\windows-x64\x64\Release\SwiftRNG.dll" _
Alias "swftGetEntropyFromEntropyServer" (ByRef i As Integer, ByVal byteCount As Integer) As Integer
Declare Ansi Function getRandomUInt64 Lib "C:\tools\windows-x64\x64\Release\SwiftRNG.dll" _
Alias "swftGetEntropyFromEntropyServer" (ByRef i As UInt64, ByVal byteCount As Integer) As Integer
Sub Main()
Dim d As Double
Dim status As Integer = getRandomDouble(d, Marshal.SizeOf(Of Double))
If status <> 0 Then
Console.WriteLine("Could not retrieve a random double from SwiftRNG device")
Else
Console.Write("Random double: ")
Console.WriteLine(d)
End If
Dim n As Integer
status = getRandomInt(n, Marshal.SizeOf(Of Integer))
If status <> 0 Then
Console.WriteLine("Could not retrieve a random integer from SwiftRNG device")
Else
Console.Write("Random integer: ")
Console.WriteLine(n)
End If
Dim ui64 As UInt64
status = getRandomUInt64(ui64, Marshal.SizeOf(Of UInt64))
If status <> 0 Then
Console.WriteLine("Could not retrieve a random UInt64 from SwiftRNG device")
Else
Console.Write("Random UInt64: ")
Console.WriteLine(ui64)
End If
End Sub
End Module
Retrieving up to 10,000,000 bytes with a single call
Module Module1
' Include the exact path to the SwiftRNG.dll (it uses additional dependencies from same location) available with the software kit.
Declare Ansi Function getRandom Lib "C:\tools\windows-x64\x64\Release\SwiftRNG.dll" _
Alias "swftGetEntropyFromEntropyServer" (randomBytes As Byte(), length As Long) As Integer
Sub Main()
Dim status As Integer
Dim randomBytes(0 To 9999999) As Byte
status = getRandom(randomBytes, randomBytes.Length)
If status = 0 Then
'Successfully generated a random byte array
Console.Write("Random byte array generated with success")
Else
'A non zero status code indicates an error
Console.WriteLine("SwiftRNG Not connected Or device Error or invalid request")
End If
End Sub
End Module