Question

I am using this code but it is showing the hash code of the same application. Please Help me out of it .

File file = new File(getApplicationContext().getPackageCodePath());
     String outputTxt= "";
        String hashcode = null;

    try {

        FileInputStream input = new FileInputStream(file);

        ByteArrayOutputStream output = new ByteArrayOutputStream ();
        byte [] buffer = new byte [65536];
        int l;


            while ((l = input.read (buffer)) > 0)
                output.write (buffer, 0, l);

            input.close ();
            output.close ();

            byte [] data = output.toByteArray ();

            MessageDigest digest = MessageDigest.getInstance( "SHA-1" ); 

            byte[] bytes = data;

            digest.update(bytes, 0, bytes.length);
            bytes = digest.digest();

            StringBuilder sb = new StringBuilder();

            for( byte b : bytes )
            {
                sb.append( String.format("%02X", b) );
            }

            hashcode = sb.toString();





    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Now I want to read the installed application file of my current android application(assuming it will be *.apk), read byte array from that file and produce the Hash value.

Thanks in Advance.

Was it helpful?

Solution

This is the best solution for this.

First u have to pass the path of the .apk file.

          private CharSequence getHash(String sourceDir)  {
          // TODO Auto-generated method stub

          File file = new File(packageInfo.applicationInfo.sourceDir);
         String outputTxt= "";
           String hashcode = null;

        try {

        FileInputStream input = new FileInputStream(file);

        ByteArrayOutputStream output = new ByteArrayOutputStream ();
        byte [] buffer = new byte [65536];
        int l;


              while ((l = input.read (buffer)) > 0)
                  output.write (buffer, 0, l);

                  input.close ();
              output.close ();

                byte [] data = output.toByteArray ();

                MessageDigest digest = MessageDigest.getInstance( "SHA-1" ); 

            byte[] bytes = data;

            digest.update(bytes, 0, bytes.length);
            bytes = digest.digest();

            StringBuilder sb = new StringBuilder();

            for( byte b : bytes )
            {
                sb.append( String.format("%02X", b) );
            }

            hashcode = sb.toString();


         } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
         } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
         } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }



          return hashcode;
         }

OTHER TIPS

public void buttonAppClick() {
    final PackageManager pm = getActivity().getPackageManager();
    //get a list of installed apps.
    List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
    outputTextView.setText("");
    for (ApplicationInfo packageInfo : packages) {
        try {
            String packageName = packageInfo.packageName;
            outputTextView.append("Apk Path : " + packageInfo.sourceDir + "\n");
            PackageInfo pi = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);

            Signature sig = pi.signatures[0];
            String md5Fingerprint = doFingerprint(sig.toByteArray(), "MD5");
            Log.d(TAG_HOME, "MD5 : " + packageInfo.sourceDir + md5Fingerprint);
            outputTextView.append("MD5 : " + md5Fingerprint + "\n");
            outputTextView.append("\n");
        }
        catch (Exception e) {
            Log.e(TAG_HOME, e.getMessage());
        }
    }
}

protected static String doFingerprint(byte[] certificateBytes, String algorithm)
        throws Exception {
    MessageDigest md = MessageDigest.getInstance(algorithm);
    md.update(certificateBytes);
    byte[] digest = md.digest();

    String toRet = "";
    for (int i = 0; i < digest.length; i++) {
        if (i != 0)
            toRet += ":";
        int b = digest[i] & 0xff;
        String hex = Integer.toHexString(b);
        if (hex.length() == 1)
            toRet += "0";
        toRet += hex;
    }
    return toRet;
}

First i got the signatures from the PackageInfo Convert that signature to byte array and then use the doFringerPrint function to get the MD5 of the android app.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top