Skip to content

Commit 973d6aa

Browse files
perminovVSminggo
authored andcommitted
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.
1 parent 5c39938 commit 973d6aa

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

cocos/base/ccUtils.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,18 +410,28 @@ Node* findChild(Node* levelRoot, int tag)
410410
}
411411

412412
std::string getFileMD5Hash(const std::string &filename)
413+
{
414+
Data data;
415+
FileUtils::getInstance()->getContents(filename, &data);
416+
417+
return getDataMD5Hash(data);
418+
}
419+
420+
std::string getDataMD5Hash(const Data &data)
413421
{
414422
static const unsigned int MD5_DIGEST_LENGTH = 16;
415423

416-
Data d;
417-
FileUtils::getInstance()->getContents(filename, &d);
424+
if (data.isNull())
425+
{
426+
return std::string();
427+
}
418428

419429
md5_state_t state;
420430
md5_byte_t digest[MD5_DIGEST_LENGTH];
421-
char hexOutput[(MD5_DIGEST_LENGTH << 1) + 1] = {0};
431+
char hexOutput[(MD5_DIGEST_LENGTH << 1) + 1] = { 0 };
422432

423433
md5_init(&state);
424-
md5_append(&state, (const md5_byte_t *)d.getBytes(), (int)d.getSize());
434+
md5_append(&state, (const md5_byte_t *)data.getBytes(), (int)data.getSize());
425435
md5_finish(&state, digest);
426436

427437
for (int di = 0; di < 16; ++di)

cocos/base/ccUtils.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ THE SOFTWARE.
2929
#include <string>
3030
#include "2d/CCNode.h"
3131
#include "base/ccMacros.h"
32+
#include "base/CCData.h"
3233

3334
/** @file ccUtils.h
3435
Misc free functions
@@ -170,6 +171,14 @@ namespace utils
170171
* @return The md5 hash for the file
171172
*/
172173
CC_DLL std::string getFileMD5Hash(const std::string &filename);
174+
175+
176+
/**
177+
* Gets the md5 hash for the given buffer.
178+
* @param data The buffer to calculate md5 hash.
179+
* @return The md5 hash for the data
180+
*/
181+
CC_DLL std::string getDataMD5Hash(const Data &data);
173182
}
174183

175184
NS_CC_END

0 commit comments

Comments
 (0)