NeoMutt  2024-12-12-14-g7b49f7
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
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.
 
void * mutt_md5_bytes (const void *buffer, size_t len, void *resbuf)
 Calculate the MD5 hash of a buffer.
 
void * mutt_md5_finish_ctx (struct Md5Ctx *md5ctx, void *resbuf)
 Process the remaining bytes in the buffer.
 
void mutt_md5_init_ctx (struct Md5Ctx *md5ctx)
 Initialise the MD5 computation.
 
void mutt_md5_process (const char *str, struct Md5Ctx *md5ctx)
 Process a NULL-terminated string.
 
void mutt_md5_process_bytes (const void *buf, size_t buflen, struct Md5Ctx *md5ctx)
 Process a block of data.
 
void mutt_md5_toascii (const void *digest, char *resbuf)
 Convert a binary MD5 digest into ASCII Hexadecimal.
 

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
Return values
ptrResults buffer

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 = { 0 };
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}
void mutt_md5_process_bytes(const void *buf, size_t buflen, struct Md5Ctx *md5ctx)
Process a block of data.
Definition: md5.c:373
void mutt_md5_init_ctx(struct Md5Ctx *md5ctx)
Initialise the MD5 computation.
Definition: md5.c:261
void * mutt_md5_finish_ctx(struct Md5Ctx *md5ctx, void *resbuf)
Process the remaining bytes in the buffer.
Definition: md5.c:285
Cursor for the MD5 hashing.
Definition: md5.h:37
md5_uint32 buffer[32]
Definition: md5.h:45
+ 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 285 of file md5.c.

286{
287 if (!md5ctx)
288 return NULL;
289
290 /* Take yet unprocessed bytes into account. */
291 md5_uint32 bytes = md5ctx->buflen;
292 size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4;
293
294 /* Now count remaining bytes. */
295 md5ctx->total[0] += bytes;
296 if (md5ctx->total[0] < bytes)
297 md5ctx->total[1]++; // LCOV_EXCL_LINE
298
299 /* Put the 64-bit file length in *bits* at the end of the buffer. */
300 md5ctx->buffer[size - 2] = SWAP(md5ctx->total[0] << 3);
301 md5ctx->buffer[size - 1] = SWAP((md5ctx->total[1] << 3) | (md5ctx->total[0] >> 29));
302
303 memcpy(&((char *) md5ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);
304
305 /* Process last bytes. */
306 mutt_md5_process_block(md5ctx->buffer, size * 4, md5ctx);
307
308 return mutt_md5_read_ctx(md5ctx, resbuf);
309}
static void * mutt_md5_read_ctx(const struct Md5Ctx *md5ctx, void *resbuf)
Read from the context into a buffer.
Definition: md5.c:240
static void mutt_md5_process_block(const void *buffer, size_t len, struct Md5Ctx *md5ctx)
Process a block with MD5.
Definition: md5.c:65
#define SWAP(n)
Definition: md5.c:41
static const unsigned char fillbuf[64]
This array contains the bytes used to pad the buffer to the next 64-byte boundary.
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
+ 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 261 of file md5.c.

262{
263 if (!md5ctx)
264 return;
265
266 md5ctx->A = 0x67452301;
267 md5ctx->B = 0xefcdab89;
268 md5ctx->C = 0x98badcfe;
269 md5ctx->D = 0x10325476;
270
271 md5ctx->total[0] = 0;
272 md5ctx->total[1] = 0;
273 md5ctx->buflen = 0;
274}
md5_uint32 D
Definition: md5.h:41
md5_uint32 C
Definition: md5.h:40
md5_uint32 A
Definition: md5.h:38
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}
+ 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 char c; \
410 type x; \
411 }, \
412 x)
413#define UNALIGNED_P(p) (((size_t) p) % alignof(md5_uint32) != 0)
414 if (UNALIGNED_P(buf))
415 {
416 while (buflen > 64)
417 {
418 mutt_md5_process_block(memcpy(md5ctx->buffer, buf, 64), 64, md5ctx);
419 buf = (const char *) buf + 64;
420 buflen -= 64;
421 }
422 }
423 else
424#endif
425 {
426 mutt_md5_process_block(buf, buflen & ~63, md5ctx);
427 buf = (const char *) buf + (buflen & ~63);
428 buflen &= 63;
429 }
430 }
431
432 /* Move remaining bytes in internal buffer. */
433 if (buflen > 0)
434 {
435 size_t left_over = md5ctx->buflen;
436
437 memcpy(&((char *) md5ctx->buffer)[left_over], buf, buflen);
438 left_over += buflen;
439 if (left_over >= 64)
440 { // LCOV_EXCL_START
441 mutt_md5_process_block(md5ctx->buffer, 64, md5ctx);
442 left_over -= 64;
443 memmove(md5ctx->buffer, &md5ctx->buffer[16], left_over);
444 } // LCOV_EXCL_STOP
445 md5ctx->buflen = left_over;
446 }
447}
#define UNALIGNED_P(p)
+ 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 456 of file md5.c.

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