8 #include <utl/Md5Sum.h>
23 const unsigned char gPadding[Md5Sum::kBufferSize] = { 0x80 };
25 const unsigned int s11 = 7;
26 const unsigned int s12 = 12;
27 const unsigned int s13 = 17;
28 const unsigned int s14 = 22;
29 const unsigned int s21 = 5;
30 const unsigned int s22 = 9;
31 const unsigned int s23 = 14;
32 const unsigned int s24 = 20;
33 const unsigned int s31 = 4;
34 const unsigned int s32 = 11;
35 const unsigned int s33 = 16;
36 const unsigned int s34 = 23;
37 const unsigned int s41 = 6;
38 const unsigned int s42 = 10;
39 const unsigned int s43 = 15;
40 const unsigned int s44 = 21;
45 F(
const unsigned int x,
const unsigned int y,
const unsigned int z)
47 return (x & y) | (~x & z);
53 G(
const unsigned int x,
const unsigned int y,
const unsigned int z)
55 return (x & z) | (y & ~z);
61 H(
const unsigned int x,
const unsigned int y,
const unsigned int z)
69 I(
const unsigned int x,
const unsigned int y,
const unsigned int z)
77 RotateLeft(
const unsigned int x,
const unsigned int n)
79 return (x << n) | (x >> (32-n));
91 const unsigned int ac)
93 a += F(b, c, d) + x + ac;
102 const unsigned int b,
103 const unsigned int c,
104 const unsigned int d,
105 const unsigned int x,
106 const unsigned int s,
107 const unsigned int ac)
109 a += G(b, c, d) + x + ac;
110 a = RotateLeft(a, s);
118 const unsigned int b,
119 const unsigned int c,
120 const unsigned int d,
121 const unsigned int x,
122 const unsigned int s,
123 const unsigned int ac)
125 a += H(b, c, d) + x + ac;
126 a = RotateLeft(a, s);
134 const unsigned int b,
135 const unsigned int c,
136 const unsigned int d,
137 const unsigned int x,
138 const unsigned int s,
139 const unsigned int ac)
141 a +=
I(b, c, d) + x + ac;
142 a = RotateLeft(a, s);
149 UIntToUChar(
unsigned char*
const outBuffer,
const unsigned int input)
151 outBuffer[0] = input & 0xff;
152 outBuffer[1] = (input >> 8) & 0xff;
153 outBuffer[2] = (input >> 16) & 0xff;
154 outBuffer[3] = (input >> 24) & 0xff;
158 const char gHexTable[] =
"0123456789abcdef";
172 fStateA = 0x67452301;
173 fStateB = 0xefcdab89;
174 fStateC = 0x98badcfe;
175 fStateD = 0x10325476;
180 Md5Sum::AddDataRange(
const unsigned char*
const begin,
const unsigned char*
const end)
189 unsigned int index = BufferIndex();
192 const unsigned int length = end - begin;
193 const unsigned int bits = (length << 3);
195 if (fCountLow < bits)
197 fCountHigh += (length >> 29);
199 for (
const unsigned char*
p = begin;
p < end; ++
p) {
202 if (index == kBufferSize) {
211 Md5Sum::ProcessBuffer()
213 const unsigned int scratchSize = 16;
214 unsigned int x[scratchSize];
216 for (
unsigned int i = 0; i < scratchSize; ++i) {
217 const unsigned int i4 = 4*i;
220 fBuffer[i4+2] << 16 |
224 unsigned int a = fStateA;
225 unsigned int b = fStateB;
226 unsigned int c = fStateC;
227 unsigned int d = fStateD;
230 FF(a, b, c, d, x[ 0], s11, 0xd76aa478);
231 FF(d, a, b, c, x[ 1], s12, 0xe8c7b756);
232 FF(c, d, a, b, x[ 2], s13, 0x242070db);
233 FF(b, c, d, a, x[ 3], s14, 0xc1bdceee);
234 FF(a, b, c, d, x[ 4], s11, 0xf57c0faf);
235 FF(d, a, b, c, x[ 5], s12, 0x4787c62a);
236 FF(c, d, a, b, x[ 6], s13, 0xa8304613);
237 FF(b, c, d, a, x[ 7], s14, 0xfd469501);
238 FF(a, b, c, d, x[ 8], s11, 0x698098d8);
239 FF(d, a, b, c, x[ 9], s12, 0x8b44f7af);
240 FF(c, d, a, b, x[10], s13, 0xffff5bb1);
241 FF(b, c, d, a, x[11], s14, 0x895cd7be);
242 FF(a, b, c, d, x[12], s11, 0x6b901122);
243 FF(d, a, b, c, x[13], s12, 0xfd987193);
244 FF(c, d, a, b, x[14], s13, 0xa679438e);
245 FF(b, c, d, a, x[15], s14, 0x49b40821);
248 GG(a, b, c, d, x[ 1], s21, 0xf61e2562);
249 GG(d, a, b, c, x[ 6], s22, 0xc040b340);
250 GG(c, d, a, b, x[11], s23, 0x265e5a51);
251 GG(b, c, d, a, x[ 0], s24, 0xe9b6c7aa);
252 GG(a, b, c, d, x[ 5], s21, 0xd62f105d);
253 GG(d, a, b, c, x[10], s22, 0x02441453);
254 GG(c, d, a, b, x[15], s23, 0xd8a1e681);
255 GG(b, c, d, a, x[ 4], s24, 0xe7d3fbc8);
256 GG(a, b, c, d, x[ 9], s21, 0x21e1cde6);
257 GG(d, a, b, c, x[14], s22, 0xc33707d6);
258 GG(c, d, a, b, x[ 3], s23, 0xf4d50d87);
259 GG(b, c, d, a, x[ 8], s24, 0x455a14ed);
260 GG(a, b, c, d, x[13], s21, 0xa9e3e905);
261 GG(d, a, b, c, x[ 2], s22, 0xfcefa3f8);
262 GG(c, d, a, b, x[ 7], s23, 0x676f02d9);
263 GG(b, c, d, a, x[12], s24, 0x8d2a4c8a);
266 HH(a, b, c, d, x[ 5], s31, 0xfffa3942);
267 HH(d, a, b, c, x[ 8], s32, 0x8771f681);
268 HH(c, d, a, b, x[11], s33, 0x6d9d6122);
269 HH(b, c, d, a, x[14], s34, 0xfde5380c);
270 HH(a, b, c, d, x[ 1], s31, 0xa4beea44);
271 HH(d, a, b, c, x[ 4], s32, 0x4bdecfa9);
272 HH(c, d, a, b, x[ 7], s33, 0xf6bb4b60);
273 HH(b, c, d, a, x[10], s34, 0xbebfbc70);
274 HH(a, b, c, d, x[13], s31, 0x289b7ec6);
275 HH(d, a, b, c, x[ 0], s32, 0xeaa127fa);
276 HH(c, d, a, b, x[ 3], s33, 0xd4ef3085);
277 HH(b, c, d, a, x[ 6], s34, 0x04881d05);
278 HH(a, b, c, d, x[ 9], s31, 0xd9d4d039);
279 HH(d, a, b, c, x[12], s32, 0xe6db99e5);
280 HH(c, d, a, b, x[15], s33, 0x1fa27cf8);
281 HH(b, c, d, a, x[ 2], s34, 0xc4ac5665);
284 II(a, b, c, d, x[ 0], s41, 0xf4292244);
285 II(d, a, b, c, x[ 7], s42, 0x432aff97);
286 II(c, d, a, b, x[14], s43, 0xab9423a7);
287 II(b, c, d, a, x[ 5], s44, 0xfc93a039);
288 II(a, b, c, d, x[12], s41, 0x655b59c3);
289 II(d, a, b, c, x[ 3], s42, 0x8f0ccc92);
290 II(c, d, a, b, x[10], s43, 0xffeff47d);
291 II(b, c, d, a, x[ 1], s44, 0x85845dd1);
292 II(a, b, c, d, x[ 8], s41, 0x6fa87e4f);
293 II(d, a, b, c, x[15], s42, 0xfe2ce6e0);
294 II(c, d, a, b, x[ 6], s43, 0xa3014314);
295 II(b, c, d, a, x[13], s44, 0x4e0811a1);
296 II(a, b, c, d, x[ 4], s41, 0xf7537e82);
297 II(d, a, b, c, x[11], s42, 0xbd3af235);
298 II(c, d, a, b, x[ 2], s43, 0x2ad7d2bb);
299 II(b, c, d, a, x[ 9], s44, 0xeb86d391);
309 Md5Sum::GetRawDigest()
312 unsigned char bitCountBuffer[8];
314 UIntToUChar(bitCountBuffer, fCountLow);
315 UIntToUChar(bitCountBuffer + 4, fCountHigh);
317 int length = int(kBufferSize - 8) - int(BufferIndex());
320 length += kBufferSize;
321 AddDataRange(gPadding, gPadding + length);
323 AddDataRange(bitCountBuffer, bitCountBuffer + 8);
325 UIntToUChar(fDigest, fStateA);
326 UIntToUChar(fDigest + 4, fStateB);
327 UIntToUChar(fDigest + 8, fStateC);
328 UIntToUChar(fDigest + 12, fStateD);
335 return make_pair(fDigest, fDigest + kDigestSize);
340 Md5Sum::GetHexDigest()
345 for (
Iterator cur = iters.first; cur != iters.second; ++cur) {
346 digest.push_back(gHexTable[(*cur >> 4) % 16]);
347 digest.push_back(gHexTable[*cur % 16]);
std::pair< Iterator, Iterator > IteratorPair
Exception for invalid operation on Iterators.
const unsigned char * Iterator