From d8adb1af874bb9a887c0643f6c1bb4680d1f7944 Mon Sep 17 00:00:00 2001 From: surtur Date: Sat, 13 Nov 2021 21:58:05 +0100 Subject: [PATCH] refactor(all): using namespace considered harmful * 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 --- .pre-commit-config.yaml | 4 ++++ generator.cpp | 28 +++++++++++++--------------- generator.h | 14 ++++++-------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a78ca80..e03fb8d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -20,6 +20,10 @@ repos: files: \.(cpp|cc|cxx,h)$ types: [file, c++] types_or: [file, c++, header] + - repo: https://github.com/gringolito/cpp-pre-commit-hooks + rev: 3481ddf0fb + hooks: + - id: check-using-namespace-directive - repo: local hooks: - id: make-test diff --git a/generator.cpp b/generator.cpp index 1c4fcd8..dd69e9b 100644 --- a/generator.cpp +++ b/generator.cpp @@ -17,8 +17,6 @@ namespace fortuna { namespace generator { -using namespace std; - struct Generator::G_state{ int64_t k; unsigned __int128 ctr; @@ -40,17 +38,17 @@ auto Generator::get_state() -> 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(); - 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 - string a = do_sha(to_be_hashed); + std::string a = do_sha(to_be_hashed); // str, size, base G.k = stol(a,nullptr,10); 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 */ using std::cout; using std::endl; @@ -58,7 +56,7 @@ auto Generator::do_sha(const string& k_n_s) -> string { using CryptoPP::HashFilter; using CryptoPP::StringSink; - string digest; + std::string digest; CryptoPP::SHA256 sha2_256; CryptoPP::SHA3_256 sha3_256; @@ -88,7 +86,7 @@ auto Generator::do_sha(const string& k_n_s) -> string { 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 * returns a string of k*(16 bytes); * do whatever atm */ @@ -97,10 +95,10 @@ auto Generator::do_crypto(int64_t k, unsigned __int128 ctr) -> string { 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(); assert (G.ctr!=0); - string r = ""; + std::string r = ""; for (int i = 0; i < k_blocks; ++i) { r += do_crypto(G.k, G.ctr); G.ctr++; @@ -108,20 +106,20 @@ auto Generator::generate_blocks(unsigned int k_blocks) -> string { return r; } -auto Generator::generate_random_data(uint n) -> string { +auto Generator::generate_random_data(uint n) -> std::string { G_state G = get_state(); - string r = ""; + std::string r = ""; if (n < 0){ /* this should not be possible */ 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)){ 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 * 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); /* re-key */ diff --git a/generator.h b/generator.h index 6f2e2e5..79bf54e 100644 --- a/generator.h +++ b/generator.h @@ -6,26 +6,24 @@ namespace fortuna { namespace generator { -using namespace std; - class Generator { public: void initialize_generator(); - auto generate_blocks(unsigned int k_blocks) -> string; + auto generate_blocks(unsigned int k_blocks) -> std::string; private: 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 */ - 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: auto get_state() -> Generator::G_state;