Thursday, May 31, 2012

Calculate SHA Digest

Digest is a practical technique to verify that a file is not corrupted. Digest is a fixed length of string which is a result of hash on blocks of input message. Depending on the hash technique, the digest length varies.

There are various types of standards of calculating digest. For example, MD2,MD4,MD5,SHA1,SHA..etc.
MD5 generates 16bytes of Digest where as SHA digest is of 20bytes(160 bits) long.

We have seen how to generate MD5 digest using c program and openSSL command in last article. Read Calculate MD5 Digest.

In this post, we will see how to generate SHA digest using c program and openSSL command line interface.

Lets see how to calculate SHA digest. SHA is abbreviated as "Secured Hash Algorithm."
This program is written using most popular security and cryptography library called openssl. openssl provides various techniques to generate digest. Following APIs are available to generate SHA Digest on given string. Header file openssl/sha.h should be included which is located at /usr/include/openssl.



#include <openssl/sha.h>

unsigned char *SHA1(const unsigned char *d, unsigned long n,unsigned char *md);
 int SHA1_Init(SHA_CTX *c);
 int SHA1_Update(SHA_CTX *c, const void *data,unsigned long len);
 int SHA1_Final(unsigned char *md, SHA_CTX *c);


#include <openssl/sha.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>


#define MAX_BUF_LEN  (128)


int main(int argc, char *argv[])
{
  unsigned char buf[MAX_BUF_LEN];
  int fd=0;
  int len,i=0;


  SHA_CTX c;
  unsigned char hash[SHA_DIGEST_LENGTH];


  /* File name is required */
  if( argc < 2 ) {
     fprintf(stderr,"Usage: %s <filename>\n",argv[0]);
      return 1;
  }


  fd=open(argv[1],O_RDONLY);
  if( fd < 0 ){
      fprintf(stderr,"ERROR: Unable to open file '%s'\n",argv[1]);
      return 1;
  }


  SHA_Init(&c);


  while ( len=read(fd,buf,MAX_BUF_LEN) )
      SHA_Update(&c,buf,len);


  SHA_Final(hash,&c);


  fprintf(stdout,"SHA(%s)= ",argv[1]);
  while ( i<SHA_DIGEST_LENGTH) {
     fprintf(stdout,"%02x",(unsigned int)hash[i]);
     i++;
  }


  printf("\n");


  close(fd);


}




A test file is used here on which sha digest is calculated and it is compared with the openssl sha digest at the end.

prompt# cat test
This is a test program by ncooltips.com
prompt#
prompt # gcc sha.c -lssl -o sha
prompt # ./sha test
SHA(test)= 98c1fd2b967f1a9c87048145bef49a25ea506a1c



Calculate SHA using openssl command, and compare. Both are same.



prompt # openssl sha test
SHA(test)= 98c1fd2b967f1a9c87048145bef49a25ea506a1c



Lets modify the content of the file, added ";" at the end of the file and see what will be digest.

prompt# vi test

prompt# cat test  
This is a test program by ncooltips.com;

prompt # ./sha test
SHA(test)= e76535db78e9a3e04b22e55052f6ffe97412aaa3
prompt # openssl sha test
SHA(test)= e76535db78e9a3e04b22e55052f6ffe97412aaa3



The digest verified.

If you observer the length of the digest , it is 20bytes as the output is in hex-decimal.

Please leave your comments.
Pin It
Related Posts Plugin for WordPress, Blogger...