Advertisement
obernardovieira

MD5 Hash encrypt

Oct 4th, 2013
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.07 KB | None | 0 0
  1. //------------------[THIS CODE IS NOT MINE]-------------------
  2. //------------------[THIS CODE IS NOT MINE]-------------------
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <stdint.h>
  8.  
  9. // Constants are the integer part of the sines of integers (in radians) * 2^32.
  10. const uint32_t k[64] = {
  11. 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee ,
  12. 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501 ,
  13. 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be ,
  14. 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821 ,
  15. 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa ,
  16. 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8 ,
  17. 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed ,
  18. 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a ,
  19. 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c ,
  20. 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70 ,
  21. 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05 ,
  22. 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665 ,
  23. 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039 ,
  24. 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1 ,
  25. 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1 ,
  26. 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 };
  27.  
  28. // r specifies the per-round shift amounts
  29. const uint32_t r[] = {7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
  30.                       5,  9, 14, 20, 5,  9, 14, 20, 5,  9, 14, 20, 5,  9, 14, 20,
  31.                       4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
  32.                       6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21};
  33.  
  34. // leftrotate function definition
  35. #define LEFTROTATE(x, c) (((x) << (c)) | ((x) >> (32 - (c))))
  36.  
  37. void to_bytes(uint32_t val, uint8_t *bytes)
  38. {
  39.     bytes[0] = (uint8_t) val;
  40.     bytes[1] = (uint8_t) (val >> 8);
  41.     bytes[2] = (uint8_t) (val >> 16);
  42.     bytes[3] = (uint8_t) (val >> 24);
  43. }
  44.  
  45. uint32_t to_int32(const uint8_t *bytes)
  46. {
  47.     return (uint32_t) bytes[0]
  48.         | ((uint32_t) bytes[1] << 8)
  49.         | ((uint32_t) bytes[2] << 16)
  50.         | ((uint32_t) bytes[3] << 24);
  51. }
  52.  
  53. void md5(const uint8_t *initial_msg, size_t initial_len, uint8_t *digest) {
  54.  
  55.     // These vars will contain the hash
  56.     uint32_t h0, h1, h2, h3;
  57.  
  58.     // Message (to prepare)
  59.     uint8_t *msg = NULL;
  60.  
  61.     size_t new_len, offset;
  62.     uint32_t w[16];
  63.     uint32_t a, b, c, d, i, f, g, temp;
  64.  
  65.     // Initialize variables - simple count in nibbles:
  66.     h0 = 0x67452301;
  67.     h1 = 0xefcdab89;
  68.     h2 = 0x98badcfe;
  69.     h3 = 0x10325476;
  70.  
  71.     //Pre-processing:
  72.     //append "1" bit to message    
  73.     //append "0" bits until message length in bits ≡ 448 (mod 512)
  74.     //append length mod (2^64) to message
  75.  
  76.     for (new_len = initial_len + 1; new_len % (512/8) != 448/8; new_len++)
  77.         ;
  78.  
  79.     msg = (uint8_t*)malloc(new_len + 8);
  80.     memcpy(msg, initial_msg, initial_len);
  81.     msg[initial_len] = 0x80; // append the "1" bit; most significant bit is "first"
  82.     for (offset = initial_len + 1; offset < new_len; offset++)
  83.         msg[offset] = 0; // append "0" bits
  84.  
  85.     // append the len in bits at the end of the buffer.
  86.     to_bytes(initial_len*8, msg + new_len);
  87.     // initial_len>>29 == initial_len*8>>32, but avoids overflow.
  88.     to_bytes(initial_len>>29, msg + new_len + 4);
  89.  
  90.     // Process the message in successive 512-bit chunks:
  91.     //for each 512-bit chunk of message:
  92.     for(offset=0; offset<new_len; offset += (512/8)) {
  93.  
  94.         // break chunk into sixteen 32-bit words w[j], 0 ≤ j ≤ 15
  95.         for (i = 0; i < 16; i++)
  96.             w[i] = to_int32(msg + offset + i*4);
  97.  
  98.         // Initialize hash value for this chunk:
  99.         a = h0;
  100.         b = h1;
  101.         c = h2;
  102.         d = h3;
  103.  
  104.         // Main loop:
  105.         for(i = 0; i<64; i++) {
  106.  
  107.             if (i < 16) {
  108.                 f = (b & c) | ((~b) & d);
  109.                 g = i;
  110.             } else if (i < 32) {
  111.                 f = (d & b) | ((~d) & c);
  112.                 g = (5*i + 1) % 16;
  113.             } else if (i < 48) {
  114.                 f = b ^ c ^ d;
  115.                 g = (3*i + 5) % 16;          
  116.             } else {
  117.                 f = c ^ (b | (~d));
  118.                 g = (7*i) % 16;
  119.             }
  120.  
  121.             temp = d;
  122.             d = c;
  123.             c = b;
  124.             b = b + LEFTROTATE((a + f + k[i] + w[g]), r[i]);
  125.             a = temp;
  126.  
  127.         }
  128.  
  129.         // Add this chunk's hash to result so far:
  130.         h0 += a;
  131.         h1 += b;
  132.         h2 += c;
  133.         h3 += d;
  134.  
  135.     }
  136.  
  137.     // cleanup
  138.     free(msg);
  139.  
  140.     //var char digest[16] := h0 append h1 append h2 append h3 //(Output is in little-endian)
  141.     to_bytes(h0, digest);
  142.     to_bytes(h1, digest + 4);
  143.     to_bytes(h2, digest + 8);
  144.     to_bytes(h3, digest + 12);
  145. }
  146.  
  147. int main(int argc, char **argv) {
  148.     char *msg = argv[1];
  149.     size_t len;
  150.     int i;
  151.     uint8_t result[16];
  152.  
  153.     if (argc < 2) {
  154.         printf("usage: %s 'string'\n", argv[0]);
  155.         return 1;
  156.     }
  157.  
  158.     len = strlen(msg);
  159.  
  160.     // benchmark
  161.     for (i = 0; i < 1000000; i++) {
  162.         md5((uint8_t*)msg, len, result);
  163.     }
  164.  
  165.     // display result
  166.     for (i = 0; i < 16; i++)
  167.         printf("%2.2x", result[i]);
  168.     puts("");
  169.  
  170.     return 0;
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement