From f1223709e193c4513047293a1a42b55b9e8874b8 Mon Sep 17 00:00:00 2001 From: Claudius 'keldu' Holeksa Date: Thu, 18 Jul 2024 15:47:29 +0200 Subject: Added argon2i as hashing alg with one test --- modules/codec/tests/base64.cpp | 6 +++--- modules/crypto/.nix/derivation.nix | 3 ++- modules/crypto/SConstruct | 6 +++++- modules/crypto/c++/hash.hpp | 6 +++--- modules/crypto/tests/argon2i.cpp | 33 +++++++++++++++++++++++++++++++++ 5 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 modules/crypto/tests/argon2i.cpp diff --git a/modules/codec/tests/base64.cpp b/modules/codec/tests/base64.cpp index 7fb986b..b52a6bf 100644 --- a/modules/codec/tests/base64.cpp +++ b/modules/codec/tests/base64.cpp @@ -2,12 +2,11 @@ #include "../c++/data.hpp" #include "../c++/base64.hpp" -#include namespace { namespace sch { using namespace saw::schema; } -SAW_TEST("Codec Base64 Encode String"){ +SAW_TEST("Argon2i Hash String"){ using namespace saw; data inp_data{"Hello, World!"}; @@ -19,6 +18,7 @@ SAW_TEST("Codec Base64 Encode String"){ auto eov = base64_codec.encode(inp_data, base64_str); SAW_EXPECT(eov.is_value(), "Couldn't encode data"); - SAW_EXPECT((base64_str == data{"SGVsbG8sIFdvcmxkIQ=="}), "Base64 not expected value"); + data check_against{"SGVsbG8sIFdvcmxkIQ=="}; + SAW_EXPECT(base64_str == check_against, "Base64 not expected value"); } } diff --git a/modules/crypto/.nix/derivation.nix b/modules/crypto/.nix/derivation.nix index d470509..827eac0 100644 --- a/modules/crypto/.nix/derivation.nix +++ b/modules/crypto/.nix/derivation.nix @@ -22,7 +22,8 @@ in stdenv.mkDerivation { ]; buildInputs = [ - forstio.core + forstio.core + forstio.codec libargon2 ]; diff --git a/modules/crypto/SConstruct b/modules/crypto/SConstruct index f71db5a..a2b6f96 100644 --- a/modules/crypto/SConstruct +++ b/modules/crypto/SConstruct @@ -46,7 +46,11 @@ env_vars.Add('prefix', env=Environment(ENV=os.environ, variables=env_vars, CPPPATH=[], CPPDEFINES=['SAW_UNIX'], CXXFLAGS=['-std=c++20','-g','-Wall','-Wextra'], - LIBS=['forstio-core']) + LIBS=[ + 'forstio-core' + ,'argon2' + ] +) env.__class__.add_source_files = add_kel_source_files env.Tool('compilation_db'); env.cdb = env.CompilationDatabase('compile_commands.json'); diff --git a/modules/crypto/c++/hash.hpp b/modules/crypto/c++/hash.hpp index 5898adc..8e8ca86 100644 --- a/modules/crypto/c++/hash.hpp +++ b/modules/crypto/c++/hash.hpp @@ -20,7 +20,7 @@ public: uint32_t t_cost = 2; uint32_t m_cost = 1<<16; uint32_t parallel = 1; - char* salt_c_ptr = nullptr; + const char* salt_c_ptr = nullptr; if(salt.size() > 0){ salt_c_ptr = &salt.at(0); } @@ -30,8 +30,8 @@ public: }catch(const std::exception&){ return make_error("Couldn't allocate hash string"); } - int rv = argon2i_hash_raw(t_cost, m_cost, parallel, &input.at(0), input.size(), &salt.at(0), salt.size(), &hash.at(0), hash.size()); - if(rc != ARGON2_OK){ + int rv = argon2i_hash_raw(t_cost, m_cost, parallel, &input.at(0), input.size(), salt_c_ptr, salt.size(), &hash.at(0), hash.size()); + if(rv != ARGON2_OK){ return make_error("Failed to hash"); } diff --git a/modules/crypto/tests/argon2i.cpp b/modules/crypto/tests/argon2i.cpp new file mode 100644 index 0000000..9f8efc5 --- /dev/null +++ b/modules/crypto/tests/argon2i.cpp @@ -0,0 +1,33 @@ +#include +#include +#include + +#include "../c++/hash.hpp" + +#include +namespace { +namespace sch { +using namespace saw::schema; +} +SAW_TEST("Codec Base64 Encode String"){ + using namespace saw; + + data inp_data{"Hello, World!"}; + data salt{"salty678"}; + + hash hasher; + + auto eov = hasher.apply(inp_data, salt); + SAW_EXPECT(eov.is_value(), (std::string{"Hashing failed. "} + std::string{eov.get_error().get_message()}) ); + auto& val = eov.get_value(); + + data base64_str; + codec base64_codec; + auto eob64 = base64_codec.encode(val, base64_str); + SAW_EXPECT(eob64.is_value(), "Couldn't encode data"); + + data check_against{"J082QuWpLuV3UnaVScg4NbUYLfb5VNKp4HAiPOevEbB+EZsxUbwSOoYUNUsXgEcvF2/lbysX0NBGDN5gcc1/TF0YIUi0xGAcNeK6DP5bZckqMrN7WRlCxugmSQLQ18SzDmIKdwxSo2vWO0ivV2m7ZGOxCdrfxZ26ivRUkeazOFA="}; + SAW_EXPECT(base64_str == check_against, "Base64 not expected value"); + +} +} -- cgit v1.2.3