Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

md5.cpp

Go to the documentation of this file.
00001 // This code was written by Colin Plumb. I've made some small changes.
00002 // (Constantin Bergemann)
00003 
00004 #ifdef __cplusplus
00005 extern "C" {
00006 #endif
00007 
00008 #include <string.h>             /* for memcpy() */
00009 #include "md5.h"
00010 
00011 #if __BYTE_ORDER == 1234
00012 #define byteReverse(buf, len)   /* Nothing */
00013 #else
00014 void byteReverse(unsigned char *buf, unsigned longs);
00015 
00016 /*
00017  * Note: this code is harmless on little-endian machines.
00018  */
00019 void byteReverse(unsigned char *buf, unsigned longs)
00020 {
00021     u_int32_t t;
00022     do {
00023         t = (u_int32_t) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
00024             ((unsigned) buf[1] << 8 | buf[0]);
00025         *(u_int32_t *) buf = t;
00026         buf += 4;
00027     } while (--longs);
00028 }
00029 #endif
00030 
00031 /*
00032  * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
00033  * initialization constants.
00034  */
00035 void MD5_Init(MD5_CTX *ctx)
00036 {
00037     ctx->buf[0] = 0x67452301;
00038     ctx->buf[1] = 0xefcdab89;
00039     ctx->buf[2] = 0x98badcfe;
00040     ctx->buf[3] = 0x10325476;
00041 
00042     ctx->bits[0] = 0;
00043     ctx->bits[1] = 0;
00044 }
00045 
00046 /*
00047  * Update context to reflect the concatenation of another buffer full
00048  * of bytes.
00049  */
00050 void MD5_Update(MD5_CTX *ctx, unsigned char const *buf, unsigned len)
00051 {
00052     u_int32_t t;
00053 
00054     /* Update bitcount */
00055 
00056     t = ctx->bits[0];
00057     if ((ctx->bits[0] = t + ((u_int32_t) len << 3)) < t)
00058         ctx->bits[1]++;         /* Carry from low to high */
00059     ctx->bits[1] += len >> 29;
00060 
00061     t = (t >> 3) & 0x3f;        /* Bytes already in shsInfo->data */
00062 
00063     /* Handle any leading odd-sized chunks */
00064 
00065     if (t) {
00066         unsigned char *p = (unsigned char *) ctx->in + t;
00067 
00068         t = 64 - t;
00069         if (len < t) {
00070             memcpy(p, buf, len);
00071             return;
00072         }
00073         memcpy(p, buf, t);
00074         byteReverse(ctx->in, 16);
00075         MD5Transform(ctx->buf, (u_int32_t *) ctx->in);
00076         buf += t;
00077         len -= t;
00078     }
00079     /* Process data in 64-byte chunks */
00080 
00081     while (len >= 64) {
00082         memcpy(ctx->in, buf, 64);
00083         byteReverse(ctx->in, 16);
00084         MD5Transform(ctx->buf, (u_int32_t *) ctx->in);
00085         buf += 64;
00086         len -= 64;
00087     }
00088 
00089     /* Handle any remaining bytes of data. */
00090 
00091     memcpy(ctx->in, buf, len);
00092 }
00093 
00094 /*
00095  * Final wrapup - pad to 64-byte boundary with the bit pattern 
00096  * 1 0* (64-bit count of bits processed, MSB-first)
00097  */
00098 void MD5_Final(unsigned char digest[16], MD5_CTX *ctx)
00099 {
00100     unsigned count;
00101     unsigned char *p;
00102 
00103     /* Compute number of bytes mod 64 */
00104     count = (ctx->bits[0] >> 3) & 0x3F;
00105 
00106     /* Set the first char of padding to 0x80.  This is safe since there is
00107        always at least one byte free */
00108     p = ctx->in + count;
00109     *p++ = 0x80;
00110 
00111     /* Bytes of padding needed to make 64 bytes */
00112     count = 64 - 1 - count;
00113 
00114     /* Pad out to 56 mod 64 */
00115     if (count < 8) {
00116         /* Two lots of padding:  Pad the first block to 64 bytes */
00117         memset(p, 0, count);
00118         byteReverse(ctx->in, 16);
00119         MD5Transform(ctx->buf, (u_int32_t *) ctx->in);
00120 
00121         /* Now fill the next block with 56 bytes */
00122         memset(ctx->in, 0, 56);
00123     } else {
00124         /* Pad block to 56 bytes */
00125         memset(p, 0, count - 8);
00126     }
00127     byteReverse(ctx->in, 14);
00128 
00129     /* Append length in bits and transform */
00130     ((u_int32_t *) ctx->in)[14] = ctx->bits[0];
00131     ((u_int32_t *) ctx->in)[15] = ctx->bits[1];
00132 
00133     MD5Transform(ctx->buf, (u_int32_t *) ctx->in);
00134     byteReverse((unsigned char *) ctx->buf, 4);
00135     memcpy(digest, ctx->buf, 16);
00136     memset((char *) ctx, 0, sizeof(ctx));       /* In case it's sensitive */
00137 }
00138 
00139 /* The four core functions - F1 is optimized somewhat */
00140 
00141 /* #define F1(x, y, z) (x & y | ~x & z) */
00142 #define F1(x, y, z) (z ^ (x & (y ^ z)))
00143 #define F2(x, y, z) F1(z, x, y)
00144 #define F3(x, y, z) (x ^ y ^ z)
00145 #define F4(x, y, z) (y ^ (x | ~z))
00146 
00147 /* This is the central step in the MD5 algorithm. */
00148 #define MD5STEP(f, w, x, y, z, data, s) \
00149         ( w += f(x, y, z) + data,  w = w<<s | w>>(32-s),  w += x )
00150 
00151 /*
00152  * The core of the MD5 algorithm, this alters an existing MD5 hash to
00153  * reflect the addition of 16 longwords of new data.  MD5Update blocks
00154  * the data and converts bytes into longwords for this routine.
00155  */
00156 void MD5Transform(u_int32_t buf[4], u_int32_t const in[16])
00157 {
00158     register u_int32_t a, b, c, d;
00159 
00160     a = buf[0];
00161     b = buf[1];
00162     c = buf[2];
00163     d = buf[3];
00164 
00165     MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
00166     MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
00167     MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
00168     MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
00169     MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
00170     MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
00171     MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
00172     MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
00173     MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
00174     MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
00175     MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
00176     MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
00177     MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
00178     MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
00179     MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
00180     MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
00181 
00182     MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
00183     MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
00184     MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
00185     MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
00186     MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
00187     MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
00188     MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
00189     MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
00190     MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
00191     MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
00192     MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
00193     MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
00194     MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
00195     MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
00196     MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
00197     MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
00198 
00199     MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
00200     MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
00201     MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
00202     MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
00203     MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
00204     MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
00205     MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
00206     MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
00207     MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
00208     MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
00209     MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
00210     MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
00211     MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
00212     MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
00213     MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
00214     MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
00215 
00216     MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
00217     MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
00218     MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
00219     MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
00220     MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
00221     MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
00222     MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
00223     MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
00224     MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
00225     MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
00226     MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
00227     MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
00228     MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
00229     MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
00230     MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
00231     MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
00232 
00233     buf[0] += a;
00234     buf[1] += b;
00235     buf[2] += c;
00236     buf[3] += d;
00237 }
00238 
00239 #ifdef __cplusplus
00240 }  // extern "C"
00241 #endif
00242 

Generated on Sat Nov 5 16:18:08 2005 for OPIE by  doxygen 1.4.2