Skip to content

Commit 45ca58c

Browse files
committed
Adding sample++.cpp
1 parent 40ddd37 commit 45ca58c

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

linux-and-macOS/swrng/Makefile

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,24 @@ SWDIAG_CL = swdiag-cl
3434
SWRAWRANDOM = swrawrandom
3535
SWRNGSEQGEN = swrngseqgen
3636
SAMPLE = sample
37+
SAMPLECPP = sample++
3738
SAMPLE_CL = sample-cl
3839

3940

40-
all: $(SAMPLE) $(SWDIAG) $(SWPERFTEST) $(BITCOUNT) $(SWRNG) $(SWRAWRANDOM) $(SWRNGSEQGEN) $(SAMPLE_CL) $(BITCOUNT_CL) $(SWDIAG_CL) $(SWPERFTEST_CL) $(SWRNG_CL)
41+
all: $(SAMPLE) $(SWDIAG) $(SWPERFTEST) $(BITCOUNT) $(SWRNG) $(SWRAWRANDOM) $(SWRNGSEQGEN) $(SAMPLE_CL) $(BITCOUNT_CL) $(SWDIAG_CL) $(SWPERFTEST_CL) $(SWRNG_CL) $(SAMPLECPP)
4142

4243
$(SAMPLE): $(SAMPLE).c $(OBJECTS)
4344
@echo
4445
@echo "Creating $(SAMPLE) ..."
4546
$(CC) -c $(SAMPLE).c $(CFLAGS) $(CLANGSTD)
4647
$(GPP) $(SAMPLE).o $(OBJECTS) -o $(SAMPLE) $(LDFLAGS)
4748

49+
$(SAMPLECPP): $(SAMPLECPP).cpp $(OBJECTS)
50+
@echo
51+
@echo "Creating $(SAMPLECPP) ..."
52+
$(GPP) -c $(SAMPLECPP).cpp $(CPPFLAGS)
53+
$(GPP) $(SAMPLECPP).o $(OBJECTS) -o $(SAMPLECPP) $(LDCPPFLAGS)
54+
4855
$(SWDIAG): $(SWDIAG).c $(OBJECTS)
4956
@echo
5057
@echo "Creating $(SWDIAG) ..."
@@ -130,7 +137,7 @@ swrng-cl-api.o:
130137

131138

132139
clean:
133-
rm -f *.o ; rm -fr $(SAMPLE) $(SWDIAG) $(SWPERFTEST) $(BITCOUNT) $(SWRNG) $(SWRAWRANDOM) $(SWRNGSEQGEN) $(SAMPLE_CL) $(BITCOUNT_CL) $(SWDIAG_CL) $(SWPERFTEST_CL) $(SWRNG_CL)
140+
rm -f *.o ; rm -fr $(SAMPLE) $(SWDIAG) $(SWPERFTEST) $(BITCOUNT) $(SWRNG) $(SWRAWRANDOM) $(SWRNGSEQGEN) $(SAMPLE_CL) $(BITCOUNT_CL) $(SWDIAG_CL) $(SWPERFTEST_CL) $(SWRNG_CL) $(SAMPLECPP)
134141

135142
install:
136143
install $(SWDIAG) $(BINDIR)/$(SWDIAG)

linux-and-macOS/swrng/sample++.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* sample++.cpp
3+
* Ver. 1.0
4+
*
5+
* A sample C++ program that demonstrates how to retrieve random bytes
6+
* from a SwiftRNG device using SwiftRNG C++ API.
7+
*
8+
*
9+
*/
10+
11+
#include <SwiftRngApi.h>
12+
#include <string>
13+
#include <iostream>
14+
15+
16+
using namespace swiftrng;
17+
18+
19+
/**
20+
* Main entry
21+
* @return int 0 - successful or error code
22+
*/
23+
int main() {
24+
25+
SwiftRngApi api;
26+
unsigned char entropyBytes [10];
27+
28+
std::cout << "---------------------------------------------------------------------------" << std::endl;
29+
std::cout << "--- Sample C++ program for retrieving random bytes from SwiftRNG device ---" << std::endl;
30+
std::cout << "---------------------------------------------------------------------------" << std::endl;
31+
32+
33+
if (api.open(0)) {
34+
std::cerr << api.get_last_error_log() << std::endl;
35+
return -1;
36+
}
37+
38+
if (api.get_entropy(entropyBytes, sizeof(entropyBytes))) {
39+
std::cerr << "Could not retrieve entropy from SwiftRNG device. " << api.get_last_error_log() << std::endl;
40+
api.close();
41+
return -1;
42+
}
43+
44+
for (int i = 0; i < (int)sizeof(entropyBytes); ++i) {
45+
std::cout << "entropy byte " << i+1 << ": " << (int)entropyBytes[i] << std::endl;
46+
}
47+
48+
api.close();
49+
return 0;
50+
}

linux-and-macOS/swrng/swrngseqgen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* swrngseqgen.cpp
3-
* Ver. 2.3
3+
* Ver. 2.4
44
*
55
* A program for generating random sequences of unique integer numbers based
66
* on true random bytes produced by a SwiftRNG device.
@@ -62,7 +62,7 @@ void printRandomSequence(int64_t *buffer);
6262
*/
6363
int main(int argc, char **argv) {
6464

65-
std::cerr << "-------------------------------------------------------------------------------------------------" << std::endl;
65+
std::cout << "-------------------------------------------------------------------------------------------------" << std::endl;
6666
std::cout << "--- A program for generating random sequences of unique integer numbers using SwiftRNG device ---" << std::endl;
6767
std::cout << "-------------------------------------------------------------------------------------------------" << std::endl;
6868

0 commit comments

Comments
 (0)