mhash - Hash Library
mhash 0.5.0
#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);
Initializing Functions
MHASH mhash_init(hashid type);
Update Functions
int mhash(MHASH thread, const void *plaintext, size_t size);
Finalizing Functions
void *mhash_end(MHASH thread);
Available Hashes
enum hashid { CRC32, MD5, SHA1, HAVAL, RIPEMD160, RIPEMD128, SNEFRU, TIGER, GOST, CRC32B };
typedef enum hashid hashid;
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.
We will describe the API of mhash in detail now. The order follows the one in the SYNOPSIS directly.
This returns the hashid
of the last available hash. Hashes are numbered from 0 to mhash_count()
.
If type exists, this returns the used blocksize of the hash type in bytes. Otherwise, it returns 0.
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.
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.
This updates the context described by thread with plaintext. size is the length of plaintext which may be binary data.
This frees all resources associated with thread and returns the result of the whole hashing operation (the ``digest'').
Hashing STDIN until EOF.
#include <stdio.h> #include <stdlib.h> #include "mhash.h"
int main(void) { int i; MHASH td; unsigned char buffer; unsigned char *hash;
td = mhash_init(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(MD5); i++) { printf ("%.2x", hash[i]); } printf ("\n");
exit(0); }
This library was originally written by Nikos Mavroyanopoulos <nmav@hellur.gr> who passed the project over to Sascha Schumann <ss@2ns.de> in May 1999.
If you find any, please send a bug report (preferrably together with a patch) to the maintainer ss@2ns.de with a detailed description on how to reproduce the bug.
Sascha Schumann <ss@2ns.de>