Browse Source

feat: bcrypt interface

dev
dnomd343 2 years ago
parent
commit
0789a636b4
  1. 1
      include/common.h
  2. 20
      src/cleardns.c
  3. 17
      src/common.c

1
include/common.h

@ -47,6 +47,7 @@ uint8_t check_port(uint16_t port);
uint16_t gen_rand_num(uint16_t limit); uint16_t gen_rand_num(uint16_t limit);
char* uint32_to_string(uint32_t number); char* uint32_to_string(uint32_t number);
char* gen_bcrypt(const char *data);
int run_command(const char *command); int run_command(const char *command);
char* string_init(const char *str); char* string_init(const char *str);

20
src/cleardns.c

@ -34,29 +34,15 @@
// fprintf(stderr, "\"\n"); // fprintf(stderr, "\"\n");
//} //}
#include "bcrypt.h"
int main(int argc, char *argv[]) { // ClearDNS server int main(int argc, char *argv[]) { // ClearDNS server
LOG_LEVEL = LOG_DEBUG; LOG_LEVEL = LOG_DEBUG;
log_info("ClearDNS server start (%s)", VERSION); log_info("ClearDNS server start (%s)", VERSION);
char *ret = gen_bcrypt("dnomd343");
char salt[BCRYPT_HASHSIZE]; log_info("%s", ret);
log_info("size -> %d", BCRYPT_HASHSIZE);
int ret = bcrypt_gensalt(10, salt);
log_info("gensalt ret -> %d", ret);
log_info("salt -> %s", salt);
char passwd[] = "dnomd343";
log_info("passwd -> %s", passwd);
char hash[BCRYPT_HASHSIZE];
ret = bcrypt_hashpw(passwd, salt, hash);
log_info("hashpw ret -> %d", ret);
log_info("hash -> %s", hash);
ret = bcrypt_checkpw(passwd, hash);
log_info("checkpw ret -> %d", ret);
// load_config("test.json"); // load_config("test.json");

17
src/common.c

@ -6,6 +6,7 @@
#include "common.h" #include "common.h"
#include "logger.h" #include "logger.h"
#include "structure.h" #include "structure.h"
#include "bcrypt.h"
char* show_bool(uint8_t value) { // return `true` or `false` char* show_bool(uint8_t value) { // return `true` or `false`
if (value) { if (value) {
@ -60,6 +61,22 @@ int run_command(const char *command) {
return system(command) / 256; return system(command) / 256;
} }
char* gen_bcrypt(const char *data) {
char salt[BCRYPT_HASHSIZE];
log_debug("BCrypt data -> `%s`", data);
if (bcrypt_gensalt(10, salt)) {
log_fatal("BCrypt generate salt error");
}
log_debug("BCrypt salt -> `%s`", salt);
char *hash = (char *)malloc(BCRYPT_HASHSIZE);
if (bcrypt_hashpw(data, salt, hash)) {
log_fatal("BCrypt generate hash error");
}
log_debug("BCrypt hash -> `%s`", hash);
return hash;
}
void save_file(const char *file, const char *content) { // save into file void save_file(const char *file, const char *content) { // save into file
log_debug("Write into `%s` -> \n%s", file, content); log_debug("Write into `%s` -> \n%s", file, content);
FILE* fp = fopen(file , "w"); FILE* fp = fopen(file , "w");

Loading…
Cancel
Save