The following demonstrates how to use C language for downloading random data from SwiftRNG device when used with SwiftRNG Software API.
A sample source code in C for retrieving random bytes and integers from SwiftRNG device
/*
* sample.c
* Ver. 2.6
*
* This is a sample C program that demonstrates how to retrieve random bytes
* from a SwiftRNG using 'swrngapi' API for C language.
*
*/
#include <swrngapi.h>
#include <stdio.h>
#define BYTE_BUFF_SIZE (10)
#define DEC_BUFF_SIZE (10)
/* Allocate memory for random bytes */
unsigned char random_byte[BYTE_BUFF_SIZE];
/* Allocate memory for random integers */
unsigned int random_int[DEC_BUFF_SIZE];
/*********************
Main entry
**********************/
int main() {
int i;
double d;
unsigned int ui;
SwrngContext ctxt;
printf("--------------------------------------------------------------------------\n");
printf("--- Sample C program for retrieving random bytes from SwiftRNG device ----\n");
printf("--------------------------------------------------------------------------\n");
/* Initialize the context */
if (swrngInitializeContext(&ctxt) != SWRNG_SUCCESS) {
printf("Could not initialize context\n");
return(1);
}
/* Open the first (device number 0) SwiftRNG device if available */
if (swrngOpen(&ctxt, 0) != SWRNG_SUCCESS) {
printf("%s\n", swrngGetLastErrorMessage(&ctxt));
swrngDestroyContext(&ctxt);
return(1);
}
printf("\nSwiftRNG device open successfully\n\n");
/* Retrieve random bytes from device */
if (swrngGetEntropy(&ctxt, random_byte, BYTE_BUFF_SIZE) != SWRNG_SUCCESS) {
printf("%s\n", swrngGetLastErrorMessage(&ctxt));
swrngDestroyContext(&ctxt);
return (1);
}
printf("*** Generating %d random bytes ***\n", BYTE_BUFF_SIZE);
/* Print random bytes */
for (i = 0; i < BYTE_BUFF_SIZE; i++) {
printf("random byte %d -> %d\n", i, (int)random_byte[i]);
}
/* Retrieve random integers from device */
if (swrngGetEntropy(&ctxt, (unsigned char*)random_int, DEC_BUFF_SIZE * sizeof(unsigned int)) != SWRNG_SUCCESS) {
printf("%s\n", swrngGetLastErrorMessage(&ctxt));
swrngDestroyContext(&ctxt);
return (1);
}
printf("\n*** Generating %d random numbers between 0 and 1 with 5 decimals ***\n", DEC_BUFF_SIZE);
/* Print random bytes */
for (i = 0; i < DEC_BUFF_SIZE; i++) {
ui = random_int[i] % 99999;
d = (double)ui / 100000.0;
printf("random number -> %lf\n", d);
}
printf("\n");
swrngDestroyContext(&ctxt);
return (0);
}
The C code sample presented above, is included as part of the SwiftRNG Software API Kit which can be downloaded at this address.
To build and test the sample code with provided Makefile using Linux command prompt, locate ...linux-and-macOS/swrng
directory and enter the following commands (you may need to run ./sample with sudo
permissions):
make sample
./sample
The output from running the code above may look like this:
--------------------------------------------------------------------------
--- Sample C program for downloading random bytes from SwiftRNG device ---
--------------------------------------------------------------------------
SwiftRNG device open successfully
*** Generating 10 random bytes ***
random byte 0 -> 150
random byte 1 -> 244
random byte 2 -> 85
random byte 3 -> 180
random byte 4 -> 146
random byte 5 -> 129
random byte 6 -> 129
random byte 7 -> 66
random byte 8 -> 44
random byte 9 -> 34
*** Generating 10 random numbers between 0 and 1 with 5 decimals ***
random number -> 0.769620
random number -> 0.807310
random number -> 0.105020
random number -> 0.829070
random number -> 0.052510
random number -> 0.304050
random number -> 0.607550
random number -> 0.114320
random number -> 0.484260
random number -> 0.561780