App打开时,将哈希值传到后端做比对,返回比对结果。若比对不通过,关闭app。
app安卓端获取apk哈希值:
/** * 校验完整性 * @return */
public String getCode() {
String apkPath = getPackageCodePath();
MessageDigest msgDigest = null;
FileInputStream fis = null;
try {
msgDigest = MessageDigest.getInstance("SHA-1");
byte[] bytes = new byte[1024];
int byteCount;
fis = new FileInputStream(new File(apkPath));
while ((byteCount = fis.read(bytes)) > 0) {
msgDigest.update(bytes, 0, byteCount);
}
BigInteger bi = new BigInteger(1, msgDigest.digest());
String sha = bi.toString(16);
return sha;
} catch (Exception e) {
e.printStackTrace();
return "";
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2、将提供的apk包通过sha1sum工具计算出哈希值,并记录到服务端数据库。
Windows处理方法
将sha1sum.exe和apk包放在同一个目录,执行命令sha1sum.exe?apk包名。
如:

Linux处理方法
Linux下自带sha1sum命令,直接执行sha1sum?app-debug.apk
?
|