forked from mtusi2026/mtusi2026lab6
lab6
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
#include "auth.hpp"
|
||||
|
||||
#include <sodium.h>
|
||||
#include <stdexcept>
|
||||
|
||||
|
||||
namespace {
|
||||
struct SodiumInit {
|
||||
SodiumInit() {
|
||||
if (sodium_init() < 0) {
|
||||
throw std::runtime_error("sodium_init failed");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const SodiumInit g_sodium_init;
|
||||
}
|
||||
|
||||
|
||||
std::string hash_password(const std::string& plain) {
|
||||
char hashed[crypto_pwhash_STRBYTES];
|
||||
if (crypto_pwhash_str(
|
||||
hashed,
|
||||
plain.data(), plain.size(),
|
||||
crypto_pwhash_OPSLIMIT_INTERACTIVE,
|
||||
crypto_pwhash_MEMLIMIT_INTERACTIVE) != 0) {
|
||||
throw std::runtime_error("crypto_pwhash_str failed (out of memory?)");
|
||||
}
|
||||
return std::string(hashed);
|
||||
}
|
||||
|
||||
bool verify_password(const std::string& plain, const std::string& hash) {
|
||||
if (hash.empty() || hash.size() >= crypto_pwhash_STRBYTES) {
|
||||
return false;
|
||||
}
|
||||
return crypto_pwhash_str_verify(
|
||||
hash.c_str(),
|
||||
plain.data(), plain.size()) == 0;
|
||||
}
|
||||
Reference in New Issue
Block a user