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

All

Previous Next


The following sample demonstrates how to use the HMAC API to calculate the HMAC for a string using RIPEMD160 hash algorithm.

#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 ];
    char key[ 256 ];

    unsigned char hmac[ SLC_RIPEMD160_DIGESTSIZE ];
    char          hmachex[ SLC_RIPEMD160_HEXDIGESTSIZE ]; /*0 terminated*/

    unsigned char context[ SLC_HMAC_CONTEXTSIZE( SLC_RIPEMD160_CONTEXTSIZE, SLC_RIPEMD160_BLOCKSIZE ) ];

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

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

        printf( "\nEnter a key: " );
        gets( key );

        printf( "\nRIPEMD160 HMAC for \"%s\" with key \"%s\": ", buff, key );

        /*****Calculate the HMAC Using CalculateHex*******************************/

        printf( "\nCalculated using CalculateHex:        " );
       
        SL_HMAC_CalculateHex( SLC_RIPEMD160_ALGID, hmachex, buff, strlen( buff ), key, strlen( key ), 0 );

        printf( "%s", hmachex );

        /*****Calculate the HMAC Using Calculate**********************************/

        printf( "\nCalculated using Calculate:           " );

        SL_HMAC_Calculate( SLC_RIPEMD160_ALGID, hmac, buff, strlen( buff ), key, strlen( key ) );

        ConvertToHex( hmachex, hmac, SLC_RIPEMD160_DIGESTSIZE );

        printf( "%s", hmachex );

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

        SL_HMAC_Init( context, SLC_RIPEMD160_ALGID, key, strlen( key ) );

        /*****Calculate the HMAC Using Update and FinalHex************************/

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

        SL_HMAC_Update( context, buff, strlen( buff ) );
        SL_HMAC_FinalHex( context, hmachex, 0 );/* SL_HMAC_FinalHex reinitializes the context for the next use */

        printf( "%s", hmachex );

        /*****Calculate the HMAC Using Update and Final***************************/

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

        SL_HMAC_Update( context, buff, strlen( buff ) );
        SL_HMAC_Final( context, hmac );/* SL_HMAC_Final reinitializes the context for the next use */

        ConvertToHex( hmachex, hmac, SLC_RIPEMD160_DIGESTSIZE );

        printf( "%s", hmachex );

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