feat(generator): add constructor/destructor
All checks were successful
continuous-integration/drone/push Build is passing

* initialize_generator() is now a private method
* call initialize_generator() inside the constructor of the Generator
* move full struct declaration to generator.h
* have a private field named G of type G_state
* remove explicit instantiation of Generator from fortuna constructor -
  Generator is a private field of fortuna
* remove direct initialize_generator() call from fortuna constructor
* remove get_state() calls as G_state G is now a private field
* refactor get_state() to simply return the *private field G_state G*
* refactor do_crypto() - I know, this should have been split to a
  separate commit - remove function parameters and useless assignments
  inside its body to reflect that once it accesses k and ctr (of the
  G_state G), it won't be necessary for them to be passed as parameters,
  since *G_state G is now a private field*...
This commit is contained in:
surtur 2021-11-19 22:45:29 +01:00
parent 2fa7e135cb
commit 444660bf89
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
3 changed files with 22 additions and 33 deletions

View File

@ -2,7 +2,6 @@
#define FORTUNA_FORTUNA_CPP
#include "fortuna.h"
#include "generator.h"
#include <cryptopp/osrng.h>
#include <cryptopp/hex.h>
@ -17,8 +16,6 @@
namespace fortuna {
Fortuna::Fortuna(){
generator::Generator g{};
g.initialize_generator();
}
Fortuna::~Fortuna() = default;

View File

@ -16,32 +16,24 @@
namespace fortuna {
namespace generator {
struct Generator::G_state{
int64_t k;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
unsigned __int128 ctr;
#pragma GCC diagnostic pop
};
Generator::Generator() /*noexcept*/ {
initialize_generator();
}
Generator::~Generator() = default;
void Generator::initialize_generator(){
// Generator::G_state *G = new G_state; // -> was told to use auto by
// clang-tidy...
auto G = new Generator::G_state{};
G->k = 0;
G->ctr = 0;
G.k = 0;
G.ctr = 0;
fmt::print("Generator initialized\n");
// TODO(me): rm this, it's here just so it's called
do_sha("fortuna");
delete(G);
};
auto Generator::get_state() -> G_state {
return G_state{};
return G;
}
auto Generator::reseed(const std::string& s) -> void {
G_state G = get_state();
std::string to_be_hashed = std::to_string(G.k) + s;
// TODO(me): wrap do_sha in a try-catch
std::string a = do_sha(to_be_hashed);
@ -80,31 +72,24 @@ auto Generator::do_sha(const std::string& k_n_s) -> std::string {
return "42";
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
auto Generator::do_crypto(int64_t k, unsigned __int128 ctr) -> std::string {
auto Generator::do_crypto() -> std::string {
/* this function calls the block cipher
* returns a string of k*(16 bytes);
* do whatever atm */
k = 0;
ctr = 0;
return "";
}
#pragma GCC diagnostic pop
auto Generator::generate_blocks(unsigned int k_blocks) -> std::string {
G_state G = get_state();
assert ((G.ctr!=0) && "Counter is not 0, generator has been seeded");
std::string r = "";
for (int i = 0; i < k_blocks; ++i) {
r += do_crypto(G.k, G.ctr);
r += do_crypto();
G.ctr++;
}
return r;
}
auto Generator::generate_random_data(uint n) -> std::string {
G_state G = get_state();
std::string r = "";
if (n < 0){
/* this should not be possible */

View File

@ -8,20 +8,27 @@ namespace generator {
class Generator {
public:
void initialize_generator();
Generator(); // ad noexcept: perhaps _do_ throw*
~Generator();
auto generate_blocks(unsigned int k_blocks) -> std::string;
private:
struct G_state;
struct G_state {
int64_t k;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
unsigned __int128 ctr;
#pragma GCC diagnostic pop
};
G_state G;
void initialize_generator();
auto reseed(const std::string& s) -> void;
auto do_sha(const std::string& k_n_s) -> std::string;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
auto do_crypto(int64_t k, unsigned __int128 ctr) -> std::string;
#pragma GCC diagnostic pop
auto do_crypto() -> std::string;
/* n is the number of random bytes to generate */
auto generate_random_data(uint n) -> std::string;