-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
111 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#include <cstdint> | ||
#include <fwoop_filereader.h> | ||
#include <fwoop_random.h> | ||
#include <sys/types.h> | ||
|
||
namespace { | ||
const std::string RANDOM_FN = "/dev/random"; | ||
} | ||
|
||
namespace fwoop { | ||
|
||
uint8_t *Random::readRandomBytes(uint32_t numBytes) | ||
{ | ||
FileReader fr(RANDOM_FN); | ||
if (fr.open() != 0) { | ||
// TODO handle error | ||
return nullptr; | ||
} | ||
uint8_t *res = fr.head(numBytes); | ||
fr.close(); | ||
return res; | ||
} | ||
|
||
uint16_t Random::getUInt16() | ||
{ | ||
uint8_t *bytes = Random::readRandomBytes(2); | ||
if (bytes) { | ||
uint16_t r = (bytes[0] << 8) + bytes[1]; | ||
delete[] bytes; | ||
return r; | ||
} | ||
return 0; | ||
} | ||
|
||
uint32_t Random::getUInt32() | ||
{ | ||
uint8_t *bytes = Random::readRandomBytes(4); | ||
if (bytes) { | ||
uint32_t r = (bytes[3] << 24) + (bytes[2] << 16) + (bytes[1] << 8) + bytes[0]; | ||
delete[] bytes; | ||
return r; | ||
} | ||
return 0; | ||
} | ||
|
||
} // namespace fwoop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include <fwoop_random.h> | ||
#include <gtest/gtest.h> | ||
|
||
TEST(Random, getInt) | ||
{ | ||
// GIVEN | ||
|
||
// WHEN | ||
uint16_t r1 = fwoop::Random::getUInt16(); | ||
uint32_t r2 = fwoop::Random::getUInt16(); | ||
std::cerr << r1 << " " << r2 << '\n'; | ||
|
||
// THEN | ||
ASSERT_LT(0, r1); | ||
ASSERT_LT(0, r2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
#include <inttypes.h> | ||
|
||
namespace fwoop { | ||
|
||
class Random { | ||
private: | ||
static uint8_t *readRandomBytes(uint32_t numBytes); | ||
|
||
public: | ||
static uint16_t getUInt16(); | ||
static uint32_t getUInt32(); | ||
}; | ||
|
||
} // namespace fwoop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters