Skip to content

Instantly share code, notes, and snippets.

@fredgrott
Created February 11, 2016 19:47
Show Gist options
  • Select an option

  • Save fredgrott/3c19f29b29d6154c191a to your computer and use it in GitHub Desktop.

Select an option

Save fredgrott/3c19f29b29d6154c191a to your computer and use it in GitHub Desktop.

ASL2.0 Copyright(C) 2016 Fred Grott first part is the clove

public class Clove {

    private static String myClove = null;

    public String setClove(String ourClove){
       return ourClove = myClove;
    }
}

the second part is the CloveFreshness

```java
public class CloveFreshness {

    public static boolean test(Context ctx, String pkgname, String correctHash){
        if (correctHash == null) return false;
        correctHash = correctHash.replaceAll("","");
        return correctHash.equals(hash(ctx, pkgname));
    }

    public static String hash(Context ctx, String pkgname){
        if (pkgname == null) return null;
        try {
            PackageManager pm = ctx.getPackageManager();
            @SuppressLint("PackageManagerGetSignatures") PackageInfo pkginfo = pm.getPackageInfo(pkgname, PackageManager.GET_SIGNATURES);
            if (pkginfo.signatures.length != 1)
                return null; //does not handle more than one signature
            Signature sig = pkginfo.signatures[0];
            byte[] cert = sig.toByteArray();
            byte[] sha256 = computeSha256(cert);
            return byte2hex(sha256);
        }catch(PackageManager.NameNotFoundException e){
            return  null;
        }
    }

    private static byte[] computeSha256(byte[] data){
        try{
            return MessageDigest.getInstance("SHA-256").digest(data);
        }catch (NoSuchAlgorithmException e){
            return null;
        }
    }

    private static String byte2hex(byte[] data){
        if (data == null) return null;
        final StringBuilder hexadecimal = new StringBuilder();
        for (final byte b : data){
            hexadecimal.append(String.format("%02x", b));
        }
        return hexadecimal.toString();
    }
}

the third part is how to initialize it in the extended Application class

public MyAppclass extends Application {
  private static String sMyCertHash = null;
     private static String myCertHash(Context context){
          if (sMyCertHash == null){
              if (BuildConfig.DEBUG){
                 Clove.setClove("0EFB7236 328348A9 89718BAD DF57F544 D5CCB4AE B9DB34BC 1E29DD26 F77C8255");
              }else{
                  String dHash = new Dhash.getDhahs;
                  Clove.setClove(dHash);
              }
         }
         return sMyCertHash
      }

     onCreate(){

          if (CloveFreshness.test(this, this.getPackageName(), myCertHash(this))){
              finish();
              return;
          }else{
              System.exit();
          }

      }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment