From 3dffd2ab53b3a19e0031f95726d7e820be40d1e9 Mon Sep 17 00:00:00 2001 From: Pietro Cerutti Date: Tue, 23 Jan 2018 13:44:55 +0000 Subject: [PATCH] MD5 - Implement and use mutt_md5_toascii --- hcache/hcache.c | 8 ++------ mutt/md5.c | 18 +++++++++++++----- mutt/md5.h | 3 ++- pop_auth.c | 4 +--- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/hcache/hcache.c b/hcache/hcache.c index acd094cd3..886503652 100644 --- a/hcache/hcache.c +++ b/hcache/hcache.c @@ -606,12 +606,8 @@ static const char *hcache_per_folder(const char *path, const char *folder, hcach unsigned char m[16]; /* binary md5sum */ char name[_POSIX_PATH_MAX]; snprintf(name, sizeof(name), "%s|%s", hcache_get_ops()->name, folder); - mutt_md5_buf(name, strlen(name), &m); - snprintf(name, sizeof(name), - "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", - m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8], m[9], m[10], - m[11], m[12], m[13], m[14], m[15]); - + mutt_md5_buf(name, strlen(name), m); + mutt_md5_toascii(m, name); rc = snprintf(hcpath, sizeof(hcpath), "%s%s%s%s", path, slash ? "" : "/", name, suffix); } diff --git a/mutt/md5.c b/mutt/md5.c index 7df108b15..792207a6b 100644 --- a/mutt/md5.c +++ b/mutt/md5.c @@ -34,6 +34,7 @@ * | mutt_md5_process_block() | Process a block with MD5 * | mutt_md5_process_bytes() | Process a block of data * | mutt_md5_read_ctx() | Read from the context into a buffer + * | mutt_md5_toascii() | Produce an ASCII MD5 digest from a buffer */ /* md5.c - Functions to compute MD5 message digest of files or memory blocks @@ -315,16 +316,16 @@ void *mutt_md5_finish_ctx(struct Md5Ctx *ctx, void *resbuf) /** * mutt_md5_buf - Calculate the MD5 hash of a buffer - * @param buffer Buffer to hash - * @param len Length of buffer - * @param resblock Buffer for result + * @param buffer Buffer to hash + * @param len Length of buffer + * @param resbuf Buffer for result * @retval ptr Results buffer * * Compute MD5 message digest for LEN bytes beginning at Buffer. The result is * always in little endian byte order, so that a byte-wise output yields to the * wanted ASCII representation of the message digest. */ -void *mutt_md5_buf(const char *buffer, size_t len, void *resblock) +void *mutt_md5_buf(const char *buffer, size_t len, void *resbuf) { struct Md5Ctx ctx; @@ -335,7 +336,7 @@ void *mutt_md5_buf(const char *buffer, size_t len, void *resblock) mutt_md5_process_bytes(buffer, len, &ctx); /* Put result in desired memory area. */ - return mutt_md5_finish_ctx(&ctx, resblock); + return mutt_md5_finish_ctx(&ctx, resbuf); } /** @@ -414,3 +415,10 @@ void mutt_md5_process_bytes(const void *buffer, size_t len, struct Md5Ctx *ctx) } } +void mutt_md5_toascii(const void *resbuf, char *digest) +{ + const unsigned char *c = resbuf; + sprintf(digest, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", + c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7], c[8], c[9], c[10], + c[11], c[12], c[13], c[14], c[15]); +} diff --git a/mutt/md5.h b/mutt/md5.h index e8d145c5e..ecdfc2a84 100644 --- a/mutt/md5.h +++ b/mutt/md5.h @@ -45,9 +45,10 @@ struct Md5Ctx md5_uint32 buffer[32]; }; -void *mutt_md5_buf(const char *buffer, size_t len, void *resblock); +void *mutt_md5_buf(const char *buffer, size_t len, void *resbuf); void *mutt_md5_finish_ctx(struct Md5Ctx *ctx, void *resbuf); void mutt_md5_init_ctx(struct Md5Ctx *ctx); void mutt_md5_process_bytes(const void *buffer, size_t len, struct Md5Ctx *ctx); +void mutt_md5_toascii(const void *resbuf, char *digest); #endif /* _MUTT_MD5_H */ diff --git a/pop_auth.c b/pop_auth.c index 598c9b842..fa9385959 100644 --- a/pop_auth.c +++ b/pop_auth.c @@ -237,9 +237,7 @@ static enum PopAuthRes pop_auth_apop(struct PopData *pop_data, const char *metho mutt_md5_process_bytes(pop_data->conn->account.pass, strlen(pop_data->conn->account.pass), &ctx); mutt_md5_finish_ctx(&ctx, digest); - - for (size_t i = 0; i < sizeof(digest); i++) - sprintf(hash + 2 * i, "%02x", digest[i]); + mutt_md5_toascii(digest, hash); /* Send APOP command to server */ snprintf(buf, sizeof(buf), "APOP %s %s\r\n", pop_data->conn->account.user, hash); -- 2.40.0