#include <stdio.h>
#include <string.h>
#include <crypt.h>
#include <stdlib.h>
#include <time.h>

/* hash src and place the result to dst */
void chash(char *dst, const char *src)
{
  /* salt is set to ".." and we take chars 2..6 of the result */
  memcpy(dst, crypt(src, "..") + 2, 4);
  dst[4] = 0;
}

/* find collisions */

int main(int argc, char **argv)
{
  int steps;
  char a[8], b[8];
  char x[8], y[8];
  char o[10];

  /* "random" start point */
  sprintf(o, "%04x", (unsigned) (time(NULL) & 0xffff));  
  strcpy(a, o);
  strcpy(b, o); 

  steps = 0;        /* step counter */
  do 
    {
      chash(a, a);  /* one step */
      chash(b, b);  /* two steps */
      chash(b, b);
      steps += 3;   /* three invocations */
    }
  while (strcmp(a,b) != 0);
  
  strcpy(b, o);     /* reset o */ 
  do 
    {
      strcpy(x, a); /* backup */
      strcpy(y, b);
      chash(a, a);  /* one step */
      chash(b, b);  /* one step */
      steps += 2;   /* two invocations */
    }
  while (strcmp(a,b) != 0);

  printf("steps = %d\n", steps);
  printf("H(%s) = %s\n", x, a);
  printf("H(%s) = %s\n", y, b);

  return 0;
}

