summaryrefslogtreecommitdiff
path: root/modules/crypto/c++/hash.hpp
blob: 8e8ca866cc657849088d2726a7a40259662a38ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#pragma once

#include <argon2.h>

namespace saw {
namespace crypto {
struct Argon2i {};
}

template<typename Crypto>
class hash;

template<>
class hash<crypto::Argon2i> {
public:
	error_or<data<schema::String>> apply(const data<schema::String>& input, const data<schema::String>& salt){
		SAW_ASSERT(input.size() > 0u){
			return make_error<err::invalid_state>("Strings for hashing shouldn't be zero");
		}
		uint32_t t_cost = 2;
		uint32_t m_cost = 1<<16;
		uint32_t parallel = 1;
		const char* salt_c_ptr = nullptr;
		if(salt.size() > 0){
			salt_c_ptr = &salt.at(0);
		}
		data<schema::String> hash;
		try {
			hash = {128u};
		}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_c_ptr, salt.size(), &hash.at(0), hash.size());
		if(rv != ARGON2_OK){
			return make_error<err::invalid_state>("Failed to hash");
		}

		return hash;
	}
};
}