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 #9 (QuickHash API - C)  

All

Previous Next


The following sample demonstrates how to use the ADLER32 API to calculate the ADLER32 checksum 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 checksum[ SLC_ADLER32_DIGESTSIZE ];
    char          checksumhex[ SLC_ADLER32_HEXDIGESTSIZE ]; /*0 terminated*/

    unsigned char context[ SLC_ADLER32_CONTEXTSIZE ];

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

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

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

        /*****Calculate the checksum using CalculateHex*****************************/

        printf( "\nCalculated using CalculateHex:        " );
       
        SL_ADLER32_CalculateHex( checksumhex, buff, strlen( buff ), 0 );

        printf( "%s", checksumhex );

        /*****Calculate the checksum using Calculate********************************/

        printf( "\nCalculated using Calculate:           " );

        SL_ADLER32_Calculate( checksum, buff, strlen( buff ) );

        ConvertToHex( checksumhex, checksum, SLC_ADLER32_DIGESTSIZE );

        printf( "%s", checksumhex );

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

        SL_ADLER32_Init( context );

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

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

        SL_ADLER32_Update( context, buff, strlen( buff ) );
        SL_ADLER32_FinalHex( context, checksumhex, 0 ); /* SL_ADLER32_FinalHex reinitializes the context for the next use */

        printf( "%s", checksumhex );

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

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

        SL_ADLER32_Update( context, buff, strlen( buff ) );
        SL_ADLER32_Final( context, checksum ); /* SL_ADLER32_Final reinitializes the context for the next use */

        ConvertToHex( checksumhex, checksum, SLC_ADLER32_DIGESTSIZE );

        printf( "%s", checksumhex );

        /*****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.