NAME

mhash - Hash Library


VERSION

mhash 0.6.1


SYNOPSIS

 #include "mhash.h"

 Informative Functions 

 size_t   mhash_count(void);
 size_t   mhash_get_block_size(hashid type);
 char    *mhash_get_hash_name(hashid type);
 size_t   mhash_get_hash_pblock(hashid type);

 Initializing Functions 

 MHASH    mhash_init(hashid type);
 MHASH    hmac_mhash_init(const hashid type, void *key, int keysize, int block);

 Update Functions 

 int      mhash(MHASH thread, const void *plaintext, size_t size);

 Finalizing Functions 

 void    *mhash_end(MHASH thread);
 void    *hmac_mhash_end(MHASH thread);

 Available Hashes 

 enum hashid {
        MHASH_CRC32,
        MHASH_MD5,
        MHASH_SHA1,
        MHASH_HAVAL,
        MHASH_RIPEMD160,
        MHASH_RIPEMD128,
        MHASH_SNEFRU,
        MHASH_TIGER,
        MHASH_GOST,
        MHASH_CRC32B
 };

 typedef enum hashid hashid;


DESCRIPTION

The mhash library provides an easy to use C interface for several hash algorithms (also known as ``one-way'' algorithm). These can be used to create checksums, message digests and more. Currently, MD5, SHA1, GOST, TIGER, RIPE-MD160, HAVAL and several other algorithms are supported.


API FUNCTIONS

We will describe the API of mhash in detail now. The order follows the one in the SYNOPSIS directly.

size_t mhash_count(void);

This returns the hashid of the last available hash. Hashes are numbered from 0 to mhash_count().

size_t mhash_get_block_size(hashid type);

If type exists, this returns the used blocksize of the hash type in bytes. Otherwise, it returns 0.

char *mhash_get_hash_name(hashid type);

If type exists, this returns the name of the hash type. Otherwise, a NULL pointer is returned. The string is allocated with malloc(3) seperately, so do not forget to free(3) it.

size_t mhash_get_hash_pblock(hashid type);

It returns the block size that the algorithm operates. This is used in hmac_mhash_init. If the return value is 0 you shouldn't use that algorithm in HMAC.

MHASH mhash_init(hashid type);

This setups a context to begin hashing using the algorithm type. It returns a descriptor to that context which will result in leaking memory, if you do not call mhash_end(3) later. Returns MHASH_FAILED on failure.

MHASH hmac_mhash_init(const hashid type, void *key, int keysize, int block);

This setups a context to begin hashing using the algorithm type in HMAC mode. HMAC is a mechanism for message authentication using cryptographic hash functions, and is described in rfc2104. key should be a pointer to the key and keysize its len. The block is the block size (in bytes) that hmac o It should be obtained by mhash_get_hash_pblock(). If its 0 it defaults to 64. After calling it you should use mhash() to update the context. It returns a descriptor to that context which will result in leaking memory, if you do not call hmac_mhash_end(3) later. Returns MHASH_FAILED on failure. =item int mhash (MHASH thread, const void *plaintext, size_t size);

This updates the context described by thread with plaintext. size is the length of plaintext which may be binary data.

void *mhash_end(MHASH thread);

This frees all resources associated with thread and returns the result of the whole hashing operation (the ``digest'').

void *hmac_mhash_end(MHASH thread);

This frees all resources associated with thread and returns the result of the whole hashing operation (the ``mac'').


EXAMPLE

Hashing STDIN until EOF.

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

 int main(void) 
 {
        int i;
        MHASH td;
        unsigned char buffer;
        unsigned char *hash;

        td = mhash_init(MHASH_MD5);

        if (td == MHASH_FAILED) exit(1);

        while (fread(&buffer, 1, 1, stdin) == 1) {
                mhash(td, &buffer, 1);
        }

        hash = mhash_end(td);

        printf("Hash:");
        for (i = 0; i < mhash_get_block_size(MHASH_MD5); i++) {
                printf("%.2x", hash[i]);
        }
        printf("\n");

        exit(0);
 }


EXAMPLE

An example program using HMAC:

 #include <mhash.h>
 #include <stdio.h>

 int main()
 {

        char password[] = "Jefe";
        int keylen = 4;
        char data[] = "what do ya want for nothing?";
        int datalen = 28;
        MHASH td;
        unsigned char *mac;
        int j;
 
        td = hmac_mhash_init(MHASH_MD5, password, keylen,
                            mhash_get_hash_pblock(MHASH_MD5));

        mhash(td, data, datalen);
        mac = hmac_mhash_end(td);

 /* 
  * The output should be 0x750c783e6ab0b503eaa86e310a5db738
  * according to RFC 2104.
  */

        printf("0x");
        for (j = 0; j < mhash_get_block_size(MHASH_MD5); j++) {
                printf("%.2x", mac[j]);
        }
        printf("\n");
        
        exit(0);
 }
 


HISTORY

This library was originally written by Nikos Mavroyanopoulos <nmav@hellug.gr> who passed the project over to Sascha Schumann <sascha@schumann.cx> in May 1999.


BUGS

If you find any, please send a bug report (preferrably together with a patch) to the maintainer Sascha Schumann <sascha@schumann.cx> with a detailed description on how to reproduce the bug.


AUTHOR

Sascha Schumann <sascha@schumann.cx>