Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _XOPEN_SOURCE 500
- #include <stdbool.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <time.h>
- unsigned int bswap(unsigned int a)
- {
- return (a >> 24) | ((a >> 8) & 0xff00) | ((a & 0xff00) << 8) | (a << 24);
- }
- unsigned int bxor(unsigned int a, unsigned int b)
- {
- return a ^ b;
- }
- unsigned int band(unsigned int a, unsigned int b)
- {
- return a & b;
- }
- unsigned int bor(unsigned int a, unsigned int b)
- {
- return a | b;
- }
- unsigned int rol(unsigned int a, unsigned int b)
- {
- return (a << b) | (a >> (32-b));
- }
- unsigned int bnot(unsigned int a)
- {
- return ~a;
- }
- unsigned int tr_f(unsigned int a, unsigned int b, unsigned int c, unsigned int d, unsigned int x, unsigned int s)
- {
- return rol(bxor(d, band(b, bxor(c, d))) + a + x, s) + b;
- }
- unsigned int tr_g(unsigned int a, unsigned int b, unsigned int c, unsigned int d, unsigned int x, unsigned int s)
- {
- return rol(bxor(c, band(d, bxor(b, c))) + a + x, s) + b;
- }
- unsigned int tr_h(unsigned int a, unsigned int b, unsigned int c, unsigned int d, unsigned int x, unsigned int s)
- {
- return rol((b ^ c ^ d) + a + x, s) + b;
- }
- unsigned int tr_i(unsigned int a, unsigned int b, unsigned int c, unsigned int d, unsigned int x, unsigned int s)
- {
- return rol(bxor(c, bor(b, bnot(d))) + a + x, s) + b;
- }
- void transform(unsigned int* x, unsigned int* a1, unsigned int* b1, unsigned int* c1, unsigned int* d1)
- {
- unsigned int a, b, c, d;
- a = *a1;
- b = *b1;
- c = *c1;
- d = *d1;
- a = tr_f(a, b, c, d, x[ 1] + 0xd76aa478, 7);
- d = tr_f(d, a, b, c, x[ 2] + 0xe8c7b756, 12);
- c = tr_f(c, d, a, b, x[ 3] + 0x242070db, 17);
- b = tr_f(b, c, d, a, x[ 4] + 0xc1bdceee, 22);
- a = tr_f(a, b, c, d, x[ 5] + 0xf57c0faf, 7);
- d = tr_f(d, a, b, c, x[ 6] + 0x4787c62a, 12);
- c = tr_f(c, d, a, b, x[ 7] + 0xa8304613, 17);
- b = tr_f(b, c, d, a, x[ 8] + 0xfd469501, 22);
- a = tr_f(a, b, c, d, x[ 9] + 0x698098d8, 7);
- d = tr_f(d, a, b, c, x[10] + 0x8b44f7af, 12);
- c = tr_f(c, d, a, b, x[11] + 0xffff5bb1, 17);
- b = tr_f(b, c, d, a, x[12] + 0x895cd7be, 22);
- a = tr_f(a, b, c, d, x[13] + 0x6b901122, 7);
- d = tr_f(d, a, b, c, x[14] + 0xfd987193, 12);
- c = tr_f(c, d, a, b, x[15] + 0xa679438e, 17);
- b = tr_f(b, c, d, a, x[16] + 0x49b40821, 22);
- a = tr_g(a, b, c, d, x[ 2] + 0xf61e2562, 5);
- d = tr_g(d, a, b, c, x[ 7] + 0xc040b340, 9);
- c = tr_g(c, d, a, b, x[12] + 0x265e5a51, 14);
- b = tr_g(b, c, d, a, x[ 1] + 0xe9b6c7aa, 20);
- a = tr_g(a, b, c, d, x[ 6] + 0xd62f105d, 5);
- d = tr_g(d, a, b, c, x[11] + 0x02441453, 9);
- c = tr_g(c, d, a, b, x[16] + 0xd8a1e681, 14);
- b = tr_g(b, c, d, a, x[ 5] + 0xe7d3fbc8, 20);
- a = tr_g(a, b, c, d, x[10] + 0x21e1cde6, 5);
- d = tr_g(d, a, b, c, x[15] + 0xc33707d6, 9);
- c = tr_g(c, d, a, b, x[ 4] + 0xf4d50d87, 14);
- b = tr_g(b, c, d, a, x[ 9] + 0x455a14ed, 20);
- a = tr_g(a, b, c, d, x[14] + 0xa9e3e905, 5);
- d = tr_g(d, a, b, c, x[ 3] + 0xfcefa3f8, 9);
- c = tr_g(c, d, a, b, x[ 8] + 0x676f02d9, 14);
- b = tr_g(b, c, d, a, x[13] + 0x8d2a4c8a, 20);
- a = tr_h(a, b, c, d, x[ 6] + 0xfffa3942, 4);
- d = tr_h(d, a, b, c, x[ 9] + 0x8771f681, 11);
- c = tr_h(c, d, a, b, x[12] + 0x6d9d6122, 16);
- b = tr_h(b, c, d, a, x[15] + 0xfde5380c, 23);
- a = tr_h(a, b, c, d, x[ 2] + 0xa4beea44, 4);
- d = tr_h(d, a, b, c, x[ 5] + 0x4bdecfa9, 11);
- c = tr_h(c, d, a, b, x[ 8] + 0xf6bb4b60, 16);
- b = tr_h(b, c, d, a, x[11] + 0xbebfbc70, 23);
- a = tr_h(a, b, c, d, x[14] + 0x289b7ec6, 4);
- d = tr_h(d, a, b, c, x[ 1] + 0xeaa127fa, 11);
- c = tr_h(c, d, a, b, x[ 4] + 0xd4ef3085, 16);
- b = tr_h(b, c, d, a, x[ 7] + 0x04881d05, 23);
- a = tr_h(a, b, c, d, x[10] + 0xd9d4d039, 4);
- d = tr_h(d, a, b, c, x[13] + 0xe6db99e5, 11);
- c = tr_h(c, d, a, b, x[16] + 0x1fa27cf8, 16);
- b = tr_h(b, c, d, a, x[ 3] + 0xc4ac5665, 23);
- a = tr_i(a, b, c, d, x[ 1] + 0xf4292244, 6);
- d = tr_i(d, a, b, c, x[ 8] + 0x432aff97, 10);
- c = tr_i(c, d, a, b, x[15] + 0xab9423a7, 15);
- b = tr_i(b, c, d, a, x[ 6] + 0xfc93a039, 21);
- a = tr_i(a, b, c, d, x[13] + 0x655b59c3, 6);
- d = tr_i(d, a, b, c, x[ 4] + 0x8f0ccc92, 10);
- c = tr_i(c, d, a, b, x[11] + 0xffeff47d, 15);
- b = tr_i(b, c, d, a, x[ 2] + 0x85845dd1, 21);
- a = tr_i(a, b, c, d, x[ 9] + 0x6fa87e4f, 6);
- d = tr_i(d, a, b, c, x[16] + 0xfe2ce6e0, 10);
- c = tr_i(c, d, a, b, x[ 7] + 0xa3014314, 15);
- b = tr_i(b, c, d, a, x[14] + 0x4e0811a1, 21);
- a = tr_i(a, b, c, d, x[ 5] + 0xf7537e82, 6);
- d = tr_i(d, a, b, c, x[12] + 0xbd3af235, 10);
- c = tr_i(c, d, a, b, x[ 3] + 0x2ad7d2bb, 15);
- b = tr_i(b, c, d, a, x[10] + 0xeb86d391, 21);
- (*a1) = a + (*a1);
- (*b1) = b + (*b1);
- (*c1) = c + (*c1);
- (*d1) = d + (*d1);
- }
- char* md5(char* msg)
- {
- int length = strlen(msg);
- int zeros_length = (63 - ((length + 8) & 63));
- int new_msg_length = 0;
- new_msg_length += 1;
- new_msg_length += zeros_length;
- new_msg_length += 4;
- new_msg_length += 4;
- new_msg_length += length;
- char zeros[zeros_length];
- for (int i = 0; i < zeros_length; i++)
- zeros[i] = '\x00';
- unsigned char bytes[4] = { (length << 3) & 255, (length >> 5) & 255, (length >> 13) & 255, (length >> 21) & 255 };
- unsigned char new_msg[new_msg_length];
- memcpy(new_msg, msg, length);
- *(new_msg+length) = 0x80;
- memcpy(new_msg+length+1, zeros, zeros_length);
- memcpy(new_msg+length+1+zeros_length, bytes, 4);
- memcpy(new_msg+length+1+zeros_length+4, "\x00\x00\x00\x00", 4);
- unsigned int a = 0x67452301;
- unsigned int b = 0xefcdab89;
- unsigned int c = 0x98badcfe;
- unsigned int d = 0x10325476;
- unsigned int x[17];
- int k = 1;
- for (int i = 0; i < new_msg_length; i += 4)
- {
- unsigned int m0 = new_msg[i];
- unsigned int m1 = new_msg[i+1];
- unsigned int m2 = new_msg[i+1+1];
- unsigned int m3 = new_msg[i+1+1+1];
- x[k] = m0 | (m1 << 8) | (m2 << 16) | (m3 << 24);
- if (k == 16)
- {
- transform(x, &a, &b, &c, &d);
- k = 1;
- }
- else
- {
- k += 1;
- }
- }
- char* hex = (char*)malloc(sizeof(char) * 32 + 1);
- sprintf(hex, "%08x%08x%08x%08x", bswap(a), bswap(b), bswap(c), bswap(d));
- return hex;
- }
- char* slurp(char* file)
- {
- char *buffer;
- FILE *fh = fopen(file, "rb");
- if (fh != NULL)
- {
- fseek(fh, 0L, SEEK_END);
- long s = ftell(fh);
- rewind(fh);
- buffer = malloc(s);
- if ( buffer != NULL )
- {
- int r = fread(buffer, sizeof(char), s, fh);
- fclose(fh);
- fh = NULL;
- return buffer;
- }
- if (fh != NULL)
- fclose(fh);
- }
- return NULL;
- }
- char* repeat(char* s, int n)
- {
- size_t slen = strlen(s);
- char* dest = (char*)malloc(sizeof(char) * ((slen * n) + 1) );
- int i = 0;
- char* p = NULL;
- for (i = 0, p = dest; i < n; ++i, p += slen )
- memcpy(p, s, slen);
- *p = '\0';
- return dest;
- }
- double bench()
- {
- char* words = slurp("romeo_juliet.txt");
- char* txt = repeat(words, 10000);
- free(words);
- int n = 80;
- while(true)
- {
- struct timespec requestStart, requestEnd;
- clock_gettime(CLOCK_REALTIME, &requestStart);
- for (int i = 0; i < n; i++)
- {
- char* ret = md5(txt);
- free(ret);
- }
- clock_gettime(CLOCK_REALTIME, &requestEnd);
- int secs = (requestEnd.tv_sec - requestStart.tv_sec);
- if (secs > 1)
- return ((secs*1E9) + (requestEnd.tv_nsec - requestStart.tv_nsec)) / (n * strlen(txt));
- else
- n++;
- }
- return 0.0;
- }
- int main()
- {
- printf("md5 %7.1f ns/char\n", bench());
- return 0;
- }
Add Comment
Please, Sign In to add comment