添加管理员密码哈希处理功能,支持生成、验证和随机生成密码;重构相关接口
This commit is contained in:
parent
479d38036e
commit
59c4723590
@ -19,11 +19,17 @@ extern "C"
|
||||
int registe(const char* user_id, const char* password);
|
||||
|
||||
int delete_user(const char* user_id);
|
||||
|
||||
|
||||
int get_user_permission(const char* user_id, int* result);
|
||||
|
||||
|
||||
int set_user_permission(const char* user_id, int permission);
|
||||
|
||||
int set_admin_password_hash(const char* hash);
|
||||
|
||||
int get_admin_password_hash(char** result);
|
||||
|
||||
int has_admin_password_hash(int* result);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
21
include/hash/hash.h
Normal file
21
include/hash/hash.h
Normal file
@ -0,0 +1,21 @@
|
||||
#ifndef JWT_H
|
||||
#define JWT_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
char* kqm_generate_hash(const char* password, size_t rounds);
|
||||
|
||||
int kqm_validate_password(const char* password, const char* hash);
|
||||
|
||||
char* kqm_random_password(size_t length);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // JWT_H
|
@ -5,6 +5,9 @@
|
||||
#include <string_view>
|
||||
|
||||
auto generate_hash(const std::string_view password, std::size_t rounds) -> std::string;
|
||||
|
||||
auto validate_password(const std::string_view password, const std::string_view hash) -> bool;
|
||||
|
||||
auto random_password(std::size_t length) -> std::string;
|
||||
|
||||
#endif // HASH_HPP
|
@ -149,4 +149,42 @@ extern "C"
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int set_admin_password_hash(const char* hash)
|
||||
{
|
||||
auto status = user_db->Put(leveldb::WriteOptions{}, "admin_password_hash", hash);
|
||||
if (!status.ok()) {
|
||||
std::println(stderr, "Failed to set admin password hash: {}", status.ToString());
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int get_admin_password_hash(char** result)
|
||||
{
|
||||
auto value = std::string{};
|
||||
auto status = user_db->Get(leveldb::ReadOptions{}, "admin_password_hash", &value);
|
||||
if (status.ok()) {
|
||||
*result = strdup(value.c_str());
|
||||
return 1;
|
||||
} else {
|
||||
std::println(stderr, "Failed to get admin password hash: {}", status.ToString());
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int has_admin_password_hash(int* result)
|
||||
{
|
||||
auto value = std::string{};
|
||||
auto status = user_db->Get(leveldb::ReadOptions{}, "admin_password_hash", &value);
|
||||
if (status.ok()) {
|
||||
*result = 1;
|
||||
} else if (status.IsNotFound()) {
|
||||
*result = 0;
|
||||
} else {
|
||||
std::println(stderr, "Failed to check admin password hash existence: {}", status.ToString());
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
#include <hash.hpp>
|
||||
#include "hash.hpp"
|
||||
#include "hash.h"
|
||||
|
||||
#include <cryptopp/algparam.h>
|
||||
#include <cryptopp/cryptlib.h>
|
||||
@ -11,6 +12,7 @@
|
||||
|
||||
#include <array>
|
||||
#include <format>
|
||||
#include <random>
|
||||
#include <vector>
|
||||
|
||||
using namespace CryptoPP;
|
||||
@ -89,3 +91,37 @@ auto validate_password(const std::string_view password, const std::string_view h
|
||||
return entered_hashed_password == stored_hashed_password;
|
||||
}
|
||||
|
||||
auto random_password(std::size_t length) -> std::string
|
||||
{
|
||||
static const auto chars = std::string_view{"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
|
||||
|
||||
auto rd = std::random_device{};
|
||||
auto gen = std::mt19937{rd()};
|
||||
auto dis = std::uniform_int_distribution{std::size_t{0}, chars.size() - 1};
|
||||
|
||||
std::string password;
|
||||
for (size_t i = 0; i < length; ++i) {
|
||||
password += chars[dis(gen)];
|
||||
}
|
||||
|
||||
return password;
|
||||
}
|
||||
|
||||
|
||||
extern "C"
|
||||
{
|
||||
char* kqm_generate_hash(const char* password, size_t rounds)
|
||||
{
|
||||
return strdup(generate_hash(password, rounds).c_str());
|
||||
}
|
||||
|
||||
int kqm_validate_password(const char* password, const char* hash)
|
||||
{
|
||||
return validate_password(password, hash);
|
||||
}
|
||||
|
||||
char* kqm_random_password(size_t length)
|
||||
{
|
||||
return strdup(random_password(length).c_str());
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
#include <server/auth.h>
|
||||
#include "server/auth.h"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
char* secret = NULL;
|
||||
char* admin_password_hash = NULL;
|
||||
|
Loading…
Reference in New Issue
Block a user