Home
Highly Reusable Software By activity User Interface Text Strings Math Processing
Stored Data
Communications
Hard World File System
|
#License - #Source code - #Example Use -
#include <librock/target/types.c> #include <librock/data.h> void librock_MD5Init (librock_MD5_CTX *ctx); void librock_MD5Update (librock_MD5_CTX *ctx, unsigned char *buf, unsigned int len); void librock_MD5Final (unsigned char result[16], /* result stored there */ librock_MD5_CTX *ctx);
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; librock_MD5Init(&md5Ctx); if (f) { while(!feof(f)) { int cnt = fread(buf,1,sizeof(buf),f); if (cnt <= 0) { break; } librock_MD5Update(&md5Ctx,(unsigned char *) buf,cnt); } } else { librock_MD5Update(&md5Ctx,"Hello!",6); } librock_MD5Final(digest,&md5Ctx); /* Now create ASCII hexadecimal representation */ ascdigest[0] = '\0'; ind = 0; while(ind < 16) { sprintf(ascdigest+strlen(ascdigest),"%02x",digest[ind] & 0xff); ind++; } printf("%s\n",ascdigest); #endif
// 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
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.