From eb626bd8ee3fddae050dbf9babfbea19961a36f2 Mon Sep 17 00:00:00 2001 From: Jeffrey Armstrong Date: Thu, 2 Jan 2014 09:07:27 -0500 Subject: Initial code commit, including db init and file hashing --- rfile.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 rfile.c (limited to 'rfile.c') diff --git a/rfile.c b/rfile.c new file mode 100644 index 0000000..497d383 --- /dev/null +++ b/rfile.c @@ -0,0 +1,39 @@ +#include +#include +#include + +#include "prism.h" +#include "md5.h" +#include "rfile.h" + +static const char hex_str[]= "0123456789abcdef"; + +int get_file_hash(const char *filename, char *hash) +{ +FILE *fp; +MD5_CTX hb; +char buf[16]; +int i; + + if(filename == NULL || hash == NULL) + return PRET_ERROR; + + fp = fopen(filename, "rb"); + if(fp == NULL) + return PRET_READERROR; + + MD5_Init(&hb); + while(fgets(buf, 15, fp)) { + MD5_Update(&hb, buf, strlen(buf)); + } + fclose(fp); + + MD5_Final(buf, &hb); + + for(i=0; i<16; i++) { + hash[2*i] = hex_str[buf[i] >> 4]; + hash[2*i+1] = hex_str[buf[i] & 0x0F]; + } + + return PRET_OK; +} -- cgit v1.2.3