types and db setup

This commit is contained in:
2026-03-11 00:10:34 +00:00
parent 2857c35936
commit 067d8434cd
9 changed files with 234 additions and 0 deletions

27
src/hashing.cpp Normal file
View File

@@ -0,0 +1,27 @@
#include <crypt.h>
#include <stdexcept>
#include "hashing.hpp"
std::string hashing::GenerateSetting(unsigned long cost) {
// "$y$" is the yescrypt prefix
const char* setting = crypt_gensalt("$y$", cost, nullptr, 0);
if (!setting)
throw std::runtime_error("crypt_gensalt() failed yescrypt may not be supported");
return setting;
}
std::string hashing::HashPassword(const std::string& password, const std::string& setting) {
crypt_data data{};
const char* hash = crypt_r(password.c_str(), setting.c_str(), &data);
if (!hash || hash[0] == '*')
throw std::runtime_error("crypt_r() failed");
return hash;
}
bool hashing::VerifyPassword(const std::string& password, const std::string& stored_hash) {
crypt_data data{};
const char* result = crypt_r(password.c_str(), stored_hash.c_str(), &data);
if (!result || result[0] == '*')
return false;
return stored_hash == result;
}