SHA1 example code with openssl




SHA1 :

what is SHA1:

SHA1 stands for  secure hash algorithm 1, message-digest algorithm with hash value of 20 bytes(160bit).
It  takes input message of any length  < 2^64 bits and outputs a message digest value of 160 bits.



RFC link:  http://tools.ietf.org/html/rfc3174

Designed by :  National Security Agency


Calculation SHA1 for a string.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "openssl\sha.h"
#pragma comment(lib,"libeay32.lib")
#pragma comment(lib,"ssleay32.lib")
int main()
{
    printf(" first approach \n");
    unsigned char digest[SHA_DIGEST_LENGTH];   


// SHA_DIGEST_LENGTH is 20
    char demuo[]="hello";
    SHA1((unsigned char *)&demo,strlen(demo),(unsigned char *)&digest);
   
    printf(" Hash Value for SHA1 : ");
    for (int i = 0 ; i < SHA_DIGEST_LENGTH;i++)
        printf("%02x", digest[i]);

    printf("\n second approach \n");

    unsigned char digestFunction[SHA_DIGEST_LENGTH];   
    SHA_CTX cx;
    SHA1_Init(&cx);
    SHA1_Update(&cx,(unsigned char *)demo,strlen(demo));
    SHA1_Final(digestFunction,&cx);

    printf(" Hash Value for SHA1 : ");
    for (int i = 0 ; i < SHA_DIGEST_LENGTH;i++)
        printf("%02x", digestFunction[i]);


    return 0;
}



Output:

 first approach
 Hash Value for SHA1 : aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
 second approach
 Hash Value for SHA1 : aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d

MD5 example code with openssl



MD5:

what is MD5:

MD5 message-digest algorithm with hash value of 16 bytes(128 bit).

RFC link: http://tools.ietf.org/html/rfc1321

Designed by : R. Rivest

Calculation MD5 for a string.

#include <stdio.h>

#include <string.h>

#include "openssl/md5.h"

#pragma comment(lib,"libeay32.lib")

#pragma comment(lib,"ssleay32.lib")

// 16 bytes =  16 * 8 ( 128 bits)

#define PRINT_HASH 33

int main()

{

    unsigned char digest[MD5_DIGEST_LENGTH];

    char string[] = "blogger";

  

    MD5((unsigned char*)&string, strlen(string), (unsigned char*)&digest);  

    printf("the function prints  %s  \t  unreadable value\n",digest);

    char convertedString[PRINT_HASH];

    printf("prints hash value in unsigned decimal\n");

    for(int i = 0; i < MD5_DIGEST_LENGTH; i++)

        printf("%u ",(unsigned int)digest[i]);

    printf("\n");

    printf(" Hexa Demical 16 byte hash value  for string %s  is : ",string);

     for (int i = 0; i < 16; i++)

     printf ("%02x", digest[i]);

  

    return 0;

}

compiling openssl in windows

Following are steps to compile openssl on windows.

Install active perl
download the openssl source and extract it. source code can be downloaded from https://www.openssl.org/source/


To compile openssl in 32 bit.
open command prompt
run vcvars32.bat in the command prompt. typical location of it (C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin)

perl Configure VC-WIN32 --prefix=C:\openssl32 no-asm
ms\do_ms
nmake -f ms\nt.mak
nmake -f ms\nt.mak install


To Compile openssl in 32 for Debug library

Open the Visual Studio Command Prompt (2010)

perl Configure debug-VC-WIN32 --prefix=C:\openssl32_debug no-asm
ms\do_ms
nmake -f ms\nt.mak
nmake -f ms\nt.mak install



Common Errors:

unresolved external symbol SSL_load_error_strings windows
unresolved external symbol SSL_library_init windows

While writing sample programs or using openssl library. above errors might come.

One reason for this errors, if perl Configure command is run without no-asm argument.

Second reason would be, wrong linking like 32 bit libraries are linked to 64 bit libraries or debug libraries are used with release libraries.