feat: move from structural to object paradigm
All checks were successful
continuous-integration/drone/push Build is passing

* declare and implement Generator class and member methods
* create an instance of Generator in main
* call initialize_generator() from main
This commit is contained in:
surtur 2021-11-02 05:10:58 +01:00
parent f2ed6918c9
commit 01eadae32f
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
3 changed files with 51 additions and 30 deletions

View File

@ -5,47 +5,57 @@
#include <tuple> #include <tuple>
#include "generator.h" #include "generator.h"
#ifndef FORTUNA_GENERATOR_CPP
#define FORTUNA_GENERATOR_CPP
namespace fortuna { namespace fortuna {
namespace generator { namespace generator {
using namespace std; using namespace std;
struct G_state{ struct Generator::G_state{
int64_t k; int64_t k;
unsigned __int128 ctr; unsigned __int128 ctr;
}; };
auto reseed(G_state G, const string& s) -> G_state { 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;
k = 0;
ctr = 0;
printf("Generator initialized\n");
delete(G);
};
auto Generator::get_state() -> G_state {
return G_state{};
}
auto Generator::reseed(G_state G, const string& s) -> G_state {
string to_be_hashed = std::to_string(G.k) + s; string to_be_hashed = std::to_string(G.k) + s;
G.k = do_sha(stoul(to_be_hashed)); G.k = do_sha(stoul(to_be_hashed));
G.ctr++; G.ctr++;
return G; return G;
} }
auto do_sha(int64_t key_with_seed) -> int64_t { auto Generator::do_sha(int64_t key_with_seed) -> int64_t {
/* do sha256 */ /* do sha256 */
int64_t shastring = key_with_seed + 1; int64_t shastring = key_with_seed + 1;
return shastring; return shastring;
} }
G_state *initialize_generator(){ auto Generator::do_crypto(int64_t k, unsigned __int128 ctr) -> string {
auto G = new G_state;
G->k = 0;
G->ctr = 0;
return G;
};
auto do_crypto(int64_t k, unsigned __int128 ctr) -> string {
/* this function calls the block cipher /* this function calls the block cipher
* returns a string of k*(16 bytes); * returns a string of k*(16 bytes);
do whatever atm */ * do whatever atm */
k = 0; k = 0;
ctr = 0; ctr = 0;
return ""; return "";
} }
/* lacking objects, we have to return both the state and the string */ auto Generator::generate_blocks(G_state G, int k_blocks) -> tuple<string, G_state> {
auto generate_blocks(G_state G, int k_blocks) -> tuple<string, G_state> {
assert (G.ctr!=0); assert (G.ctr!=0);
string r = ""; string r = "";
for (int i = 0; i < k_blocks; ++i) { for (int i = 0; i < k_blocks; ++i) {
@ -55,8 +65,7 @@ auto generate_blocks(G_state G, int k_blocks) -> tuple<string, G_state> {
return {r, G}; return {r, G};
} }
/* n is number of random bytes to generate */ auto Generator::generate_random_data(G_state G, uint n) -> tuple<string, G_state> {
auto generate_random_data(G_state G, uint n) -> tuple<string, G_state> {
string r = ""; string r = "";
if (n < 0){ if (n < 0){
/* this should not be possible */ /* this should not be possible */
@ -74,12 +83,10 @@ auto generate_random_data(G_state G, uint n) -> tuple<string, G_state> {
/* re-key */ /* re-key */
// TODO: check conversions // TODO: check conversions
G.k = stoul(std::get<0>(generate_blocks(G, 2))); G.k = stoul(std::get<0>(generate_blocks(G, 2)));
// returning just r throws away our state, this should be reworked
// using OOP
// return r;
return {r, G}; return {r, G};
}; };
} // namespace generator } // namespace generator
} // namespace fortuna } // namespace fortuna
#endif

View File

@ -2,27 +2,38 @@
#define FORTUNA_GENERATOR_H #define FORTUNA_GENERATOR_H
#include <string> #include <string>
#include <cstring>
#include <tuple> #include <tuple>
namespace fortuna { namespace fortuna {
namespace generator { namespace generator {
struct G_state; using namespace std;
/* initializes generator */ class Generator {
G_state *initialize_generator(); public:
void initialize_generator();
auto do_crypto(int64_t k, unsigned __int128 ctr) -> std::string; protected:
struct G_state;
auto get_state() -> Generator::G_state;
auto reseed(G_state G, const std::string& s) -> G_state; private:
int64_t k;
unsigned __int128 ctr;
auto do_sha(int64_t key_with_seed) -> int64_t; auto reseed(Generator::G_state G, const string& s) -> Generator::G_state;
auto generate_blocks(G_state G, int k_blocks) -> std::tuple<std::string, G_state>; auto do_sha(int64_t key_with_seed) -> int64_t;
/* returns output of 0 <= n <= 2^20 bytes */ auto do_crypto(int64_t k, unsigned __int128 ctr) -> string;
auto generate_random_data(G_state G, int n) -> std::tuple<std::string, G_state>;
/* TODO(me): lacking objects (no more!), we have to return both the state and the string */
auto generate_blocks(Generator::G_state G, int k_blocks) -> tuple<string, Generator::G_state>;
/* n is the number of random bytes to generate */
auto generate_random_data(Generator::G_state G, uint n) -> tuple<string, Generator::G_state>;
}; // class generator
} //namespace generator } //namespace generator
} //namespace fortuna } //namespace fortuna

View File

@ -1,8 +1,11 @@
#include <iostream> #include <iostream>
#include "generator.h"
int main() { int main() {
using std::cout; using std::cout;
using std::endl; using std::endl;
cout << "[*] doing evil stuff" << endl; cout << "[*] doing evil stuff" << endl;
fortuna::generator::Generator g{};
g.initialize_generator();
return 0; return 0;
} }