Another simple hash function

Another simple example:

/* 
   Simple Hash Example 
   CG - 15112009
*/      

#include <stdio.h>

#define L 32     //the length of the message is 32 bit
#define N 8      //the length of the message block is 8 bit 

int main(){
   char M[L] = {1,0,1,0,1,0,1,0,1,0,0,1,0,1,1,0,1,1,0,0,1,1,0,0,1,1,1,1,0,0,1,1};
   int x = L/N;
   int i, j, k;

   char m[x][N];   
   int c[N];

   j = 0;
   k = 0;
   for (i = 0; i < L; i++){
      m[j][k] = M[i];
//      printf("\nm[%d][%d] = %d", j, k, m[j][k]);
      k++;
      if ((i != 0) && ((i % N) == 0)){
         j++;
         k = 0;
      }
   }   
   
   printf("\nM = ");
   for (i = 0; i < L; i++)
      printf("%d ", M[i]);

   for (j = 0; j < x; j++)
      for (k = 0; k < N; k++){
         printf("\nm[%d][%d] = %d", j, k, m[j][k]);
      }
   printf("\n");

   for (i = 0; i < N; i++)
      c[i] = 0;

   for (k = 0; k < N; k++)
      for (j = 0; j < x; j++)
         c[k] = c[k] ^ m[j][k];
     
   printf("\nThe N-bit hash code : \n");
   for (i = 0; i < N; i++)
      printf("%d ", c[i]);
 
   printf("\n");
}

The result is:


M = 1 0 1 0 1 0 1 0 1 0 0 1 0 1 1 0 1 1 0 0 1 1 0 0 1 1 1 1 0 0 1 1
m[0][0] = 1
m[0][1] = 0
m[0][2] = 1
m[0][3] = 0
m[0][4] = 1
m[0][5] = 0
m[0][6] = 1
m[0][7] = 0
m[1][0] = 0
m[1][1] = 0
m[1][2] = 1
m[1][3] = 0
m[1][4] = 1
m[1][5] = 1
m[1][6] = 0
m[1][7] = 1
m[2][0] = 1
m[2][1] = 0
m[2][2] = 0
m[2][3] = 1
m[2][4] = 1
m[2][5] = 0
m[2][6] = 0
m[2][7] = 1
m[3][0] = 1
m[3][1] = 1
m[3][2] = 1
m[3][3] = 0
m[3][4] = 0
m[3][5] = 1
m[3][6] = 1
m[3][7] = 0

The N-bit hash code :
1 1 1 1 1 0 0 0

Advertisements