database functions

This commit is contained in:
2026-03-11 17:48:24 +00:00
parent fe8a46a488
commit 1333de2b37
9 changed files with 308 additions and 4 deletions

View File

@@ -1,6 +1,9 @@
#include <crypt.h>
#include <stdexcept>
#include "hashing.hpp"
#include <random>
#include <sstream>
#include <iomanip>
std::string hashing::GenerateSetting(unsigned long cost) {
// "$y$" is the yescrypt prefix
@@ -24,4 +27,18 @@ bool hashing::VerifyPassword(const std::string& password, const std::string& sto
if (!result || result[0] == '*')
return false;
return stored_hash == result;
}
std::string hashing::generate_token(size_t bytes) {
std::random_device rd;
std::string token;
token.reserve(bytes * 2);
std::uniform_int_distribution<uint8_t> dist(0, 255);
for (size_t i = 0; i < bytes; ++i) {
std::ostringstream ss;
ss << std::hex << std::setw(2) << std::setfill('0') << (int)dist(rd);
token += ss.str();
}
return token;
}