从内存中安装的apk文件生成哈希

时间:2014-01-22 12:23:20

标签: android sha1 hashcode android-install-apk

我正在使用此代码,但它显示的是同一应用程序的哈希码。 请帮助我。

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();
    }

现在我想阅读我当前安装的应用程序的已安装应用程序文件(假设它将是* .apk),从该文件读取字节数组并生成哈希值。

先谢谢。

2 个答案:

答案 0 :(得分:3)

这是最佳解决方案。

首先,你必须传递.apk文件的路径。

          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;
         }

答案 1 :(得分:1)

{{1}}

首先我收到了PackageInfo的签名 将该签名转换为字节数组,然后使用doFringerPrint函数获取Android应用程序的MD5。