rendered paste body#include "stdafx.h"#include "MD5alg.h"HMODULE hLib;MD5INIT init;MD5PROCESS process;//przesuniecia bitowe w funkcji ROTATE_LEFT#define S11 7#define S12 12#define S13 17#define S14 22#define S21 5#define S22 9#define S23 14#define S24 20#define S31 4#define S32 11#define S33 16#define S34 23#define S41 6#define S42 10#define S43 15#define S44 21//stale o wartosciach: floor(abs(sin(i + 1)) × (2 pow 32)) #define T1 0xd76aa478#define T2 0xe8c7b756 #define T3 0x242070db#define T4 0xc1bdceee #define T5 0xf57c0faf #define T6 0x4787c62a#define T7 0xa8304613 #define T8 0xfd469501 #define T9 0x698098d8#define T10 0x8b44f7af #define T11 0xffff5bb1 #define T12 0x895cd7be #define T13 0x6b901122#define T14 0xfd987193 #define T15 0xa679438e #define T16 0x49b40821#define T17 0xf61e2562 #define T18 0xc040b340 #define T19 0x265e5a51#define T20 0xe9b6c7aa #define T21 0xd62f105d #define T22 0x02441453#define T23 0xd8a1e681 #define T24 0xe7d3fbc8 #define T25 0x21e1cde6#define T26 0xc33707d6#define T27 0xf4d50d87 #define T28 0x455a14ed#define T29 0xa9e3e905 #define T30 0xfcefa3f8 #define T31 0x676f02d9#define T32 0x8d2a4c8a #define T33 0xfffa3942 #define T34 0x8771f681 #define T35 0x6d9d6122#define T36 0xfde5380c #define T37 0xa4beea44 #define T38 0x4bdecfa9#define T39 0xf6bb4b60 #define T40 0xbebfbc70 #define T41 0x289b7ec6#define T42 0xeaa127fa #define T43 0xd4ef3085 #define T44 0x04881d05#define T45 0xd9d4d039 #define T46 0xe6db99e5 #define T47 0x1fa27cf8#define T48 0xc4ac5665 #define T49 0xf4292244 #define T50 0x432aff97#define T51 0xab9423a7 #define T52 0xfc93a039 #define T53 0x655b59c3#define T54 0x8f0ccc92 #define T55 0xffeff47d #define T56 0x85845dd1 #define T57 0x6fa87e4f#define T58 0xfe2ce6e0 #define T59 0xa3014314 #define T60 0x4e0811a1#define T61 0xf7537e82 #define T62 0xbd3af235 #define T63 0x2ad7d2bb#define T64 0xeb86d391//definicje makr/funkcji pomocniczych#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))#define F(x, y, z) (((x) & (y)) | (~(x) & (z)))#define G(x, y, z) (((x) & (z)) | ((y) & ~(z)))#define H(x, y, z) ((x) ^ (y) ^ (z))#define I(x, y, z) ((y) ^ ((x) | ~(z)))#define FF(a, b, c, d, k, s, Ti)\ t = a + F(b,c,d) + X[k] + Ti;\ a = ROTATE_LEFT(t, s) + b#define GG(a, b, c, d, k, s, Ti)\ t = a + G(b,c,d) + X[k] + Ti;\ a = ROTATE_LEFT(t, s) + b#define HH(a, b, c, d, k, s, Ti)\ t = a + H(b,c,d) + X[k] + Ti;\ a = ROTATE_LEFT(t, s) + b#define II(a, b, c, d, k, s, Ti)\ t = a + I(b,c,d) + X[k] + Ti;\ a = ROTATE_LEFT(t, s) + b//glowna procedura haszujaca (C)static void md5ProcessC(md5_state_t *pms, const md5_byte_t *data /*[64]*/){ md5_word_t a = pms->abcd[0], b = pms->abcd[1], c = pms->abcd[2], d = pms->abcd[3]; //przechowanie aktualnych wartosci sumy md5 md5_word_t t; md5_word_t xbuf[16]; const md5_word_t *X; static const int w = 1; //podzial blokow 512bitowych na 16x32bit if (*((const md5_byte_t *)&w)) { if (!((data - (const md5_byte_t *)0) & 3)) X = (const md5_word_t *)data; else { memcpy(xbuf, data, 64); X = xbuf; } } FF(a, b, c, d, 0, S11, T1); FF(d, a, b, c, 1, S12, T2); FF(c, d, a, b, 2, S13, T3); FF(b, c, d, a, 3, S14, T4); FF(a, b, c, d, 4, S11, T5); FF(d, a, b, c, 5, S12, T6); FF(c, d, a, b, 6, S13, T7); FF(b, c, d, a, 7, S14, T8); FF(a, b, c, d, 8, S11, T9); FF(d, a, b, c, 9, S12, T10); FF(c, d, a, b, 10, S13, T11); FF(b, c, d, a, 11, S14, T12); FF(a, b, c, d, 12, S11, T13); FF(d, a, b, c, 13, S12, T14); FF(c, d, a, b, 14, S13, T15); FF(b, c, d, a, 15, S14, T16); GG(a, b, c, d, 1, S21, T17); GG(d, a, b, c, 6, S22, T18); GG(c, d, a, b, 11, S23, T19); GG(b, c, d, a, 0, S24, T20); GG(a, b, c, d, 5, S21, T21); GG(d, a, b, c, 10, S22, T22); GG(c, d, a, b, 15, S23, T23); GG(b, c, d, a, 4, S24, T24); GG(a, b, c, d, 9, S21, T25); GG(d, a, b, c, 14, S22, T26); GG(c, d, a, b, 3, S23, T27); GG(b, c, d, a, 8, S24, T28); GG(a, b, c, d, 13, S21, T29); GG(d, a, b, c, 2, S22, T30); GG(c, d, a, b, 7, S23, T31); GG(b, c, d, a, 12, S24, T32); HH(a, b, c, d, 5, S31, T33); HH(d, a, b, c, 8, S32, T34); HH(c, d, a, b, 11, S33, T35); HH(b, c, d, a, 14, S34, T36); HH(a, b, c, d, 1, S31, T37); HH(d, a, b, c, 4, S32, T38); HH(c, d, a, b, 7, S33, T39); HH(b, c, d, a, 10, S34, T40); HH(a, b, c, d, 13, S31, T41); HH(d, a, b, c, 0, S32, T42); HH(c, d, a, b, 3, S33, T43); HH(b, c, d, a, 6, S34, T44); HH(a, b, c, d, 9, S31, T45); HH(d, a, b, c, 12, S32, T46); HH(c, d, a, b, 15, S33, T47); HH(b, c, d, a, 2, S34, T48); II(a, b, c, d, 0, S41, T49); II(d, a, b, c, 7, S42, T50); II(c, d, a, b, 14, S43, T51); II(b, c, d, a, 5, S44, T52); II(a, b, c, d, 12, S41, T53); II(d, a, b, c, 3, S42, T54); II(c, d, a, b, 10, S43, T55); II(b, c, d, a, 1, S44, T56); II(a, b, c, d, 8, S41, T57); II(d, a, b, c, 15, S42, T58); II(c, d, a, b, 6, S43, T59); II(b, c, d, a, 13, S44, T60); II(a, b, c, d, 4, S41, T61); II(d, a, b, c, 11, S42, T62); II(c, d, a, b, 2, S43, T63); II(b, c, d, a, 9, S44, T64); pms->abcd[0] += a; pms->abcd[1] += b; pms->abcd[2] += c; pms->abcd[3] += d;}//glowna procedura haszujaca (ASM), korzysta z procedury MD5Process napisanej w ASMvoid md5ProcessAsm(md5_state_t *pms, const md5_byte_t *data /*[64]*/){ if (process != NULL) process(pms, data); return;}//stan poczatkowy sumy MD5 (C)void md5InitC(md5_state_t *pms){ pms->count[0] = pms->count[1] = 0; pms->abcd[0] = 0x67452301; pms->abcd[1] = 0xefcdab89; pms->abcd[2] = 0x98badcfe; pms->abcd[3] = 0x10325476;}//stan poczatkowy sumy MD5 (ASM), korzysta z procedury MD5Init napsianej w ASMvoid md5InitAsm(md5_state_t *pms){ if (init != NULL) init(pms); return;}//procedury md5_append oraz md5-finish obsluguja zarowno haszowanie w C oraz ASM, jednak ich wplyw na czas haszownia jest znikomy//uzupelnienie danych wejsciowych o wymagane dodatki//doklejamy do wiadomości wejściowej bit o wartości 1//doklejamy tyle zer ile trzeba żeby ciąg składał się z 512-bitowych bloków, i ostatniego niepełnego - 448-bitowego//doklejamy 64-bitowy (zaczynając od najmniej znaczącego bitu) licznik oznaczający rozmiar wiadomości. W ten sposób otrzymujemy wiadomość złożoną z 512-bitowych fragmentów.void md5Append(md5_state_t *pms, const md5_byte_t *data, int nbytes, int language){ const md5_byte_t *p = data; int left = nbytes; int offset = (pms->count[0] >> 3) & 63; md5_word_t nbits = (md5_word_t)(nbytes << 3); //zamiana bajtow na bity (pozostala dlugosc wiadomosci) if (nbytes <= 0) return; //dlugosc wiadomosci pms->count[1] += nbytes >> 29; //wielkosc danych powyzej 32bit, rotacja 29+3(poniewaz bajt=2^3) = 32) pms->count[0] += nbits; //wielkosc do 32 bit if (pms->count[0] < nbits) pms->count[1]++; //przetwarzanie bloku inicjujacego if (offset) { int copy = (offset + nbytes > 64 ? 64 - offset : nbytes); memcpy(pms->buf + offset, p, copy); if (offset + copy < 64) return; p += copy; left -= copy; if (language == 0) md5ProcessC(pms, pms->buf); else md5ProcessAsm(pms, pms->buf); } //przetwarzanie pelnych blokow for (; left >= 64; p += 64, left -= 64) if (language == 0) md5ProcessC(pms, p); else md5ProcessAsm(pms, p); //blok koncowy if (left) memcpy(pms->buf, p, left);}void md5Finish(md5_state_t *pms, md5_byte_t digest[16], int language){ static const md5_byte_t pad[64] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; md5_byte_t data[8]; int i; //zapisujemy wielkosc danych przed dodaniem odpowiednich warotsci for (i = 0; i < 8; ++i) data[i] = (md5_byte_t)(pms->count[i >> 2] >> ((i & 3) << 3)); //dodajemy do 448 mod 64 bitow md5Append(pms, pad, ((55 - (pms->count[0] >> 3)) & 63) + 1, language); //dodajemy dlugosc wiadomosci md5Append(pms, data, 8, language); for (i = 0; i < 16; ++i) //ostateczna forma sumy MD5 - digest[i] digest[i] = (md5_byte_t)(pms->abcd[i >> 2] >> ((i & 3) << 3));}//ladowanie DLLvoid loadLib(){ if ((hLib = LoadLibrary(TEXT("MD5asm.dll"))) != NULL) { init = (MD5INIT)GetProcAddress(hLib,"MD5Init"); process = (MD5PROCESS)GetProcAddress(hLib,"MD5Process"); } return;}//zwalnianie DLLvoid freeLib(){ FreeLibrary (hLib);}