generator: implement do_sha()
All checks were successful
continuous-integration/drone/push Build is passing

* link against cryptopp
* use both sha2 and sha3 generation functions
* try calling do_sha() directly
* call reseed() that in turn calls do_sha()
* return a bogus number, not the proper digest for now (see TODOs)

cryptopp needed to be installed to archlinux for the valgrind step to
pass successfully
This commit is contained in:
surtur 2021-11-11 02:35:17 +01:00
parent 73f8bd75a2
commit f16c630ae7
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
4 changed files with 53 additions and 8 deletions

View File

@ -58,7 +58,7 @@ def main(ctx):
"depends_on": ["build debug"],
"commands": [
"uname -r",
"pacman -Sy gcc cmake ninja valgrind --noconfirm --needed",
"pacman -Sy gcc cmake crypto++ ninja valgrind --noconfirm --needed",
"valgrind --version",
"make valgrind"
]
@ -70,7 +70,7 @@ def main(ctx):
"depends_on": ["build release"],
"commands": [
"uname -r",
"pacman -Sy gcc cmake ninja valgrind --noconfirm --needed",
"pacman -Sy gcc cmake crypto++ ninja valgrind --noconfirm --needed",
"valgrind --version",
"make valgrind-release"
]

View File

@ -1,6 +1,6 @@
cppch = cppcheck
cppch_args = --language=c++ --std=c++20 --enable=all --verbose ./*.{cpp,h}
cpp_flags = CMAKE_CXX_FLAGS=
cpp_flags = CMAKE_CXX_FLAGS=-lcryptopp
c = cmake
c_args = -G Ninja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=
n = ninja

View File

@ -2,6 +2,14 @@
#include <cassert>
#include <cstdint>
#include <stdexcept>
#include <iostream>
#include <algorithm>
#include <cryptopp/hex.h>
#include <cryptopp/filters.h>
#include <cryptopp/sha.h>
#include <cryptopp/sha3.h>
#include "generator.h"
#ifndef FORTUNA_GENERATOR_CPP
@ -23,6 +31,10 @@ void Generator::initialize_generator(){
G->k = 0;
G->ctr = 0;
printf("Generator initialized\n");
// TODO(me): rm this, it's here just so it's called
do_sha("fortuna");
// TODO(me): bogus call of reseed just so it's called
reseed("");
delete(G);
};
@ -33,14 +45,47 @@ auto Generator::get_state() -> 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));
// TODO(me): wrap do_sha in a try-catch
string a = do_sha(to_be_hashed);
// str, size, base
G.k = stol(a,nullptr,10);
G.ctr++;
}
auto Generator::do_sha(int64_t key_with_seed) -> int64_t {
auto Generator::do_sha(const string& k_n_s) -> string {
/* do sha256 */
int64_t shastring = key_with_seed + 1;
return shastring;
using std::cout;
using std::endl;
using CryptoPP::HexEncoder;
using CryptoPP::HashFilter;
using CryptoPP::StringSink;
string digest;
CryptoPP::SHA256 sha2_256;
CryptoPP::SHA3_256 sha3_256;
digest.erase();
CryptoPP::StringSource foo(k_n_s,true,
new HashFilter(sha2_256,new HexEncoder(new StringSink(digest)))
);
digest = strtolowerpls(digest);
cout << digest << endl;
digest.erase();
CryptoPP::StringSource bar(k_n_s,true,
new HashFilter(sha3_256,new HexEncoder(new StringSink(digest)))
);
digest = strtolowerpls(digest);
cout << digest << endl;
// return digest;
// TODO(me)
// do not return the proper digest just yet, too long to fit into
// int64_t. G.k has to be a 256bit-wide type. for now just return a
// small enough number
return "42";
}
auto Generator::do_crypto(int64_t k, unsigned __int128 ctr) -> string {

View File

@ -18,7 +18,7 @@ private:
auto reseed(const string& s) -> void;
auto do_sha(int64_t key_with_seed) -> int64_t;
auto do_sha(const string& k_n_s) -> string;
auto do_crypto(int64_t k, unsigned __int128 ctr) -> string;