Share Pastebin
Guest
Public paste!

Seven7

By: a guest | Jul 30th, 2010 | Syntax: C++ | Size: 9.39 KB | Hits: 14 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. #include "md5.h"
  2. #include <stdio.h>
  3.  
  4.  
  5. // Constants for MD5Transform routine.
  6. #define S11 7
  7. #define S12 12
  8. #define S13 17
  9. #define S14 22
  10. #define S21 5
  11. #define S22 9
  12. #define S23 14
  13. #define S24 20
  14. #define S31 4
  15. #define S32 11
  16. #define S33 16
  17. #define S34 23
  18. #define S41 6
  19. #define S42 10
  20. #define S43 15
  21. #define S44 21
  22.  
  23. ///////////////////////////////////////////////
  24.  
  25. // F, G, H and I are basic MD5 functions.
  26. inline MD5::uint4 MD5::F(uint4 x, uint4 y, uint4 z) {
  27.   return x&y | ~x&z;
  28. }
  29.  
  30. inline MD5::uint4 MD5::G(uint4 x, uint4 y, uint4 z) {
  31.   return x&z | y&~z;
  32. }
  33.  
  34. inline MD5::uint4 MD5::H(uint4 x, uint4 y, uint4 z) {
  35.   return x^y^z;
  36. }
  37.  
  38. inline MD5::uint4 MD5::I(uint4 x, uint4 y, uint4 z) {
  39.   return y ^ (x | ~z);
  40. }
  41.  
  42. // rotate_left rotates x left n bits.
  43. inline MD5::uint4 MD5::rotate_left(uint4 x, int n) {
  44.   return (x << n) | (x >> (32-n));
  45. }
  46.  
  47. // FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  48. // Rotation is separate from addition to prevent recomputation.
  49. inline void MD5::FF(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac) {
  50.   a = rotate_left(a+ F(b,c,d) + x + ac, s) + b;
  51. }
  52.  
  53. inline void MD5::GG(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac) {
  54.   a = rotate_left(a + G(b,c,d) + x + ac, s) + b;
  55. }
  56.  
  57. inline void MD5::HH(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac) {
  58.   a = rotate_left(a + H(b,c,d) + x + ac, s) + b;
  59. }
  60.  
  61. inline void MD5::II(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac) {
  62.   a = rotate_left(a + I(b,c,d) + x + ac, s) + b;
  63. }
  64.  
  65. //////////////////////////////////////////////
  66.  
  67. // default ctor, just initailize
  68. MD5::MD5()
  69. {
  70.   init();
  71. }
  72.  
  73. //////////////////////////////////////////////
  74.  
  75. // nifty shortcut ctor, compute MD5 for string and finalize it right away
  76. MD5::MD5(const std::string &text)
  77. {
  78.   init();
  79.   update(text.c_str(), text.length());
  80.   finalize();
  81. }
  82.  
  83. //////////////////////////////
  84.  
  85. void MD5::init()
  86. {
  87.   finalized=false;
  88.  
  89.   count[0] = 0;
  90.   count[1] = 0;
  91.  
  92.   // load magic initialization constants.
  93.   state[0] = 0x67452301;
  94.   state[1] = 0xefcdab89;
  95.   state[2] = 0x98badcfe;
  96.   state[3] = 0x10325476;
  97. }
  98.  
  99. //////////////////////////////
  100.  
  101. // decodes input (unsigned char) into output (uint4). Assumes len is a multiple of 4.
  102. void MD5::decode(uint4 output[], const uint1 input[], size_type len)
  103. {
  104.   for (unsigned int i = 0, j = 0; j < len; i++, j += 4)
  105.     output[i] = ((uint4)input[j]) | (((uint4)input[j+1]) << 8) |
  106.       (((uint4)input[j+2]) << 16) | (((uint4)input[j+3]) << 24);
  107. }
  108.  
  109. //////////////////////////////
  110.  
  111. // encodes input (uint4) into output (unsigned char). Assumes len is
  112. // a multiple of 4.
  113. void MD5::encode(uint1 output[], const uint4 input[], size_type len)
  114. {
  115.   for (size_type i = 0, j = 0; j < len; i++, j += 4) {
  116.     output[j] = input[i] & 0xff;
  117.     output[j+1] = (input[i] >> 8) & 0xff;
  118.     output[j+2] = (input[i] >> 16) & 0xff;
  119.     output[j+3] = (input[i] >> 24) & 0xff;
  120.   }
  121. }
  122.  
  123. //////////////////////////////
  124.  
  125. // apply MD5 algo on a block
  126. void MD5::transform(const uint1 block[blocksize])
  127. {
  128.   uint4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  129.   decode (x, block, blocksize);
  130.  
  131.   /* Round 1 */
  132.   FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
  133.   FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
  134.   FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
  135.   FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
  136.   FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
  137.   FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
  138.   FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
  139.   FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
  140.   FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
  141.   FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
  142.   FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  143.   FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  144.   FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  145.   FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  146.   FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  147.   FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  148.  
  149.   /* Round 2 */
  150.   GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
  151.   GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
  152.   GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  153.   GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
  154.   GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
  155.   GG (d, a, b, c, x[10], S22,  0x2441453); /* 22 */
  156.   GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  157.   GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
  158.   GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
  159.   GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  160.   GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
  161.   GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
  162.   GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  163.   GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
  164.   GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
  165.   GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  166.  
  167.   /* Round 3 */
  168.   HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
  169.   HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
  170.   HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  171.   HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  172.   HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
  173.   HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
  174.   HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
  175.   HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  176.   HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  177.   HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
  178.   HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
  179.   HH (b, c, d, a, x[ 6], S34,  0x4881d05); /* 44 */
  180.   HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
  181.   HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  182.   HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  183.   HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
  184.  
  185.   /* Round 4 */
  186.   II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
  187.   II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
  188.   II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  189.   II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
  190.   II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  191.   II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
  192.   II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  193.   II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
  194.   II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
  195.   II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  196.   II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
  197.   II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  198.   II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
  199.   II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  200.   II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
  201.   II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
  202.  
  203.   state[0] += a;
  204.   state[1] += b;
  205.   state[2] += c;
  206.   state[3] += d;
  207.  
  208.   // Zeroize sensitive information.
  209.   memset(x, 0, sizeof x);
  210. }
  211.  
  212. //////////////////////////////
  213.  
  214. // MD5 block update operation. Continues an MD5 message-digest
  215. // operation, processing another message block
  216. void MD5::update(const unsigned char input[], size_type length)
  217. {
  218.   // compute number of bytes mod 64
  219.   size_type index = count[0] / 8 % blocksize;
  220.  
  221.   // Update number of bits
  222.   if ((count[0] += (length << 3)) < (length << 3))
  223.     count[1]++;
  224.   count[1] += (length >> 29);
  225.  
  226.   // number of bytes we need to fill in buffer
  227.   size_type firstpart = 64 - index;
  228.  
  229.   size_type i;
  230.  
  231.   // transform as many times as possible.
  232.   if (length >= firstpart)
  233.   {
  234.     // fill buffer first, transform
  235.     memcpy(&buffer[index], input, firstpart);
  236.     transform(buffer);
  237.  
  238.     // transform chunks of blocksize (64 bytes)
  239.     for (i = firstpart; i + blocksize <= length; i += blocksize)
  240.       transform(&input[i]);
  241.  
  242.     index = 0;
  243.   }
  244.   else
  245.     i = 0;
  246.  
  247.   // buffer remaining input
  248.   memcpy(&buffer[index], &input[i], length-i);
  249. }
  250.  
  251. //////////////////////////////
  252.  
  253. // for convenience provide a verson with signed char
  254. void MD5::update(const char input[], size_type length)
  255. {
  256.   update((const unsigned char*)input, length);
  257. }
  258.  
  259. //////////////////////////////
  260.  
  261. // MD5 finalization. Ends an MD5 message-digest operation, writing the
  262. // the message digest and zeroizing the context.
  263. MD5& MD5::finalize()
  264. {
  265.   static unsigned char padding[64] = {
  266.     0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  267.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  268.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  269.   };
  270.  
  271.   if (!finalized) {
  272.     // Save number of bits
  273.     unsigned char bits[8];
  274.     encode(bits, count, 8);
  275.  
  276.     // pad out to 56 mod 64.
  277.     size_type index = count[0] / 8 % 64;
  278.     size_type padLen = (index < 56) ? (56 - index) : (120 - index);
  279.     update(padding, padLen);
  280.  
  281.     // Append length (before padding)
  282.     update(bits, 8);
  283.  
  284.     // Store state in digest
  285.     encode(digest, state, 16);
  286.  
  287.     // Zeroize sensitive information.
  288.     memset(buffer, 0, sizeof buffer);
  289.     memset(count, 0, sizeof count);
  290.  
  291.     finalized=true;
  292.   }
  293.  
  294.   return *this;
  295. }
  296.  
  297. //////////////////////////////
  298.  
  299. // return hex representation of digest as string
  300. std::string MD5::hexdigest() const
  301. {
  302.   if (!finalized)
  303.     return "";
  304.  
  305.   char buf[33];
  306.   for (int i=0; i<16; i++)
  307.     sprintf(buf+i*2, "%02x", digest[i]);
  308.   buf[32]=0;
  309.  
  310.   return std::string(buf);
  311. }
  312.  
  313. //////////////////////////////
  314.  
  315. std::ostream& operator<<(std::ostream& out, MD5 md5)
  316. {
  317.   return out << md5.hexdigest();
  318. }
  319.  
  320. //////////////////////////////
  321.  
  322. std::string md5(const std::string str)
  323. {
  324.     MD5 md5 = MD5(str);
  325.  
  326.     return md5.hexdigest();
  327. }