106 checksum.
update( source );
113 std::ostringstream result;
114 for (
size_t i = 0; i < digest.size(); ++i) {
115 result << std::hex << std::setfill(
'0') << std::setw(2);
116 result << static_cast<int>( digest[i] );
125 bool const all_hex = std::all_of( hex.begin(), hex.end(), [](
char c ){
126 return std::isxdigit( c );
128 if( hex.size() != 32 || ! all_hex ) {
129 throw std::runtime_error(
"Invalid MD5 hex string." );
134 for (
size_t i = 0; i < result.size(); ++i) {
139 std::string
const sub = hex.substr( 2*i, 2 );
141 auto const res = strtoul( sub.c_str(), &endptr, 16 );
143 throw std::runtime_error(
"Invalid MD5 hex string: \"" + hex +
"\"" );
145 result[i] =
static_cast<uint32_t
>(res);
180 update( s.c_str(), s.size() );
190 size_t cnt = is.gcount();
203 auto const* in_uchar =
reinterpret_cast<unsigned char const*
>( input );
204 update_( in_uchar,
length );
215 static unsigned char padding[64] = {
216 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
217 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
218 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
222 unsigned char bits[8];
223 encode_(bits, count_, 8);
227 size_type padLen = (index < 56) ? (56 - index) : (120 - index);
228 update_(padding, padLen);
234 encode_( digest_.data(), state_, 16 );
237 memset(buffer_, 0,
sizeof buffer_);
238 memset(count_, 0,
sizeof count_);
254 state_[0] = 0x67452301;
255 state_[1] = 0xefcdab89;
256 state_[2] = 0x98badcfe;
257 state_[3] = 0x10325476;
260 void MD5::update_(
unsigned char const* input, size_type
length )
269 count_[1] += (
length >> 29);
272 assert( index < 64 );
277 if(
length >= firstpart ) {
279 memcpy( &buffer_[index], input, firstpart );
284 transform_( &input[i] );
294 memcpy( &buffer_[index], &input[i],
length-i );
297 inline uint32_t MD5::F_(uint32_t x, uint32_t y, uint32_t z)
299 return ( x & y ) | ( ~x & z );
302 inline uint32_t MD5::G_(uint32_t x, uint32_t y, uint32_t z)
304 return ( x & z ) | ( y & ~z );
307 inline uint32_t MD5::H_(uint32_t x, uint32_t y, uint32_t z)
312 inline uint32_t MD5::I_(uint32_t x, uint32_t y, uint32_t z)
314 return y ^ ( x | ~z );
317 inline uint32_t MD5::rotate_left_(uint32_t x,
int n)
319 return (x << n) | (x >> (32-n));
322 inline void MD5::FF_(
323 uint32_t &a, uint32_t b, uint32_t c, uint32_t d, uint32_t x, uint32_t s, uint32_t ac
325 a = rotate_left_(a+ F_(b,c,d) + x + ac, s) + b;
328 inline void MD5::GG_(
329 uint32_t &a, uint32_t b, uint32_t c, uint32_t d, uint32_t x, uint32_t s, uint32_t ac
331 a = rotate_left_(a + G_(b,c,d) + x + ac, s) + b;
334 inline void MD5::HH_(
335 uint32_t &a, uint32_t b, uint32_t c, uint32_t d, uint32_t x, uint32_t s, uint32_t ac
337 a = rotate_left_(a + H_(b,c,d) + x + ac, s) + b;
340 inline void MD5::II_(
341 uint32_t &a, uint32_t b, uint32_t c, uint32_t d, uint32_t x, uint32_t s, uint32_t ac
343 a = rotate_left_(a + I_(b,c,d) + x + ac, s) + b;
348 uint32_t a = state_[0], b = state_[1], c = state_[2], d = state_[3], x[16];
352 static uint32_t S11 = 7;
353 static uint32_t S12 = 12;
354 static uint32_t S13 = 17;
355 static uint32_t S14 = 22;
356 static uint32_t S21 = 5;
357 static uint32_t S22 = 9;
358 static uint32_t S23 = 14;
359 static uint32_t S24 = 20;
360 static uint32_t S31 = 4;
361 static uint32_t S32 = 11;
362 static uint32_t S33 = 16;
363 static uint32_t S34 = 23;
364 static uint32_t S41 = 6;
365 static uint32_t S42 = 10;
366 static uint32_t S43 = 15;
367 static uint32_t S44 = 21;
370 FF_ (a, b, c, d, x[ 0], S11, 0xd76aa478);
371 FF_ (d, a, b, c, x[ 1], S12, 0xe8c7b756);
372 FF_ (c, d, a, b, x[ 2], S13, 0x242070db);
373 FF_ (b, c, d, a, x[ 3], S14, 0xc1bdceee);
374 FF_ (a, b, c, d, x[ 4], S11, 0xf57c0faf);
375 FF_ (d, a, b, c, x[ 5], S12, 0x4787c62a);
376 FF_ (c, d, a, b, x[ 6], S13, 0xa8304613);
377 FF_ (b, c, d, a, x[ 7], S14, 0xfd469501);
378 FF_ (a, b, c, d, x[ 8], S11, 0x698098d8);
379 FF_ (d, a, b, c, x[ 9], S12, 0x8b44f7af);
380 FF_ (c, d, a, b, x[10], S13, 0xffff5bb1);
381 FF_ (b, c, d, a, x[11], S14, 0x895cd7be);
382 FF_ (a, b, c, d, x[12], S11, 0x6b901122);
383 FF_ (d, a, b, c, x[13], S12, 0xfd987193);
384 FF_ (c, d, a, b, x[14], S13, 0xa679438e);
385 FF_ (b, c, d, a, x[15], S14, 0x49b40821);
388 GG_ (a, b, c, d, x[ 1], S21, 0xf61e2562);
389 GG_ (d, a, b, c, x[ 6], S22, 0xc040b340);
390 GG_ (c, d, a, b, x[11], S23, 0x265e5a51);
391 GG_ (b, c, d, a, x[ 0], S24, 0xe9b6c7aa);
392 GG_ (a, b, c, d, x[ 5], S21, 0xd62f105d);
393 GG_ (d, a, b, c, x[10], S22, 0x2441453);
394 GG_ (c, d, a, b, x[15], S23, 0xd8a1e681);
395 GG_ (b, c, d, a, x[ 4], S24, 0xe7d3fbc8);
396 GG_ (a, b, c, d, x[ 9], S21, 0x21e1cde6);
397 GG_ (d, a, b, c, x[14], S22, 0xc33707d6);
398 GG_ (c, d, a, b, x[ 3], S23, 0xf4d50d87);
399 GG_ (b, c, d, a, x[ 8], S24, 0x455a14ed);
400 GG_ (a, b, c, d, x[13], S21, 0xa9e3e905);
401 GG_ (d, a, b, c, x[ 2], S22, 0xfcefa3f8);
402 GG_ (c, d, a, b, x[ 7], S23, 0x676f02d9);
403 GG_ (b, c, d, a, x[12], S24, 0x8d2a4c8a);
406 HH_ (a, b, c, d, x[ 5], S31, 0xfffa3942);
407 HH_ (d, a, b, c, x[ 8], S32, 0x8771f681);
408 HH_ (c, d, a, b, x[11], S33, 0x6d9d6122);
409 HH_ (b, c, d, a, x[14], S34, 0xfde5380c);
410 HH_ (a, b, c, d, x[ 1], S31, 0xa4beea44);
411 HH_ (d, a, b, c, x[ 4], S32, 0x4bdecfa9);
412 HH_ (c, d, a, b, x[ 7], S33, 0xf6bb4b60);
413 HH_ (b, c, d, a, x[10], S34, 0xbebfbc70);
414 HH_ (a, b, c, d, x[13], S31, 0x289b7ec6);
415 HH_ (d, a, b, c, x[ 0], S32, 0xeaa127fa);
416 HH_ (c, d, a, b, x[ 3], S33, 0xd4ef3085);
417 HH_ (b, c, d, a, x[ 6], S34, 0x4881d05);
418 HH_ (a, b, c, d, x[ 9], S31, 0xd9d4d039);
419 HH_ (d, a, b, c, x[12], S32, 0xe6db99e5);
420 HH_ (c, d, a, b, x[15], S33, 0x1fa27cf8);
421 HH_ (b, c, d, a, x[ 2], S34, 0xc4ac5665);
424 II_ (a, b, c, d, x[ 0], S41, 0xf4292244);
425 II_ (d, a, b, c, x[ 7], S42, 0x432aff97);
426 II_ (c, d, a, b, x[14], S43, 0xab9423a7);
427 II_ (b, c, d, a, x[ 5], S44, 0xfc93a039);
428 II_ (a, b, c, d, x[12], S41, 0x655b59c3);
429 II_ (d, a, b, c, x[ 3], S42, 0x8f0ccc92);
430 II_ (c, d, a, b, x[10], S43, 0xffeff47d);
431 II_ (b, c, d, a, x[ 1], S44, 0x85845dd1);
432 II_ (a, b, c, d, x[ 8], S41, 0x6fa87e4f);
433 II_ (d, a, b, c, x[15], S42, 0xfe2ce6e0);
434 II_ (c, d, a, b, x[ 6], S43, 0xa3014314);
435 II_ (b, c, d, a, x[13], S44, 0x4e0811a1);
436 II_ (a, b, c, d, x[ 4], S41, 0xf7537e82);
437 II_ (d, a, b, c, x[11], S42, 0xbd3af235);
438 II_ (c, d, a, b, x[ 2], S43, 0x2ad7d2bb);
439 II_ (b, c, d, a, x[ 9], S44, 0xeb86d391);
447 memset(x, 0,
sizeof x);
450 void MD5::decode_( uint32_t output[],
const uint8_t input[], size_type len )
452 for (
unsigned int i = 0, j = 0; j < len; i++, j += 4) {
454 = ((uint32_t)input[j])
455 | (((uint32_t)input[j+1]) << 8)
456 | (((uint32_t)input[j+2]) << 16)
457 | (((uint32_t)input[j+3]) << 24)
462 void MD5::encode_( uint8_t output[],
const uint32_t input[], size_type len )
464 for (
size_type i = 0, j = 0; j < len; i++, j += 4) {
465 output[j] = input[i] & 0xff;
466 output[j+1] = (input[i] >> 8) & 0xff;
467 output[j+2] = (input[i] >> 16) & 0xff;
468 output[j+3] = (input[i] >> 24) & 0xff;