Issue
DESCRIPTION
I'm trying to get md5 hash of the binary (.dex
) file, which is stored in the /data/data/my.package.name/file.dex
. I use Android NDK (C++).
PROBLEM
Several ways to get hash of the file:
- From my laptop:
md5sum file.dex
->5a65273b2ee336ad2c45a9306be162f6
- Using
adb shell
:md5sum file.dex
->5a65273b2ee336ad2c45a9306be162f6
- From my C++ code:
int err = 0;
zip *z = zip_open(getBaseApkAbsolutePath(), 0, &err);
const char *name = "classes.dex";
struct zip_stat st;
zip_stat_init(&st);
zip_stat(z, name, 0, &st);
char *contents = new char[st.size];
zip_file *f = zip_fopen(z, name, 0);
zip_fread(f, contents, st.size);
std::ofstream ofstream;
ofstream.open("/data/data/my.package/classes.dex", std::ofstream::binary);
ofstream.write(contents, st.size);
joyee::MD5 md5 = joyee::MD5();
md5.update(contents, sizeof(contents));
md5.finalize();
LOG(md5.toString());
LOG(joyee::md5(contents));
zip_fclose(f);
zip_close(z);
And then I get this -> aaaeb407992f9fe57cc6235ece90ec35
CLARIFICATION
- My code works well with simple string like
Hello, World!
- MD5 implementation works well
QUESTION
How can I get md5 hash of the binary file in Android using C++ ?
Solution
Here is the problem:
md5.update(contents, sizeof(contents));
You're taking the size of your pointer (which is 4 or 8) instead of the buffer it points to. So effectively you are calculating the md5sum of those first few bytes. Pass st.size
instead to check the full file.
Answered By - Botje
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.