chore: make methods aware of objects
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
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
This commit is contained in:
parent
01eadae32f
commit
926216fad3
|
@ -2,7 +2,6 @@
|
|||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <stdexcept>
|
||||
#include <tuple>
|
||||
#include "generator.h"
|
||||
|
||||
#ifndef FORTUNA_GENERATOR_CPP
|
||||
|
@ -33,11 +32,11 @@ auto Generator::get_state() -> G_state {
|
|||
return G_state{};
|
||||
}
|
||||
|
||||
auto Generator::reseed(G_state G, const string& s) -> 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 Generator::do_sha(int64_t key_with_seed) -> int64_t {
|
||||
|
@ -55,17 +54,19 @@ auto Generator::do_crypto(int64_t k, unsigned __int128 ctr) -> string {
|
|||
return "";
|
||||
}
|
||||
|
||||
auto Generator::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;
|
||||
}
|
||||
|
||||
auto Generator::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 */
|
||||
|
@ -77,13 +78,13 @@ auto Generator::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)));
|
||||
return {r, G};
|
||||
G.k = stoul(generate_blocks(2));
|
||||
return r;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
#define FORTUNA_GENERATOR_H
|
||||
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
|
||||
namespace fortuna {
|
||||
namespace generator {
|
||||
|
@ -21,17 +20,16 @@ private:
|
|||
int64_t k;
|
||||
unsigned __int128 ctr;
|
||||
|
||||
auto reseed(Generator::G_state G, const string& s) -> Generator::G_state;
|
||||
auto reseed(const string& s) -> void;
|
||||
|
||||
auto do_sha(int64_t key_with_seed) -> int64_t;
|
||||
|
||||
auto do_crypto(int64_t k, unsigned __int128 ctr) -> string;
|
||||
|
||||
/* 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>;
|
||||
auto generate_blocks(int k_blocks) -> string;
|
||||
|
||||
/* n is the number of random bytes to generate */
|
||||
auto generate_random_data(Generator::G_state G, uint n) -> tuple<string, Generator::G_state>;
|
||||
auto generate_random_data(uint n) -> string;
|
||||
|
||||
}; // class generator
|
||||
|
||||
|
|
Reference in New Issue