From 870b68dc8f4e333b37f22a66021acc532e181a66 Mon Sep 17 00:00:00 2001 From: alex Date: Thu, 21 Oct 2021 22:41:51 -0700 Subject: [PATCH] ... --- Makefile.am | 2 ++ inc/add.h | 1 + inc/file.h | 3 +++ inc/hash.h | 11 +++++++++++ src/add.c | 10 ++++++++++ src/file.c | 2 ++ src/hash.c | 31 +++++++++++++++++++++++++++++++ src/main.c | 1 - 8 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 inc/hash.h create mode 100644 src/hash.c diff --git a/Makefile.am b/Makefile.am index 5556a76..c9390b3 100644 --- a/Makefile.am +++ b/Makefile.am @@ -15,6 +15,7 @@ seederd_SOURCES = \ src/add.c \ src/default.c \ src/file.c \ + src/hash.c \ src/hashmap.c \ src/init.c \ src/log.c \ @@ -42,6 +43,7 @@ seederd_SOURCES += \ inc/add.h \ inc/default.h \ inc/file.h \ + inc/hash.h \ inc/hashmap.h \ inc/init.h \ inc/log.h \ diff --git a/inc/add.h b/inc/add.h index ba80d5e..89c4d0a 100644 --- a/inc/add.h +++ b/inc/add.h @@ -3,6 +3,7 @@ #include +#include #include #include #include diff --git a/inc/file.h b/inc/file.h index c7eaee5..2728529 100644 --- a/inc/file.h +++ b/inc/file.h @@ -6,9 +6,12 @@ #include #include +#include + struct file { char *name; char *path; + unsigned char root[crypto_hash_sha256_BYTES]; }; void file_free(struct file*); diff --git a/inc/hash.h b/inc/hash.h new file mode 100644 index 0000000..c61ae69 --- /dev/null +++ b/inc/hash.h @@ -0,0 +1,11 @@ +#ifndef __HASH_H_ +#define __HASH_H_ + +#include + +int hash(void*,size_t,unsigned char*,size_t); +int hash_init(crypto_hash_sha256_state*); +int hash_update(crypto_hash_sha256_state*,const void*,size_t); +int hash_final(crypto_hash_sha256_state*,unsigned char*,size_t); + +#endif diff --git a/src/add.c b/src/add.c index 3beb3df..2b61a9c 100644 --- a/src/add.c +++ b/src/add.c @@ -71,6 +71,16 @@ static void *add_hash(void *unused) { p = get_next(); pthread_mutex_unlock(&adding_mutex); + if(hash( + p->path, + strlen(p->path), + p->root, + crypto_hash_sha256_BYTES + )<0) { + log_err("hash failed\n"); + return NULL; + } + if(NULL==p) { return NULL; } log_info("to add: %s\n",p->path); } diff --git a/src/file.c b/src/file.c index c34a9dc..c9866b5 100644 --- a/src/file.c +++ b/src/file.c @@ -31,5 +31,7 @@ int file_init(struct file **p, const char *path) { (*p)->name = strdup(b); + memset((*p)->root,0,crypto_hash_sha256_BYTES); + return 1; } diff --git a/src/hash.c b/src/hash.c new file mode 100644 index 0000000..71f4a91 --- /dev/null +++ b/src/hash.c @@ -0,0 +1,31 @@ +#include + +int hash(void *data, size_t data_len, unsigned char *out, size_t out_size) { + if(crypto_hash_sha256(out,data,data_len)!=0) { return -1; } + + return 1; +} + +int hash_init(crypto_hash_sha256_state *state) { + if(crypto_hash_sha256_init(state)!=0) { return -1; } + + return 1; +} + +int hash_update(crypto_hash_sha256_state *state, const void *data, size_t data_len) { + if(crypto_hash_sha256_update( + state, + (const unsigned char*)data, + (unsigned long long) data_len + )!=0) { + return -1; + } + + return 1; +} + +int hash_final(crypto_hash_sha256_state *state, unsigned char *out, size_t out_len) { + if(crypto_hash_sha256_final(state,out)!=0) { return -1; } + + return 1; +} diff --git a/src/main.c b/src/main.c index ded065c..eb0ff1f 100644 --- a/src/main.c +++ b/src/main.c @@ -8,7 +8,6 @@ int main(int argc, char **argv) { if(add()<0) { return EXIT_FAILURE; } log_err("this is a test %d %s\n",10,"what?"); - //while(1) { } return EXIT_FAILURE; } -- 2.39.5