Reuse, not rework

License Awareness

Highly Reusable Software

By activity
Professions, Sciences, Humanities, Business, ...

User Interface
Text-based, GUI, Audio, Video, Keyboards, Mouse, Images,...

Text Strings
Conversions, tests, processing, manipulation,...

Integer, Floating point, Matrix, Statistics, Boolean, ...

Algorithms, Memory, Process control, Debugging, ...

Stored Data
Data storage, Integrity, Encryption, Compression, ...

Networks, protocols, Interprocess, Remote, Client Server, ...

Hard World
Timing, Calendar and Clock, Audio, Video, Printer, Controls...

File System
Management, Filtering, File & Directory access, Viewers, ...


librock_md5 - Compute RSA MD5 message digest.
#License - #Source code - #Example Use -


#include <librock/target/types.c>
#include <librock/data.h>

librock_MD5Init (librock_MD5_CTX *ctx);

librock_MD5Update (librock_MD5_CTX *ctx,
                   unsigned char *buf,
                   unsigned int len);

librock_MD5Final (unsigned char result[16], /* result stored there */
                  librock_MD5_CTX *ctx);


This is the RSA Data Security, Inc. MD5 Message-Digest Algorithm, implemented in md5c.c from the RSAref library. The names in the file were changed to use the librock_ prefix, and the necessary definitions from the include files were incorporated. The implementation is unchanged.

To compute the MD5 Message-Digest, declare a librock_MD5_CTX (a typedef'ed structure) which is initialized with a call to librock_MD5Init(). After making one or more calls to librock_MD5Update(), call librock_MD5Final() to place the result into a 16 byte buffer.

The algorithm is described in IETF RFC1321 (R. Rivest. RFC 1321: The MD5 Message-Digest Algorithm. April 1992.) The design property that is that the 16 byte result is unique for unique input, and it is computationally infeasible to create text with a given result. That RFC also has this implementation source code.

MD5 message digests are used as a unique "fingerprint" (which can verify the integrity of a file or character sequence without a byte by byte comparison.) They are also used as unique hash values. (The computational load to compute the md5 result is significant compared to CRC32, so it should be used only when the uniqueness of the result for unique input is essential, or protecting against generated input which matches a known result is required.)

librock_MD5Update() may save not more than 64 bytes of a buffer in the librock_MD5_CTX structure in between calls. That memory is zeroed on librock_MD5Final. But callers may want to use care when working with input which must not be exposed.

Typical use:

#ifdef librock_TYPICAL_USE_MD5Update
    #include <librock/target/types.c>
    #include <librock/data.h>
    #include <stdio.h>
    unsigned char digest[16];
    char ascdigest[16*2+1];
    char buf[8192];
    int ind;
    librock_MD5_CTX md5Ctx;
    FILE * f = 0;


    if (f) {
        while(!feof(f)) {
            int cnt = fread(buf,1,sizeof(buf),f);
            if (cnt <= 0) {
            librock_MD5Update(&md5Ctx,(unsigned char *) buf,cnt);
    } else {
    /* Now create ASCII hexadecimal representation */
    ascdigest[0] = '\0';
    ind = 0;
    while(ind < 16) {
        sprintf(ascdigest+strlen(ascdigest),"%02x",digest[ind] & 0xff);


  // No external calls
  // Well-behaved in multi-threaded uses.


  Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  rights reserved.
  Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block.
  License text in <librock/license/md5rsa.txt> librock_LIDESC_HC=ec1a2c9549fec7790a691ac943b38f06

Source Code

./data/integrity/md5c.c (implementation, plus source of this manual page)

This software is part of Librock

Rapid reuse, without rework. Details
This page copyright (C) 2002-2003 Forrest J. Cavalier III, d-b-a Mib Software, Saylorsburg PA 18353, USA

Verbatim copying and distribution of this generated page is permitted in any medium provided that no changes are made.
(The source of this manual page may be covered by a more permissive license which allows modifications.)

Want to help? We welcome comments, patches. -- Need help? Request paid support.