diff --git a/cocos/base/ccUtils.cpp b/cocos/base/ccUtils.cpp index 653e95e7ce..7ccc11d91a 100644 --- a/cocos/base/ccUtils.cpp +++ b/cocos/base/ccUtils.cpp @@ -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) diff --git a/cocos/base/ccUtils.h b/cocos/base/ccUtils.h index 8dfc417e61..d613f96e2e 100644 --- a/cocos/base/ccUtils.h +++ b/cocos/base/ccUtils.h @@ -29,6 +29,7 @@ THE SOFTWARE. #include #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