QuickHash Library API

The following sample demonstrates how to use the CRC16C API to calculate the CRC16C 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_CRC16C_DIGESTSIZE ];
    char          checksumhex[ SLC_CRC16C_HEXDIGESTSIZE ]; /*0 terminated*/

    unsigned char context[ SLC_CRC16C_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_CRC16C_CalculateHex( checksumhex, buff, strlen( buff ), 0 );

        printf( "%s", checksumhex );

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

        printf( "\nCalculated using Calculate:           " );

        SL_CRC16C_Calculate( checksum, buff, strlen( buff ) );

        ConvertToHex( checksumhex, checksum, SLC_CRC16C_DIGESTSIZE );

        printf( "%s", checksumhex );

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

        SL_CRC16C_Init( context );

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

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

        SL_CRC16C_Update( context, buff, strlen( buff ) );
        SL_CRC16C_FinalHex( context, checksumhex, 0 ); /* SL_CRC16C_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_CRC16C_Update( context, buff, strlen( buff ) );
        SL_CRC16C_Final( context, checksum ); /* SL_CRC16C_Final reinitializes the context for the next use */

        ConvertToHex( checksumhex, checksum, SLC_CRC16C_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';
}

        

        

 


 

 

Send Feedback to SlavaSoft Inc. Tell a friend about QuickHash Library