The following demonstrates how to use AlphaRNG Software API in C++ language to retrieve random data from a AlphaRNG device when used with Entropy Server
for Windows. The following code will only work when running AlphaRNG Entropy Server.
This is the recommended way for achieving maximum concurrent speed and performance.
Before using the following example, make sure the AlphaRNG device is plugged into one of USB ports available and
entropy-server.exe
is running on same system.
A C++ sample source code for retrieving entropy values from an AlphaRNG device when using Entropy Server for Windows through a named pipe connection.
#include <EntropyServerConnector.h>
#include <iostream>
#include <array>
using namespace entropy::server::api;
/**
* @return 0 if ran successfully
*/
int main() {
std::array<uint8_t, 10> rnd_array{ };
EntropyServerConnector pipe;
cout << "------------------------------------------------------------------------------" << endl;
cout << "--- Sample C++ program for retrieving random bytes from the entropy server ---" << endl;
cout << "------------------------------------------------------------------------------" << endl;
// Connecting to the entropy server
if (!pipe.open_named_pipe()) {
cerr << pipe.get_last_error() << endl;
cerr << "Is entropy server running?" << endl;
return -1;
}
cout << endl << "Pipe open successfully" << endl << endl;
cout << "*** Generating " << rnd_array.size() << " random bytes ***" << endl;
// Retrieve random bytes from device
if (!pipe.get_entropy(rnd_array.data(), rnd_array.size())) {
cerr << pipe.get_last_error() << endl;
return -1;
}
for (int i = 0; i < rnd_array.size(); i++) {
cout << "entropy byte " << i << "-> " << (int)rnd_array[i] << endl;
}
return 0;
}
A C++ sample source code for extracting entropy bytes from an AlphaRNG device using SHA extractors through a named pipe connection.
#include <EntropyServerConnector.h>
#include <iostream>
#include <array>
using namespace entropy::server::api;
/**
* @return 0 if ran successfully
*/
int main() {
std::array<uint8_t, 10> rnd_array{ };
EntropyServerConnector pipe;
cout << "---------------------------------------------------------------------------------------------------" << endl;
cout << "--- Sample C++ program for extracting random bytes from the entropy server using SHA extractors ---" << endl;
cout << "---------------------------------------------------------------------------------------------------" << endl;
// Connecting to the entropy server
if (!pipe.open_named_pipe()) {
cerr << pipe.get_last_error() << endl;
cerr << "Is entropy server running?" << endl;
return -1;
}
cout << endl << "Pipe open successfully" << endl << endl;
cout << "*** extracting " << rnd_array.size() << " entropy bytes using SHA-256 entropy extractor ***" << endl;
// Extract entropy bytes
if (!pipe.extract_sha256_entropy(rnd_array.data(), rnd_array.size())) {
cerr << pipe.get_last_error() << endl;
return -1;
}
for (int i = 0; i < rnd_array.size(); i++) {
cout << "entropy byte " << i << "-> " << (int)rnd_array[i] << endl;
}
cout << endl;
cout << "*** extracting " << rnd_array.size() << " entropy bytes using SHA-512 entropy extractor ***" << endl;
// Extract entropy bytes
if (!pipe.extract_sha512_entropy(rnd_array.data(), rnd_array.size())) {
cerr << pipe.get_last_error() << endl;
return -1;
}
for (int i = 0; i < rnd_array.size(); i++) {
cout << "entropy byte " << i << "-> " << (int)rnd_array[i] << endl;
}
return 0;
}