NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN
md5.h File Reference

Calculate the MD5 checksum of a buffer. More...

#include <stdint.h>
#include <stdio.h>
+ Include dependency graph for md5.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  Md5Ctx
 Cursor for the MD5 hashing. More...
 

Typedefs

typedef uint32_t md5_uint32
 

Functions

void * mutt_md5 (const char *str, void *buf)
 Calculate the MD5 hash of a NULL-terminated string. More...
 
void * mutt_md5_bytes (const void *buffer, size_t len, void *resbuf)
 Calculate the MD5 hash of a buffer. More...
 
void * mutt_md5_finish_ctx (struct Md5Ctx *md5ctx, void *resbuf)
 Process the remaining bytes in the buffer. More...
 
void mutt_md5_init_ctx (struct Md5Ctx *md5ctx)
 Initialise the MD5 computation. More...
 
void mutt_md5_process (const char *str, struct Md5Ctx *md5ctx)
 Process a NULL-terminated string. More...
 
void mutt_md5_process_bytes (const void *buf, size_t buflen, struct Md5Ctx *md5ctx)
 Process a block of data. More...
 
void mutt_md5_toascii (const void *digest, char *resbuf)
 Convert a binary MD5 digest into ASCII Hexadecimal. More...
 

Detailed Description

Calculate the MD5 checksum of a buffer.

Authors
  • Free Software Foundation, Inc.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file md5.h.

Typedef Documentation

◆ md5_uint32

typedef uint32_t md5_uint32

Definition at line 29 of file md5.h.

Function Documentation

◆ mutt_md5()

void* mutt_md5 ( const char *  str,
void *  buf 
)

Calculate the MD5 hash of a NULL-terminated string.

Parameters
strString to hash
bufBuffer for result

Definition at line 317 of file md5.c.

318 {
319  if (!str)
320  return NULL;
321 
322  return mutt_md5_bytes(str, strlen(str), buf);
323 }
void * mutt_md5_bytes(const void *buffer, size_t len, void *resbuf)
Calculate the MD5 hash of a buffer.
Definition: md5.c:336
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_md5_bytes()

void* mutt_md5_bytes ( const void *  buffer,
size_t  len,
void *  resbuf 
)

Calculate the MD5 hash of a buffer.

Parameters
bufferBuffer to hash
lenLength of buffer
resbufBuffer for result
Return values
ptrResults 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.

Definition at line 336 of file md5.c.

337 {
338  struct Md5Ctx md5ctx;
339 
340  /* Initialize the computation context. */
341  mutt_md5_init_ctx(&md5ctx);
342 
343  /* Process whole buffer but last len % 64 bytes. */
344  mutt_md5_process_bytes(buffer, len, &md5ctx);
345 
346  /* Put result in desired memory area. */
347  return mutt_md5_finish_ctx(&md5ctx, resbuf);
348 }
Cursor for the MD5 hashing.
Definition: md5.h:36
void mutt_md5_init_ctx(struct Md5Ctx *md5ctx)
Initialise the MD5 computation.
Definition: md5.c:262
void * mutt_md5_finish_ctx(struct Md5Ctx *md5ctx, void *resbuf)
Process the remaining bytes in the buffer.
Definition: md5.c:286
md5_uint32 buffer[32]
Definition: md5.h:45
void mutt_md5_process_bytes(const void *buf, size_t buflen, struct Md5Ctx *md5ctx)
Process a block of data.
Definition: md5.c:373
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_md5_finish_ctx()

void* mutt_md5_finish_ctx ( struct Md5Ctx md5ctx,
void *  resbuf 
)

Process the remaining bytes in the buffer.

Parameters
md5ctxMD5 context
resbufBuffer for result
Return values
ptrResults buffer

Process the remaining bytes in the internal buffer and the usual prologue according to the standard and write the result to RESBUF.

Definition at line 286 of file md5.c.

287 {
288  if (!md5ctx)
289  return NULL;
290 
291  /* Take yet unprocessed bytes into account. */
292  md5_uint32 bytes = md5ctx->buflen;
293  size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4;
294 
295  /* Now count remaining bytes. */
296  md5ctx->total[0] += bytes;
297  if (md5ctx->total[0] < bytes)
298  md5ctx->total[1]++;
299 
300  /* Put the 64-bit file length in *bits* at the end of the buffer. */
301  md5ctx->buffer[size - 2] = SWAP(md5ctx->total[0] << 3);
302  md5ctx->buffer[size - 1] = SWAP((md5ctx->total[1] << 3) | (md5ctx->total[0] >> 29));
303 
304  memcpy(&((char *) md5ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);
305 
306  /* Process last bytes. */
307  mutt_md5_process_block(md5ctx->buffer, size * 4, md5ctx);
308 
309  return mutt_md5_read_ctx(md5ctx, resbuf);
310 }
#define SWAP(n)
Definition: md5.c:41
static const unsigned char fillbuf[64]
Definition: md5.c:46
uint32_t md5_uint32
Definition: md5.h:29
md5_uint32 total[2]
Definition: md5.h:43
md5_uint32 buflen
Definition: md5.h:44
md5_uint32 buffer[32]
Definition: md5.h:45
static void * mutt_md5_read_ctx(const struct Md5Ctx *md5ctx, void *resbuf)
Read from the context into a buffer.
Definition: md5.c:241
static void mutt_md5_process_block(const void *buffer, size_t len, struct Md5Ctx *md5ctx)
Process a block with MD5.
Definition: md5.c:66
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_md5_init_ctx()

void mutt_md5_init_ctx ( struct Md5Ctx md5ctx)

Initialise the MD5 computation.

Parameters
md5ctxMD5 context

RFC1321, 3.3: Step 3

Definition at line 262 of file md5.c.

263 {
264  if (!md5ctx)
265  return;
266 
267  md5ctx->A = 0x67452301;
268  md5ctx->B = 0xefcdab89;
269  md5ctx->C = 0x98badcfe;
270  md5ctx->D = 0x10325476;
271 
272  md5ctx->total[0] = 0;
273  md5ctx->total[1] = 0;
274  md5ctx->buflen = 0;
275 }
md5_uint32 A
Definition: md5.h:38
md5_uint32 D
Definition: md5.h:41
md5_uint32 total[2]
Definition: md5.h:43
md5_uint32 buflen
Definition: md5.h:44
md5_uint32 C
Definition: md5.h:40
md5_uint32 B
Definition: md5.h:39
+ Here is the caller graph for this function:

◆ mutt_md5_process()

void mutt_md5_process ( const char *  str,
struct Md5Ctx md5ctx 
)

Process a NULL-terminated string.

Parameters
strString to process
md5ctxMD5 context

Definition at line 355 of file md5.c.

356 {
357  if (!str)
358  return;
359 
360  mutt_md5_process_bytes(str, strlen(str), md5ctx);
361 }
void mutt_md5_process_bytes(const void *buf, size_t buflen, struct Md5Ctx *md5ctx)
Process a block of data.
Definition: md5.c:373
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_md5_process_bytes()

void mutt_md5_process_bytes ( const void *  buf,
size_t  buflen,
struct Md5Ctx md5ctx 
)

Process a block of data.

Parameters
bufBuffer to process
buflenLength of buffer
md5ctxMD5 context

Starting with the result of former calls of this function (or the initialization function update the context for the next BUFLEN bytes starting at Buffer. It is NOT required that BUFLEN is a multiple of 64.

Definition at line 373 of file md5.c.

374 {
375  if (!buf || !md5ctx)
376  return;
377 
378  /* When we already have some bits in our internal buffer concatenate both
379  * inputs first. */
380  if (md5ctx->buflen != 0)
381  {
382  size_t left_over = md5ctx->buflen;
383  size_t add = ((128 - left_over) > buflen) ? buflen : (128 - left_over);
384 
385  memcpy(&((char *) md5ctx->buffer)[left_over], buf, add);
386  md5ctx->buflen += add;
387 
388  if (md5ctx->buflen > 64)
389  {
390  mutt_md5_process_block(md5ctx->buffer, md5ctx->buflen & ~63, md5ctx);
391 
392  md5ctx->buflen &= 63;
393  /* The regions in the following copy operation can't overlap. */
394  memcpy(md5ctx->buffer, &((char *) md5ctx->buffer)[(left_over + add) & ~63],
395  md5ctx->buflen);
396  }
397 
398  buf = (const char *) buf + add;
399  buflen -= add;
400  }
401 
402  /* Process available complete blocks. */
403  if (buflen >= 64)
404  {
405 #if !defined(_STRING_ARCH_unaligned)
406 #define alignof(type) \
407  offsetof( \
408  struct \
409  { \
410  char c; \
411  type x; \
412  }, \
413  x)
414 #define UNALIGNED_P(p) (((size_t) p) % alignof(md5_uint32) != 0)
415  if (UNALIGNED_P(buf))
416  {
417  while (buflen > 64)
418  {
419  mutt_md5_process_block(memcpy(md5ctx->buffer, buf, 64), 64, md5ctx);
420  buf = (const char *) buf + 64;
421  buflen -= 64;
422  }
423  }
424  else
425 #endif
426  {
427  mutt_md5_process_block(buf, buflen & ~63, md5ctx);
428  buf = (const char *) buf + (buflen & ~63);
429  buflen &= 63;
430  }
431  }
432 
433  /* Move remaining bytes in internal buffer. */
434  if (buflen > 0)
435  {
436  size_t left_over = md5ctx->buflen;
437 
438  memcpy(&((char *) md5ctx->buffer)[left_over], buf, buflen);
439  left_over += buflen;
440  if (left_over >= 64)
441  {
442  mutt_md5_process_block(md5ctx->buffer, 64, md5ctx);
443  left_over -= 64;
444  memmove(md5ctx->buffer, &md5ctx->buffer[16], left_over);
445  }
446  md5ctx->buflen = left_over;
447  }
448 }
#define UNALIGNED_P(p)
md5_uint32 buflen
Definition: md5.h:44
md5_uint32 buffer[32]
Definition: md5.h:45
static void mutt_md5_process_block(const void *buffer, size_t len, struct Md5Ctx *md5ctx)
Process a block with MD5.
Definition: md5.c:66
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_md5_toascii()

void mutt_md5_toascii ( const void *  digest,
char *  resbuf 
)

Convert a binary MD5 digest into ASCII Hexadecimal.

Parameters
digestBinary MD5 digest
resbufBuffer for the ASCII result
Note
refbuf must be at least 33 bytes long.

Definition at line 457 of file md5.c.

458 {
459  if (!digest || !resbuf)
460  return;
461 
462  const unsigned char *c = digest;
463  sprintf(resbuf, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
464  c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7], c[8], c[9], c[10],
465  c[11], c[12], c[13], c[14], c[15]);
466 }
+ Here is the caller graph for this function: