squashed the following: commita28354d585
Author: surtur <a_mirre@utb.cz> Date: Wed Nov 3 02:14:35 2021 +0100 chore: make G_state a private structure * make G_state private * move protected section behind private * also do cleanup a little -> k and ctr won't exist as part of the generator outside of the G_state struct commit926216fad3
Author: surtur <a_mirre@utb.cz> Date: Wed Nov 3 01:37:28 2021 +0100 chore: make methods aware of objects as a consequence we stopped returning the generator state from every function and equally stopped using it as a parameter to every generator method that handles the state in any way commit01eadae32f
Author: surtur <a_mirre@utb.cz> Date: Tue Nov 2 05:10:58 2021 +0100 feat: move from structural to object paradigm * 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:
parent
f2ed6918c9
commit
f64295f052
|
@ -2,61 +2,69 @@
|
|||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <stdexcept>
|
||||
#include <tuple>
|
||||
#include "generator.h"
|
||||
|
||||
#ifndef FORTUNA_GENERATOR_CPP
|
||||
#define FORTUNA_GENERATOR_CPP
|
||||
namespace fortuna {
|
||||
namespace generator {
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct G_state{
|
||||
struct Generator::G_state{
|
||||
int64_t k;
|
||||
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;
|
||||
printf("Generator initialized\n");
|
||||
delete(G);
|
||||
};
|
||||
|
||||
auto Generator::get_state() -> G_state {
|
||||
return G_state{};
|
||||
}
|
||||
|
||||
auto Generator::reseed(const string& s) -> void {
|
||||
G_state G = get_state();
|
||||
string to_be_hashed = std::to_string(G.k) + s;
|
||||
G.k = do_sha(stoul(to_be_hashed));
|
||||
G.ctr++;
|
||||
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 */
|
||||
int64_t shastring = key_with_seed + 1;
|
||||
return shastring;
|
||||
}
|
||||
|
||||
G_state *initialize_generator(){
|
||||
auto G = new G_state;
|
||||
G->k = 0;
|
||||
G->ctr = 0;
|
||||
return G;
|
||||
};
|
||||
|
||||
auto do_crypto(int64_t k, unsigned __int128 ctr) -> string {
|
||||
auto Generator::do_crypto(int64_t k, unsigned __int128 ctr) -> string {
|
||||
/* this function calls the block cipher
|
||||
* returns a string of k*(16 bytes);
|
||||
do whatever atm */
|
||||
* do whatever atm */
|
||||
k = 0;
|
||||
ctr = 0;
|
||||
return "";
|
||||
}
|
||||
|
||||
/* lacking objects, we have to return both the state and the string */
|
||||
auto generate_blocks(G_state G, int k_blocks) -> tuple<string, G_state> {
|
||||
auto Generator::generate_blocks(int k_blocks) -> string {
|
||||
G_state G = get_state();
|
||||
assert (G.ctr!=0);
|
||||
string r = "";
|
||||
for (int i = 0; i < k_blocks; ++i) {
|
||||
r += do_crypto(G.k, G.ctr);
|
||||
G.ctr++;
|
||||
}
|
||||
return {r, G};
|
||||
return r;
|
||||
}
|
||||
|
||||
/* n is number of random bytes to generate */
|
||||
auto generate_random_data(G_state G, uint n) -> tuple<string, G_state> {
|
||||
auto Generator::generate_random_data(uint n) -> string {
|
||||
G_state G = get_state();
|
||||
string r = "";
|
||||
if (n < 0){
|
||||
/* this should not be possible */
|
||||
|
@ -68,18 +76,16 @@ auto generate_random_data(G_state G, uint n) -> tuple<string, G_state> {
|
|||
}
|
||||
/* do magic to compute r
|
||||
* r ← first-n-bytes(GenerateBlocks(G, ceil(n/16) )) */
|
||||
string rr = std::get<0>(generate_blocks(G,ceil(n/16)));
|
||||
string rr = generate_blocks(ceil(n/16));
|
||||
r = rr.substr(0,n);
|
||||
|
||||
/* re-key */
|
||||
// TODO: check conversions
|
||||
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};
|
||||
G.k = stoul(generate_blocks(2));
|
||||
return r;
|
||||
};
|
||||
|
||||
|
||||
} // namespace generator
|
||||
} // namespace fortuna
|
||||
|
||||
#endif
|
||||
|
|
29
generator.h
29
generator.h
|
@ -2,27 +2,34 @@
|
|||
#define FORTUNA_GENERATOR_H
|
||||
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <tuple>
|
||||
|
||||
namespace fortuna {
|
||||
namespace generator {
|
||||
|
||||
struct G_state;
|
||||
using namespace std;
|
||||
|
||||
/* initializes generator */
|
||||
G_state *initialize_generator();
|
||||
class Generator {
|
||||
public:
|
||||
void initialize_generator();
|
||||
|
||||
auto do_crypto(int64_t k, unsigned __int128 ctr) -> std::string;
|
||||
private:
|
||||
struct G_state;
|
||||
|
||||
auto reseed(G_state G, const std::string& s) -> G_state;
|
||||
auto reseed(const string& s) -> void;
|
||||
|
||||
auto do_sha(int64_t key_with_seed) -> int64_t;
|
||||
auto do_sha(int64_t key_with_seed) -> int64_t;
|
||||
|
||||
auto generate_blocks(G_state G, int k_blocks) -> std::tuple<std::string, G_state>;
|
||||
auto do_crypto(int64_t k, unsigned __int128 ctr) -> string;
|
||||
|
||||
/* returns output of 0 <= n <= 2^20 bytes */
|
||||
auto generate_random_data(G_state G, int n) -> std::tuple<std::string, G_state>;
|
||||
auto generate_blocks(int k_blocks) -> string;
|
||||
|
||||
/* n is the number of random bytes to generate */
|
||||
auto generate_random_data(uint n) -> string;
|
||||
|
||||
protected:
|
||||
auto get_state() -> Generator::G_state;
|
||||
|
||||
}; // class generator
|
||||
|
||||
} //namespace generator
|
||||
} //namespace fortuna
|
||||
|
|
Reference in New Issue