实现登录接口。

This commit is contained in:
root
2025-03-01 09:09:30 +00:00
parent 63aa6a6270
commit d1b8bf6342
15 changed files with 608 additions and 8 deletions

12
Base/CMakeLists.txt Normal file
View File

@ -0,0 +1,12 @@
add_library(Base
DataStructure.h DataStructure.cpp
)
get_filename_component(PARENT_DIR ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY)
target_include_directories(Base
INTERFACE ${PARENT_DIR}
)
target_link_libraries(Base
PRIVATE OpenSSL::Crypto
)

71
Base/DataStructure.cpp Normal file
View File

@ -0,0 +1,71 @@
#include "DataStructure.h"
#include <openssl/evp.h>
#include <openssl/kdf.h>
#include <openssl/rand.h>
#include <regex>
#include <stdexcept>
namespace Older {
int PBKDF2_ITERATIONS = 100000;
constexpr int SALT_LENGTH = 16;
constexpr int HASH_LENGTH = 32;
Account Account::hashPassword(const std::string &password) {
Account ret;
// 生成随机盐
ret.salt.resize(SALT_LENGTH);
if (RAND_bytes(ret.salt.data(), SALT_LENGTH) != 1) {
throw std::runtime_error("Salt generation failed");
}
// PBKDF2 派生
EVP_KDF *kdf = EVP_KDF_fetch(nullptr, "PBKDF2", nullptr);
EVP_KDF_CTX *ctx = EVP_KDF_CTX_new(kdf);
OSSL_PARAM params[] = {OSSL_PARAM_construct_utf8_string("digest", "SHA256", 0),
OSSL_PARAM_construct_octet_string("salt", ret.salt.data(), ret.salt.size()),
OSSL_PARAM_construct_octet_string("pass", (void *)password.data(), password.size()),
OSSL_PARAM_construct_int("iter", &PBKDF2_ITERATIONS), OSSL_PARAM_construct_end()};
ret.passwordHash.resize(HASH_LENGTH);
if (EVP_KDF_derive(ctx, ret.passwordHash.data(), HASH_LENGTH, params) != 1) {
EVP_KDF_CTX_free(ctx);
EVP_KDF_free(kdf);
throw std::runtime_error("PBKDF2 failed");
}
EVP_KDF_CTX_free(ctx);
EVP_KDF_free(kdf);
return ret;
}
bool Account::verifyPassword(const Account &account, const std::string &password) {
// 重新计算哈希
EVP_KDF *kdf = EVP_KDF_fetch(nullptr, "PBKDF2", nullptr);
EVP_KDF_CTX *ctx = EVP_KDF_CTX_new(kdf);
OSSL_PARAM params[5] = {OSSL_PARAM_construct_utf8_string("digest", const_cast<char *>("SHA256"), 0),
OSSL_PARAM_construct_octet_string("salt", (void *)account.salt.data(), account.salt.size()),
OSSL_PARAM_construct_octet_string("pass", const_cast<char *>(password.data()), password.size()),
OSSL_PARAM_construct_int("iter", &PBKDF2_ITERATIONS), OSSL_PARAM_construct_end()};
std::vector<unsigned char> new_hash(HASH_LENGTH);
if (EVP_KDF_derive(ctx, new_hash.data(), HASH_LENGTH, params) != 1) {
EVP_KDF_CTX_free(ctx);
EVP_KDF_free(kdf);
return false;
}
EVP_KDF_CTX_free(ctx);
EVP_KDF_free(kdf);
// 安全比较(防时序攻击)
return CRYPTO_memcmp(account.passwordHash.data(), new_hash.data(), HASH_LENGTH) == 0;
}
bool Account::validateEmail(const std::string &email) {
const std::regex pattern(R"([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})");
return std::regex_match(email, pattern);
}
} // namespace Older

View File

@ -1,17 +1,34 @@
#ifndef __DATASTRUCTURE_H__
#define __DATASTRUCTURE_H__
#include <cstdint>
#include <string>
#include <vector>
struct VisitorStats{
namespace Older {
struct VisitorStats {
std::string url;
int visitorCount=0;
int totalViews =0;
int64_t lastViewTime =0;
int visitorCount = 0;
int totalViews = 0;
int64_t lastViewTime = 0;
};
struct SiteStats{
int totalViews =0;
int totalVisitors =0;
struct SiteStats {
int totalViews = 0;
int totalVisitors = 0;
};
struct Account {
int64_t id = 0;
std::string username;
std::string email;
std::vector<uint8_t> passwordHash;
std::vector<uint8_t> salt;
int64_t createdAt = 0;
static Account hashPassword(const std::string &password);
static bool verifyPassword(const Account &account, const std::string &password);
static bool validateEmail(const std::string& email);
};}
#endif // __DATASTRUCTURE_H__