types and db setup
This commit is contained in:
27
src/hashing.cpp
Normal file
27
src/hashing.cpp
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user