From 8fdd84c6a26ac6591187a58c65bcb83d03ec3e8e Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 5 Mar 2023 14:39:40 +0800 Subject: [PATCH] feat: bcrypt hash verify --- .gitignore | 1 + include/bcrypt/bcrypt.h | 10 ++++++++-- include/common/sundry.h | 2 +- src/applet/adguard.c | 2 +- src/bcrypt/hash.c | 11 ++++++++++- src/cleardns.c | 19 +++++++++++++++++++ src/common/sundry.c | 2 +- 7 files changed, 41 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index a591128..f22e8a0 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ /build/ /.idea/ /assets/*.txt +/cmake-build/ /cmake-build-debug/ /cmake-build-release/ /src/to-json/target/ diff --git a/include/bcrypt/bcrypt.h b/include/bcrypt/bcrypt.h index d7d4df8..3d7e35e 100644 --- a/include/bcrypt/bcrypt.h +++ b/include/bcrypt/bcrypt.h @@ -60,10 +60,16 @@ int bcrypt_hashpw(const char *passwd, const char salt[BCRYPT_HASHSIZE], int bcrypt_checkpw(const char *passwd, const char hash[BCRYPT_HASHSIZE]); /* - * This function expects a string and return bcrypt result (random salt) + * This function expects a string and return bcrypt result with random salt. */ -char* bcrypt_cal(const char *data); +char* bcrypt_hash(const char *data); + +/* + * This function verifies that the data matches the hash value. + */ + +int bcrypt_verify(const char *data, const char *hash); /* * Brief Example diff --git a/include/common/sundry.h b/include/common/sundry.h index 97f53ca..09a3b8e 100644 --- a/include/common/sundry.h +++ b/include/common/sundry.h @@ -3,8 +3,8 @@ #include -char* show_bool(uint8_t value); uint8_t check_port(uint16_t port); +const char* show_bool(uint8_t value); char* string_load(const char *fmt, ...); char* uint32_to_string(uint32_t number); char* string_join(const char *base, const char *add); diff --git a/src/applet/adguard.c b/src/applet/adguard.c index 5f85c13..2c64cfe 100644 --- a/src/applet/adguard.c +++ b/src/applet/adguard.c @@ -48,7 +48,7 @@ char *adguard_config(adguard *info, const char *raw_config) { // modify adguard cJSON *user_config = cJSON_CreateObject(); // setting up username and password cJSON *users_config = cJSON_CreateArray(); - char *password = bcrypt_cal(info->password); + char *password = bcrypt_hash(info->password); cJSON_AddItemToObject(user_config, "name", cJSON_CreateString(info->username)); cJSON_AddItemToObject(user_config, "password", cJSON_CreateString(password)); cJSON_AddItemToArray(users_config, user_config); diff --git a/src/bcrypt/hash.c b/src/bcrypt/hash.c index 929ff20..6ee37ed 100644 --- a/src/bcrypt/hash.c +++ b/src/bcrypt/hash.c @@ -1,8 +1,10 @@ +#include #include #include "bcrypt.h" #include "logger.h" +#include "constant.h" -char* bcrypt_cal(const char *data) { +char* bcrypt_hash(const char *data) { char salt[BCRYPT_HASHSIZE]; log_debug("BCrypt data -> `%s`", data); if (bcrypt_gensalt(10, salt)) { @@ -17,3 +19,10 @@ char* bcrypt_cal(const char *data) { log_debug("BCrypt hash -> `%s`", hash); return hash; } + +int bcrypt_verify(const char *data, const char *hash) { + if (bcrypt_checkpw(data, hash) == 0) { + return TRUE; + } + return FALSE; +} diff --git a/src/cleardns.c b/src/cleardns.c index 31c85a3..2b129dd 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -117,9 +117,28 @@ void cleardns() { // cleardns service process_list_daemon(); // daemon all process } +#include "bcrypt.h" + int main(int argc, char *argv[]) { init(argc, argv); log_info("ClearDNS server start (%s)", VERSION); + + LOG_LEVEL = LOG_DEBUG; + log_debug("test mode start"); + + const char demo_str[] = "dnomd343"; + char *hash_ret = bcrypt_hash(demo_str); + + log_info("bcrypt hash -> `%s`", hash_ret); + + log_info("check ret -> `%s`", show_bool(bcrypt_verify(demo_str, hash_ret))); + + log_info("check ret -> `%s`", show_bool(bcrypt_verify("233333", hash_ret))); + + log_info("exit"); + + return 0; + cleardns(); return 0; } diff --git a/src/common/sundry.c b/src/common/sundry.c index a203505..e425b64 100644 --- a/src/common/sundry.c +++ b/src/common/sundry.c @@ -10,7 +10,7 @@ #include "constant.h" #include "structure.h" -char* show_bool(uint8_t value) { // return `true` or `false` +const char* show_bool(uint8_t value) { // return `true` or `false` if (value) { return "true"; }