Advertisement
Guest User

Akihira

a guest
Mar 4th, 2018
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.08 KB | None | 0 0
  1. #ifndef MD5_H
  2. #define MD5_H
  3.  
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. #pragma region MD5 defines
  8. #define S11 7
  9. #define S12 12
  10. #define S13 17
  11. #define S14 22
  12. #define S21 5
  13. #define S22 9
  14. #define S23 14
  15. #define S24 20
  16. #define S31 4
  17. #define S32 11
  18. #define S33 16
  19. #define S34 23
  20. #define S41 6
  21. #define S42 10
  22. #define S43 15
  23. #define S44 21
  24.  
  25. static unsigned char PADDING[64] = {
  26.     0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  27.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  28.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  29. };
  30.  
  31. // F, G, H and I are basic MD5 functions.
  32. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  33. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  34. #define H(x, y, z) ((x) ^ (y) ^ (z))
  35. #define I(x, y, z) ((y) ^ ((x) | (~z)))
  36.  
  37. // ROTATE_LEFT rotates x left n bits.
  38. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  39.  
  40. // FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  41. // Rotation is separate from addition to prevent recomputation.
  42. #define FF(a, b, c, d, x, s, ac) { \
  43.     (a) += F((b), (c), (d)) + (x)+(UINT4)(ac); \
  44.     (a) = ROTATE_LEFT((a), (s)); \
  45.     (a) += (b); \
  46. }
  47. #define GG(a, b, c, d, x, s, ac) { \
  48.     (a) += G((b), (c), (d)) + (x)+(UINT4)(ac); \
  49.     (a) = ROTATE_LEFT((a), (s)); \
  50.     (a) += (b); \
  51.     }
  52. #define HH(a, b, c, d, x, s, ac) { \
  53.     (a) += H((b), (c), (d)) + (x)+(UINT4)(ac); \
  54.     (a) = ROTATE_LEFT((a), (s)); \
  55.     (a) += (b); \
  56.     }
  57. #define II(a, b, c, d, x, s, ac) { \
  58.     (a) += I((b), (c), (d)) + (x)+(UINT4)(ac); \
  59.     (a) = ROTATE_LEFT((a), (s)); \
  60.     (a) += (b); \
  61.     }
  62. #pragma endregion
  63.  
  64. typedef unsigned char BYTE;
  65.  
  66. // POINTER defines a generic pointer type
  67. typedef unsigned char *POINTER;
  68.  
  69. // UINT2 defines a two byte word
  70. typedef unsigned short int UINT2;
  71.  
  72. // UINT4 defines a four byte word
  73. typedef unsigned long int UINT4;
  74.  
  75.  
  76. // convenient object that wraps
  77. // the C-functions for use in C++ only
  78. class MD5
  79. {
  80. private:
  81.     struct __context_t {
  82.         UINT4 state[4];                                   /* state (ABCD) */
  83.         UINT4 count[2];        /* number of bits, modulo 2^64 (lsb first) */
  84.         unsigned char buffer[64];                         /* input buffer */
  85.     } context;
  86.  
  87. #pragma region static helper functions
  88.     // The core of the MD5 algorithm is here.
  89.     // MD5 basic transformation. Transforms state based on block.
  90.     static void MD5Transform(UINT4 state[4], unsigned char block[64])
  91.     {
  92.         UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  93.  
  94.         Decode(x, block, 64);
  95.  
  96.         /* Round 1 */
  97.         FF(a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */
  98.         FF(d, a, b, c, x[1], S12, 0xe8c7b756); /* 2 */
  99.         FF(c, d, a, b, x[2], S13, 0x242070db); /* 3 */
  100.         FF(b, c, d, a, x[3], S14, 0xc1bdceee); /* 4 */
  101.         FF(a, b, c, d, x[4], S11, 0xf57c0faf); /* 5 */
  102.         FF(d, a, b, c, x[5], S12, 0x4787c62a); /* 6 */
  103.         FF(c, d, a, b, x[6], S13, 0xa8304613); /* 7 */
  104.         FF(b, c, d, a, x[7], S14, 0xfd469501); /* 8 */
  105.         FF(a, b, c, d, x[8], S11, 0x698098d8); /* 9 */
  106.         FF(d, a, b, c, x[9], S12, 0x8b44f7af); /* 10 */
  107.         FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  108.         FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  109.         FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  110.         FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  111.         FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  112.         FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  113.  
  114.         /* Round 2 */
  115.         GG(a, b, c, d, x[1], S21, 0xf61e2562); /* 17 */
  116.         GG(d, a, b, c, x[6], S22, 0xc040b340); /* 18 */
  117.         GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  118.         GG(b, c, d, a, x[0], S24, 0xe9b6c7aa); /* 20 */
  119.         GG(a, b, c, d, x[5], S21, 0xd62f105d); /* 21 */
  120.         GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */
  121.         GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  122.         GG(b, c, d, a, x[4], S24, 0xe7d3fbc8); /* 24 */
  123.         GG(a, b, c, d, x[9], S21, 0x21e1cde6); /* 25 */
  124.         GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  125.         GG(c, d, a, b, x[3], S23, 0xf4d50d87); /* 27 */
  126.         GG(b, c, d, a, x[8], S24, 0x455a14ed); /* 28 */
  127.         GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  128.         GG(d, a, b, c, x[2], S22, 0xfcefa3f8); /* 30 */
  129.         GG(c, d, a, b, x[7], S23, 0x676f02d9); /* 31 */
  130.         GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  131.  
  132.         /* Round 3 */
  133.         HH(a, b, c, d, x[5], S31, 0xfffa3942); /* 33 */
  134.         HH(d, a, b, c, x[8], S32, 0x8771f681); /* 34 */
  135.         HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  136.         HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  137.         HH(a, b, c, d, x[1], S31, 0xa4beea44); /* 37 */
  138.         HH(d, a, b, c, x[4], S32, 0x4bdecfa9); /* 38 */
  139.         HH(c, d, a, b, x[7], S33, 0xf6bb4b60); /* 39 */
  140.         HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  141.         HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  142.         HH(d, a, b, c, x[0], S32, 0xeaa127fa); /* 42 */
  143.         HH(c, d, a, b, x[3], S33, 0xd4ef3085); /* 43 */
  144.         HH(b, c, d, a, x[6], S34, 0x4881d05); /* 44 */
  145.         HH(a, b, c, d, x[9], S31, 0xd9d4d039); /* 45 */
  146.         HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  147.         HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  148.         HH(b, c, d, a, x[2], S34, 0xc4ac5665); /* 48 */
  149.  
  150.         /* Round 4 */
  151.         II(a, b, c, d, x[0], S41, 0xf4292244); /* 49 */
  152.         II(d, a, b, c, x[7], S42, 0x432aff97); /* 50 */
  153.         II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  154.         II(b, c, d, a, x[5], S44, 0xfc93a039); /* 52 */
  155.         II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  156.         II(d, a, b, c, x[3], S42, 0x8f0ccc92); /* 54 */
  157.         II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  158.         II(b, c, d, a, x[1], S44, 0x85845dd1); /* 56 */
  159.         II(a, b, c, d, x[8], S41, 0x6fa87e4f); /* 57 */
  160.         II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  161.         II(c, d, a, b, x[6], S43, 0xa3014314); /* 59 */
  162.         II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  163.         II(a, b, c, d, x[4], S41, 0xf7537e82); /* 61 */
  164.         II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  165.         II(c, d, a, b, x[2], S43, 0x2ad7d2bb); /* 63 */
  166.         II(b, c, d, a, x[9], S44, 0xeb86d391); /* 64 */
  167.  
  168.         state[0] += a;
  169.         state[1] += b;
  170.         state[2] += c;
  171.         state[3] += d;
  172.  
  173.         // Zeroize sensitive information.
  174.         memset((POINTER)x, 0, sizeof (x));
  175.     }
  176.  
  177.     // Encodes input (UINT4) into output (unsigned char). Assumes len is
  178.     // a multiple of 4.
  179.     static void Encode(unsigned char *output, UINT4 *input, unsigned int len)
  180.     {
  181.         unsigned int i, j;
  182.  
  183.         for (i = 0, j = 0; j < len; i++, j += 4) {
  184.             output[j] = (unsigned char)(input[i] & 0xff);
  185.             output[j + 1] = (unsigned char)((input[i] >> 8) & 0xff);
  186.             output[j + 2] = (unsigned char)((input[i] >> 16) & 0xff);
  187.             output[j + 3] = (unsigned char)((input[i] >> 24) & 0xff);
  188.         }
  189.     }
  190.  
  191.     // Decodes input (unsigned char) into output (UINT4). Assumes len is
  192.     // a multiple of 4.
  193.     static void Decode(UINT4 *output, unsigned char *input, unsigned int len)
  194.     {
  195.         unsigned int i, j;
  196.  
  197.         for (i = 0, j = 0; j < len; i++, j += 4)
  198.             output[i] = ((UINT4)input[j]) | (((UINT4)input[j + 1]) << 8) |
  199.             (((UINT4)input[j + 2]) << 16) | (((UINT4)input[j + 3]) << 24);
  200.     }
  201. #pragma endregion
  202.  
  203.  
  204. public:
  205.     // MAIN FUNCTIONS
  206.     MD5()
  207.     {
  208.         Init();
  209.     }
  210.  
  211.     // MD5 initialization. Begins an MD5 operation, writing a new context.
  212.     void Init()
  213.     {
  214.         context.count[0] = context.count[1] = 0;
  215.  
  216.         // Load magic initialization constants.
  217.         context.state[0] = 0x67452301;
  218.         context.state[1] = 0xefcdab89;
  219.         context.state[2] = 0x98badcfe;
  220.         context.state[3] = 0x10325476;
  221.     }
  222.  
  223.     // MD5 block update operation. Continues an MD5 message-digest
  224.     // operation, processing another message block, and updating the
  225.     // context.
  226.     void Update(
  227.         unsigned char *input,   // input block
  228.         unsigned int inputLen) // length of input block
  229.     {
  230.         unsigned int i, index, partLen;
  231.  
  232.         // Compute number of bytes mod 64
  233.         index = (unsigned int)((context.count[0] >> 3) & 0x3F);
  234.  
  235.         // Update number of bits
  236.         if ((context.count[0] += ((UINT4)inputLen << 3))
  237.             < ((UINT4)inputLen << 3))
  238.             context.count[1]++;
  239.         context.count[1] += ((UINT4)inputLen >> 29);
  240.  
  241.         partLen = 64 - index;
  242.  
  243.         // Transform as many times as possible.
  244.         if (inputLen >= partLen) {
  245.             memcpy((POINTER)&context.buffer[index], (POINTER)input, partLen);
  246.             MD5Transform(context.state, context.buffer);
  247.  
  248.             for (i = partLen; i + 63 < inputLen; i += 64)
  249.                 MD5Transform(context.state, &input[i]);
  250.  
  251.             index = 0;
  252.         }
  253.         else
  254.             i = 0;
  255.  
  256.         /* Buffer remaining input */
  257.         memcpy((POINTER)&context.buffer[index], (POINTER)&input[i], inputLen - i);
  258.     }
  259.  
  260.     // MD5 finalization. Ends an MD5 message-digest operation, writing the
  261.     // the message digest and zeroizing the context.
  262.     // Writes to digestRaw
  263.     void Final()
  264.     {
  265.         unsigned char bits[8];
  266.         unsigned int index, padLen;
  267.  
  268.         // Save number of bits
  269.         Encode(bits, context.count, 8);
  270.  
  271.         // Pad out to 56 mod 64.
  272.         index = (unsigned int)((context.count[0] >> 3) & 0x3f);
  273.         padLen = (index < 56) ? (56 - index) : (120 - index);
  274.         Update(PADDING, padLen);
  275.  
  276.         // Append length (before padding)
  277.         Update(bits, 8);
  278.  
  279.         // Store state in digest
  280.         Encode(digestRaw, context.state, 16);
  281.  
  282.         // Zeroize sensitive information.
  283.         memset((POINTER)&context, 0, sizeof (context));
  284.  
  285.         writeToString();
  286.     }
  287.  
  288.     /// Buffer must be 32+1 (nul) = 33 chars long at least
  289.     void writeToString()
  290.     {
  291.         int pos;
  292.  
  293.         for (pos = 0; pos < 16; pos++)
  294.             sprintf(digestChars + (pos * 2), "%02x", digestRaw[pos]);
  295.     }
  296.  
  297.  
  298. public:
  299.     // an MD5 digest is a 16-byte number (32 hex digits)
  300.     BYTE digestRaw[16];
  301.  
  302.     // This version of the digest is actually
  303.     // a "printf'd" version of the digest.
  304.     char digestChars[33];
  305.  
  306.     /// Load a file from disk and digest it
  307.     // Digests a file and returns the result.
  308.     char* digestFile(char *filename)
  309.     {
  310.         Init();
  311.  
  312.         FILE *file;
  313.  
  314.         int len;
  315.         unsigned char buffer[1024];
  316.  
  317.         if ((file = fopen(filename, "rb")) == NULL)
  318.             printf("%s can't be opened\n", filename);
  319.         else
  320.         {
  321.             while (len = fread(buffer, 1, 1024, file))
  322.                 Update(buffer, len);
  323.             Final();
  324.  
  325.             fclose(file);
  326.         }
  327.  
  328.         return digestChars;
  329.     }
  330.  
  331.     /// Digests a byte-array already in memory
  332.     char* digestMemory(BYTE *memchunk, int len)
  333.     {
  334.         Init();
  335.         Update(memchunk, len);
  336.         Final();
  337.  
  338.         return digestChars;
  339.     }
  340.  
  341.     // Digests a string and prints the result.
  342.     char* digestString(char *string)
  343.     {
  344.         Init();
  345.         Update((unsigned char*)string, strlen(string));
  346.         Final();
  347.  
  348.         return digestChars;
  349.     }
  350. };
  351.  
  352. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement