diff --git a/include/netutils/md5.h b/include/netutils/md5.h index ff10c4e2d..5e5ab6620 100644 --- a/include/netutils/md5.h +++ b/include/netutils/md5.h @@ -95,6 +95,7 @@ void md5_final(unsigned char digest[16], struct md5_context_s *context); void md5_transform(uint32_t buf[4], uint32_t const in[16]); void md5_sum(const uint8_t *addr, const size_t len, uint8_t *mac); +int md5_file(const char *path, uint8_t *mac); char *md5_hash(const uint8_t *addr, const size_t len); #ifdef __cplusplus diff --git a/netutils/codecs/md5.c b/netutils/codecs/md5.c index a319ec14b..38c637dc6 100644 --- a/netutils/codecs/md5.c +++ b/netutils/codecs/md5.c @@ -64,6 +64,7 @@ #include #include #include +#include #include "netutils/md5.h" @@ -86,6 +87,10 @@ # define MD5STEP(f, w, x, y, z, data, s) \ (w += f(x, y, z) + data, w = w<>(32-s), w += x) +/* Encoding Memory Block Size */ + +#define MD5_BUFSIZE 1024 + /**************************************************************************** * Private Functions ****************************************************************************/ @@ -406,4 +411,59 @@ char *md5_hash(const uint8_t * addr, const size_t len) return hash; } +/**************************************************************************** + * Name: md5_file + * + * Description: + * MD5 hash for a file + * + * Input Parameters: + * path: File Path + * mac : Buffer for the hash + * + ****************************************************************************/ + +int md5_file(const char *path, uint8_t *mac) +{ + int fd; + int ret; + unsigned char *buf; + MD5_CTX ctx; + + fd = open(path, O_RDONLY); + if (fd < 0) + { + return -errno; + } + + buf = malloc(MD5_BUFSIZE); + if (buf == NULL) + { + ret = -ENOMEM; + goto out; + } + + md5_init(&ctx); + + while (1) + { + /* Block calculation md5 */ + + ret = read(fd, buf, MD5_BUFSIZE); + if (ret <= 0) + { + ret = ret < 0 ? -errno : 0; + break; + } + + md5_update(&ctx, buf, ret); + } + + md5_final(mac, &ctx); + free(buf); +out: + close(fd); + return ret; +} + #endif /* CONFIG_CODECS_HASH_MD5 */