Guest User

Untitled

a guest
Oct 18th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.99 KB | None | 0 0
  1. #define _XOPEN_SOURCE 500
  2.  
  3. #include <stdbool.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <time.h>
  8.  
  9. unsigned int bswap(unsigned int a)
  10. {
  11. return (a >> 24) | ((a >> 8) & 0xff00) | ((a & 0xff00) << 8) | (a << 24);
  12. }
  13.  
  14. unsigned int bxor(unsigned int a, unsigned int b)
  15. {
  16. return a ^ b;
  17. }
  18.  
  19. unsigned int band(unsigned int a, unsigned int b)
  20. {
  21. return a & b;
  22. }
  23.  
  24. unsigned int bor(unsigned int a, unsigned int b)
  25. {
  26. return a | b;
  27. }
  28.  
  29. unsigned int rol(unsigned int a, unsigned int b)
  30. {
  31. return (a << b) | (a >> (32-b));
  32. }
  33.  
  34. unsigned int bnot(unsigned int a)
  35. {
  36. return ~a;
  37. }
  38.  
  39. unsigned int tr_f(unsigned int a, unsigned int b, unsigned int c, unsigned int d, unsigned int x, unsigned int s)
  40. {
  41. return rol(bxor(d, band(b, bxor(c, d))) + a + x, s) + b;
  42. }
  43.  
  44. unsigned int tr_g(unsigned int a, unsigned int b, unsigned int c, unsigned int d, unsigned int x, unsigned int s)
  45. {
  46. return rol(bxor(c, band(d, bxor(b, c))) + a + x, s) + b;
  47. }
  48.  
  49. unsigned int tr_h(unsigned int a, unsigned int b, unsigned int c, unsigned int d, unsigned int x, unsigned int s)
  50. {
  51. return rol((b ^ c ^ d) + a + x, s) + b;
  52. }
  53.  
  54. unsigned int tr_i(unsigned int a, unsigned int b, unsigned int c, unsigned int d, unsigned int x, unsigned int s)
  55. {
  56. return rol(bxor(c, bor(b, bnot(d))) + a + x, s) + b;
  57. }
  58.  
  59. void transform(unsigned int* x, unsigned int* a1, unsigned int* b1, unsigned int* c1, unsigned int* d1)
  60. {
  61. unsigned int a, b, c, d;
  62.  
  63. a = *a1;
  64. b = *b1;
  65. c = *c1;
  66. d = *d1;
  67.  
  68. a = tr_f(a, b, c, d, x[ 1] + 0xd76aa478, 7);
  69. d = tr_f(d, a, b, c, x[ 2] + 0xe8c7b756, 12);
  70. c = tr_f(c, d, a, b, x[ 3] + 0x242070db, 17);
  71. b = tr_f(b, c, d, a, x[ 4] + 0xc1bdceee, 22);
  72. a = tr_f(a, b, c, d, x[ 5] + 0xf57c0faf, 7);
  73. d = tr_f(d, a, b, c, x[ 6] + 0x4787c62a, 12);
  74. c = tr_f(c, d, a, b, x[ 7] + 0xa8304613, 17);
  75. b = tr_f(b, c, d, a, x[ 8] + 0xfd469501, 22);
  76. a = tr_f(a, b, c, d, x[ 9] + 0x698098d8, 7);
  77. d = tr_f(d, a, b, c, x[10] + 0x8b44f7af, 12);
  78. c = tr_f(c, d, a, b, x[11] + 0xffff5bb1, 17);
  79. b = tr_f(b, c, d, a, x[12] + 0x895cd7be, 22);
  80. a = tr_f(a, b, c, d, x[13] + 0x6b901122, 7);
  81. d = tr_f(d, a, b, c, x[14] + 0xfd987193, 12);
  82. c = tr_f(c, d, a, b, x[15] + 0xa679438e, 17);
  83. b = tr_f(b, c, d, a, x[16] + 0x49b40821, 22);
  84.  
  85. a = tr_g(a, b, c, d, x[ 2] + 0xf61e2562, 5);
  86. d = tr_g(d, a, b, c, x[ 7] + 0xc040b340, 9);
  87. c = tr_g(c, d, a, b, x[12] + 0x265e5a51, 14);
  88. b = tr_g(b, c, d, a, x[ 1] + 0xe9b6c7aa, 20);
  89. a = tr_g(a, b, c, d, x[ 6] + 0xd62f105d, 5);
  90. d = tr_g(d, a, b, c, x[11] + 0x02441453, 9);
  91. c = tr_g(c, d, a, b, x[16] + 0xd8a1e681, 14);
  92. b = tr_g(b, c, d, a, x[ 5] + 0xe7d3fbc8, 20);
  93. a = tr_g(a, b, c, d, x[10] + 0x21e1cde6, 5);
  94. d = tr_g(d, a, b, c, x[15] + 0xc33707d6, 9);
  95. c = tr_g(c, d, a, b, x[ 4] + 0xf4d50d87, 14);
  96. b = tr_g(b, c, d, a, x[ 9] + 0x455a14ed, 20);
  97. a = tr_g(a, b, c, d, x[14] + 0xa9e3e905, 5);
  98. d = tr_g(d, a, b, c, x[ 3] + 0xfcefa3f8, 9);
  99. c = tr_g(c, d, a, b, x[ 8] + 0x676f02d9, 14);
  100. b = tr_g(b, c, d, a, x[13] + 0x8d2a4c8a, 20);
  101.  
  102. a = tr_h(a, b, c, d, x[ 6] + 0xfffa3942, 4);
  103. d = tr_h(d, a, b, c, x[ 9] + 0x8771f681, 11);
  104. c = tr_h(c, d, a, b, x[12] + 0x6d9d6122, 16);
  105. b = tr_h(b, c, d, a, x[15] + 0xfde5380c, 23);
  106. a = tr_h(a, b, c, d, x[ 2] + 0xa4beea44, 4);
  107. d = tr_h(d, a, b, c, x[ 5] + 0x4bdecfa9, 11);
  108. c = tr_h(c, d, a, b, x[ 8] + 0xf6bb4b60, 16);
  109. b = tr_h(b, c, d, a, x[11] + 0xbebfbc70, 23);
  110. a = tr_h(a, b, c, d, x[14] + 0x289b7ec6, 4);
  111. d = tr_h(d, a, b, c, x[ 1] + 0xeaa127fa, 11);
  112. c = tr_h(c, d, a, b, x[ 4] + 0xd4ef3085, 16);
  113. b = tr_h(b, c, d, a, x[ 7] + 0x04881d05, 23);
  114. a = tr_h(a, b, c, d, x[10] + 0xd9d4d039, 4);
  115. d = tr_h(d, a, b, c, x[13] + 0xe6db99e5, 11);
  116. c = tr_h(c, d, a, b, x[16] + 0x1fa27cf8, 16);
  117. b = tr_h(b, c, d, a, x[ 3] + 0xc4ac5665, 23);
  118.  
  119. a = tr_i(a, b, c, d, x[ 1] + 0xf4292244, 6);
  120. d = tr_i(d, a, b, c, x[ 8] + 0x432aff97, 10);
  121. c = tr_i(c, d, a, b, x[15] + 0xab9423a7, 15);
  122. b = tr_i(b, c, d, a, x[ 6] + 0xfc93a039, 21);
  123. a = tr_i(a, b, c, d, x[13] + 0x655b59c3, 6);
  124. d = tr_i(d, a, b, c, x[ 4] + 0x8f0ccc92, 10);
  125. c = tr_i(c, d, a, b, x[11] + 0xffeff47d, 15);
  126. b = tr_i(b, c, d, a, x[ 2] + 0x85845dd1, 21);
  127. a = tr_i(a, b, c, d, x[ 9] + 0x6fa87e4f, 6);
  128. d = tr_i(d, a, b, c, x[16] + 0xfe2ce6e0, 10);
  129. c = tr_i(c, d, a, b, x[ 7] + 0xa3014314, 15);
  130. b = tr_i(b, c, d, a, x[14] + 0x4e0811a1, 21);
  131. a = tr_i(a, b, c, d, x[ 5] + 0xf7537e82, 6);
  132. d = tr_i(d, a, b, c, x[12] + 0xbd3af235, 10);
  133. c = tr_i(c, d, a, b, x[ 3] + 0x2ad7d2bb, 15);
  134. b = tr_i(b, c, d, a, x[10] + 0xeb86d391, 21);
  135.  
  136. (*a1) = a + (*a1);
  137. (*b1) = b + (*b1);
  138. (*c1) = c + (*c1);
  139. (*d1) = d + (*d1);
  140. }
  141.  
  142. char* md5(char* msg)
  143. {
  144. int length = strlen(msg);
  145.  
  146. int zeros_length = (63 - ((length + 8) & 63));
  147.  
  148. int new_msg_length = 0;
  149. new_msg_length += 1;
  150. new_msg_length += zeros_length;
  151. new_msg_length += 4;
  152. new_msg_length += 4;
  153. new_msg_length += length;
  154.  
  155. char zeros[zeros_length];
  156. for (int i = 0; i < zeros_length; i++)
  157. zeros[i] = '\x00';
  158. unsigned char bytes[4] = { (length << 3) & 255, (length >> 5) & 255, (length >> 13) & 255, (length >> 21) & 255 };
  159.  
  160. unsigned char new_msg[new_msg_length];
  161. memcpy(new_msg, msg, length);
  162. *(new_msg+length) = 0x80;
  163. memcpy(new_msg+length+1, zeros, zeros_length);
  164. memcpy(new_msg+length+1+zeros_length, bytes, 4);
  165. memcpy(new_msg+length+1+zeros_length+4, "\x00\x00\x00\x00", 4);
  166.  
  167. unsigned int a = 0x67452301;
  168. unsigned int b = 0xefcdab89;
  169. unsigned int c = 0x98badcfe;
  170. unsigned int d = 0x10325476;
  171.  
  172. unsigned int x[17];
  173. int k = 1;
  174.  
  175. for (int i = 0; i < new_msg_length; i += 4)
  176. {
  177. unsigned int m0 = new_msg[i];
  178. unsigned int m1 = new_msg[i+1];
  179. unsigned int m2 = new_msg[i+1+1];
  180. unsigned int m3 = new_msg[i+1+1+1];
  181.  
  182. x[k] = m0 | (m1 << 8) | (m2 << 16) | (m3 << 24);
  183.  
  184. if (k == 16)
  185. {
  186. transform(x, &a, &b, &c, &d);
  187. k = 1;
  188. }
  189. else
  190. {
  191. k += 1;
  192. }
  193. }
  194.  
  195. char* hex = (char*)malloc(sizeof(char) * 32 + 1);
  196. sprintf(hex, "%08x%08x%08x%08x", bswap(a), bswap(b), bswap(c), bswap(d));
  197. return hex;
  198. }
  199.  
  200. char* slurp(char* file)
  201. {
  202. char *buffer;
  203. FILE *fh = fopen(file, "rb");
  204.  
  205. if (fh != NULL)
  206. {
  207. fseek(fh, 0L, SEEK_END);
  208. long s = ftell(fh);
  209. rewind(fh);
  210. buffer = malloc(s);
  211. if ( buffer != NULL )
  212. {
  213. int r = fread(buffer, sizeof(char), s, fh);
  214. fclose(fh);
  215. fh = NULL;
  216.  
  217. return buffer;
  218. }
  219.  
  220. if (fh != NULL)
  221. fclose(fh);
  222. }
  223.  
  224. return NULL;
  225. }
  226.  
  227. char* repeat(char* s, int n)
  228. {
  229. size_t slen = strlen(s);
  230. char* dest = (char*)malloc(sizeof(char) * ((slen * n) + 1) );
  231.  
  232. int i = 0;
  233. char* p = NULL;
  234. for (i = 0, p = dest; i < n; ++i, p += slen )
  235. memcpy(p, s, slen);
  236. *p = '\0';
  237.  
  238. return dest;
  239. }
  240.  
  241. double bench()
  242. {
  243. char* words = slurp("romeo_juliet.txt");
  244. char* txt = repeat(words, 10000);
  245. free(words);
  246.  
  247. int n = 80;
  248.  
  249. while(true)
  250. {
  251. struct timespec requestStart, requestEnd;
  252. clock_gettime(CLOCK_REALTIME, &requestStart);
  253.  
  254. for (int i = 0; i < n; i++)
  255. {
  256. char* ret = md5(txt);
  257. free(ret);
  258. }
  259.  
  260. clock_gettime(CLOCK_REALTIME, &requestEnd);
  261.  
  262. int secs = (requestEnd.tv_sec - requestStart.tv_sec);
  263. if (secs > 1)
  264. return ((secs*1E9) + (requestEnd.tv_nsec - requestStart.tv_nsec)) / (n * strlen(txt));
  265. else
  266. n++;
  267. }
  268.  
  269. return 0.0;
  270. }
  271.  
  272. int main()
  273. {
  274. printf("md5 %7.1f ns/char\n", bench());
  275.  
  276. return 0;
  277. }
Add Comment
Please, Sign In to add comment