Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <math.h>
- #include <string.h>
- #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
- int func_f (int b, int c, int d);
- int func_g (int b, int c, int d);
- int func_h (int b, int c, int d);
- int func_g (int b, int c, int d);
- int main (int argc,char* argv[])
- {
- char* message = "abc";
- char* malloced_data = NULL;
- char add_1 = 1;
- char result[33] = {0};
- unsigned long long message_size = strlen(message);
- unsigned long long nbr_paquet = (message_size/56)+1;
- unsigned int a, b, c, d, f, g, temp, i = 0, j = 0;
- unsigned int w[16] = {0};
- unsigned int r[64] = {7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
- 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
- 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
- 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21};
- unsigned int k[64] = {
- 74957514, 149892196, 224781220, 299601773, 374331064, 448946331, 523424844, 597743917, //0-7
- 671880911,745813244, 819518394, 892973912, 966157421, 1039046629, 1111629334, 1183853428, //8-15
- 1255726910, 1327217884, 1398304576, 1468965330, 1539178623, 1608923067, 1678177418, 1746920580, //16-23
- 1815131612, 1882789738, 1949874349, 2016365008, 2082241463, 2147483648, 2212071687, 2275985909, //24-31
- 2339206843, 2401715232, 2463492035, 2524518435, 2584775842, 2644245901, 2702910498, 2760751761, //32-39
- 2817752073, 2873894071, 2929160652, 2983534983, 3037000499, 3089540917, 3141140230, 3191782721, //40-47
- 3241452965, 3290135830, 3337816488, 3384480415, 3430113397, 3474701532, 3518231240, 3560689261, //48-55
- 3602062661, 3642338838, 3681505523, 3719550786, 3756463038, 3792231035, 3826843881, 3860291034}; //56-63
- unsigned int h[4] = {0x67452301,0xEFCDAB89,0x98BADCFE,0x10325476};
- malloced_data = calloc(1,((56*nbr_paquet)+sizeof(long long int)));
- if (malloced_data == NULL){
- printf ("!calloc \n");
- return -1;}
- memcpy(malloced_data,message,message_size);
- memcpy(malloced_data+message_size,&add_1,1);
- memcpy(malloced_data+56*nbr_paquet,&message_size,sizeof(long long int));
- for (i = 0 ; i < (56*nbr_paquet)+8; i++){
- printf("%02x ",malloced_data[i]);}
- printf ("\n\n");
- for (j = 0 ; j < nbr_paquet ; j++){
- for (i = 0 ; i < 16 ; i++){
- memcpy(&w[i],(malloced_data+(j*512))+(i*4),4);
- printf("%02x ",w[i]);}
- printf ("\n\n");
- a = h[0];
- b = h[1];
- c = h[2];
- d = h[3];
- for (i = 0 ; i < 64 ; i++){
- if ( (i>= 0) | ( i<=15)){
- f = func_f(b, c, d);
- g = i;}
- else if ((i>= 16) | ( i<=31)){
- f = func_g (b, c, d);
- g = (5*i + 1)%16;}
- else if ((i>= 32) | ( i<=47)){
- f = func_h(b, c, d);
- g = (3*i + 5)%16;}
- else{
- f = func_i(b, c, d);
- g = (7*i)%16;}
- temp = d;
- d = c;
- c = b;
- b = ROTATE_LEFT (a + f + k[i] + w[g], r[i]) + b;
- a = temp;}
- h[0] += a;
- h[1] += b;
- h[2] += c;
- h[3] += d;}
- printf ("Hash : \n");
- printf ("%02x",h[0]);
- printf ("%02x",h[1]);
- printf ("%02x",h[2]);
- printf ("%02x\n",h[3]);
- free(malloced_data);
- return 0;
- }
- int func_f (int b, int c, int d)
- {
- return ((b & c)|((~b) & d));
- }
- int func_g (int b, int c, int d)
- {
- return ((b & d)|(c & (~d)));
- }
- int func_h (int b, int c, int d)
- {
- return b^c^d;
- }
- int func_i (int b, int c, int d)
- {
- return c ^ (b | (~d));
- }
Advertisement
Add Comment
Please, Sign In to add comment