SHARE
TWEET

Untitled

a guest Apr 20th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7. #define MD5_INPUT_LENGTH 64
  8.  
  9. // Unsigned character - 8 bits
  10. typedef unsigned char uchar_8;
  11.  
  12. // Unsigned int - 32 bits
  13. typedef unsigned int uint_32;
  14.  
  15. // Unsigned long - 64 bit (for lengths)
  16. typedef unsigned long uint_64;
  17.  
  18. static const uint_32 T[64] = {
  19.   0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
  20.   0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
  21.   0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
  22.   0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
  23.   0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
  24.   0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
  25.   0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
  26.   0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
  27.   0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
  28.   0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
  29.   0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
  30.   0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
  31.   0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
  32.   0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
  33.   0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
  34.   0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
  35. };
  36.  
  37. // Magic values for initialization
  38. static const uint_32 A0 = 0x67452301;
  39. static const uint_32 B0 = 0xefcdab89;
  40. static const uint_32 C0 = 0x98badcfe;
  41. static const uint_32 D0 = 0x10325476;
  42.  
  43. inline uint_32 F(uint_32 x, uint_32 y, uint_32 z) {
  44.   return ((x & y) | (~x & z));
  45. }
  46.  
  47. inline uint_32 G(uint_32 x, uint_32 y, uint_32 z) {
  48.   return ((x & y) | (y & ~z));
  49. }
  50.  
  51. inline uint_32 H(uint_32 x, uint_32 y, uint_32 z) {
  52.   return (x ^ y ^ z);
  53. }
  54.  
  55. inline uint_32 I(uint_32 x, uint_32 y, uint_32 z) {
  56.   return (y ^ (x | ~z));
  57. }
  58.  
  59. // Logical wrapping left shift handling
  60. inline uint_32 shift(uint_32 x, uint_32 s) {
  61.   return ((x << s) | (x >> (32 - s)));
  62. }
  63.  
  64. // Round 1 handler
  65. inline void FF(uint_32 &a, uint_32 b, uint_32 c, uint_32 d, uint_32 x, uint_32 s, uint_32 t) {
  66.   a += F(b, c, d) + x + t;
  67.   a = shift(a, s) + b;
  68. }
  69.  
  70. // Round 2 handler
  71. inline void GG(uint_32 &a, uint_32 b, uint_32 c, uint_32 d, uint_32 x, uint_32 s, uint_32 t) {
  72.   a += G(b, c, d) + x + t;
  73.   a = shift(a, s) + b;
  74. }
  75.  
  76. // Round 3 handler
  77. inline void HH(uint_32 &a, uint_32 b, uint_32 c, uint_32 d, uint_32 x, uint_32 s, uint_32 t) {
  78.   a += H(b, c, d) + x + t;
  79.   a = shift(a, s) + b;
  80. }
  81.  
  82. // Round 4 handler
  83. inline void II(uint_32 &a, uint_32 b, uint_32 c, uint_32 d, uint_32 x, uint_32 s, uint_32 t) {
  84.   a += I(b, c, d) + x + t;
  85.   a = shift(a, s) + b;
  86. }
  87.  
  88. uint_32 *md5Pad(char *charBuf, uint_64 len) {
  89.   // Number of characters in buffer * 8 mod 64
  90.   uint_64 newLen;
  91.   for (newLen = len * 8 + 1; newLen % 512 != 448; newLen++);
  92.   newLen /= 8;
  93.  
  94.   // Initialize result buffer with null characters
  95.   uint_32 *buf = new uint_32[newLen + 64];
  96.   memset(buf, 0, newLen + 64);
  97.  
  98.   // Copy bytes from characters into uint memory space, no conversion necessary
  99.   memcpy(buf, charBuf, len);
  100.  
  101.   // Append terminating char
  102.   buf[len] = 0x80;
  103.  
  104.   // Append length to buffer
  105.   uint_32 bitsLen = len * 8;
  106.   memcpy(buf + newLen, &bitsLen, 4);
  107.  
  108.   return buf;
  109. }
  110.  
  111. void MD5(uint_32 *outBuf, uint_32 *inBuf) {
  112.   uint_32 a, b, c, d;
  113.  
  114.   const uint_32 a0 = 0x67452301;
  115.   const uint_32 b0 = 0xEFCDAB89;
  116.   const uint_32 c0 = 0x98BADCFE;
  117.   const uint_32 d0 = 0x10325476;
  118.  
  119.   a = a0;
  120.   b = b0;
  121.   c = c0;
  122.   d = d0;
  123.  
  124.   // Shift amounts 1st round
  125.   static const uchar_8 S11 =  7;
  126.   static const uchar_8 S12 = 12;
  127.   static const uchar_8 S13 = 17;
  128.   static const uchar_8 S14 = 22;
  129.  
  130.   FF ( a, b, c, d, inBuf[ 0], S11, T[ 0]); /* 1 */
  131.   FF ( d, a, b, c, inBuf[ 1], S12, T[ 1]); /* 2 */
  132.   FF ( c, d, a, b, inBuf[ 2], S13, T[ 2]); /* 3 */
  133.   FF ( b, c, d, a, inBuf[ 3], S14, T[ 3]); /* 4 */
  134.  
  135.   FF ( a, b, c, d, inBuf[ 4], S11, T[ 4]); /* 5 */
  136.   FF ( d, a, b, c, inBuf[ 5], S12, T[ 5]); /* 6 */
  137.   FF ( c, d, a, b, inBuf[ 6], S13, T[ 6]); /* 7 */
  138.   FF ( b, c, d, a, inBuf[ 7], S14, T[ 7]); /* 8 */
  139.  
  140.   FF ( a, b, c, d, inBuf[ 8], S11, T[ 8]); /* 9 */
  141.   FF ( d, a, b, c, inBuf[ 9], S12, T[ 9]); /* 10 */
  142.   FF ( c, d, a, b, inBuf[10], S13, T[10]); /* 11 */
  143.   FF ( b, c, d, a, inBuf[11], S14, T[1]); /* 12 */
  144.  
  145.   FF ( a, b, c, d, inBuf[12], S11, T[12]); /* 13 */
  146.   FF ( d, a, b, c, inBuf[13], S12, T[13]); /* 14 */
  147.   FF ( c, d, a, b, inBuf[14], S13, T[14]); /* 15 */
  148.   FF ( b, c, d, a, inBuf[15], S14, T[15]); /* 16 */
  149.  
  150.   // Shift amounts 2nd round
  151.   static const uchar_8 S21 =  5;
  152.   static const uchar_8 S22 =  9;
  153.   static const uchar_8 S23 = 14;
  154.   static const uchar_8 S24 = 20;
  155.  
  156.   GG ( a, b, c, d, inBuf[ 1], S21, T[16]); /* 17 */
  157.   GG ( d, a, b, c, inBuf[ 6], S22, T[17]); /* 18 */
  158.   GG ( c, d, a, b, inBuf[11], S23, T[18]); /* 19 */
  159.   GG ( b, c, d, a, inBuf[ 0], S24, T[19]); /* 20 */
  160.  
  161.   GG ( a, b, c, d, inBuf[ 5], S21, T[20]); /* 21 */
  162.   GG ( d, a, b, c, inBuf[10], S22, T[21]); /* 22 */
  163.   GG ( c, d, a, b, inBuf[15], S23, T[22]); /* 23 */
  164.   GG ( b, c, d, a, inBuf[ 4], S24, T[23]); /* 24 */
  165.  
  166.   GG ( a, b, c, d, inBuf[ 9], S21, T[24]); /* 25 */
  167.   GG ( d, a, b, c, inBuf[14], S22, T[25]); /* 26 */
  168.   GG ( c, d, a, b, inBuf[ 3], S23, T[26]); /* 27 */
  169.   GG ( b, c, d, a, inBuf[ 8], S24, T[27]); /* 28 */
  170.  
  171.   GG ( a, b, c, d, inBuf[13], S21, T[28]); /* 29 */
  172.   GG ( d, a, b, c, inBuf[ 2], S22, T[29]); /* 30 */
  173.   GG ( c, d, a, b, inBuf[ 7], S23, T[30]); /* 31 */
  174.   GG ( b, c, d, a, inBuf[12], S24, T[31]); /* 32 */
  175.  
  176.   // Shift amounts 3rd round
  177.   static const uchar_8 S31 =  4;
  178.   static const uchar_8 S32 = 11;
  179.   static const uchar_8 S33 = 16;
  180.   static const uchar_8 S34 = 23;
  181.  
  182.   HH ( a, b, c, d, inBuf[ 5], S31, T[32]); /* 33 */
  183.   HH ( d, a, b, c, inBuf[ 8], S32, T[33]); /* 34 */
  184.   HH ( c, d, a, b, inBuf[11], S33, T[34]); /* 35 */
  185.   HH ( b, c, d, a, inBuf[14], S34, T[35]); /* 36 */
  186.  
  187.   HH ( a, b, c, d, inBuf[ 1], S31, T[36]); /* 37 */
  188.   HH ( d, a, b, c, inBuf[ 4], S32, T[37]); /* 38 */
  189.   HH ( c, d, a, b, inBuf[ 7], S33, T[38]); /* 39 */
  190.   HH ( b, c, d, a, inBuf[10], S34, T[39]); /* 40 */
  191.  
  192.   HH ( a, b, c, d, inBuf[13], S31, T[40]); /* 41 */
  193.   HH ( d, a, b, c, inBuf[ 0], S32, T[41]); /* 42 */
  194.   HH ( c, d, a, b, inBuf[ 3], S33, T[42]); /* 43 */
  195.   HH ( b, c, d, a, inBuf[ 6], S34, T[43]); /* 44 */
  196.  
  197.   HH ( a, b, c, d, inBuf[ 9], S31, T[44]); /* 45 */
  198.   HH ( d, a, b, c, inBuf[12], S32, T[45]); /* 46 */
  199.   HH ( c, d, a, b, inBuf[15], S33, T[46]); /* 47 */
  200.   HH ( b, c, d, a, inBuf[ 2], S34, T[47]); /* 48 */
  201.  
  202.   // Shift amounts 4th round
  203.   static const uchar_8 S41 =  6;
  204.   static const uchar_8 S42 = 10;
  205.   static const uchar_8 S43 = 15;
  206.   static const uchar_8 S44 = 21;
  207.  
  208.   II ( a, b, c, d, inBuf[ 0], S41, T[48]); /* 49 */
  209.   II ( d, a, b, c, inBuf[ 7], S42, T[49]); /* 50 */
  210.   II ( c, d, a, b, inBuf[14], S43, T[50]); /* 51 */
  211.   II ( b, c, d, a, inBuf[ 5], S44, T[51]); /* 52 */
  212.  
  213.   II ( a, b, c, d, inBuf[12], S41, T[52]); /* 53 */
  214.   II ( d, a, b, c, inBuf[ 3], S42, T[53]); /* 54 */
  215.   II ( c, d, a, b, inBuf[10], S43, T[54]); /* 55 */
  216.   II ( b, c, d, a, inBuf[ 1], S44, T[55]); /* 56 */
  217.  
  218.   II ( a, b, c, d, inBuf[ 8], S41, T[56]); /* 57 */
  219.   II ( d, a, b, c, inBuf[15], S42, T[57]); /* 58 */
  220.   II ( c, d, a, b, inBuf[ 6], S43, T[58]); /* 59 */
  221.   II ( b, c, d, a, inBuf[13], S44, T[59]); /* 60 */
  222.  
  223.   II ( a, b, c, d, inBuf[ 4], S41, T[60]); /* 61 */
  224.   II ( d, a, b, c, inBuf[11], S42, T[61]); /* 62 */
  225.   II ( c, d, a, b, inBuf[ 2], S43, T[62]); /* 63 */
  226.   II ( b, c, d, a, inBuf[ 9], S44, T[63]); /* 64 */
  227.  
  228.   // Re-add initialization magic values
  229.     a += a0;
  230.     b += b0;
  231.     c += c0;
  232.     d += d0;
  233.  
  234.     outBuf[0] = a;
  235.     outBuf[1] = b;
  236.     outBuf[2] = c;
  237.     outBuf[3] = d;
  238.  
  239.   return;
  240. }
  241.  
  242. int main() {
  243.   char *srcStr = "";
  244.   uint_32 *padded = md5Pad(srcStr, strlen(srcStr));
  245.  
  246.   uint_32 result[4] = {0};
  247.  
  248.     MD5(result, padded);
  249.     uchar_8 digestChars[16] = {0};
  250.   memcpy(digestChars, result, 16);
  251.  
  252.   printf("Digest: %2.2x%2.2x%2.2x%2.2x\n", result[0], result[1], result[2], result[3]);
  253.  
  254.   delete[] padded;
  255.  
  256.   return 0;
  257. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top