Guest User

Untitled

a guest
Oct 25th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. import android.content.Context;
  2. import android.content.pm.PackageInfo;
  3. import android.content.pm.PackageManager;
  4. import android.content.pm.PackageManager.NameNotFoundException;
  5. import android.content.pm.Signature;
  6.  
  7. public class TamperCheck {
  8.  
  9. //we store the hash of the signture for a little more protection
  10. private static final String APP_SIGNATURE = "1038C0E34658923C4192E61B16846";
  11.  
  12. /**
  13. * Query the signature for this application to detect whether it matches the
  14. * signature of the real developer. If it doesn't the app must have been
  15. * resigned, which indicates it may been tampered with.
  16. *
  17. * @param context
  18. * @return true if the app's signature matches the expected signature.
  19. * @throws NameNotFoundException
  20. */
  21. public boolean validateAppSignature(Context context) throws NameNotFoundException {
  22.  
  23. PackageInfo packageInfo = context.getPackageManager().getPackageInfo(
  24. getPackageName(), PackageManager.GET_SIGNATURES);
  25. //note sample just checks the first signature
  26. for (Signature signature : packageInfo.signatures) {
  27. // SHA1 the signature
  28. String sha1 = getSHA1(signature.toByteArray());
  29. // check is matches hardcoded value
  30. return APP_SIGNATURE.equals(sha1);
  31. }
  32.  
  33. return false;
  34. }
  35.  
  36. //computed the sha1 hash of the signature
  37. public static String getSHA1(byte[] sig) {
  38. MessageDigest digest = MessageDigest.getInstance("SHA1", "BC");
  39. digest.update(sig);
  40. byte[] hashtext = digest.digest();
  41. return bytesToHex(hashtext);
  42. }
  43.  
  44. //util method to convert byte array to hex string
  45. public static String bytesToHex(byte[] bytes) {
  46. final char[] hexArray = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
  47. '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  48. char[] hexChars = new char[bytes.length * 2];
  49. int v;
  50. for (int j = 0; j < bytes.length; j++) {
  51. v = bytes[j] & 0xFF;
  52. hexChars[j * 2] = hexArray[v >>> 4];
  53. hexChars[j * 2 + 1] = hexArray[v & 0x0F];
  54. }
  55. return new String(hexChars);
  56. }
  57.  
  58.  
  59. }
Add Comment
Please, Sign In to add comment