55 #if defined(__GNUC__) && defined(__i386__)
56 static inline unsigned int rol(
unsigned int x,
int n)
58 __asm__(
"roll %%cl,%0"
64 #define rol(x,n) ( ((x) << (n)) | ((x) >> (32-(n))) )
71 #define FF(b, c, d) (d ^ (b & (c ^ d)))
72 #define FG(b, c, d) FF (d, b, c)
73 #define FH(b, c, d) (b ^ c ^ d)
74 #define FI(b, c, d) (c ^ (b | ~d))
79 typedef unsigned int u32;
80 typedef unsigned char byte;
97 this->write(buf, nbytes);
100 ret_val = (
byte*) malloc(16);
101 memset(ret_val, 0, 16);
102 memcpy(ret_val, this->read(), 16);
110 this->A = 0x67452301;
111 this->B = 0xefcdab89;
112 this->C = 0x98badcfe;
113 this->D = 0x10325476;
125 u32 correct_words[16];
130 u32* cwp = correct_words;
132 #ifdef BIG_ENDIAN_HOST
137 for (i = 0, p1 = data, p2 = (
byte*)correct_words; i < 16; i++, p2 += 4)
146 memcpy(correct_words, data, 64);
149 #define OP(a, b, c, d, s, T) \
152 a += FF (b, c, d) + (*cwp++) + T; \
163 OP (A, B, C, D, 7, 0xd76aa478);
164 OP (D, A, B, C, 12, 0xe8c7b756);
165 OP (C, D, A, B, 17, 0x242070db);
166 OP (B, C, D, A, 22, 0xc1bdceee);
167 OP (A, B, C, D, 7, 0xf57c0faf);
168 OP (D, A, B, C, 12, 0x4787c62a);
169 OP (C, D, A, B, 17, 0xa8304613);
170 OP (B, C, D, A, 22, 0xfd469501);
171 OP (A, B, C, D, 7, 0x698098d8);
172 OP (D, A, B, C, 12, 0x8b44f7af);
173 OP (C, D, A, B, 17, 0xffff5bb1);
174 OP (B, C, D, A, 22, 0x895cd7be);
175 OP (A, B, C, D, 7, 0x6b901122);
176 OP (D, A, B, C, 12, 0xfd987193);
177 OP (C, D, A, B, 17, 0xa679438e);
178 OP (B, C, D, A, 22, 0x49b40821);
181 #define OP(f, a, b, c, d, k, s, T) \
184 a += f (b, c, d) + correct_words[k] + T; \
190 OP (
FG, A, B, C, D, 1, 5, 0xf61e2562);
191 OP (
FG, D, A, B, C, 6, 9, 0xc040b340);
192 OP (
FG, C, D, A, B, 11, 14, 0x265e5a51);
193 OP (
FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
194 OP (
FG, A, B, C, D, 5, 5, 0xd62f105d);
195 OP (
FG, D, A, B, C, 10, 9, 0x02441453);
196 OP (
FG, C, D, A, B, 15, 14, 0xd8a1e681);
197 OP (
FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
198 OP (
FG, A, B, C, D, 9, 5, 0x21e1cde6);
199 OP (
FG, D, A, B, C, 14, 9, 0xc33707d6);
200 OP (
FG, C, D, A, B, 3, 14, 0xf4d50d87);
201 OP (
FG, B, C, D, A, 8, 20, 0x455a14ed);
202 OP (
FG, A, B, C, D, 13, 5, 0xa9e3e905);
203 OP (
FG, D, A, B, C, 2, 9, 0xfcefa3f8);
204 OP (
FG, C, D, A, B, 7, 14, 0x676f02d9);
205 OP (
FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
208 OP (
FH, A, B, C, D, 5, 4, 0xfffa3942);
209 OP (
FH, D, A, B, C, 8, 11, 0x8771f681);
210 OP (
FH, C, D, A, B, 11, 16, 0x6d9d6122);
211 OP (
FH, B, C, D, A, 14, 23, 0xfde5380c);
212 OP (
FH, A, B, C, D, 1, 4, 0xa4beea44);
213 OP (
FH, D, A, B, C, 4, 11, 0x4bdecfa9);
214 OP (
FH, C, D, A, B, 7, 16, 0xf6bb4b60);
215 OP (
FH, B, C, D, A, 10, 23, 0xbebfbc70);
216 OP (
FH, A, B, C, D, 13, 4, 0x289b7ec6);
217 OP (
FH, D, A, B, C, 0, 11, 0xeaa127fa);
218 OP (
FH, C, D, A, B, 3, 16, 0xd4ef3085);
219 OP (
FH, B, C, D, A, 6, 23, 0x04881d05);
220 OP (
FH, A, B, C, D, 9, 4, 0xd9d4d039);
221 OP (
FH, D, A, B, C, 12, 11, 0xe6db99e5);
222 OP (
FH, C, D, A, B, 15, 16, 0x1fa27cf8);
223 OP (
FH, B, C, D, A, 2, 23, 0xc4ac5665);
226 OP (
FI, A, B, C, D, 0, 6, 0xf4292244);
227 OP (
FI, D, A, B, C, 7, 10, 0x432aff97);
228 OP (
FI, C, D, A, B, 14, 15, 0xab9423a7);
229 OP (
FI, B, C, D, A, 5, 21, 0xfc93a039);
230 OP (
FI, A, B, C, D, 12, 6, 0x655b59c3);
231 OP (
FI, D, A, B, C, 3, 10, 0x8f0ccc92);
232 OP (
FI, C, D, A, B, 10, 15, 0xffeff47d);
233 OP (
FI, B, C, D, A, 1, 21, 0x85845dd1);
234 OP (
FI, A, B, C, D, 8, 6, 0x6fa87e4f);
235 OP (
FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
236 OP (
FI, C, D, A, B, 6, 15, 0xa3014314);
237 OP (
FI, B, C, D, A, 13, 21, 0x4e0811a1);
238 OP (
FI, A, B, C, D, 4, 6, 0xf7537e82);
239 OP (
FI, D, A, B, C, 11, 10, 0xbd3af235);
240 OP (
FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
241 OP (
FI, B, C, D, A, 9, 21, 0xeb86d391);
256 if (this->count == 64)
258 this->transform(this->buf);
268 for (; inlen && this->count < 64; inlen--) {
269 this->buf[this->count++] = *inbuf++;
272 this->write(NULL, 0);
278 while (inlen >= 64) {
279 this->transform(inbuf);
286 for (; inlen && this->count < 64; inlen--) {
287 this->buf[this->count++] = *inbuf++;
301 this->write(NULL, 0);
306 if ((lsb = t << 6) < t)
314 if ((lsb = t + this->count) < t)
321 if ((lsb = t << 3) < t)
328 if (this->count < 56)
330 this->buf[this->count++] = 0x80;
332 while (this->count < 56) {
333 this->buf[this->count++] = 0;
337 this->buf[this->count++] = 0x80;
339 while (this->count < 64) {
340 this->buf[this->count++] = 0;
343 this->write(NULL, 0);
345 memset(this->buf, 0, 56);
350 this->buf[57] = lsb >> 8;
351 this->buf[58] = lsb >> 16;
352 this->buf[59] = lsb >> 24;
354 this->buf[61] = msb >> 8;
355 this->buf[62] = msb >> 16;
356 this->buf[63] = msb >> 24;
358 this->transform(this->buf);
362 #ifdef BIG_ENDIAN_HOST
363 #define X(a) do { *p++ = this->##a ; *p++ = this->##a >> 8; \
364 *p++ = this->##a >> 16; *p++ = this->##a >> 24; } while(0)
369 #define X(a) do { *(u32*)p = (*this).a ; p += 4; } while(0)
395 free((
unsigned char*) hash);
398 MD5::MD5(
const string& s) :
400 hash(hasher->hash((unsigned char*) s.c_str(), strlen(s.c_str())),
412 for (
int i = 0; i < 16; ++i) {
413 if (pretty && ((i % 4) == 0) && i != 0) {
416 s << setw(2) << setfill(
'0') << (int)
hash[i];