Added function for calculating md5 hash from Data. (#17762)

* Added function for calculating md5 hash from Data.

getFileMD5Hash now use getDataMD5Hash.

For example, AssetsManagerEx VerifyCallback.
Get Data from file, check equal size, if equal to get md5 hash.

* Data::isNull check size equal 0

* Build on linux and android.
This commit is contained in:
Vladimir Perminov 2017-05-04 04:21:22 +03:00 committed by minggo
parent 5c3993836e
commit 973d6aa955
2 changed files with 23 additions and 4 deletions

View File

@ -410,18 +410,28 @@ Node* findChild(Node* levelRoot, int tag)
}
std::string getFileMD5Hash(const std::string &filename)
{
Data data;
FileUtils::getInstance()->getContents(filename, &data);
return getDataMD5Hash(data);
}
std::string getDataMD5Hash(const Data &data)
{
static const unsigned int MD5_DIGEST_LENGTH = 16;
Data d;
FileUtils::getInstance()->getContents(filename, &d);
if (data.isNull())
{
return std::string();
}
md5_state_t state;
md5_byte_t digest[MD5_DIGEST_LENGTH];
char hexOutput[(MD5_DIGEST_LENGTH << 1) + 1] = {0};
char hexOutput[(MD5_DIGEST_LENGTH << 1) + 1] = { 0 };
md5_init(&state);
md5_append(&state, (const md5_byte_t *)d.getBytes(), (int)d.getSize());
md5_append(&state, (const md5_byte_t *)data.getBytes(), (int)data.getSize());
md5_finish(&state, digest);
for (int di = 0; di < 16; ++di)

View File

@ -29,6 +29,7 @@ THE SOFTWARE.
#include <string>
#include "2d/CCNode.h"
#include "base/ccMacros.h"
#include "base/CCData.h"
/** @file ccUtils.h
Misc free functions
@ -170,6 +171,14 @@ namespace utils
* @return The md5 hash for the file
*/
CC_DLL std::string getFileMD5Hash(const std::string &filename);
/**
* Gets the md5 hash for the given buffer.
* @param data The buffer to calculate md5 hash.
* @return The md5 hash for the data
*/
CC_DLL std::string getDataMD5Hash(const Data &data);
}
NS_CC_END