
 /* This program is free software. It comes without any warranty, to
 * the extent permitted by applicable law. You can redistribute it
 * and/or modify it under the terms of the Do What The Fuck You Want
 * To Public License, Version 2, as published by Sam Hocevar. See
 * http://sam.zoy.org/wtfpl/COPYING for more details. */

// Copyright 2008 Mathias Gottschlag

#ifndef _MD5_H_
#define _MD5_H_

#ifdef __cplusplus
extern "C" {
#endif

/**
 * \brief Struct holding a md5 checksum
 *
 * The proper way to print this checksum is:
 * \code
 * int i;
 * for (i = 0; i < 16; i++)
 * {
 *     printf("%02x", ((unsigned char*)md5.h)[i]);
 * }
 * printf("\n");
 * \endcode
 */
typedef struct md5_t
{
	unsigned int h[4];
} md5_t;

/**
 * \brief Struct holding data needed while creating a md5 checksum
 */
typedef struct md5_tmp_t
{
	md5_t md5;
	char block[64];
	int size;
} md5_tmp_t;

/**
 * \brief Creates a md5 struct and fills it with the initial values
 *
 * The struct can be used for md5_process_data() afterwards.
 *
 * Note that you have to call md5_finish() to get a useful md5 hash as padding
 * has to be done!
 */
md5_tmp_t md5_initialize(void);
/**
 * \brief Adds data to the md5 checksum
 */
void md5_process_data(md5_tmp_t* md5, const void* data, int length);
/**
 * \brief Computes the final md5 checksum
 */
md5_t md5_finish(md5_tmp_t* md5);

/**
 * \brief Creates a checksum from data
 */
md5_t md5_process(const void* data, unsigned int length);

/**
 * \brief Copies the md5 data to a string.
 *
 * s must point to at least 33 bytes of allocated memory!
 */
void md5_to_string(md5_t md5, char* s);

/**
 * \brief Reads in a file and stores the checksum in md5.
 * \return Returns 0 if successful, -1 if not.
 */
int md5_process_file(md5_t *md5, const char* filename);

#ifdef __cplusplus
}
#endif

#endif

