refactor(all): using namespace considered harmful
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
* project-wide refactor to accomodate removal of "using namespace xyz" to stop polluting top-level namespace with "std" as recommended by the Google C++ style guide. * use a pre-commit hook to enforce this ref: https://google.github.io/styleguide/cppguide.html#Namespaces
This commit is contained in:
parent
bad86c31df
commit
d8adb1af87
|
@ -20,6 +20,10 @@ repos:
|
||||||
files: \.(cpp|cc|cxx,h)$
|
files: \.(cpp|cc|cxx,h)$
|
||||||
types: [file, c++]
|
types: [file, c++]
|
||||||
types_or: [file, c++, header]
|
types_or: [file, c++, header]
|
||||||
|
- repo: https://github.com/gringolito/cpp-pre-commit-hooks
|
||||||
|
rev: 3481ddf0fb
|
||||||
|
hooks:
|
||||||
|
- id: check-using-namespace-directive
|
||||||
- repo: local
|
- repo: local
|
||||||
hooks:
|
hooks:
|
||||||
- id: make-test
|
- id: make-test
|
||||||
|
|
|
@ -17,8 +17,6 @@
|
||||||
namespace fortuna {
|
namespace fortuna {
|
||||||
namespace generator {
|
namespace generator {
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
struct Generator::G_state{
|
struct Generator::G_state{
|
||||||
int64_t k;
|
int64_t k;
|
||||||
unsigned __int128 ctr;
|
unsigned __int128 ctr;
|
||||||
|
@ -40,17 +38,17 @@ auto Generator::get_state() -> G_state {
|
||||||
return G_state{};
|
return G_state{};
|
||||||
}
|
}
|
||||||
|
|
||||||
auto Generator::reseed(const string& s) -> void {
|
auto Generator::reseed(const std::string& s) -> void {
|
||||||
G_state G = get_state();
|
G_state G = get_state();
|
||||||
string to_be_hashed = std::to_string(G.k) + s;
|
std::string to_be_hashed = std::to_string(G.k) + s;
|
||||||
// TODO(me): wrap do_sha in a try-catch
|
// TODO(me): wrap do_sha in a try-catch
|
||||||
string a = do_sha(to_be_hashed);
|
std::string a = do_sha(to_be_hashed);
|
||||||
// str, size, base
|
// str, size, base
|
||||||
G.k = stol(a,nullptr,10);
|
G.k = stol(a,nullptr,10);
|
||||||
G.ctr++;
|
G.ctr++;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto Generator::do_sha(const string& k_n_s) -> string {
|
auto Generator::do_sha(const std::string& k_n_s) -> std::string {
|
||||||
/* do sha256 */
|
/* do sha256 */
|
||||||
using std::cout;
|
using std::cout;
|
||||||
using std::endl;
|
using std::endl;
|
||||||
|
@ -58,7 +56,7 @@ auto Generator::do_sha(const string& k_n_s) -> string {
|
||||||
using CryptoPP::HashFilter;
|
using CryptoPP::HashFilter;
|
||||||
using CryptoPP::StringSink;
|
using CryptoPP::StringSink;
|
||||||
|
|
||||||
string digest;
|
std::string digest;
|
||||||
|
|
||||||
CryptoPP::SHA256 sha2_256;
|
CryptoPP::SHA256 sha2_256;
|
||||||
CryptoPP::SHA3_256 sha3_256;
|
CryptoPP::SHA3_256 sha3_256;
|
||||||
|
@ -88,7 +86,7 @@ auto Generator::do_sha(const string& k_n_s) -> string {
|
||||||
return "42";
|
return "42";
|
||||||
}
|
}
|
||||||
|
|
||||||
auto Generator::do_crypto(int64_t k, unsigned __int128 ctr) -> string {
|
auto Generator::do_crypto(int64_t k, unsigned __int128 ctr) -> std::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 */
|
||||||
|
@ -97,10 +95,10 @@ auto Generator::do_crypto(int64_t k, unsigned __int128 ctr) -> string {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
auto Generator::generate_blocks(unsigned int k_blocks) -> string {
|
auto Generator::generate_blocks(unsigned int k_blocks) -> std::string {
|
||||||
G_state G = get_state();
|
G_state G = get_state();
|
||||||
assert (G.ctr!=0);
|
assert (G.ctr!=0);
|
||||||
string r = "";
|
std::string r = "";
|
||||||
for (int i = 0; i < k_blocks; ++i) {
|
for (int i = 0; i < k_blocks; ++i) {
|
||||||
r += do_crypto(G.k, G.ctr);
|
r += do_crypto(G.k, G.ctr);
|
||||||
G.ctr++;
|
G.ctr++;
|
||||||
|
@ -108,20 +106,20 @@ auto Generator::generate_blocks(unsigned int k_blocks) -> string {
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto Generator::generate_random_data(uint n) -> string {
|
auto Generator::generate_random_data(uint n) -> std::string {
|
||||||
G_state G = get_state();
|
G_state G = get_state();
|
||||||
string r = "";
|
std::string r = "";
|
||||||
if (n < 0){
|
if (n < 0){
|
||||||
/* this should not be possible */
|
/* this should not be possible */
|
||||||
printf("[*] error: n cannot be < 0\n");
|
printf("[*] error: n cannot be < 0\n");
|
||||||
throw invalid_argument("n cannot be < 0");
|
throw std::invalid_argument("n cannot be < 0");
|
||||||
} else if (n > pow(2,20)){
|
} else if (n > pow(2,20)){
|
||||||
printf("[*] error: n cannot be > 2^20\n");
|
printf("[*] error: n cannot be > 2^20\n");
|
||||||
throw invalid_argument("n cannot be > 2^20");
|
throw std::invalid_argument("n cannot be > 2^20");
|
||||||
}
|
}
|
||||||
/* do magic to compute r
|
/* do magic to compute r
|
||||||
* r ← first-n-bytes(GenerateBlocks(G, ceil(n/16) )) */
|
* r ← first-n-bytes(GenerateBlocks(G, ceil(n/16) )) */
|
||||||
string rr = generate_blocks(ceil(n/16));
|
std::string rr = generate_blocks(ceil(n/16));
|
||||||
r = rr.substr(0,n);
|
r = rr.substr(0,n);
|
||||||
|
|
||||||
/* re-key */
|
/* re-key */
|
||||||
|
|
14
generator.h
14
generator.h
|
@ -6,26 +6,24 @@
|
||||||
namespace fortuna {
|
namespace fortuna {
|
||||||
namespace generator {
|
namespace generator {
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
class Generator {
|
class Generator {
|
||||||
public:
|
public:
|
||||||
void initialize_generator();
|
void initialize_generator();
|
||||||
auto generate_blocks(unsigned int k_blocks) -> string;
|
auto generate_blocks(unsigned int k_blocks) -> std::string;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct G_state;
|
struct G_state;
|
||||||
|
|
||||||
auto reseed(const string& s) -> void;
|
auto reseed(const std::string& s) -> void;
|
||||||
|
|
||||||
auto do_sha(const string& k_n_s) -> string;
|
auto do_sha(const std::string& k_n_s) -> std::string;
|
||||||
|
|
||||||
auto do_crypto(int64_t k, unsigned __int128 ctr) -> string;
|
auto do_crypto(int64_t k, unsigned __int128 ctr) -> std::string;
|
||||||
|
|
||||||
/* n is the number of random bytes to generate */
|
/* n is the number of random bytes to generate */
|
||||||
auto generate_random_data(uint n) -> string;
|
auto generate_random_data(uint n) -> std::string;
|
||||||
|
|
||||||
auto strtolowerpls(string &s) -> string;
|
auto strtolowerpls(std::string &s) -> std::string;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
auto get_state() -> Generator::G_state;
|
auto get_state() -> Generator::G_state;
|
||||||
|
|
Reference in New Issue