QuickHash Library API

The following sample demonstrates how to use the HMAC API to calculate the HMAC for a string using RIPEMD320 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_RIPEMD320_DIGESTSIZE ];
    char          hmachex[ SLC_RIPEMD320_HEXDIGESTSIZE ]; /*0 terminated*/

    unsigned char context[ SLC_HMAC_CONTEXTSIZE( SLC_RIPEMD320_CONTEXTSIZE, SLC_RIPEMD320_BLOCKSIZE ) ];

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

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

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

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

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

        printf( "\nCalculated using CalculateHex:        " );
        
        SL_HMAC_CalculateHex( SLC_RIPEMD320_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_RIPEMD320_ALGID, hmac, buff, strlen( buff ), key, strlen( key ) );

        ConvertToHex( hmachex, hmac, SLC_RIPEMD320_DIGESTSIZE );

        printf( "%s", hmachex );

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

        SL_HMAC_Init( context, SLC_RIPEMD320_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_RIPEMD320_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';
}


 

 

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