Android apk完整性檢測的實現(xiàn)思路和代碼實現(xiàn)
需求和背景
行業(yè)相關,對安全性較高的程序一般都需要添加完整性檢測的功能,以防止程序被篡改,從而導致安全問題的發(fā)生。
相關的支付應用項目今年也做了好幾個,這些程序也都已通過了行業(yè)相關安全標準的認證。
實現(xiàn)
下面來分享Android APP完整性校驗的實現(xiàn)思路和代碼實現(xiàn)。
通過sp判斷當前是否是第一次安裝apk,第一次安裝默認apk是從市場下載安裝,默認認為是沒有被篡改過的??梢圆挥脵z查,只計算當前的hash值并保存到文件中。
可以在application中執(zhí)行,計算apk的hash值并寫文件的操作是耗時操作,記得開子線程進行。
private boolean integrityCheckResult = false; private boolean isFirstRun;//可以通過文件保存,例如SP @Override public void onCreate() { super.onCreate(); ThreadPoolManager.getInstance().runInBackground(new Runnable() { @Override public void run() { //檢測apk完整性 if (isFirstRun){//skip and calculate apk's hash SecurityManager.getInstance().checkIntegrity(true); integrityCheckResult = true; }else { integrityCheckResult = SecurityManager.getInstance().checkIntegrity(false); } } }); public boolean isIntegrityCheckResult() { return integrityCheckResult; }
在入口activity中判斷是否完整性校驗通過,假如不通過,可以彈窗提示然后鎖定APP,讓用戶重新在安全的平臺重新下載安裝。當前APP無法使用,存在安全問題。
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (App.getApp().isIntegrityCheckResult()) { Log.d(TAG, "onCreate: checkIntegrity success"); } else { Log.d(TAG, "onCreate: checkIntegrity failed"); } }
安全管理類
新建一個安全管理類,用于管理所有和安全相關的類
public class SecurityManager { //做一個單例 private static SecurityManager instance = null; private final Integrity integrity; private SecurityManager(){ integrity = new Integrity(); } public static synchronized SecurityManager getInstance() { if (instance == null) instance = new SecurityManager(); return instance; } public boolean checkIntegrity(boolean isFirstInstall) { return integrity.checkIntegrity(isFirstInstall); } }
實現(xiàn)完整性檢測類的接口
public interface IIntegrity { boolean checkApkIntegrity(); }
完整性檢測實現(xiàn)類:
public class Integrity implements IIntegrity { public boolean checkIntegrity(boolean isFirstInstall) { if (isFirstInstall) { calcAndSaveApkSoHash(); return true; } else { return compareHashsWithLastTime(); } } private void calcAndSaveApkSoHash() { File apk = new File(BaseApplication.getAppContext().getPackageCodePath()); byte[] apkHash = HashCalculator.calculateHashBytes(apk, HashCalculator.SHA_256); FileUtils.writeBytesToFile(filePath + APK_HASH_FILE, apkHash); } private boolean compareHashsWithLastTime() { //檢測apk so return checkApkIntegrity(); } @Override public boolean checkApkIntegrity() { if (BuildConfig.DEBUG) { Log.w(TAG, "Debug version,skip apk‘s hash verification"); return true; } try { String apkPath = BaseApplication.getAppContext().getPackageCodePath(); byte[] originalApkHash = FileUtils.readFileToBytes(filePath + APK_HASH_FILE); return calcSrcAndCompareWithLastHash(originalApkHash, new File(apkPath)); } catch (IOException e) { Log.e(TAG, "checkApkAndLibs: ", e); } return false; } /** * 計算明文數(shù)據(jù)并和上一次hash進行比較 * * @param decHashBytes 明文hash數(shù)據(jù) * @param decSrc 明文源數(shù)據(jù) */ private static boolean calcSrcAndCompareWithLastHash(byte[] decHashBytes, File decSrc) { String decHash = Utils.bcd2Str(decHashBytes); //計算解密ksn的hash String calcHash = HashCalculator.calculateHash(decSrc, HashCalculator.SHA_256); LogUtils.i(TAG, "calculate hash = " + Utils.bcd2Str( HashCalculator.calculateHashBytes(decSrc, HashCalculator.SHA_256))); return decHash.equalsIgnoreCase(calcHash); } }
相關工具類
這個只是工具類,方便獲取Application ,只要獲取context即可,可以隨意發(fā)揮。
public class BaseApplication extends Application { private static BaseApplication mBaseApplication ; mBaseApplication = this; } public static BaseApplication getAppContext(){ return mBaseApplication; }
編碼轉換工具:
@NonNull public static String bcd2Str(@Nullable byte[] b, int length) { if (b == null) { return ""; } StringBuilder sb = new StringBuilder(length * 2); for (int i = 0; i < length; ++i) { sb.append(ARRAY_OF_CHAR[((b[i] & 0xF0) >>> 4)]); sb.append(ARRAY_OF_CHAR[(b[i] & 0xF)]); } return sb.toString(); }
hash計算器
@NonNull public static String bcd2Str(@Nullable byte[] b, int length) { if (b == null) { return ""; } StringBuilder sb = new StringBuilder(length * 2); for (int i = 0; i < length; ++i) { sb.append(ARRAY_OF_CHAR[((b[i] & 0xF0) >>> 4)]); sb.append(ARRAY_OF_CHAR[(b[i] & 0xF)]); } return sb.toString(); }
文件工具類
/** * 文件鎖定(File Locking) * 強制刷新緩沖(Force Flushing Buffer): */ public static boolean writeBytesToFile(String filePath, byte[] bytes) { try (FileOutputStream fos = new FileOutputStream(filePath)) { fos.write(bytes); // 獲取文件鎖定 FileChannel fileChannel = fos.getChannel(); try (FileLock fileLock = fileChannel.lock()) { // 強制刷新緩沖 fileChannel.force(true); } return true; } catch (IOException e) { LogUtils.e(e); return false; } }
到此這篇關于Android apk完整性檢測的實現(xiàn)思路和實現(xiàn)過程全記錄的文章就介紹到這了,更多相關Android apk完整性檢測內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
android開發(fā)教程之startActivityForResult使用方法
這篇文章主要介紹了android開發(fā)教程之startActivityForResult使用方法,需要的朋友可以參考下2014-03-032021最新Android筆試題總結美團Android崗職能要求
這篇文章主要介紹了2021最新Android筆試題總結以及美團Android崗職能要求,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08Android?studio實現(xiàn)動態(tài)背景頁面
這篇文章主要為大家詳細介紹了Android?studio實現(xiàn)動態(tài)背景頁面,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04