Browse Source

feat: bcrypt hash verify

dev
Dnomd343 2 years ago
parent
commit
8fdd84c6a2
  1. 1
      .gitignore
  2. 10
      include/bcrypt/bcrypt.h
  3. 2
      include/common/sundry.h
  4. 2
      src/applet/adguard.c
  5. 11
      src/bcrypt/hash.c
  6. 19
      src/cleardns.c
  7. 2
      src/common/sundry.c

1
.gitignore

@ -2,6 +2,7 @@
/build/ /build/
/.idea/ /.idea/
/assets/*.txt /assets/*.txt
/cmake-build/
/cmake-build-debug/ /cmake-build-debug/
/cmake-build-release/ /cmake-build-release/
/src/to-json/target/ /src/to-json/target/

10
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]); 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 * Brief Example

2
include/common/sundry.h

@ -3,8 +3,8 @@
#include <stdint.h> #include <stdint.h>
char* show_bool(uint8_t value);
uint8_t check_port(uint16_t port); uint8_t check_port(uint16_t port);
const char* show_bool(uint8_t value);
char* string_load(const char *fmt, ...); char* string_load(const char *fmt, ...);
char* uint32_to_string(uint32_t number); char* uint32_to_string(uint32_t number);
char* string_join(const char *base, const char *add); char* string_join(const char *base, const char *add);

2
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 *user_config = cJSON_CreateObject(); // setting up username and password
cJSON *users_config = cJSON_CreateArray(); 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, "name", cJSON_CreateString(info->username));
cJSON_AddItemToObject(user_config, "password", cJSON_CreateString(password)); cJSON_AddItemToObject(user_config, "password", cJSON_CreateString(password));
cJSON_AddItemToArray(users_config, user_config); cJSON_AddItemToArray(users_config, user_config);

11
src/bcrypt/hash.c

@ -1,8 +1,10 @@
#include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include "bcrypt.h" #include "bcrypt.h"
#include "logger.h" #include "logger.h"
#include "constant.h"
char* bcrypt_cal(const char *data) { char* bcrypt_hash(const char *data) {
char salt[BCRYPT_HASHSIZE]; char salt[BCRYPT_HASHSIZE];
log_debug("BCrypt data -> `%s`", data); log_debug("BCrypt data -> `%s`", data);
if (bcrypt_gensalt(10, salt)) { if (bcrypt_gensalt(10, salt)) {
@ -17,3 +19,10 @@ char* bcrypt_cal(const char *data) {
log_debug("BCrypt hash -> `%s`", hash); log_debug("BCrypt hash -> `%s`", hash);
return hash; return hash;
} }
int bcrypt_verify(const char *data, const char *hash) {
if (bcrypt_checkpw(data, hash) == 0) {
return TRUE;
}
return FALSE;
}

19
src/cleardns.c

@ -117,9 +117,28 @@ void cleardns() { // cleardns service
process_list_daemon(); // daemon all process process_list_daemon(); // daemon all process
} }
#include "bcrypt.h"
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
init(argc, argv); init(argc, argv);
log_info("ClearDNS server start (%s)", VERSION); 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(); cleardns();
return 0; return 0;
} }

2
src/common/sundry.c

@ -10,7 +10,7 @@
#include "constant.h" #include "constant.h"
#include "structure.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) { if (value) {
return "true"; return "true";
} }

Loading…
Cancel
Save