71 #define __STDC_FORMAT_MACROS
90 "<inttypes.h> did not provide a definition of `SCNx8`, " \
91 "which we hence define here as `hhx`" \
114 checksum.
update( source );
121 checksum.
update( source );
128 std::ostringstream result;
129 for (
size_t i = 0; i < digest.size(); ++i) {
130 result << std::hex << std::setfill(
'0') << std::setw(2);
131 result << static_cast<int>( digest[i] );
140 bool const all_hex = std::all_of( hex.begin(), hex.end(), [](
char c ){
141 return std::isxdigit( c );
143 if( hex.size() != 32 || ! all_hex ) {
144 throw std::runtime_error(
"Invalid MD5 hex string." );
160 for (
size_t i = 0; i < result.size(); ++i) {
162 auto const n = sscanf( &hex[ 2 * i ],
"%2" SCNx8, &(result[i]) );
164 throw std::runtime_error(
"Invalid MD5 hex string." );
200 update( s.c_str(), s.size() );
210 size_t cnt = is.gcount();
223 auto const* in_uchar =
reinterpret_cast<unsigned char const*
>( input );
224 update_( in_uchar,
length);
235 static unsigned char padding[64] = {
236 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
237 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
238 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
242 unsigned char bits[8];
243 encode_(bits, count_, 8);
247 size_type padLen = (index < 56) ? (56 - index) : (120 - index);
248 update_(padding, padLen);
254 encode_( digest_.data(), state_, 16 );
257 memset(buffer_, 0,
sizeof buffer_);
258 memset(count_, 0,
sizeof count_);
274 state_[0] = 0x67452301;
275 state_[1] = 0xefcdab89;
276 state_[2] = 0x98badcfe;
277 state_[3] = 0x10325476;
280 void MD5::update_(
unsigned char const* input, size_type
length )
289 count_[1] += (
length >> 29);
296 if (
length >= firstpart) {
298 memcpy( &buffer_[index], input, firstpart );
303 transform_( &input[i] );
311 memcpy( &buffer_[index], &input[i],
length-i );
314 inline uint32_t MD5::F_(uint32_t x, uint32_t y, uint32_t z)
316 return ( x & y ) | ( ~x & z );
319 inline uint32_t MD5::G_(uint32_t x, uint32_t y, uint32_t z)
321 return ( x & z ) | ( y & ~z );
324 inline uint32_t MD5::H_(uint32_t x, uint32_t y, uint32_t z)
329 inline uint32_t MD5::I_(uint32_t x, uint32_t y, uint32_t z)
331 return y ^ ( x | ~z );
334 inline uint32_t MD5::rotate_left_(uint32_t x,
int n)
336 return (x << n) | (x >> (32-n));
339 inline void MD5::FF_(
340 uint32_t &a, uint32_t b, uint32_t c, uint32_t d, uint32_t x, uint32_t s, uint32_t ac
342 a = rotate_left_(a+ F_(b,c,d) + x + ac, s) + b;
345 inline void MD5::GG_(
346 uint32_t &a, uint32_t b, uint32_t c, uint32_t d, uint32_t x, uint32_t s, uint32_t ac
348 a = rotate_left_(a + G_(b,c,d) + x + ac, s) + b;
351 inline void MD5::HH_(
352 uint32_t &a, uint32_t b, uint32_t c, uint32_t d, uint32_t x, uint32_t s, uint32_t ac
354 a = rotate_left_(a + H_(b,c,d) + x + ac, s) + b;
357 inline void MD5::II_(
358 uint32_t &a, uint32_t b, uint32_t c, uint32_t d, uint32_t x, uint32_t s, uint32_t ac
360 a = rotate_left_(a + I_(b,c,d) + x + ac, s) + b;
365 uint32_t a = state_[0], b = state_[1], c = state_[2], d = state_[3], x[16];
369 static uint32_t S11 = 7;
370 static uint32_t S12 = 12;
371 static uint32_t S13 = 17;
372 static uint32_t S14 = 22;
373 static uint32_t S21 = 5;
374 static uint32_t S22 = 9;
375 static uint32_t S23 = 14;
376 static uint32_t S24 = 20;
377 static uint32_t S31 = 4;
378 static uint32_t S32 = 11;
379 static uint32_t S33 = 16;
380 static uint32_t S34 = 23;
381 static uint32_t S41 = 6;
382 static uint32_t S42 = 10;
383 static uint32_t S43 = 15;
384 static uint32_t S44 = 21;
387 FF_ (a, b, c, d, x[ 0], S11, 0xd76aa478);
388 FF_ (d, a, b, c, x[ 1], S12, 0xe8c7b756);
389 FF_ (c, d, a, b, x[ 2], S13, 0x242070db);
390 FF_ (b, c, d, a, x[ 3], S14, 0xc1bdceee);
391 FF_ (a, b, c, d, x[ 4], S11, 0xf57c0faf);
392 FF_ (d, a, b, c, x[ 5], S12, 0x4787c62a);
393 FF_ (c, d, a, b, x[ 6], S13, 0xa8304613);
394 FF_ (b, c, d, a, x[ 7], S14, 0xfd469501);
395 FF_ (a, b, c, d, x[ 8], S11, 0x698098d8);
396 FF_ (d, a, b, c, x[ 9], S12, 0x8b44f7af);
397 FF_ (c, d, a, b, x[10], S13, 0xffff5bb1);
398 FF_ (b, c, d, a, x[11], S14, 0x895cd7be);
399 FF_ (a, b, c, d, x[12], S11, 0x6b901122);
400 FF_ (d, a, b, c, x[13], S12, 0xfd987193);
401 FF_ (c, d, a, b, x[14], S13, 0xa679438e);
402 FF_ (b, c, d, a, x[15], S14, 0x49b40821);
405 GG_ (a, b, c, d, x[ 1], S21, 0xf61e2562);
406 GG_ (d, a, b, c, x[ 6], S22, 0xc040b340);
407 GG_ (c, d, a, b, x[11], S23, 0x265e5a51);
408 GG_ (b, c, d, a, x[ 0], S24, 0xe9b6c7aa);
409 GG_ (a, b, c, d, x[ 5], S21, 0xd62f105d);
410 GG_ (d, a, b, c, x[10], S22, 0x2441453);
411 GG_ (c, d, a, b, x[15], S23, 0xd8a1e681);
412 GG_ (b, c, d, a, x[ 4], S24, 0xe7d3fbc8);
413 GG_ (a, b, c, d, x[ 9], S21, 0x21e1cde6);
414 GG_ (d, a, b, c, x[14], S22, 0xc33707d6);
415 GG_ (c, d, a, b, x[ 3], S23, 0xf4d50d87);
416 GG_ (b, c, d, a, x[ 8], S24, 0x455a14ed);
417 GG_ (a, b, c, d, x[13], S21, 0xa9e3e905);
418 GG_ (d, a, b, c, x[ 2], S22, 0xfcefa3f8);
419 GG_ (c, d, a, b, x[ 7], S23, 0x676f02d9);
420 GG_ (b, c, d, a, x[12], S24, 0x8d2a4c8a);
423 HH_ (a, b, c, d, x[ 5], S31, 0xfffa3942);
424 HH_ (d, a, b, c, x[ 8], S32, 0x8771f681);
425 HH_ (c, d, a, b, x[11], S33, 0x6d9d6122);
426 HH_ (b, c, d, a, x[14], S34, 0xfde5380c);
427 HH_ (a, b, c, d, x[ 1], S31, 0xa4beea44);
428 HH_ (d, a, b, c, x[ 4], S32, 0x4bdecfa9);
429 HH_ (c, d, a, b, x[ 7], S33, 0xf6bb4b60);
430 HH_ (b, c, d, a, x[10], S34, 0xbebfbc70);
431 HH_ (a, b, c, d, x[13], S31, 0x289b7ec6);
432 HH_ (d, a, b, c, x[ 0], S32, 0xeaa127fa);
433 HH_ (c, d, a, b, x[ 3], S33, 0xd4ef3085);
434 HH_ (b, c, d, a, x[ 6], S34, 0x4881d05);
435 HH_ (a, b, c, d, x[ 9], S31, 0xd9d4d039);
436 HH_ (d, a, b, c, x[12], S32, 0xe6db99e5);
437 HH_ (c, d, a, b, x[15], S33, 0x1fa27cf8);
438 HH_ (b, c, d, a, x[ 2], S34, 0xc4ac5665);
441 II_ (a, b, c, d, x[ 0], S41, 0xf4292244);
442 II_ (d, a, b, c, x[ 7], S42, 0x432aff97);
443 II_ (c, d, a, b, x[14], S43, 0xab9423a7);
444 II_ (b, c, d, a, x[ 5], S44, 0xfc93a039);
445 II_ (a, b, c, d, x[12], S41, 0x655b59c3);
446 II_ (d, a, b, c, x[ 3], S42, 0x8f0ccc92);
447 II_ (c, d, a, b, x[10], S43, 0xffeff47d);
448 II_ (b, c, d, a, x[ 1], S44, 0x85845dd1);
449 II_ (a, b, c, d, x[ 8], S41, 0x6fa87e4f);
450 II_ (d, a, b, c, x[15], S42, 0xfe2ce6e0);
451 II_ (c, d, a, b, x[ 6], S43, 0xa3014314);
452 II_ (b, c, d, a, x[13], S44, 0x4e0811a1);
453 II_ (a, b, c, d, x[ 4], S41, 0xf7537e82);
454 II_ (d, a, b, c, x[11], S42, 0xbd3af235);
455 II_ (c, d, a, b, x[ 2], S43, 0x2ad7d2bb);
456 II_ (b, c, d, a, x[ 9], S44, 0xeb86d391);
464 memset(x, 0,
sizeof x);
467 void MD5::decode_( uint32_t output[],
const uint8_t input[], size_type len )
469 for (
unsigned int i = 0, j = 0; j < len; i++, j += 4) {
471 = ((uint32_t)input[j])
472 | (((uint32_t)input[j+1]) << 8)
473 | (((uint32_t)input[j+2]) << 16)
474 | (((uint32_t)input[j+3]) << 24)
479 void MD5::encode_( uint8_t output[],
const uint32_t input[], size_type len )
481 for (
size_type i = 0, j = 0; j < len; i++, j += 4) {
482 output[j] = input[i] & 0xff;
483 output[j+1] = (input[i] >> 8) & 0xff;
484 output[j+2] = (input[i] >> 16) & 0xff;
485 output[j+3] = (input[i] >> 24) & 0xff;