93 #define T_MASK ((unsigned int)~0)
94 #define T1 (T_MASK ^ 0x28955b87)
95 #define T2 (T_MASK ^ 0x173848a9)
97 #define T4 (T_MASK ^ 0x3e423111)
98 #define T5 (T_MASK ^ 0x0a83f050)
100 #define T7 (T_MASK ^ 0x57cfb9ec)
101 #define T8 (T_MASK ^ 0x02b96afe)
102 #define T9 0x698098d8
103 #define T10 (T_MASK ^ 0x74bb0850)
104 #define T11 (T_MASK ^ 0x0000a44e)
105 #define T12 (T_MASK ^ 0x76a32841)
106 #define T13 0x6b901122
107 #define T14 (T_MASK ^ 0x02678e6c)
108 #define T15 (T_MASK ^ 0x5986bc71)
109 #define T16 0x49b40821
110 #define T17 (T_MASK ^ 0x09e1da9d)
111 #define T18 (T_MASK ^ 0x3fbf4cbf)
112 #define T19 0x265e5a51
113 #define T20 (T_MASK ^ 0x16493855)
114 #define T21 (T_MASK ^ 0x29d0efa2)
115 #define T22 0x02441453
116 #define T23 (T_MASK ^ 0x275e197e)
117 #define T24 (T_MASK ^ 0x182c0437)
118 #define T25 0x21e1cde6
119 #define T26 (T_MASK ^ 0x3cc8f829)
120 #define T27 (T_MASK ^ 0x0b2af278)
121 #define T28 0x455a14ed
122 #define T29 (T_MASK ^ 0x561c16fa)
123 #define T30 (T_MASK ^ 0x03105c07)
124 #define T31 0x676f02d9
125 #define T32 (T_MASK ^ 0x72d5b375)
126 #define T33 (T_MASK ^ 0x0005c6bd)
127 #define T34 (T_MASK ^ 0x788e097e)
128 #define T35 0x6d9d6122
129 #define T36 (T_MASK ^ 0x021ac7f3)
130 #define T37 (T_MASK ^ 0x5b4115bb)
131 #define T38 0x4bdecfa9
132 #define T39 (T_MASK ^ 0x0944b49f)
133 #define T40 (T_MASK ^ 0x4140438f)
134 #define T41 0x289b7ec6
135 #define T42 (T_MASK ^ 0x155ed805)
136 #define T43 (T_MASK ^ 0x2b10cf7a)
137 #define T44 0x04881d05
138 #define T45 (T_MASK ^ 0x262b2fc6)
139 #define T46 (T_MASK ^ 0x1924661a)
140 #define T47 0x1fa27cf8
141 #define T48 (T_MASK ^ 0x3b53a99a)
142 #define T49 (T_MASK ^ 0x0bd6ddbb)
143 #define T50 0x432aff97
144 #define T51 (T_MASK ^ 0x546bdc58)
145 #define T52 (T_MASK ^ 0x036c5fc6)
146 #define T53 0x655b59c3
147 #define T54 (T_MASK ^ 0x70f3336d)
148 #define T55 (T_MASK ^ 0x00100b82)
149 #define T56 (T_MASK ^ 0x7a7ba22e)
150 #define T57 0x6fa87e4f
151 #define T58 (T_MASK ^ 0x01d3191f)
152 #define T59 (T_MASK ^ 0x5cfebceb)
153 #define T60 0x4e0811a1
154 #define T61 (T_MASK ^ 0x08ac817d)
155 #define T62 (T_MASK ^ 0x42c50dca)
156 #define T63 0x2ad7d2bb
157 #define T64 (T_MASK ^ 0x14792c6e)
160 const unsigned char MD5::pad[64] =
162 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
163 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
164 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
165 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
168 const std::string
MD5::md5(
const std::string& data )
176 : m_finished( false )
185 void MD5::process(
const unsigned char* data )
187 unsigned int a = m_state.abcd[0];
188 unsigned int b = m_state.abcd[1];
189 unsigned int c = m_state.abcd[2];
190 unsigned int d = m_state.abcd[3];
197 unsigned int xbuf[16];
198 const unsigned int *X;
208 static const int w = 1;
210 if( *((
const unsigned char *)&w) )
218 if( !((data - (
const unsigned char*)0) & 3) )
221 X = (
const unsigned int*)data;
226 memcpy( xbuf, data, 64 );
234 #if BYTE_ORDER >= 0 // big-endian
240 const unsigned char* xp = data;
248 for( i = 0; i < 16; ++i, xp += 4 )
249 xbuf[i] = xp[0] + ( xp[1] << 8 ) + ( xp[2] << 16 ) + ( xp[3] << 24 );
254 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
259 #define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
260 #define SET(a, b, c, d, k, s, Ti)\
261 t = a + F(b,c,d) + X[k] + Ti;\
262 a = ROTATE_LEFT(t, s) + b
264 SET(a, b, c, d, 0, 7, T1);
265 SET(d, a, b, c, 1, 12, T2);
266 SET(c, d, a, b, 2, 17, T3);
267 SET(b, c, d, a, 3, 22, T4);
268 SET(a, b, c, d, 4, 7, T5);
269 SET(d, a, b, c, 5, 12, T6);
270 SET(c, d, a, b, 6, 17, T7);
271 SET(b, c, d, a, 7, 22, T8);
272 SET(a, b, c, d, 8, 7, T9);
273 SET(d, a, b, c, 9, 12, T10);
274 SET(c, d, a, b, 10, 17, T11);
275 SET(b, c, d, a, 11, 22, T12);
276 SET(a, b, c, d, 12, 7, T13);
277 SET(d, a, b, c, 13, 12, T14);
278 SET(c, d, a, b, 14, 17, T15);
279 SET(b, c, d, a, 15, 22, T16);
285 #define G(x, y, z) (((x) & (z)) | ((y) & ~(z)))
286 #define SET(a, b, c, d, k, s, Ti)\
287 t = a + G(b,c,d) + X[k] + Ti;\
288 a = ROTATE_LEFT(t, s) + b
290 SET(a, b, c, d, 1, 5, T17);
291 SET(d, a, b, c, 6, 9, T18);
292 SET(c, d, a, b, 11, 14, T19);
293 SET(b, c, d, a, 0, 20, T20);
294 SET(a, b, c, d, 5, 5, T21);
295 SET(d, a, b, c, 10, 9, T22);
296 SET(c, d, a, b, 15, 14, T23);
297 SET(b, c, d, a, 4, 20, T24);
298 SET(a, b, c, d, 9, 5, T25);
299 SET(d, a, b, c, 14, 9, T26);
300 SET(c, d, a, b, 3, 14, T27);
301 SET(b, c, d, a, 8, 20, T28);
302 SET(a, b, c, d, 13, 5, T29);
303 SET(d, a, b, c, 2, 9, T30);
304 SET(c, d, a, b, 7, 14, T31);
305 SET(b, c, d, a, 12, 20, T32);
311 #define H(x, y, z) ((x) ^ (y) ^ (z))
312 #define SET(a, b, c, d, k, s, Ti)\
313 t = a + H(b,c,d) + X[k] + Ti;\
314 a = ROTATE_LEFT(t, s) + b
316 SET(a, b, c, d, 5, 4, T33);
317 SET(d, a, b, c, 8, 11, T34);
318 SET(c, d, a, b, 11, 16, T35);
319 SET(b, c, d, a, 14, 23, T36);
320 SET(a, b, c, d, 1, 4, T37);
321 SET(d, a, b, c, 4, 11, T38);
322 SET(c, d, a, b, 7, 16, T39);
323 SET(b, c, d, a, 10, 23, T40);
324 SET(a, b, c, d, 13, 4, T41);
325 SET(d, a, b, c, 0, 11, T42);
326 SET(c, d, a, b, 3, 16, T43);
327 SET(b, c, d, a, 6, 23, T44);
328 SET(a, b, c, d, 9, 4, T45);
329 SET(d, a, b, c, 12, 11, T46);
330 SET(c, d, a, b, 15, 16, T47);
331 SET(b, c, d, a, 2, 23, T48);
337 #define I(x, y, z) ((y) ^ ((x) | ~(z)))
338 #define SET(a, b, c, d, k, s, Ti)\
339 t = a + I(b,c,d) + X[k] + Ti;\
340 a = ROTATE_LEFT(t, s) + b
342 SET(a, b, c, d, 0, 6, T49);
343 SET(d, a, b, c, 7, 10, T50);
344 SET(c, d, a, b, 14, 15, T51);
345 SET(b, c, d, a, 5, 21, T52);
346 SET(a, b, c, d, 12, 6, T53);
347 SET(d, a, b, c, 3, 10, T54);
348 SET(c, d, a, b, 10, 15, T55);
349 SET(b, c, d, a, 1, 21, T56);
350 SET(a, b, c, d, 8, 6, T57);
351 SET(d, a, b, c, 15, 10, T58);
352 SET(c, d, a, b, 6, 15, T59);
353 SET(b, c, d, a, 13, 21, T60);
354 SET(a, b, c, d, 4, 6, T61);
355 SET(d, a, b, c, 11, 10, T62);
356 SET(c, d, a, b, 2, 15, T63);
357 SET(b, c, d, a, 9, 21, T64);
363 m_state.abcd[0] += a;
364 m_state.abcd[1] += b;
365 m_state.abcd[2] += c;
366 m_state.abcd[3] += d;
372 m_state.count[0] = 0;
373 m_state.count[1] = 0;
374 m_state.abcd[0] = 0x67452301;
375 m_state.abcd[1] = T_MASK ^ 0x10325476;
376 m_state.abcd[2] = T_MASK ^ 0x67452301;
377 m_state.abcd[3] = 0x10325476;
382 feed( (
const unsigned char*)data.c_str(), (int)data.length() );
387 const unsigned char* p = data;
389 int offset = ( m_state.count[0] >> 3 ) & 63;
390 unsigned int nbits = (
unsigned int)( bytes << 3 );
396 m_state.count[1] += bytes >> 29;
397 m_state.count[0] += nbits;
398 if( m_state.count[0] < nbits )
404 int copy = ( offset + bytes > 64 ? 64 - offset : bytes );
406 memcpy( m_state.buf + offset, p, copy );
407 if( offset + copy < 64 )
411 process( m_state.buf );
415 for( ; left >= 64; p += 64, left -= 64 )
420 memcpy( m_state.buf, p, left );
428 unsigned char data[8];
431 for(
int i = 0; i < 8; ++i )
432 data[i] = (
unsigned char)( m_state.count[i >> 2] >> ( ( i & 3 ) << 3 ) );
435 feed( pad, ( ( 55 - ( m_state.count[0] >> 3 ) ) & 63 ) + 1 );
450 for(
int i = 0; i < 16; ++i )
451 sprintf( buf + i * 2,
"%02x", (
unsigned char)( m_state.abcd[i >> 2] >> ( ( i & 3 ) << 3 ) ) );
453 return std::string( buf, 32 );
461 unsigned char digest[16];
462 for(
int i = 0; i < 16; ++i )
463 digest[i] = (
unsigned char)( m_state.abcd[i >> 2] >> ( ( i & 3 ) << 3 ) );
465 return std::string( (
char*)digest, 16 );