QuickHash Library API

SL_HMAC_FinalHex

int SL_HASHCALL SL_HMAC_FinalHex( void* pContext, void* pDest, int bUpper );

Return value

Nonzero if the function succeeds, otherwise 0. In particular, this function returns 0 when the context was improperly initialized.

Parameters

pContext

    [in/out] Pointer to the context.

 pDest

    [out] Pointer to the text buffer that will receive the HMAC.

 bUpper

    [in] Uppercase flag. If bUpper is 0, the received HMAC represents a lowercase string, otherwise it represents an uppercase string.

Remarks

Call this function to retrieve the HMAC from the context pointed by pContext. The HMAC is retrieved as a null-terminated hexadecimal string in the text buffer pointed by pDest. The size of the text buffer pointed by pDest must be at least HEXDIGESTSIZE characters. HEXDIGESTSIZE is a predefined constant that specifies the size of the hexadecimal string representation of the HMAC for a specific hash algorithm. For example, you have to allocate at least SLC_SHA512_HEXDIGESTSIZE characters for the text buffer pointed by pDest if you need to calculate the HMAC using SHA-512 hash algorithm.

All supported hash algorithms with their corresponding HEXDIGESTSIZEs are listed in Table1.

After the SL_HMAC_FinalHex function  is performed, the context is initialized for new calculations, as it would be called SL_HMAC_Init again.

Table1. Supported hash algorithms and their HEXDIGESTSIZEs.

Algorithm Name Algorithm HEXDIGESTSIZE
MD4 SLC_MD4_HEXDIGESTSIZE
MD5 SLC_MD5_HEXDIGESTSIZE
SHA-1 SLC_SHA1_HEXDIGESTSIZE
SHA-256 SLC_SHA256_HEXDIGESTSIZE
SHA-512 SLC_SHA512_HEXDIGESTSIZE
SHA-384 SLC_SHA384_HEXDIGESTSIZE
RIPEMD128 SLC_RIPEMD128_HEXDIGESTSIZE
RIPEMD160 SLC_RIPEMD160_HEXDIGESTSIZE
RIPEMD256 SLC_RIPEMD256_HEXDIGESTSIZE
RIPEMD320 SLC_RIPEMD320_HEXDIGESTSIZE
PANAMA SLC_PANAMA_HEXDIGESTSIZE
TIGER SLC_TIGER_HEXDIGESTSIZE

 

Example

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

#define BUFF_SIZE  1024

int main()
{
    FILE* file;
    
    unsigned char buff[ BUFF_SIZE ];
    char key[] = "File Password";

    unsigned char context[ SLC_HMAC_CONTEXTSIZE( SLC_SHA1_CONTEXTSIZE, SLC_SHA1_BLOCKSIZE ) ];
    char hmachex[ SLC_SHA1_HEXDIGESTSIZE ]; /*0 terminated*/

    file = fopen( "c:\\test.txt", "rb" );

    if( file == NULL )
        return 1;

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

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

    /*****Calculate the HMAC by calling Update for each block of the file*****/

    while( !feof( file ) )
    {
        unsigned int nCount = fread( buff, sizeof( char ), BUFF_SIZE, file );
        SL_HMAC_Update( context, buff, nCount );
    }

    /*****Do final changes and get the SHA1 HMAC in hex format****************/

    SL_HMAC_FinalHex( context, hmachex, 0 );

    /*****Use the HMAC********************************************************/
    /*...*/

    fclose( file );

    return 0;
}

 

HMAC API Overview   |   HMAC API Functions   |   Useful Links   |   HashCalc

See Also    SL_HMAC_Final, SL_HMAC_Update, SL_HMAC_UpdateStr, SL_HMAC_Init, SL_HMAC_InitKeyStr


 

 

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