Guest User

Untitled

a guest
Mar 7th, 2011
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.61 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <string.h>
  5.  
  6. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
  7.  
  8. int func_f (int b, int c, int d);
  9. int func_g (int b, int c, int d);
  10. int func_h (int b, int c, int d);
  11. int func_g (int b, int c, int d);
  12.  
  13. int main (int argc,char* argv[])
  14.   {  
  15.  
  16.     char* message = "abc";
  17.     char* malloced_data = NULL;
  18.     char  add_1 = 1;
  19.   char  result[33] = {0};
  20.  
  21.     unsigned long  long message_size = strlen(message);
  22.     unsigned long long nbr_paquet = (message_size/56)+1;
  23.    
  24.     unsigned int a, b, c, d, f, g, temp, i = 0, j = 0;
  25.  
  26.   unsigned int w[16] = {0};
  27.  
  28.     unsigned int r[64] = {7, 12, 17, 22,  7, 12, 17, 22,  7, 12, 17, 22,  7, 12, 17, 22,
  29.                  5,  9, 14, 20,  5,  9, 14, 20,  5,  9, 14, 20,  5,  9, 14, 20,
  30.                  4, 11, 16, 23,  4, 11, 16, 23,  4, 11, 16, 23,  4, 11, 16, 23,
  31.                  6, 10, 15, 21,  6, 10, 15, 21,  6, 10, 15, 21,  6, 10, 15, 21};
  32.  
  33.   unsigned int k[64] = {
  34.          74957514, 149892196, 224781220, 299601773, 374331064, 448946331, 523424844, 597743917,       //0-7
  35.          671880911,745813244, 819518394, 892973912, 966157421, 1039046629, 1111629334, 1183853428,      //8-15
  36.          1255726910, 1327217884, 1398304576, 1468965330, 1539178623, 1608923067, 1678177418, 1746920580,   //16-23
  37.          1815131612, 1882789738, 1949874349, 2016365008, 2082241463, 2147483648, 2212071687, 2275985909,   //24-31
  38.          2339206843, 2401715232, 2463492035, 2524518435, 2584775842, 2644245901, 2702910498, 2760751761,   //32-39
  39.          2817752073, 2873894071, 2929160652, 2983534983, 3037000499, 3089540917, 3141140230, 3191782721,   //40-47
  40.          3241452965, 3290135830, 3337816488, 3384480415, 3430113397, 3474701532, 3518231240, 3560689261,   //48-55
  41.          3602062661, 3642338838, 3681505523, 3719550786, 3756463038, 3792231035, 3826843881, 3860291034};  //56-63
  42.  
  43.  
  44.     unsigned int h[4] = {0x67452301,0xEFCDAB89,0x98BADCFE,0x10325476};
  45.  
  46.  
  47.     malloced_data = calloc(1,((56*nbr_paquet)+sizeof(long long int)));
  48.  
  49.     if (malloced_data == NULL){
  50.      printf ("!calloc \n");
  51.      return -1;}
  52.  
  53.     memcpy(malloced_data,message,message_size);
  54.     memcpy(malloced_data+message_size,&add_1,1);
  55.     memcpy(malloced_data+56*nbr_paquet,&message_size,sizeof(long long int));
  56.  
  57.   for (i = 0 ; i < (56*nbr_paquet)+8; i++){
  58.    printf("%02x ",malloced_data[i]);}
  59.    printf ("\n\n");
  60.  
  61.   for (j = 0 ; j < nbr_paquet ; j++){
  62.  
  63.    for (i = 0 ; i < 16 ; i++){
  64.     memcpy(&w[i],(malloced_data+(j*512))+(i*4),4);
  65.     printf("%02x ",w[i]);}
  66.    printf ("\n\n");
  67.        
  68.    
  69.    a = h[0];
  70.    b = h[1];
  71.    c = h[2];
  72.    d = h[3];
  73.  
  74.    for (i = 0 ; i < 64 ; i++){
  75.     if ( (i>= 0) | ( i<=15)){
  76.      f = func_f(b, c, d);
  77.      g = i;}
  78.     else if ((i>= 16) | ( i<=31)){
  79.      f = func_g (b, c, d);
  80.      g = (5*i + 1)%16;}
  81.     else if ((i>= 32) | ( i<=47)){
  82.      f = func_h(b, c, d);
  83.      g = (3*i + 5)%16;}
  84.     else{
  85.      f = func_i(b, c, d);
  86.      g = (7*i)%16;}
  87.  
  88.     temp = d;
  89.     d = c;
  90.     c = b;
  91.     b = ROTATE_LEFT (a + f + k[i] + w[g], r[i]) + b;
  92.     a = temp;}
  93.    
  94.    h[0] += a;
  95.    h[1] += b;
  96.    h[2] += c;
  97.    h[3] += d;}
  98.  
  99.    printf ("Hash : \n");
  100.    printf ("%02x",h[0]);
  101.    printf ("%02x",h[1]);
  102.    printf ("%02x",h[2]);
  103.    printf ("%02x\n",h[3]);
  104.  
  105.    free(malloced_data);
  106.    
  107.     return 0;
  108.   }
  109.  
  110. int func_f (int b, int c, int d)
  111.  {
  112.   return ((b & c)|((~b) & d));
  113.  }
  114.  
  115. int func_g (int b, int c, int d)
  116.  {
  117.   return ((b & d)|(c & (~d)));
  118.  }
  119. int func_h (int b, int c, int d)
  120.  {
  121.   return b^c^d;
  122.  }
  123. int func_i (int b, int c, int d)
  124.  {
  125.   return c ^ (b | (~d));
  126.  }
Advertisement
Add Comment
Please, Sign In to add comment