Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

MD5Hash.cpp

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

Generated on Sat Nov 5 11:20:16 2005 for Cpp Freecell Solver by  doxygen 1.4.4