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:

 

Tuesday Dec 05, 2006

what i learnt today :encrypt

Today :

I came across a utilty ,encrypt for encrypting files.

 encrypt: usage: encrypt -l | -a <algorithm> [-k <keyfile>] [-i <infile>]
                        [-o <outfile>]
This comes with the Solaris package SUNWcsu .

These are the algorithms it supports :

#encrypt -l
Algorithm       Keysize:  Min   Max (bits)
------------------------------------------
aes                       128   128
arcfour                   8    128
des                        64     64
3des                     192  192
 

Friday Dec 01, 2006

Getting the output of a shell command from a C program using popen

 Sometimes its necessary to access the output of a shell command(more than just the return value) in a C program. One way could be to redirect it to a file and then access it .The other would be by using the popen function.

#include<stdio.h>

main(){
  char  cmd[80];
  FILE *fptr;
  char out[256];
  int ret;
  strcpy(cmd,"ls -l");
  fptr = popen(cmd, "r");
  while(1){
        fgets(out, 256, fptr);
        if(feof(fptr)) break;
        puts(out);
  }
 ret = pclose(fptr);
}


/* Noted tested with S10 gcc only ..*/


Archives
Links
Referrers