🚚 moved hash function to seperate header

This commit is contained in:
EntireTwix 2021-07-01 23:29:22 -07:00
parent 00a3d0c075
commit d93786f4ff
6 changed files with 19 additions and 14 deletions

View file

@ -14,14 +14,6 @@ using BankResponse = std::pair<drogon::HttpStatusCode, Json::Value>;
class Bank
{
struct xxHashStringGen
{
inline uint64_t operator()(const std::string &str) const
{
return XXH3_64bits(str.data(), str.size());
}
};
phmap::parallel_flat_hash_map<
std::string, User,
xxHashStringGen,

View file

@ -1,7 +1,6 @@
#pragma once
#include <json/json.h> //to be removed later
#include <string>
#include <xxhash.h>
#include "xxhash_str.h"
#include "log.h"
struct User

8
include/xxhash_str.h Normal file
View file

@ -0,0 +1,8 @@
#pragma once
#include <string>
#include <xxhash.h>
struct xxHashStringGen
{
uint64_t operator()(const std::string &str) const noexcept;
};

View file

@ -106,7 +106,7 @@ bool Bank::VerifyPassword(const std::string &name, const std::string &attempt) c
{
bool res = false;
users.if_contains(name, [&res, &attempt](const User &u) {
res = (u.password == XXH3_64bits(attempt.data(), attempt.size()));
res = (u.password == xxHashStringGen{}(attempt));
});
return res;
}
@ -114,7 +114,7 @@ bool Bank::VerifyPassword(const std::string &name, const std::string &attempt) c
void Bank::ChangePassword(const std::string &name, std::string &&new_pass) noexcept
{
users.modify_if(name, [&new_pass](User &u) {
u.password = XXH3_64bits(new_pass.data(), new_pass.size());
u.password = xxHashStringGen{}(new_pass);
});
#if CONSERVATIVE_DISK_SAVE
save_flag.SetChangesOn();

View file

@ -5,7 +5,7 @@
*
* @param init_pass initial password
*/
User::User(std::string &&init_pass) noexcept : password(XXH3_64bits(init_pass.data(), init_pass.size())) {}
User::User(std::string &&init_pass) noexcept : password(xxHashStringGen{}(init_pass)) {}
/**
* @brief User Constructor for admins
@ -13,7 +13,7 @@ User::User(std::string &&init_pass) noexcept : password(XXH3_64bits(init_pass.da
* @param init_bal initial balance
* @param init_pass initial password
*/
User::User(uint32_t init_bal, std::string &&init_pass) noexcept : balance(init_bal), password(XXH3_64bits(init_pass.data(), init_pass.size())) {}
User::User(uint32_t init_bal, std::string &&init_pass) noexcept : balance(init_bal), password(xxHashStringGen{}(init_pass)) {}
/**
* @brief User Constructor for loading

6
src/xxhash_str.cpp Normal file
View file

@ -0,0 +1,6 @@
#include "xxhash_str.h"
uint64_t xxHashStringGen::operator()(const std::string &str) const noexcept
{
return XXH3_64bits(str.data(), str.size());
}