The following examples show how to use the AlphaRNG Software API in C++ to retrieve random data from an AlphaRNG device through the Entropy Server for Windows. The code only works while the AlphaRNG Entropy Server is running. This is the recommended way to achieve maximum concurrent throughput. Before using the examples, make sure the AlphaRNG device is plugged into an available USB port and entropy-server.exe is running on the same system.

Retrieving entropy bytes 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;
}


Extracting entropy bytes with the 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;
}