89 #define T_MASK ((unsigned int)~0)
90 #define T1 (T_MASK ^ 0x28955b87)
91 #define T2 (T_MASK ^ 0x173848a9)
93 #define T4 (T_MASK ^ 0x3e423111)
94 #define T5 (T_MASK ^ 0x0a83f050)
96 #define T7 (T_MASK ^ 0x57cfb9ec)
97 #define T8 (T_MASK ^ 0x02b96afe)
99 #define T10 (T_MASK ^ 0x74bb0850)
100 #define T11 (T_MASK ^ 0x0000a44e)
101 #define T12 (T_MASK ^ 0x76a32841)
102 #define T13 0x6b901122
103 #define T14 (T_MASK ^ 0x02678e6c)
104 #define T15 (T_MASK ^ 0x5986bc71)
105 #define T16 0x49b40821
106 #define T17 (T_MASK ^ 0x09e1da9d)
107 #define T18 (T_MASK ^ 0x3fbf4cbf)
108 #define T19 0x265e5a51
109 #define T20 (T_MASK ^ 0x16493855)
110 #define T21 (T_MASK ^ 0x29d0efa2)
111 #define T22 0x02441453
112 #define T23 (T_MASK ^ 0x275e197e)
113 #define T24 (T_MASK ^ 0x182c0437)
114 #define T25 0x21e1cde6
115 #define T26 (T_MASK ^ 0x3cc8f829)
116 #define T27 (T_MASK ^ 0x0b2af278)
117 #define T28 0x455a14ed
118 #define T29 (T_MASK ^ 0x561c16fa)
119 #define T30 (T_MASK ^ 0x03105c07)
120 #define T31 0x676f02d9
121 #define T32 (T_MASK ^ 0x72d5b375)
122 #define T33 (T_MASK ^ 0x0005c6bd)
123 #define T34 (T_MASK ^ 0x788e097e)
124 #define T35 0x6d9d6122
125 #define T36 (T_MASK ^ 0x021ac7f3)
126 #define T37 (T_MASK ^ 0x5b4115bb)
127 #define T38 0x4bdecfa9
128 #define T39 (T_MASK ^ 0x0944b49f)
129 #define T40 (T_MASK ^ 0x4140438f)
130 #define T41 0x289b7ec6
131 #define T42 (T_MASK ^ 0x155ed805)
132 #define T43 (T_MASK ^ 0x2b10cf7a)
133 #define T44 0x04881d05
134 #define T45 (T_MASK ^ 0x262b2fc6)
135 #define T46 (T_MASK ^ 0x1924661a)
136 #define T47 0x1fa27cf8
137 #define T48 (T_MASK ^ 0x3b53a99a)
138 #define T49 (T_MASK ^ 0x0bd6ddbb)
139 #define T50 0x432aff97
140 #define T51 (T_MASK ^ 0x546bdc58)
141 #define T52 (T_MASK ^ 0x036c5fc6)
142 #define T53 0x655b59c3
143 #define T54 (T_MASK ^ 0x70f3336d)
144 #define T55 (T_MASK ^ 0x00100b82)
145 #define T56 (T_MASK ^ 0x7a7ba22e)
146 #define T57 0x6fa87e4f
147 #define T58 (T_MASK ^ 0x01d3191f)
148 #define T59 (T_MASK ^ 0x5cfebceb)
149 #define T60 0x4e0811a1
150 #define T61 (T_MASK ^ 0x08ac817d)
151 #define T62 (T_MASK ^ 0x42c50dca)
152 #define T63 0x2ad7d2bb
153 #define T64 (T_MASK ^ 0x14792c6e)
156 const unsigned char MD5::pad[64] =
158 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
159 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
160 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
161 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
165 : m_finished( false )
174 void MD5::process(
const unsigned char *data )
176 unsigned int a = m_state.abcd[0];
177 unsigned int b = m_state.abcd[1];
178 unsigned int c = m_state.abcd[2];
179 unsigned int d = m_state.abcd[3];
186 unsigned int xbuf[16];
187 const unsigned int *X;
197 static const int w = 1;
199 if( *((
const unsigned char *)&w) )
207 if( !((data - (
const unsigned char*)0) & 3) )
210 X = (
const unsigned int*)data;
215 memcpy( xbuf, data, 64 );
223 #if BYTE_ORDER >= 0 // big-endian
229 const unsigned char *xp = data;
237 for( i = 0; i < 16; ++i, xp += 4 )
238 xbuf[i] = xp[0] + ( xp[1] << 8 ) + ( xp[2] << 16 ) + ( xp[3] << 24 );
243 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
248 #define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
249 #define SET(a, b, c, d, k, s, Ti)\
250 t = a + F(b,c,d) + X[k] + Ti;\
251 a = ROTATE_LEFT(t, s) + b
253 SET(a, b, c, d, 0, 7, T1);
254 SET(d, a, b, c, 1, 12, T2);
255 SET(c, d, a, b, 2, 17, T3);
256 SET(b, c, d, a, 3, 22, T4);
257 SET(a, b, c, d, 4, 7, T5);
258 SET(d, a, b, c, 5, 12, T6);
259 SET(c, d, a, b, 6, 17, T7);
260 SET(b, c, d, a, 7, 22, T8);
261 SET(a, b, c, d, 8, 7, T9);
262 SET(d, a, b, c, 9, 12, T10);
263 SET(c, d, a, b, 10, 17, T11);
264 SET(b, c, d, a, 11, 22, T12);
265 SET(a, b, c, d, 12, 7, T13);
266 SET(d, a, b, c, 13, 12, T14);
267 SET(c, d, a, b, 14, 17, T15);
268 SET(b, c, d, a, 15, 22, T16);
274 #define G(x, y, z) (((x) & (z)) | ((y) & ~(z)))
275 #define SET(a, b, c, d, k, s, Ti)\
276 t = a + G(b,c,d) + X[k] + Ti;\
277 a = ROTATE_LEFT(t, s) + b
279 SET(a, b, c, d, 1, 5, T17);
280 SET(d, a, b, c, 6, 9, T18);
281 SET(c, d, a, b, 11, 14, T19);
282 SET(b, c, d, a, 0, 20, T20);
283 SET(a, b, c, d, 5, 5, T21);
284 SET(d, a, b, c, 10, 9, T22);
285 SET(c, d, a, b, 15, 14, T23);
286 SET(b, c, d, a, 4, 20, T24);
287 SET(a, b, c, d, 9, 5, T25);
288 SET(d, a, b, c, 14, 9, T26);
289 SET(c, d, a, b, 3, 14, T27);
290 SET(b, c, d, a, 8, 20, T28);
291 SET(a, b, c, d, 13, 5, T29);
292 SET(d, a, b, c, 2, 9, T30);
293 SET(c, d, a, b, 7, 14, T31);
294 SET(b, c, d, a, 12, 20, T32);
300 #define H(x, y, z) ((x) ^ (y) ^ (z))
301 #define SET(a, b, c, d, k, s, Ti)\
302 t = a + H(b,c,d) + X[k] + Ti;\
303 a = ROTATE_LEFT(t, s) + b
305 SET(a, b, c, d, 5, 4, T33);
306 SET(d, a, b, c, 8, 11, T34);
307 SET(c, d, a, b, 11, 16, T35);
308 SET(b, c, d, a, 14, 23, T36);
309 SET(a, b, c, d, 1, 4, T37);
310 SET(d, a, b, c, 4, 11, T38);
311 SET(c, d, a, b, 7, 16, T39);
312 SET(b, c, d, a, 10, 23, T40);
313 SET(a, b, c, d, 13, 4, T41);
314 SET(d, a, b, c, 0, 11, T42);
315 SET(c, d, a, b, 3, 16, T43);
316 SET(b, c, d, a, 6, 23, T44);
317 SET(a, b, c, d, 9, 4, T45);
318 SET(d, a, b, c, 12, 11, T46);
319 SET(c, d, a, b, 15, 16, T47);
320 SET(b, c, d, a, 2, 23, T48);
326 #define I(x, y, z) ((y) ^ ((x) | ~(z)))
327 #define SET(a, b, c, d, k, s, Ti)\
328 t = a + I(b,c,d) + X[k] + Ti;\
329 a = ROTATE_LEFT(t, s) + b
331 SET(a, b, c, d, 0, 6, T49);
332 SET(d, a, b, c, 7, 10, T50);
333 SET(c, d, a, b, 14, 15, T51);
334 SET(b, c, d, a, 5, 21, T52);
335 SET(a, b, c, d, 12, 6, T53);
336 SET(d, a, b, c, 3, 10, T54);
337 SET(c, d, a, b, 10, 15, T55);
338 SET(b, c, d, a, 1, 21, T56);
339 SET(a, b, c, d, 8, 6, T57);
340 SET(d, a, b, c, 15, 10, T58);
341 SET(c, d, a, b, 6, 15, T59);
342 SET(b, c, d, a, 13, 21, T60);
343 SET(a, b, c, d, 4, 6, T61);
344 SET(d, a, b, c, 11, 10, T62);
345 SET(c, d, a, b, 2, 15, T63);
346 SET(b, c, d, a, 9, 21, T64);
352 m_state.abcd[0] += a;
353 m_state.abcd[1] += b;
354 m_state.abcd[2] += c;
355 m_state.abcd[3] += d;
361 m_state.count[0] = 0;
362 m_state.count[1] = 0;
363 m_state.abcd[0] = 0x67452301;
364 m_state.abcd[1] = T_MASK ^ 0x10325476;
365 m_state.abcd[2] = T_MASK ^ 0x67452301;
366 m_state.abcd[3] = 0x10325476;
371 feed( (
const unsigned char*)data.c_str(), data.length() );
376 const unsigned char *p = data;
378 int offset = ( m_state.count[0] >> 3 ) & 63;
379 unsigned int nbits = (
unsigned int)( bytes << 3 );
385 m_state.count[1] += bytes >> 29;
386 m_state.count[0] += nbits;
387 if( m_state.count[0] < nbits )
393 int copy = ( offset + bytes > 64 ? 64 - offset : bytes );
395 memcpy( m_state.buf + offset, p, copy );
396 if( offset + copy < 64 )
400 process( m_state.buf );
404 for( ; left >= 64; p += 64, left -= 64 )
409 memcpy( m_state.buf, p, left );
417 unsigned char data[8];
421 for( i = 0; i < 8; ++i )
422 data[i] = (
unsigned char)( m_state.count[i >> 2] >> ( ( i & 3 ) << 3 ) );
425 feed( pad, ( ( 55 - ( m_state.count[0] >> 3 ) ) & 63 ) + 1 );
440 for(
int i = 0; i < 16; ++i )
441 sprintf( buf + i * 2,
"%02x", (
unsigned char)( m_state.abcd[i >> 2] >> ( ( i & 3 ) << 3 ) ) );
451 unsigned char digest[16];
452 for(
int i = 0; i < 16; ++i )
453 digest[i] = (
unsigned char)( m_state.abcd[i >> 2] >> ( ( i & 3 ) << 3 ) );
455 return std::string( (
char*)digest, 16 );