#ifndef HashMD5_class
#define HashMD5_class

#ifdef WIN32
       #define DLL true
#endif

#include <openssl/evp.h>
#include "Hash.h"


using namespace std;

class HashMD5 : public Hash {


	private:
		EVP_MD_CTX mdctx;
		const EVP_MD *md;
		unsigned char md_value[EVP_MAX_MD_SIZE];
		unsigned int md_len, i;
		char* vTmp;

	public:
		HashMD5();
		~HashMD5();
		virtual void Compute(string);
};


HashMD5::HashMD5(){
	salt="";
	vTmp=new char[2];
	OpenSSL_add_all_digests();
	md = EVP_get_digestbyname("MD5"); 
}


HashMD5::~HashMD5(){
	delete vTmp;
	delete this;
}


void HashMD5::Compute(string pass){
	this->password=pass;
	EVP_MD_CTX_init(&mdctx);
	EVP_DigestInit_ex(&mdctx, md, NULL);
	EVP_DigestUpdate(&mdctx, pass.c_str(), pass.length());
	EVP_DigestFinal_ex(&mdctx, md_value, &md_len);
	EVP_MD_CTX_cleanup(&mdctx);
	this->hash="";
	for(i = 0; i < md_len; i++) {
		sprintf(vTmp,"%02x",md_value[i]);
		this->hash+=vTmp;
	}
}

	

// the class factories
EXPORT Hash* create() {
    	return new HashMD5;
}

EXPORT void destroy(Hash* p) {
	delete p;
}



#endif
