Dakshina`s Blog

My views..
Sunday Dec 10, 2006

Aaj ki seekh: MD5 api

The MD5 algo takes as input a messge of arbitrary length and
produces a 128-bit message digest or fingerprint of the input. 

For more info on MD5 refer to the RFC page

http://www.ietf.org/rfc/rfc1321.txt

Just a simple program to show usage of  MD5 API to calculate a message digest. 

#include <stdio.h>
#include <md5.h>

/* Link  with the -lmd5 library */
/* gcc -lmd5 <program_name>  */

/* this program generates a 128 bit MD5 digest of a given string */

main(){

  int i=0;
  unsigned char *buffer="We shall generate a message digest of this string";
  unsigned char *output=(unsigned char *)malloc(16*sizeof(char))  ; /* store the 128 bit digest in this string */

  printf("\n-Using the md5_calc functions ----\n");
  md5_calc(output, buffer,strlen(buffer));
  for(i=0;i<16;i++)
  printf("%d:",(int)(output[i]));

  printf("\n-Using the MD5 functions ----\n");
  MD5_CTX context;
  MD5Init(&context);
  MD5Update(&context,buffer,strlen(buffer));
  MD5Final(output,&context);
  for(i=0;i<16;i++){
      printf("%d:",(int)(output[i]));
  }
   free(output);
}

 
The output: (the digest is displayed as colon seperated )

--Using the md5_calc functions ----
208:123:118:146:90:32:215:98:10:11:23:26:78:229:127:76:
-Using the MD5 functions ----
208:123:118:146:90:32:215:98:10:11:23:26:78:229:127:76:

 

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed

Archives
Links
Referrers