summaryrefslogtreecommitdiff
path: root/modules/crypto
diff options
context:
space:
mode:
authorClaudius 'keldu' Holeksa <mail@keldu.de>2024-07-18 15:47:29 +0200
committerClaudius 'keldu' Holeksa <mail@keldu.de>2024-07-18 15:47:29 +0200
commitf1223709e193c4513047293a1a42b55b9e8874b8 (patch)
tree874e76039ec3885e1448d69f14abd42763f4ba7b /modules/crypto
parente732c6efd96a22296591f3becc1c63fc80299938 (diff)
Added argon2i as hashing alg with one test
Diffstat (limited to 'modules/crypto')
-rw-r--r--modules/crypto/.nix/derivation.nix3
-rw-r--r--modules/crypto/SConstruct6
-rw-r--r--modules/crypto/c++/hash.hpp6
-rw-r--r--modules/crypto/tests/argon2i.cpp33
4 files changed, 43 insertions, 5 deletions
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<err::out_of_memory>("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<err::invalid_state>("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 <forstio/test/suite.hpp>
+#include <forstio/codec/data.hpp>
+#include <forstio/codec/base64.hpp>
+
+#include "../c++/hash.hpp"
+
+#include <iostream>
+namespace {
+namespace sch {
+using namespace saw::schema;
+}
+SAW_TEST("Codec Base64 Encode String"){
+ using namespace saw;
+
+ data<sch::String> inp_data{"Hello, World!"};
+ data<sch::String> salt{"salty678"};
+
+ hash<crypto::Argon2i> 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<sch::String, encode::Base64> base64_str;
+ codec<sch::String, encode::Base64> base64_codec;
+ auto eob64 = base64_codec.encode(val, base64_str);
+ SAW_EXPECT(eob64.is_value(), "Couldn't encode data");
+
+ data<sch::String, encode::Base64> check_against{"J082QuWpLuV3UnaVScg4NbUYLfb5VNKp4HAiPOevEbB+EZsxUbwSOoYUNUsXgEcvF2/lbysX0NBGDN5gcc1/TF0YIUi0xGAcNeK6DP5bZckqMrN7WRlCxugmSQLQ18SzDmIKdwxSo2vWO0ivV2m7ZGOxCdrfxZ26ivRUkeazOFA="};
+ SAW_EXPECT(base64_str == check_against, "Base64 not expected value");
+
+}
+}