Created
December 24, 2020 10:25
-
-
Save khahani/afa403c437095711a35cf61297988e9d to your computer and use it in GitHub Desktop.
How to check Android app signature at runtime
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class foo { | |
| private boolean isOldSignature() { | |
| Context context = this; | |
| final String SIGNATURE = "Put your app signature after retrive in log"; | |
| final boolean VALID = true; | |
| final boolean INVALID = false; | |
| try { | |
| PackageInfo packageInfo = context.getPackageManager() | |
| .getPackageInfo(context.getPackageName(), | |
| PackageManager.GET_SIGNATURES); | |
| for (Signature signature : packageInfo.signatures) { | |
| byte[] signatureBytes = signature.toByteArray(); | |
| MessageDigest md = MessageDigest.getInstance("SHA"); | |
| md.update(signature.toByteArray()); | |
| final String currentSignature = Base64.encodeToString(md.digest(), Base64.DEFAULT); | |
| Log.d("REMOVE_ME", "Include this string as a value for SIGNATURE:" + currentSignature); | |
| //compare signatures | |
| if (SIGNATURE.equals(currentSignature)) { | |
| return VALID; | |
| } | |
| } | |
| } catch (Exception e) { | |
| //assumes an issue in checking signature., but we let the caller decide on what to do. | |
| } | |
| return INVALID; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment