fork download
  1. /*
  2.  * Simple MD5 implementation
  3.  *
  4.  * Compile with: gcc -o md5 md5.c
  5.  */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdint.h>
  10. #include <time.h>
  11.  
  12. #define N 100000
  13.  
  14. // leftrotate function definition
  15.  
  16. #define F(x, y, z) (z ^ ( x & (y ^ z) ))
  17. #define G(x, y, z) (y ^ ( z & (y ^ x) ))
  18. #define H(x, y, z) ((x) ^ (y) ^ (z))
  19. #define I(x, y, z) ((y) ^ ((x) | (~z)))
  20.  
  21. #define LEFTROTATE(x, c) (((x) << (c)) | ((x) >> (32 - (c))))
  22.  
  23. #define FF(a, b, c, d, x, s, ac) { \
  24. (a) += F((b), (c), (d)) + (x) + (uint32_t)(ac); \
  25. (a) = LEFTROTATE((a), (s)); \
  26. (a) += (b); \
  27. }
  28.  
  29. #define GG(a, b, c, d, x, s, ac) { \
  30. (a) += G((b), (c), (d)) + (x) + (uint32_t)(ac); \
  31. (a) = LEFTROTATE((a), (s)); \
  32. (a) += (b); \
  33. }
  34.  
  35. #define HH(a, b, c, d, x, s, ac) { \
  36. (a) += H((b), (c), (d)) + (x) + (uint32_t)(ac); \
  37. (a) = LEFTROTATE((a), (s)); \
  38. (a) += (b); \
  39. }
  40.  
  41. #define II(a, b, c, d, x, s, ac) { \
  42. (a) += I((b), (c), (d)) + (x) + (uint32_t)(ac); \
  43. (a) = LEFTROTATE((a), (s)); \
  44. (a) += (b); \
  45. }
  46.  
  47. void to_bytes(uint32_t val, uint8_t *bytes)
  48. {
  49. bytes[0] = (uint8_t) val;
  50. bytes[1] = (uint8_t) (val >> 8);
  51. bytes[2] = (uint8_t) (val >> 16);
  52. bytes[3] = (uint8_t) (val >> 24);
  53. }
  54.  
  55. uint32_t to_int32(const uint8_t *bytes)
  56. {
  57. return (uint32_t) bytes[0]
  58. | ((uint32_t) bytes[1] << 8)
  59. | ((uint32_t) bytes[2] << 16)
  60. | ((uint32_t) bytes[3] << 24);
  61. }
  62.  
  63. void md5(const uint8_t *initial_msg, size_t initial_len, uint8_t *digest) {
  64.  
  65. // These vars will contain the hash
  66. register uint32_t h0, h1, h2, h3;
  67.  
  68. // Message (to prepare)
  69. uint8_t *msg = NULL;
  70.  
  71. size_t new_len, offset;
  72. uint32_t w[16];
  73. register uint32_t a, b, c, d, i, f, temp;
  74.  
  75. // Initialize variables - simple count in nibbles:
  76. h0 = 0x67452301;
  77. h1 = 0xefcdab89;
  78. h2 = 0x98badcfe;
  79. h3 = 0x10325476;
  80.  
  81. //Pre-processing:
  82. //append "1" bit to message
  83. //append "0" bits until message length in bits is 448 (mod 512)
  84. //append length mod (2^64) to message
  85.  
  86.  
  87. new_len = initial_len + 1;
  88.  
  89. int mod = new_len & 0x3F;
  90.  
  91. if(mod > 56) new_len += 120 - mod;
  92. else if(mod < 56) new_len += 56 - mod;
  93.  
  94.  
  95. // allocate a msg with new length
  96. msg = (uint8_t*)malloc(new_len + 8);
  97. // copy the original msg to the new one
  98. memcpy(msg, initial_msg, initial_len);
  99. // append "1" bit. Note that for a computer, 8bit is the minimum length of a datatype
  100. msg[initial_len] = 0x80;
  101.  
  102. if(initial_len + 1 < new_len)
  103. memset(&msg[initial_len + 1], 0, sizeof(uint8_t) * (new_len - 1 - initial_len));
  104.  
  105. for (offset = initial_len + 1; offset < new_len; ++offset)
  106. msg[offset] = 0; // append "0" bits
  107.  
  108. // append the lower 32 bits of len in bits at the end of the buffer.
  109. to_bytes(initial_len<<3, msg + new_len);
  110. // append the higher 32 bits of len in bits at the end of the buffer.
  111. to_bytes(initial_len >> 29, msg + new_len + 4);
  112.  
  113. // Process the message in successive 512-bit chunks:
  114. //for each 512-bit chunk of message:
  115. for(offset=0; offset<new_len; offset += 64) {
  116.  
  117. // break chunk into sixteen 32-bit words w[j], from 0 to 15
  118. for (i = 0; i < 16; ++i)
  119. w[i] = *((uint32_t*)(msg + offset + (i<<2)));
  120.  
  121. // Initialize hash value for this chunk:
  122. a = h0;
  123. b = h1;
  124. c = h2;
  125. d = h3;
  126.  
  127. FF(a, b, c, d, w[ 0], 7, 0xD76AA478);
  128. FF(d, a, b, c, w[ 1], 12, 0xE8C7B756);
  129. FF(c, d, a, b, w[ 2], 17, 0x242070DB);
  130. FF(b, c, d, a, w[ 3], 22, 0xC1BDCEEE);
  131. FF(a, b, c, d, w[ 4], 7, 0xF57C0FAF);
  132. FF(d, a, b, c, w[ 5], 12, 0x4787C62A);
  133. FF(c, d, a, b, w[ 6], 17, 0xA8304613);
  134. FF(b, c, d, a, w[ 7], 22, 0xFD469501);
  135. FF(a, b, c, d, w[ 8], 7, 0x698098D8);
  136. FF(d, a, b, c, w[ 9], 12, 0x8B44F7AF);
  137. FF(c, d, a, b, w[10], 17, 0xFFFF5BB1);
  138. FF(b, c, d, a, w[11], 22, 0x895CD7BE);
  139. FF(a, b, c, d, w[12], 7, 0x6B901122);
  140. FF(d, a, b, c, w[13], 12, 0xFD987193);
  141. FF(c, d, a, b, w[14], 17, 0xA679438E);
  142. FF(b, c, d, a, w[15], 22, 0x49B40821);
  143. GG(a, b, c, d, w[ 1], 5, 0xF61E2562);
  144. GG(d, a, b, c, w[ 6], 9, 0xC040B340);
  145. GG(c, d, a, b, w[11], 14, 0x265E5A51);
  146. GG(b, c, d, a, w[ 0], 20, 0xE9B6C7AA);
  147. GG(a, b, c, d, w[ 5], 5, 0xD62F105D);
  148. GG(d, a, b, c, w[10], 9, 0x02441453);
  149. GG(c, d, a, b, w[15], 14, 0xD8A1E681);
  150. GG(b, c, d, a, w[ 4], 20, 0xE7D3FBC8);
  151. GG(a, b, c, d, w[ 9], 5, 0x21E1CDE6);
  152. GG(d, a, b, c, w[14], 9, 0xC33707D6);
  153. GG(c, d, a, b, w[ 3], 14, 0xF4D50D87);
  154. GG(b, c, d, a, w[ 8], 20, 0x455A14ED);
  155. GG(a, b, c, d, w[13], 5, 0xA9E3E905);
  156. GG(d, a, b, c, w[ 2], 9, 0xFCEFA3F8);
  157. GG(c, d, a, b, w[ 7], 14, 0x676F02D9);
  158. GG(b, c, d, a, w[12], 20, 0x8D2A4C8A);
  159. HH(a, b, c, d, w[ 5], 4, 0xFFFA3942);
  160. HH(d, a, b, c, w[ 8], 11, 0x8771F681);
  161. HH(c, d, a, b, w[11], 16, 0x6D9D6122);
  162. HH(b, c, d, a, w[14], 23, 0xFDE5380C);
  163. HH(a, b, c, d, w[ 1], 4, 0xA4BEEA44);
  164. HH(d, a, b, c, w[ 4], 11, 0x4BDECFA9);
  165. HH(c, d, a, b, w[ 7], 16, 0xF6BB4B60);
  166. HH(b, c, d, a, w[10], 23, 0xBEBFBC70);
  167. HH(a, b, c, d, w[13], 4, 0x289B7EC6);
  168. HH(d, a, b, c, w[ 0], 11, 0xEAA127FA);
  169. HH(c, d, a, b, w[ 3], 16, 0xD4EF3085);
  170. HH(b, c, d, a, w[ 6], 23, 0x04881D05);
  171. HH(a, b, c, d, w[ 9], 4, 0xD9D4D039);
  172. HH(d, a, b, c, w[12], 11, 0xE6DB99E5);
  173. HH(c, d, a, b, w[15], 16, 0x1FA27CF8);
  174. HH(b, c, d, a, w[ 2], 23, 0xC4AC5665);
  175. II(a, b, c, d, w[ 0], 6, 0xF4292244);
  176. II(d, a, b, c, w[ 7], 10, 0x432AFF97);
  177. II(c, d, a, b, w[14], 15, 0xAB9423A7);
  178. II(b, c, d, a, w[ 5], 21, 0xFC93A039);
  179. II(a, b, c, d, w[12], 6, 0x655B59C3);
  180. II(d, a, b, c, w[ 3], 10, 0x8F0CCC92);
  181. II(c, d, a, b, w[10], 15, 0xFFEFF47D);
  182. II(b, c, d, a, w[ 1], 21, 0x85845DD1);
  183. II(a, b, c, d, w[ 8], 6, 0x6FA87E4F);
  184. II(d, a, b, c, w[15], 10, 0xFE2CE6E0);
  185. II(c, d, a, b, w[ 6], 15, 0xA3014314);
  186. II(b, c, d, a, w[13], 21, 0x4E0811A1);
  187. II(a, b, c, d, w[ 4], 6, 0xF7537E82);
  188. II(d, a, b, c, w[11], 10, 0xBD3AF235);
  189. II(c, d, a, b, w[ 2], 15, 0x2AD7D2BB);
  190. II(b, c, d, a, w[ 9], 21, 0xEB86D391);
  191.  
  192. // Add this chunk's hash to result so far:
  193. h0 += a;
  194. h1 += b;
  195. h2 += c;
  196. h3 += d;
  197.  
  198. }
  199.  
  200. // cleanup
  201. free(msg);
  202.  
  203. //var char digest[16] := h0 append h1 append h2 append h3 //(Output is in little-endian)
  204. *((uint32_t*)digest) = h0;
  205. *((uint32_t*)(digest + 4)) = h1;
  206. *((uint32_t*)(digest + 8)) = h2;
  207. *((uint32_t*)(digest + 12)) = h3;
  208. }
  209.  
  210. int main(int argc, char **argv) {
  211. char *msg = argv[1];
  212. size_t len;
  213. int i;
  214. uint8_t result[16];
  215.  
  216. if (argc < 2) {
  217. printf("usage: %s 'string'\n", argv[0]);
  218. return 1;
  219. }
  220.  
  221. len = strlen(msg);
  222.  
  223. clock_t start_time = clock();
  224.  
  225. // benchmark
  226. for (i = 0; i < N; i++) {
  227. md5((uint8_t*)msg, len, result);
  228. }
  229.  
  230. // display result
  231. for (i = 0; i < 16; i++)
  232. printf("%2.2x", result[i]);
  233. puts("");
  234.  
  235. return 0;
  236. }
Runtime error #stdin #stdout 0s 15240KB
stdin
duy
stdout
usage: ./prog 'string'