dfhack/library/depends/md5/md5wrapper.cpp

168 lines
3.2 KiB
C++

2009-09-13 18:02:46 -06:00
/*
* This is part of my wrapper-class to create
* a MD5 Hash from a string and a file.
*
* This code is completly free, you
* can copy it, modify it, or do
* what ever you want with it.
*
* Feb. 2005
* Benjamin Grüdelbach
*/
/*
* Changed unsigned long int types into uint32_t to make this work on 64bit systems.
* Sep. 5. 2009
* Petr Mrázek
*/
#if defined(_MSC_VER) && _MSC_VER >= 1400
#define _CRT_SECURE_NO_WARNINGS
#endif
2009-09-13 18:02:46 -06:00
//----------------------------------------------------------------------
//basic includes
#include <fstream>
#include <iostream>
2011-11-01 06:06:27 -06:00
#include <errno.h>
#include <string.h>
2009-09-13 18:02:46 -06:00
//my includes
#include "md5wrapper.h"
#include "md5.h"
//---------privates--------------------------
/*
* internal hash function, calling
* the basic methods from md5.h
*/
std::string md5wrapper::hashit(std::string text)
{
MD5Context ctx;
2009-09-13 18:02:46 -06:00
//init md5
MD5Init(&ctx);
2009-09-13 18:02:46 -06:00
//update with our string
MD5Update(&ctx,
2009-09-13 18:02:46 -06:00
(unsigned char*)text.c_str(),
text.length());
//create the hash
unsigned char buff[16] = "";
MD5Final((unsigned char*)buff,&ctx);
2009-09-13 18:02:46 -06:00
//converte the hash to a string and return it
return convToString(buff);
}
/*
* converts the numeric hash to
* a valid std::string.
* (based on Jim Howard's code;
* http://www.codeproject.com/cpp/cmd5.asp)
*/
std::string md5wrapper::convToString(unsigned char *bytes)
{
char asciihash[33];
int p = 0;
for(int i=0; i<16; i++)
{
::sprintf(&asciihash[p],"%02x",bytes[i]);
p += 2;
}
asciihash[32] = '\0';
return std::string(asciihash);
}
//---------publics--------------------------
//constructor
md5wrapper::md5wrapper()
{
}
//destructor
md5wrapper::~md5wrapper()
{
}
/*
* creates a MD5 hash from
* "text" and returns it as
* string
*/
std::string md5wrapper::getHashFromString(std::string text)
{
return this->hashit(text);
}
/*
* creates a MD5 hash from
* a file specified in "filename" and
* returns it as string
* (based on Ronald L. Rivest's code
* from RFC1321 "The MD5 Message-Digest Algorithm")
*/
2011-11-02 19:40:49 -06:00
std::string md5wrapper::getHashFromFile(std::string filename, uint32_t & length, char * first_kb)
2009-09-13 18:02:46 -06:00
{
FILE *file;
MD5Context context;
int len;
2011-11-02 19:40:49 -06:00
int saved = 0;
unsigned char buffer[1024], digest[16];
//open file
if ((file = fopen (filename.c_str(), "rb")) == NULL)
{
2011-11-01 06:06:27 -06:00
return "file unreadable.";
}
2011-11-02 19:40:49 -06:00
length = 0;
//init md5
MD5Init (&context);
//read the filecontent
2011-11-01 06:06:27 -06:00
while (1)
{
2011-11-01 06:06:27 -06:00
errno = 0;
len = fread (buffer, 1, 1024, file);
2011-11-02 19:40:49 -06:00
if(saved < 1024 && first_kb)
{
memcpy(first_kb + saved, buffer, std::min (len, 1024 - saved));
saved += len;
}
length += len;
2011-11-01 06:06:27 -06:00
if(len != 1024)
{
int err = ferror(file);
//FIXME: check errno here.
if(err)
{
fclose(file);
return strerror(err);
}
if(feof(file))
{
MD5Update (&context, buffer, len);
2011-11-01 06:06:27 -06:00
break;
}
}
MD5Update (&context, buffer, len);
}
/*
generate hash, close the file and return the
hash as std::string
*/
MD5Final (digest, &context);
fclose (file);
return convToString(digest);
2009-09-13 18:02:46 -06:00
}
/*
* EOF
*/