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
169 : m_finished( false )
178 void MD5::process(
const unsigned char* data )
180 unsigned int a = m_state.abcd[0];
181 unsigned int b = m_state.abcd[1];
182 unsigned int c = m_state.abcd[2];
183 unsigned int d = m_state.abcd[3];
190 unsigned int xbuf[16];
191 const unsigned int *X;
201 static const int w = 1;
203 if( *((
const unsigned char *)&w) )
211 if( !((data - (
const unsigned char*)0) & 3) )
214 X = (
const unsigned int*)data;
219 memcpy( xbuf, data, 64 );
227 #if BYTE_ORDER >= 0 // big-endian
233 const unsigned char* xp = data;
241 for( i = 0; i < 16; ++i, xp += 4 )
242 xbuf[i] = xp[0] + ( xp[1] << 8 ) + ( xp[2] << 16 ) + ( xp[3] << 24 );
247 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
252 #define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
253 #define SET(a, b, c, d, k, s, Ti)\
254 t = a + F(b,c,d) + X[k] + Ti;\
255 a = ROTATE_LEFT(t, s) + b
257 SET(a, b, c, d, 0, 7, T1);
258 SET(d, a, b, c, 1, 12, T2);
259 SET(c, d, a, b, 2, 17, T3);
260 SET(b, c, d, a, 3, 22, T4);
261 SET(a, b, c, d, 4, 7, T5);
262 SET(d, a, b, c, 5, 12, T6);
263 SET(c, d, a, b, 6, 17, T7);
264 SET(b, c, d, a, 7, 22, T8);
265 SET(a, b, c, d, 8, 7, T9);
266 SET(d, a, b, c, 9, 12, T10);
267 SET(c, d, a, b, 10, 17, T11);
268 SET(b, c, d, a, 11, 22, T12);
269 SET(a, b, c, d, 12, 7, T13);
270 SET(d, a, b, c, 13, 12, T14);
271 SET(c, d, a, b, 14, 17, T15);
272 SET(b, c, d, a, 15, 22, T16);
278 #define G(x, y, z) (((x) & (z)) | ((y) & ~(z)))
279 #define SET(a, b, c, d, k, s, Ti)\
280 t = a + G(b,c,d) + X[k] + Ti;\
281 a = ROTATE_LEFT(t, s) + b
283 SET(a, b, c, d, 1, 5, T17);
284 SET(d, a, b, c, 6, 9, T18);
285 SET(c, d, a, b, 11, 14, T19);
286 SET(b, c, d, a, 0, 20, T20);
287 SET(a, b, c, d, 5, 5, T21);
288 SET(d, a, b, c, 10, 9, T22);
289 SET(c, d, a, b, 15, 14, T23);
290 SET(b, c, d, a, 4, 20, T24);
291 SET(a, b, c, d, 9, 5, T25);
292 SET(d, a, b, c, 14, 9, T26);
293 SET(c, d, a, b, 3, 14, T27);
294 SET(b, c, d, a, 8, 20, T28);
295 SET(a, b, c, d, 13, 5, T29);
296 SET(d, a, b, c, 2, 9, T30);
297 SET(c, d, a, b, 7, 14, T31);
298 SET(b, c, d, a, 12, 20, T32);
304 #define H(x, y, z) ((x) ^ (y) ^ (z))
305 #define SET(a, b, c, d, k, s, Ti)\
306 t = a + H(b,c,d) + X[k] + Ti;\
307 a = ROTATE_LEFT(t, s) + b
309 SET(a, b, c, d, 5, 4, T33);
310 SET(d, a, b, c, 8, 11, T34);
311 SET(c, d, a, b, 11, 16, T35);
312 SET(b, c, d, a, 14, 23, T36);
313 SET(a, b, c, d, 1, 4, T37);
314 SET(d, a, b, c, 4, 11, T38);
315 SET(c, d, a, b, 7, 16, T39);
316 SET(b, c, d, a, 10, 23, T40);
317 SET(a, b, c, d, 13, 4, T41);
318 SET(d, a, b, c, 0, 11, T42);
319 SET(c, d, a, b, 3, 16, T43);
320 SET(b, c, d, a, 6, 23, T44);
321 SET(a, b, c, d, 9, 4, T45);
322 SET(d, a, b, c, 12, 11, T46);
323 SET(c, d, a, b, 15, 16, T47);
324 SET(b, c, d, a, 2, 23, T48);
330 #define I(x, y, z) ((y) ^ ((x) | ~(z)))
331 #define SET(a, b, c, d, k, s, Ti)\
332 t = a + I(b,c,d) + X[k] + Ti;\
333 a = ROTATE_LEFT(t, s) + b
335 SET(a, b, c, d, 0, 6, T49);
336 SET(d, a, b, c, 7, 10, T50);
337 SET(c, d, a, b, 14, 15, T51);
338 SET(b, c, d, a, 5, 21, T52);
339 SET(a, b, c, d, 12, 6, T53);
340 SET(d, a, b, c, 3, 10, T54);
341 SET(c, d, a, b, 10, 15, T55);
342 SET(b, c, d, a, 1, 21, T56);
343 SET(a, b, c, d, 8, 6, T57);
344 SET(d, a, b, c, 15, 10, T58);
345 SET(c, d, a, b, 6, 15, T59);
346 SET(b, c, d, a, 13, 21, T60);
347 SET(a, b, c, d, 4, 6, T61);
348 SET(d, a, b, c, 11, 10, T62);
349 SET(c, d, a, b, 2, 15, T63);
350 SET(b, c, d, a, 9, 21, T64);
356 m_state.abcd[0] += a;
357 m_state.abcd[1] += b;
358 m_state.abcd[2] += c;
359 m_state.abcd[3] += d;
365 m_state.count[0] = 0;
366 m_state.count[1] = 0;
367 m_state.abcd[0] = 0x67452301;
368 m_state.abcd[1] = T_MASK ^ 0x10325476;
369 m_state.abcd[2] = T_MASK ^ 0x67452301;
370 m_state.abcd[3] = 0x10325476;
375 feed( (
const unsigned char*)data.c_str(), (int)data.length() );
380 const unsigned char* p = data;
382 int offset = ( m_state.count[0] >> 3 ) & 63;
383 unsigned int nbits = (
unsigned int)( bytes << 3 );
389 m_state.count[1] += bytes >> 29;
390 m_state.count[0] += nbits;
391 if( m_state.count[0] < nbits )
397 int copy = ( offset + bytes > 64 ? 64 - offset : bytes );
399 memcpy( m_state.buf + offset, p, copy );
400 if( offset + copy < 64 )
404 process( m_state.buf );
408 for( ; left >= 64; p += 64, left -= 64 )
413 memcpy( m_state.buf, p, left );
421 unsigned char data[8];
424 for(
int i = 0; i < 8; ++i )
425 data[i] = (
unsigned char)( m_state.count[i >> 2] >> ( ( i & 3 ) << 3 ) );
428 feed( pad, ( ( 55 - ( m_state.count[0] >> 3 ) ) & 63 ) + 1 );
443 for(
int i = 0; i < 16; ++i )
444 sprintf( buf + i * 2,
"%02x", (
unsigned char)( m_state.abcd[i >> 2] >> ( ( i & 3 ) << 3 ) ) );
446 return std::string( buf, 32 );
454 unsigned char digest[16];
455 for(
int i = 0; i < 16; ++i )
456 digest[i] = (
unsigned char)( m_state.abcd[i >> 2] >> ( ( i & 3 ) << 3 ) );
458 return std::string( (
char*)digest, 16 );