hmac_file.cpp

Voici un exemple de programme permettant d'effectuer le calcul du HMAC d'un fichier. Ce code est générique et permet d'utiliser tous les algorithmes présent dans la librairie. Les éléments fixés ne le sont qu'à titre d'exemple.

/* -----------------------------------------------------------------------
 *
 * Copyright (c) 2002-2005 Mr Ludovic FLAMENT <ludovic.flament@free.fr>, LIEVIN, FRANCE
 *
 * Termes :
 *
 * L'utilisation et/ou la redistribution de ce code (avec ou sans modification),
 * ainsi que de tous les composants du produit (librairies, documentation, exemples, ...)
 * est soumise aux termes de la licence qui vous est attribuée par son auteur.
 *
 * -----------------------------------------------------------------------
 */

#include <stdlib.h>
#include <stdio.h>

#include <include/ecctk-prototype.h>
#include <include/ecctk-hash.h>

void hmac_file()
{
    s2byte  ret     = -1;

    /* set hash algorithm
     */
    s2byte  algo    = ECCTK_HASH_HMAC_ALGO_RMD128;
    
    /* set name of file to hash
     */
    s1byte  file[]  = "file_to_hash";

    /* create output stream with maximum size for the hash value
     */
#ifndef ECCTK_EVALUATION
    u1byte  hash[ECCTK_HASH_SIZE_SHA512];
#else
    u1byte  hash[ECCTK_HASH_SIZE_SHA160];
#endif

    /* set the value of the key
     */
    u1byte  key[] = "This is an example of text key";

    /* the size of hash value, only use to print the result
     */
    u2byte  hashsize;

    switch (algo)
    {
    case ECCTK_HASH_HMAC_ALGO_MD5:
        hashsize = ECCTK_HASH_SIZE_MD5;
        ret = MD5_FileHashMAC(file, key, sizeof(key), hash, hashsize);
        break;
    case ECCTK_HASH_HMAC_ALGO_SHA160:
        hashsize = ECCTK_HASH_SIZE_SHA160;
        ret = SHA160_FileHashMAC(file, key, sizeof(key), hash, hashsize);
        break;
#ifndef ECCTK_EVALUATION
    case ECCTK_HASH_HMAC_ALGO_SHA256:
        hashsize = ECCTK_HASH_SIZE_SHA256;
        ret = SHA256_FileHashMAC(file, key, sizeof(key), hash, hashsize);
        break;
    case ECCTK_HASH_HMAC_ALGO_SHA384:
        hashsize = ECCTK_HASH_SIZE_SHA384;
        ret = SHA384_FileHashMAC(file, key, sizeof(key), hash, hashsize);
        break;
    case ECCTK_HASH_HMAC_ALGO_SHA512:
        hashsize = ECCTK_HASH_SIZE_SHA512;
        ret = SHA512_FileHashMAC(file, key, sizeof(key), hash, hashsize);
        break;
#endif

    case ECCTK_HASH_HMAC_ALGO_RMD128:
        hashsize = ECCTK_HASH_SIZE_RMD128;
        ret = RIPEMD128_FileHashMAC(file, key, sizeof(key), hash, hashsize);
        break;
#ifndef ECCTK_EVALUATION
    case ECCTK_HASH_HMAC_ALGO_RMD160:
        hashsize = ECCTK_HASH_SIZE_RMD160;
        ret = RIPEMD160_FileHashMAC(file, key, sizeof(key), hash, hashsize);
        break;
    case ECCTK_HASH_HMAC_ALGO_RMD256:
        hashsize = ECCTK_HASH_SIZE_RMD256;
        ret = RIPEMD256_FileHashMAC(file, key, sizeof(key), hash, hashsize);
        break;
    case ECCTK_HASH_HMAC_ALGO_RMD320:
        hashsize = ECCTK_HASH_SIZE_RMD320;
        ret = RIPEMD320_FileHashMAC(file, key, sizeof(key), hash, hashsize);
        break;
#endif
    default:
        break;
    }

    if (ret != ECCTK_HASH_NO_ERROR)
        printf("HashMAC %s failed (Error : %d)\n", file, ret);
    else
    {
        u4byte i;

        printf("HashMAC (%s) = ", file);
        for (i = 0; i < hashsize; i++)
            printf("%02x", hash[i]);
        printf("\n");
    }
}


Généré pour ECCTK (Elliptic Curve Cryptographic ToolKit) avec  doxygen