Android 如何以编程方式计算APK文件的哈希?
我想从应用程序内部计算APK文件的MD5哈希Android 如何以编程方式计算APK文件的哈希?,android,apk,android-package-managers,Android,Apk,Android Package Managers,我想从应用程序内部计算APK文件的MD5哈希 PackageInfo info = App.getInstance().getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_META_DATA); File file = new File(info.applicationInfo.sourceDir); String hash = MD5.calculateMD5(file); MD5哈希的计算方式如下:
PackageInfo info = App.getInstance().getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_META_DATA);
File file = new File(info.applicationInfo.sourceDir);
String hash = MD5.calculateMD5(file);
MD5哈希的计算方式如下:
private String calculateMD5() {
MessageDigest digest;
try {
digest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
return null;
}
InputStream is;
try {
is = new FileInputStream(file);
} catch (FileNotFoundException e) {
return null;
}
byte[] buffer = new byte[8192];
int read;
try {
while ((read = is.read(buffer)) > 0) {
digest.update(buffer, 0, read);
}
byte[] md5sum = digest.digest();
BigInteger bigInt = new BigInteger(1, md5sum);
String output = bigInt.toString(16);
output = String.format("%32s", output).replace(' ', '0');
return output;
} catch (IOException e) {
throw new RuntimeException("Unable to process file for MD5", e);
} finally {
try {
is.close();
} catch (IOException e) {
}
}
}
但是,在模拟器中运行时,即使在更改源代码时,我也会得到相同的哈希值
这里出了什么问题?请仔细检查您是否真正针对修改后的版本运行。我建议完全卸载,然后在代码更改后重新安装。在调试了一个web应用程序两个小时后,我发现我没有部署我所做的更改,这是一个艰难的过程。再次检查您是否真的针对修改后的版本运行。我建议完全卸载,然后在代码更改后重新安装。你说得对!单击“调试应用程序”是不够的,我必须重建项目以获得不同的哈希值。你想写一个答案还是我应该写?重建项目以获得新的哈希值就足够了,而无需卸载。在那之后,仅仅点击“调试应用程序”显然就足以得到新的散列,但重建似乎是安全的。