#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); hashid mhash_get_mhash_algo( MHASH);
Key Generation Functions
int mhash_keygen_ext(keygenid algorithm, KEYGEN algorithm_data, void* keyword, int keysize, unsigned char* password, int passwordlen);
Initializing Functions
MHASH mhash_init(hashid type); MHASH mhash_hmac_init(const hashid type, void *key, int keysize, int block); MHASH mhash_cp( MHASH);
Update Functions
int mhash(MHASH thread, const void *plaintext, size_t size);
Save/Restore Functions
int mhash_save_state_mem(MHASH thread, void *mem, int* mem_size ); MHASH mhash_restore_state_mem(void* mem);
Finalizing Functions
void mhash_deinit(MHASH thread, void *result); void *mhash_end(MHASH thread); void *mhash_end_m(MHASH thread, void* (*hash_malloc)(size_t));
void *mhash_hmac_end(MHASH thread); void *mhash_hmac_end_m(MHASH thread, void* (*hash_malloc)(size_t)); int mhash_hmac_deinit(MHASH thread, void *result);
Available Hashes
CRC32: The crc32 algorithm is used to compute checksums. The two variants used in mhash are: MHASH_CRC32 (like the one used in ethernet) and MHASH_CRC32B (like the one used in ZIP programs).
ADLER32: The adler32 algorithm is used to compute checksums. It is faster than CRC32 and it is considered to be as reliable as CRC32. This algorithm is defined as MHASH_ADLER32.
MD5: The MD5 algorithm by Ron Rivest and RSA. In mhash this algorithm is defined as MHASH_MD5.
MD4: The MD4 algorithm by Ron Rivest and RSA. This algorithm is considered broken, so don't use it. In mhash this algorithm is defined as MHASH_MD4.
SHA1/SHA256: The SHA algorithm by US. NIST/NSA. This algorithm is specified for use in the NIST's Digital Signature Standard. In mhash these algorithm are defined as MHASH_SHA1 and MHASH_SHA256.
HAVAL: HAVAL is a one-way hashing algorithm with variable length of output. HAVAL is a modification of MD5. Defined in mhash as: MHASH_HAVAL256, MHASH_HAVAL192, MHASH_HAVAL160, MHASH_HAVAL128.
RIPEMD160:). In mhash this algorithm is defined as MHASH_RIPEMD160.
TIGER: Tiger is a fast hash function, by Eli Biham and Ross Anderson. Tiger was designed to be very fast on modern computers, and in particular on the state-of-the-art 64-bit computers, while it is still not slower than other suggested hash functions on 32-bit machines. In mhash this algorithm is defined as: MHASH_TIGER, MHASH_TIGER160, MHASH_TIGER128.
GOST: GOST algorithm is a russian standard and it uses the GOST encryption algorithm to produce a 256 bit hash value. This algorithm is specified for use in the Russian Digital Signature Standard. In mhash this algorithm is defined as MHASH_GOST.
Available Key Generation algorithms
KEYGEN_MCRYPT: The key generator used in mcrypt.
KEYGEN_ASIS: Just returns the password as binary key.
KEYGEN_HEX: Just converts a hex key into a binary one.
KEYGEN_PKDES: The transformation used in Phil Karn's DES encryption program.
KEYGEN_S2K_SIMPLE: The OpenPGP (rfc2440) Simple S2K.
KEYGEN_S2K_SALTED: The OpenPGP Salted S2K.
KEYGEN_S2K_ISALTED: The OpenPGP Iterated Salted S2K.
The algorithm(s) specified in algorithm_data.hash_algorithm, should be hash algorithms and may be used by the key generation algorithm. Some key generation algorithms may use more than one hash algorithms (view also mhash_keygen_uses_hash_algorithm()). If it is desirable (and supported by the algorithm, eg. KEYGEN_S2K_SALTED) a salt may be specified in algorithm_data.salt of size algorithm_data.salt_size or may be NULL.
The algorithm may use the algorithm_data.count internally (eg. KEYGEN_S2K_ISALTED). The generated keyword is stored in keyword, which should be (at least) keysize bytes long. The generated keyword is a binary one. Returns a negative number on failure.
#include <mhash.h> #include <stdio.h> #include <stdlib.h>
int main(void) { int i; MHASH td; unsigned char buffer; unsigned char hash[16]; /* enough size for MD5 */
td = mhash_init(MHASH_MD5);
if (td == MHASH_FAILED) exit(1);
while (fread(&buffer, 1, 1, stdin) == 1) { mhash(td, &buffer, 1); }
mhash_deinit(td, hash);
printf("Hash:"); for (i = 0; i < mhash_get_block_size(MHASH_MD5); i++) { printf("%.2x", hash[i]); } printf("\n");
exit(0); }
#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[16]; int j;
td = mhash_hmac_init(MHASH_MD5, password, keylen, mhash_get_hash_pblock(MHASH_MD5));
mhash(td, data, datalen); mhash_hmac_deinit(td, mac);
/* * 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); }