Home    | Products    | Downloads    | Purchase    | Support   

 Products

 Paint Express

 HashCalc

 FSUM

 QuickHash Library

    Download

    Purchase

    Samples

    License Agreement

    Related Links

    F.A.Q.

    Overview

 FastCRC Library

 Company

 About Us

 Contact Us

 Miscellaneous

 Affiliate Program

 Site Map

SlavaSoft QuickHash Library Samples
Sample #7 (QuickHash API - C)  

All

Previous Next


The following sample demonstrates how to use the SHA1 API to calculate the SHA1 message digest for a string.

#include <stdio.h>
#include <string.h>
#include <QuickHash.h>

void ConvertToHex( char* dest, const unsigned char* src, unsigned int count );

int main()
{
    char buff[ 256 ];

    unsigned char digest[ SLC_SHA1_DIGESTSIZE ];
    char          digesthex[ SLC_SHA1_HEXDIGESTSIZE ]; /*0 terminated*/

    unsigned char context[ SLC_SHA1_CONTEXTSIZE ];

    do
    {
        /*****Get the string from the user****************************************/

        printf( "\nEnter a string: " );
        gets( buff );

        printf( "\nDigest for \"%s\":", buff );

        /*****Calculate the digest using CalculateHex*****************************/

        printf( "\nCalculated using CalculateHex:        " );
       
        SL_SHA1_CalculateHex( digesthex, buff, strlen( buff ), 0 );

        printf( "%s", digesthex );

        /*****Calculate the digest using Calculate********************************/

        printf( "\nCalculated using Calculate:           " );

        SL_SHA1_Calculate( digest, buff, strlen( buff ) );

        ConvertToHex( digesthex, digest, SLC_SHA1_DIGESTSIZE );

        printf( "%s", digesthex );

        /*****Initialize the context before calling Update, Final, or FinalHex****/

        SL_SHA1_Init( context );

        /*****Calculate the digest using Update and FinalHex**********************/

        printf( "\nCalculated using Update and FinalHex: " );

        SL_SHA1_Update( context, buff, strlen( buff ) );
        SL_SHA1_FinalHex( context, digesthex, 0 ); /* SL_SHA1_FinalHex reinitializes the context for the next use */

        printf( "%s", digesthex );

        /*****Calculate the digest using Update and Final*************************/

        printf( "\nCalculated using Update and Final:    " );

        SL_SHA1_Update( context, buff, strlen( buff ) );
        SL_SHA1_Final( context, digest ); /* SL_SHA1_Final reinitializes the context for the next use */

        ConvertToHex( digesthex, digest, SLC_SHA1_DIGESTSIZE );

        printf( "%s", digesthex );

        /*****Continue?***********************************************************/

        printf( "\nContinue (Y/N)?" );
        gets( buff );

    }while( *buff == 'y' || *buff == 'Y' );

    return 0;
}

void ConvertToHex( char* dest, const unsigned char* src, unsigned int count )
{
    static char hex[ 16 ] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    unsigned int i = 0;

    for( ; i < count; ++i )
    {
        *dest++ = hex[ *src / 16 ];
        *dest++ = hex[ *src++ % 16 ];
    }

    *dest = '\0';
}
 
  Copyright SlavaSoft Inc. All rights reserved.